<commit_before>#include <stdio.h>
#include <time.h>
#include "boreutils.h"

// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html

static int set_time(char *date, char **argv) {
    size_t len = strlen(date);
    struct tm tmp;
    char *ret;

    // For determining the current year.
    time_t now;
    time(&now);
    struct tm *local = localtime(&now);

    memset(&tmp, 0, sizeof(tmp));

    if (len == 12) {
        // MMDDhhmmCCYY
        ret = strptime(date, "%m%d%H%M%Y", &tmp);
    } else if (len == 10) {
        // MMDDhhmmYY
        ret = strptime(date, "%m%d%H%M%y", &tmp);
    } else if (len == 8) {
        // MMDDhhmm
        // Set the year, since it's not specified.
        tmp.tm_year = local->tm_year;
        ret = strptime(date, "%m%d%H%M", &tmp);
    } else {
        fprintf(stderr, "Invalid date string.\nSee '%s --help' for usage information.\n", argv[0]);
        return 1;
    }

    if (ret == NULL) {
        fprintf(stderr, "Somehow tried using invalid date string?\nThis error should never happen.\n");
        return 1;
    }

    if (ret[0] != '\0') {
        fprintf(stderr, "Somehow gave extra data to strptime()?\nThis error should never happen.\n");
        return 1;
    }


    struct timespec ts;

    // Convert from `struct tm` to `time_t`.
    time_t tval = mktime(&tmp);
    if (tval == -1) {
        fprintf(stderr, "Error calling mktime().\n");
        return 1;
    }

    // Convert tval to local time.
    struct tm *local_ts = localtime(&tval);
    tval = mktime(local_ts);
    if (tval == -1) {
        fprintf(stderr, "Error calling mktime(). (Second call.)\n");
        return 1;
    }

    ts.tv_sec = tval;
    ts.tv_nsec = 0;

    if (clock_settime(CLOCK_REALTIME, &ts) == 0) {
        perror(argv[0]);
        return 1;
    }

    return 0;
}

int main(int argc, char **argv)
{
    if (has_arg(argc, argv, "-h") || has_arg(argc, argv, "--help")) {
        printf("Usage: %s [-u] [+FORMAT]\n", argv[0]);
        printf("       %s [-u] MMDDhhmm[[CC]YY]\n", argv[0]);
        printf("Print the current time in the specified FORMAT, or set the date.\n");
        return 1;
    }

    if (bu_handle_version(argc, argv)) {
        return 0;
    }<commit_msg>Correct condition for printing error. Fixes #3.<commit_after>#include <stdio.h>
#include <time.h>
#include "boreutils.h"

// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html

static int set_time(char *date, char **argv) {
    size_t len = strlen(date);
    struct tm tmp;
    char *ret;

    // For determining the current year.
    time_t now;
    time(&now);
    struct tm *local = localtime(&now);

    memset(&tmp, 0, sizeof(tmp));

    if (len == 12) {
        // MMDDhhmmCCYY
        ret = strptime(date, "%m%d%H%M%Y", &tmp);
    } else if (len == 10) {
        // MMDDhhmmYY
        ret = strptime(date, "%m%d%H%M%y", &tmp);
    } else if (len == 8) {
        // MMDDhhmm
        // Set the year, since it's not specified.
        tmp.tm_year = local->tm_year;
        ret = strptime(date, "%m%d%H%M", &tmp);
    } else {
        fprintf(stderr, "Invalid date string.\nSee '%s --help' for usage information.\n", argv[0]);
        return 1;
    }

    if (ret == NULL) {
        fprintf(stderr, "Somehow tried using invalid date string?\nThis error should never happen.\n");
        return 1;
    }

    if (ret[0] != '\0') {
        fprintf(stderr, "Somehow gave extra data to strptime()?\nThis error should never happen.\n");
        return 1;
    }


    struct timespec ts;

    // Convert from `struct tm` to `time_t`.
    time_t tval = mktime(&tmp);
    if (tval == -1) {
        fprintf(stderr, "Error calling mktime().\n");
        return 1;
    }

    // Convert tval to local time.
    struct tm *local_ts = localtime(&tval);
    tval = mktime(local_ts);
    if (tval == -1) {
        fprintf(stderr, "Error calling mktime(). (Second call.)\n");
        return 1;
    }

    ts.tv_sec = tval;
    ts.tv_nsec = 0;

    if (clock_settime(CLOCK_REALTIME, &ts) == -1) {
        perror(argv[0]);
        return 1;
    }

    return 0;
}

int main(int argc, char **argv)
{
    if (has_arg(argc, argv, "-h") || has_arg(argc, argv, "--help")) {
        printf("Usage: %s [-u] [+FORMAT]\n", argv[0]);
        printf("       %s [-u] MMDDhhmm[[CC]YY]\n", argv[0]);
        printf("Print the current time in the specified FORMAT, or set the date.\n");
        return 1;
    }

    if (bu_handle_version(argc, argv)) {
        return 0;
    }
{'fraction_non_alphanumeric': 0.16418242491657398, 'fraction_numerical': 0.015795328142380424, 'mean_word_length': 2.2068473609129815, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3564068', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1609, 'n_words': 432}