From a08a556f8f7da64696e3f735dcc008e2176741df Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 18 Mar 2022 21:10:04 -0400 Subject: main: Allow faking current time Drop use of faketime in tests. --- diff --git a/src/main.c b/src/main.c index c8e90f4..636fede 100644 --- a/src/main.c +++ b/src/main.c @@ -91,18 +91,46 @@ _print_help(const char *program_name) #endif } -static void -_list_formats(void) +static bool +_set_now(struct tm *tm, char buf[]) { - time_t tim; - struct tm *tm; - char *out; - size_t buf_sz; - char *buf; - size_t i; + char *endptr; + + if (strlen(buf) != 19) { + return false; + } + if (buf[4] != '-' || buf[7] != '-' || buf[10] != ' ' || + buf[13] != ':' || buf[16] != ':') { + return false; + } + 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; - tim = time(NULL); - tm = localtime(&tim); + return true; +} + +static void +_list_formats(const struct tm *tm) +{ + char *out; + size_t buf_sz; + char *buf; + size_t i; puts("Time formats:"); i = 0; @@ -182,6 +210,8 @@ int main(int argc, char * const argv[]) { bool dbg; + bool fmt; + struct tm now_tm; int opt; char *buf; time_t now; @@ -190,17 +220,18 @@ main(int argc, char * const argv[]) double dif; dbg = false; + fmt = false; optind = 1; opterr = 0; #if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG - while ((opt = getopt_long(argc, argv, "FhVd", LONGOPTS_, NULL)) > 0) { + while ((opt = getopt_long(argc, argv, "FhVd:", LONGOPTS_, NULL)) > 0) { #else - while ((opt = getopt(argc, argv, "FhVd")) > 0) { + while ((opt = getopt(argc, argv, "FhVd:")) > 0) { #endif switch (opt) { case 'F': - _list_formats(); - return EXIT_SUCCESS; + fmt = true; + break; case 'h': _print_help(argv[0]); return EXIT_SUCCESS; @@ -209,6 +240,10 @@ main(int argc, char * const argv[]) return EXIT_SUCCESS; case 'd': dbg = true; + if (_set_now(&now_tm, optarg) == false) { + return EXIT_FAILURE; + } + now_tm_p = &now_tm; break; default: _print_usage(stderr, argv[0]); @@ -225,14 +260,21 @@ main(int argc, char * const argv[]) argc -= optind; argv += optind; + if (dbg == false) { + now = time(NULL); + now_tm_p = localtime(&now); + } + + if (fmt == true) { + _list_formats(now_tm_p); + return EXIT_SUCCESS; + } + buf = _concat_args(argc, argv); if (buf == NULL) { return EXIT_FAILURE; } - now = time(NULL); - now_tm_p = localtime(&now); - if (datetime_parse(now_tm_p, buf, &arg) < 0) { free(buf); return EXIT_FAILURE; diff --git a/tests/parse.sh b/tests/parse.sh index 95a8a93..08a8dea 100755 --- a/tests/parse.sh +++ b/tests/parse.sh @@ -21,7 +21,7 @@ set -eu . "${TOP_SRCDIR}/tests/aux/tap-functions.sh" -fmts="$(TZ=UTC0 faketime -f '1970-01-01 00:00:00' "${TOP_BUILDDIR}/@" -F)" +fmts="$(TZ=UTC0 "${TOP_BUILDDIR}/@" -d '1970-01-01 00:00:00' -F)" time_fmts="$(printf '%s\n' "${fmts}" | \ sed -n '/^Time formats:$/,/^Date formats:$/s/^ \* //p')" date_fmts="$(printf '%s\n' "${fmts}" | \ @@ -42,8 +42,8 @@ run_test() datetime="${1}" shift 1 - got="$(TZ=UTC0 faketime -f '1969-12-31 23:59:30' \ - "${TOP_BUILDDIR}/@" -d "${datetime}" 2>&1)" || : + got="$(TZ=UTC0 "${TOP_BUILDDIR}/@" -d '1969-12-31 23:59:30' \ + "${datetime}" 2>&1)" || : if [ x"${got}" = x'0' ]; then ok_ -- "${datetime}" else -- cgit v0.9.1