diff options
author | P. J. McDermott <pj@pehjota.net> | 2022-03-19 11:33:58 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2022-03-19 11:33:58 (EDT) |
commit | 4cf5f31c257c79b38ed756d024fada284bab5367 (patch) | |
tree | 365e4f003921e05992b3d41e4109dc6d7c102590 | |
parent | a08a556f8f7da64696e3f735dcc008e2176741df (diff) | |
download | atsign-4cf5f31c257c79b38ed756d024fada284bab5367.zip atsign-4cf5f31c257c79b38ed756d024fada284bab5367.tar.gz atsign-4cf5f31c257c79b38ed756d024fada284bab5367.tar.bz2 |
main: Populate day of week in "now" time
Needed for format listing.
-rw-r--r-- | src/main.c | 61 |
1 files changed, 33 insertions, 28 deletions
@@ -91,37 +91,43 @@ _print_help(const char *program_name) #endif } -static bool -_set_now(struct tm *tm, char buf[]) +static struct tm * +_set_now(char buf[]) { - char *endptr; + struct tm tm; + char *endptr; + time_t t; + struct tm *tm_p; if (strlen(buf) != 19) { - return false; + return NULL; } if (buf[4] != '-' || buf[7] != '-' || buf[10] != ' ' || buf[13] != ':' || buf[16] != ':') { - return false; + return NULL; } buf[4] = '\0', buf[7] = '\0', buf[10] = '\0'; buf[13] = '\0', buf[16] = '\0'; - tm->tm_sec = strtol(buf + 17, &endptr, 10); - if (endptr[0] != '\0') { return false; } - tm->tm_min = strtol(buf + 14, &endptr, 10); - if (endptr[0] != '\0') { return false; } - tm->tm_hour = strtol(buf + 11, &endptr, 10); - if (endptr[0] != '\0') { return false; } - tm->tm_mday = strtol(buf + 8, &endptr, 10); - if (endptr[0] != '\0') { return false; } - tm->tm_mon = strtol(buf + 5, &endptr, 10) - 1; - if (endptr[0] != '\0') { return false; } - tm->tm_year = strtol(buf + 0, &endptr, 10) - 1900; - if (endptr[0] != '\0') { return false; } - tm->tm_wday = -1; - tm->tm_yday = -1; - tm->tm_isdst = -1; + tm.tm_sec = strtol(buf + 17, &endptr, 10); + if (endptr[0] != '\0') { return NULL; } + tm.tm_min = strtol(buf + 14, &endptr, 10); + if (endptr[0] != '\0') { return NULL; } + tm.tm_hour = strtol(buf + 11, &endptr, 10); + if (endptr[0] != '\0') { return NULL; } + tm.tm_mday = strtol(buf + 8, &endptr, 10); + if (endptr[0] != '\0') { return NULL; } + tm.tm_mon = strtol(buf + 5, &endptr, 10) - 1; + if (endptr[0] != '\0') { return NULL; } + tm.tm_year = strtol(buf + 0, &endptr, 10) - 1900; + if (endptr[0] != '\0') { return NULL; } + tm.tm_wday = -1; + tm.tm_yday = -1; + tm.tm_isdst = -1; + + t = mktime(&tm); + tm_p = localtime(&t); - return true; + return tm_p; } static void @@ -211,11 +217,10 @@ main(int argc, char * const argv[]) { bool dbg; bool fmt; - struct tm now_tm; int opt; char *buf; time_t now; - struct tm *now_tm_p; + struct tm *now_tm; time_t arg; double dif; @@ -240,10 +245,10 @@ main(int argc, char * const argv[]) return EXIT_SUCCESS; case 'd': dbg = true; - if (_set_now(&now_tm, optarg) == false) { + now_tm = _set_now(optarg); + if (now_tm == NULL) { return EXIT_FAILURE; } - now_tm_p = &now_tm; break; default: _print_usage(stderr, argv[0]); @@ -262,11 +267,11 @@ main(int argc, char * const argv[]) if (dbg == false) { now = time(NULL); - now_tm_p = localtime(&now); + now_tm = localtime(&now); } if (fmt == true) { - _list_formats(now_tm_p); + _list_formats(now_tm); return EXIT_SUCCESS; } @@ -275,7 +280,7 @@ main(int argc, char * const argv[]) return EXIT_FAILURE; } - if (datetime_parse(now_tm_p, buf, &arg) < 0) { + if (datetime_parse(now_tm, buf, &arg) < 0) { free(buf); return EXIT_FAILURE; } |