From: Thomas Weißschuh Date: Fri, 20 Jan 2023 02:13:36 +0000 (+0000) Subject: lib/timeutils: parse_timestamp: add unittests X-Git-Tag: v2.39-rc1~125^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d9cc65cb7e538beb9ab1cfdabae7127209e3b46d;p=thirdparty%2Futil-linux.git lib/timeutils: parse_timestamp: add unittests Signed-off-by: Thomas Weißschuh --- diff --git a/lib/timeutils.c b/lib/timeutils.c index c474d750cb..6a7d1c26c1 100644 --- a/lib/timeutils.c +++ b/lib/timeutils.c @@ -580,16 +580,65 @@ time_t timegm(struct tm *tm) #ifdef TEST_PROGRAM_TIMEUTILS +static int run_unittest_timestamp(void) +{ + int rc = EXIT_SUCCESS; + time_t reference = 1674180427; + static const struct testcase { + const char * const input; + usec_t expected; + } testcases[] = { + { "2012-09-22 16:34:22", 1348331662000000 }, + { "@1348331662" , 1348331662000000 }, + { "2012-09-22 16:34" , 1348331640000000 }, + { "2012-09-22" , 1348272000000000 }, + { "16:34:22" , 1674232462000000 }, + { "16:34" , 1674232440000000 }, + { "now" , 1674180427000000 }, + { "yesterday" , 1674086400000000 }, + { "today" , 1674172800000000 }, + { "tomorrow" , 1674259200000000 }, + { "+5min" , 1674180727000000 }, + { "-5days" , 1673748427000000 }, + }; + + if (unsetenv("TZ")) + rc = EXIT_FAILURE; + tzset(); + + for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) { + struct testcase t = testcases[i]; + usec_t result; + int r = parse_timestamp_reference(reference, t.input, &result); + if (r) { + fprintf(stderr, "Could not parse '%s'\n", t.input); + rc = EXIT_FAILURE; + } + + if (result != t.expected) { + fprintf(stderr, "#%02zu %-25s: %"PRId64" != %"PRId64"\n", + i, t.input, result, t.expected); + rc = EXIT_FAILURE; + } + } + + return rc; +} + int main(int argc, char *argv[]) { struct timeval tv = { 0 }; char buf[ISO_BUFSIZ]; if (argc < 2) { - fprintf(stderr, "usage: %s [