]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: test parse_timestamp() in various timezone
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 3 Mar 2023 03:09:59 +0000 (12:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 3 Mar 2023 11:39:10 +0000 (20:39 +0900)
src/test/test-time-util.c

index cec5bffd79ac64acce8b020ceec46bcb150075df..e974520bbe2c621489fc31d392da7cb098d12a64 100644 (file)
@@ -628,15 +628,17 @@ TEST(format_timestamp_range) {
 }
 
 static void test_parse_timestamp_one(const char *str, usec_t max_diff, usec_t expected) {
-        usec_t usec;
+        usec_t usec = USEC_INFINITY;
+        int r;
 
-        log_debug("/* %s(%s) */", __func__, str);
-        assert_se(parse_timestamp(str, &usec) >= 0);
+        r = parse_timestamp(str, &usec);
+        log_debug("/* %s(%s): max_diff="USEC_FMT", expected="USEC_FMT", result="USEC_FMT"*/", __func__, str, max_diff, expected, usec);
+        assert_se(r >= 0);
         assert_se(usec >= expected);
         assert_se(usec_sub_unsigned(usec, expected) <= max_diff);
 }
 
-TEST(parse_timestamp) {
+static void test_parse_timestamp_impl(const char *tz) {
         usec_t today, now_usec;
 
         /* UTC */
@@ -681,10 +683,9 @@ TEST(parse_timestamp) {
                 test_parse_timestamp_one("70-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC);
                 test_parse_timestamp_one("70-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
                 test_parse_timestamp_one("70-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
+        }
 
-                const char *saved_tz = getenv("TZ");
-                assert_se(setenv("TZ", ":Asia/Tokyo", 1) >= 0);
-
+        if (streq_ptr(tz, "Asia/Tokyo")) {
                 /* JST (+0900) */
                 test_parse_timestamp_one("Thu 1970-01-01 09:01 JST", 0, USEC_PER_MINUTE);
                 test_parse_timestamp_one("Thu 1970-01-01 09:00:01 JST", 0, USEC_PER_SEC);
@@ -705,9 +706,6 @@ TEST(parse_timestamp) {
                 test_parse_timestamp_one("70-01-01 09:00:01 JST", 0, USEC_PER_SEC);
                 test_parse_timestamp_one("70-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000);
                 test_parse_timestamp_one("70-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000);
-
-                assert_se(set_unset_env("TZ", saved_tz, true) == 0);
-                tzset();
         }
 
         if (timezone_is_valid("America/New_York", LOG_DEBUG)) {
@@ -731,10 +729,9 @@ TEST(parse_timestamp) {
                 test_parse_timestamp_one("69-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC);
                 test_parse_timestamp_one("69-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000);
                 test_parse_timestamp_one("69-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000);
+        }
 
-                const char *saved_tz = getenv("TZ");
-                assert_se(setenv("TZ", ":America/New_York", 1) >= 0);
-
+        if (streq_ptr(tz, "America/New_York")) {
                 /* EST (-0500) */
                 test_parse_timestamp_one("Wed 1969-12-31 19:01 EST", 0, USEC_PER_MINUTE);
                 test_parse_timestamp_one("Wed 1969-12-31 19:00:01 EST", 0, USEC_PER_SEC);
@@ -755,9 +752,6 @@ TEST(parse_timestamp) {
                 test_parse_timestamp_one("69-12-31 19:00:01 EST", 0, USEC_PER_SEC);
                 test_parse_timestamp_one("69-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000);
                 test_parse_timestamp_one("69-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000);
-
-                assert_se(set_unset_env("TZ", saved_tz, true) == 0);
-                tzset();
         }
 
         /* -06 */
@@ -842,6 +836,39 @@ TEST(parse_timestamp) {
                 test_parse_timestamp_one("30minutes ago", USEC_PER_MINUTE, now_usec - 30 * USEC_PER_MINUTE);
 }
 
+TEST(parse_timestamp) {
+        test_parse_timestamp_impl(NULL);
+}
+
+static void test_parse_timestamp_with_tz_one(const char *tz) {
+        const char *saved_tz, *colon_tz;
+
+        if (!timezone_is_valid(tz, LOG_DEBUG))
+                return;
+
+        log_info("/* %s(%s) */", __func__, tz);
+
+        saved_tz = getenv("TZ");
+
+        assert_se(colon_tz = strjoina(":", tz));
+        assert_se(setenv("TZ", colon_tz, 1) >= 0);
+        tzset();
+        log_debug("%s: tzname[0]=%s, tzname[1]=%s", tz, strempty(tzname[0]), strempty(tzname[1]));
+
+        test_parse_timestamp_impl(tz);
+
+        assert_se(set_unset_env("TZ", saved_tz, true) == 0);
+        tzset();
+}
+
+TEST(parse_timestamp_with_tz) {
+        _cleanup_strv_free_ char **timezones = NULL;
+
+        assert_se(get_timezones(&timezones) >= 0);
+        STRV_FOREACH(tz, timezones)
+                test_parse_timestamp_with_tz_one(*tz);
+}
+
 TEST(deserialize_dual_timestamp) {
         int r;
         dual_timestamp t;