| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "string-util.h" |
| 8 |
#include "tests.h" |
| 9 |
#include "time-util.h" |
| 10 |
|
| 11 |
static void test_should_pass(const char *p) { |
| 12 |
usec_t t, q; |
| 13 |
char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX]; |
| 14 |
|
| 15 |
log_info("Test: %s", p); |
| 16 |
ASSERT_OK(parse_timestamp(p, &t)); |
| 17 |
ASSERT_NOT_NULL(format_timestamp_style(buf, sizeof(buf), t, TIMESTAMP_US)); |
| 18 |
log_info("\"%s\" → \"%s\"", p, buf); |
| 19 |
|
| 20 |
ASSERT_OK(parse_timestamp(buf, &q)); |
| 21 |
if (q != t) |
| 22 |
log_error("round-trip failed: \"%s\" → \"%s\"", |
| 23 |
buf, FORMAT_TIMESTAMP_STYLE(q, TIMESTAMP_US)); |
| 24 |
ASSERT_EQ(q, t); |
| 25 |
|
| 26 |
ASSERT_NOT_NULL(format_timestamp_relative(buf_relative, sizeof(buf_relative), t)); |
| 27 |
log_info("%s", strna(buf_relative)); |
| 28 |
} |
| 29 |
|
| 30 |
static void test_should_parse(const char *p) { |
| 31 |
usec_t t; |
| 32 |
|
| 33 |
log_info("Test: %s", p); |
| 34 |
ASSERT_OK(parse_timestamp(p, &t)); |
| 35 |
log_info("\"%s\" → \"@%" PRI_USEC "\"", p, t); |
| 36 |
} |
| 37 |
|
| 38 |
static void test_should_fail(const char *p) { |
| 39 |
usec_t t; |
| 40 |
int r; |
| 41 |
|
| 42 |
log_info("Test: %s", p); |
| 43 |
r = parse_timestamp(p, &t); |
| 44 |
if (r >= 0) |
| 45 |
log_info("\"%s\" → \"@%" PRI_USEC "\" (unexpected)", p, t); |
| 46 |
else |
| 47 |
log_info("parse_timestamp() returns %d (expected)", r); |
| 48 |
ASSERT_LT(r, 0); |
| 49 |
} |
| 50 |
|
| 51 |
static void test_one(const char *p) { |
| 52 |
_cleanup_free_ char *with_utc = NULL; |
| 53 |
|
| 54 |
with_utc = strjoin(p, " UTC"); |
| 55 |
test_should_pass(p); |
| 56 |
test_should_pass(with_utc); |
| 57 |
} |
| 58 |
|
| 59 |
static void test_one_noutc(const char *p) { |
| 60 |
_cleanup_free_ char *with_utc = NULL; |
| 61 |
|
| 62 |
with_utc = strjoin(p, " UTC"); |
| 63 |
test_should_pass(p); |
| 64 |
test_should_fail(with_utc); |
| 65 |
} |
| 66 |
|
| 67 |
int main(int argc, char *argv[]) { |
| 68 |
/* Tests have hard-coded results that do not expect a specific timezone to be set by the caller */ |
| 69 |
ASSERT_OK_ERRNO(unsetenv("TZ")); |
| 70 |
|
| 71 |
test_setup_logging(LOG_DEBUG); |
| 72 |
|
| 73 |
test_one("17:41"); |
| 74 |
test_one("18:42:44"); |
| 75 |
test_one("18:42:44.0"); |
| 76 |
test_one("18:42:44.999999999999"); |
| 77 |
test_one("12-10-02 12:13:14"); |
| 78 |
test_one("12-10-2 12:13:14"); |
| 79 |
test_one("12-10-03 12:13"); |
| 80 |
test_one("2012-12-30 18:42"); |
| 81 |
test_one("2012-10-02"); |
| 82 |
test_one("Mar 12 12:01:01"); |
| 83 |
test_one("Mar 12 12:01:01.687197"); |
| 84 |
test_one("Tue 2012-10-02"); |
| 85 |
test_one("yesterday"); |
| 86 |
test_one("today"); |
| 87 |
test_one("tomorrow"); |
| 88 |
test_one_noutc("16:20 UTC"); |
| 89 |
if (access("/usr/share/zoneinfo/Asia/Seoul", F_OK) >= 0) { |
| 90 |
test_one_noutc("16:20 Asia/Seoul"); |
| 91 |
test_one_noutc("tomorrow Asia/Seoul"); |
| 92 |
test_one_noutc("2012-12-30 18:42 Asia/Seoul"); |
| 93 |
} |
| 94 |
test_one_noutc("now"); |
| 95 |
test_one_noutc("+2d"); |
| 96 |
test_one_noutc("+2y 4d"); |
| 97 |
test_one_noutc("5months ago"); |
| 98 |
test_one_noutc("@1395716396"); |
| 99 |
test_should_parse("1970-1-1 UTC"); |
| 100 |
test_should_pass("1970-1-1 00:00:01 UTC"); |
| 101 |
test_should_fail("1969-12-31 UTC"); |
| 102 |
test_should_fail("-1000y"); |
| 103 |
test_should_fail("today UTC UTC"); |
| 104 |
if (access("/usr/share/zoneinfo/Asia/Seoul", F_OK) >= 0) { |
| 105 |
test_should_fail("now Asia/Seoul"); |
| 106 |
test_should_fail("+2d Asia/Seoul"); |
| 107 |
test_should_fail("@1395716396 Asia/Seoul"); |
| 108 |
} |
| 109 |
#if SIZEOF_TIME_T == 8 |
| 110 |
test_should_pass("9999-12-30 23:59:59 UTC"); |
| 111 |
test_should_fail("9999-12-31 00:00:00 UTC"); |
| 112 |
test_should_fail("10000-01-01 00:00:00 UTC"); |
| 113 |
#elif SIZEOF_TIME_T == 4 |
| 114 |
test_should_pass("2038-01-18 03:14:07 UTC"); |
| 115 |
test_should_fail("2038-01-18 03:14:08 UTC"); |
| 116 |
#endif |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |