HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/test/test-time-util.c

66,713 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 4f9f5033e7fec8806601c21388d0e89e9cb50873

Lines 1–1000 of 1,373

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <stdlib.h>
 
#include "env-util.h"
#include "random-util.h"
#include "serialize.h"
#include "string-util.h"
#include "strv.h"
#include "tests.h"
#include "time-util.h"
 
#define TRIAL 100u
 
static void set_timezone(const char *tz) {
        ASSERT_OK(set_unset_env("TZ", tz, /* overwrite= */ true));
        tzset();
        log_info("TZ=%s, tzname[0]=%s, tzname[1]=%s", strna(getenv("TZ")), strempty(get_tzname(/* dst= */ false)), strempty(get_tzname(/* dst= */ true)));
}
 
TEST(parse_sec) {
        usec_t u;
 
        assert_se(parse_sec("5s", &u) >= 0);
        assert_se(u == 5 * USEC_PER_SEC);
        assert_se(parse_sec("5s500ms", &u) >= 0);
        assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
        assert_se(parse_sec(" 5s 500ms  ", &u) >= 0);
        assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
        assert_se(parse_sec(" 5.5s  ", &u) >= 0);
        assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
        assert_se(parse_sec(" 5.5s 0.5ms ", &u) >= 0);
        assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC + 500);
        assert_se(parse_sec(" .22s ", &u) >= 0);
        assert_se(u == 220 * USEC_PER_MSEC);
        assert_se(parse_sec(" .50y ", &u) >= 0);
        assert_se(u == USEC_PER_YEAR / 2);
        assert_se(parse_sec("2.5", &u) >= 0);
        assert_se(u == 2500 * USEC_PER_MSEC);
        assert_se(parse_sec(".7", &u) >= 0);
        assert_se(u == 700 * USEC_PER_MSEC);
        assert_se(parse_sec("23us", &u) >= 0);
        assert_se(u == 23);
        assert_se(parse_sec("23μs", &u) >= 0); /* greek small letter mu */
        assert_se(u == 23);
        assert_se(parse_sec("23µs", &u) >= 0); /* micro symbol */
        assert_se(u == 23);
        assert_se(parse_sec("infinity", &u) >= 0);
        assert_se(u == USEC_INFINITY);
        assert_se(parse_sec(" infinity ", &u) >= 0);
        assert_se(u == USEC_INFINITY);
        assert_se(parse_sec("+3.1s", &u) >= 0);
        assert_se(u == 3100 * USEC_PER_MSEC);
        assert_se(parse_sec("3.1s.2", &u) >= 0);
        assert_se(u == 3300 * USEC_PER_MSEC);
        assert_se(parse_sec("3.1 .2", &u) >= 0);
        assert_se(u == 3300 * USEC_PER_MSEC);
        assert_se(parse_sec("3.1 sec .2 sec", &u) >= 0);
        assert_se(u == 3300 * USEC_PER_MSEC);
        assert_se(parse_sec("3.1 sec 1.2 sec", &u) >= 0);
        assert_se(u == 4300 * USEC_PER_MSEC);
 
        assert_se(parse_sec(" xyz ", &u) < 0);
        assert_se(parse_sec("", &u) < 0);
        assert_se(parse_sec(" . ", &u) < 0);
        assert_se(parse_sec(" 5. ", &u) < 0);
        assert_se(parse_sec(".s ", &u) < 0);
        assert_se(parse_sec("-5s ", &u) < 0);
        assert_se(parse_sec("-0.3s ", &u) < 0);
        assert_se(parse_sec("-0.0s ", &u) < 0);
        assert_se(parse_sec("-0.-0s ", &u) < 0);
        assert_se(parse_sec("0.-0s ", &u) < 0);
        assert_se(parse_sec("3.-0s ", &u) < 0);
        assert_se(parse_sec(" infinity .7", &u) < 0);
        assert_se(parse_sec(".3 infinity", &u) < 0);
        assert_se(parse_sec("3.+1s", &u) < 0);
        assert_se(parse_sec("3. 1s", &u) < 0);
        assert_se(parse_sec("3.s", &u) < 0);
        assert_se(parse_sec("12.34.56", &u) < 0);
        assert_se(parse_sec("12..34", &u) < 0);
        assert_se(parse_sec("..1234", &u) < 0);
        assert_se(parse_sec("1234..", &u) < 0);
}
 
TEST(parse_sec_fix_0) {
        usec_t u;
 
        assert_se(parse_sec_fix_0("5s", &u) >= 0);
        assert_se(u == 5 * USEC_PER_SEC);
        assert_se(parse_sec_fix_0("0s", &u) >= 0);
        assert_se(u == USEC_INFINITY);
        assert_se(parse_sec_fix_0("0", &u) >= 0);
        assert_se(u == USEC_INFINITY);
        assert_se(parse_sec_fix_0(" 0", &u) >= 0);
        assert_se(u == USEC_INFINITY);
}
 
TEST(parse_sec_def_infinity) {
        usec_t u;
 
        assert_se(parse_sec_def_infinity("5s", &u) >= 0);
        assert_se(u == 5 * USEC_PER_SEC);
        assert_se(parse_sec_def_infinity("", &u) >= 0);
        assert_se(u == USEC_INFINITY);
        assert_se(parse_sec_def_infinity("     ", &u) >= 0);
        assert_se(u == USEC_INFINITY);
        assert_se(parse_sec_def_infinity("0s", &u) >= 0);
        assert_se(u == 0);
        assert_se(parse_sec_def_infinity("0", &u) >= 0);
        assert_se(u == 0);
        assert_se(parse_sec_def_infinity(" 0", &u) >= 0);
        assert_se(u == 0);
        assert_se(parse_sec_def_infinity("-5s", &u) < 0);
}
 
TEST(parse_time) {
        usec_t u;
 
        assert_se(parse_time("5", &u, 1) >= 0);
        assert_se(u == 5);
 
        assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0);
        assert_se(u == 5 * USEC_PER_MSEC);
 
        assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0);
        assert_se(u == 5 * USEC_PER_SEC);
 
        assert_se(parse_time("5s", &u, 1) >= 0);
        assert_se(u == 5 * USEC_PER_SEC);
 
        assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0);
        assert_se(u == 5 * USEC_PER_SEC);
 
        assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0);
        assert_se(u == 5 * USEC_PER_SEC);
 
        assert_se(parse_time("11111111111111y", &u, 1) == -ERANGE);
        assert_se(parse_time("1.1111111111111y", &u, 1) >= 0);
}
 
TEST(parse_nsec) {
        nsec_t u;
 
        assert_se(parse_nsec("5s", &u) >= 0);
        assert_se(u == 5 * NSEC_PER_SEC);
        assert_se(parse_nsec("5s500ms", &u) >= 0);
        assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
        assert_se(parse_nsec(" 5s 500ms  ", &u) >= 0);
        assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
        assert_se(parse_nsec(" 5.5s  ", &u) >= 0);
        assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
        assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0);
        assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC);
        assert_se(parse_nsec(" .22s ", &u) >= 0);
        assert_se(u == 220 * NSEC_PER_MSEC);
        assert_se(parse_nsec(" .50y ", &u) >= 0);
        assert_se(u == NSEC_PER_YEAR / 2);
        assert_se(parse_nsec("2.5", &u) >= 0);
        assert_se(u == 2);
        assert_se(parse_nsec(".7", &u) >= 0);
        assert_se(u == 0);
        assert_se(parse_nsec("infinity", &u) >= 0);
        assert_se(u == NSEC_INFINITY);
        assert_se(parse_nsec(" infinity ", &u) >= 0);
        assert_se(u == NSEC_INFINITY);
        assert_se(parse_nsec("+3.1s", &u) >= 0);
        assert_se(u == 3100 * NSEC_PER_MSEC);
        assert_se(parse_nsec("3.1s.2", &u) >= 0);
        assert_se(u == 3100 * NSEC_PER_MSEC);
        assert_se(parse_nsec("3.1 .2s", &u) >= 0);
        assert_se(u == 200 * NSEC_PER_MSEC + 3);
        assert_se(parse_nsec("3.1 sec .2 sec", &u) >= 0);
        assert_se(u == 3300 * NSEC_PER_MSEC);
        assert_se(parse_nsec("3.1 sec 1.2 sec", &u) >= 0);
        assert_se(u == 4300 * NSEC_PER_MSEC);
 
        assert_se(parse_nsec(" xyz ", &u) < 0);
        assert_se(parse_nsec("", &u) < 0);
        assert_se(parse_nsec(" . ", &u) < 0);
        assert_se(parse_nsec(" 5. ", &u) < 0);
        assert_se(parse_nsec(".s ", &u) < 0);
        assert_se(parse_nsec(" infinity .7", &u) < 0);
        assert_se(parse_nsec(".3 infinity", &u) < 0);
        assert_se(parse_nsec("-5s ", &u) < 0);
        assert_se(parse_nsec("-0.3s ", &u) < 0);
        assert_se(parse_nsec("-0.0s ", &u) < 0);
        assert_se(parse_nsec("-0.-0s ", &u) < 0);
        assert_se(parse_nsec("0.-0s ", &u) < 0);
        assert_se(parse_nsec("3.-0s ", &u) < 0);
        assert_se(parse_nsec(" infinity .7", &u) < 0);
        assert_se(parse_nsec(".3 infinity", &u) < 0);
        assert_se(parse_nsec("3.+1s", &u) < 0);
        assert_se(parse_nsec("3. 1s", &u) < 0);
        assert_se(parse_nsec("3.s", &u) < 0);
        assert_se(parse_nsec("12.34.56", &u) < 0);
        assert_se(parse_nsec("12..34", &u) < 0);
        assert_se(parse_nsec("..1234", &u) < 0);
        assert_se(parse_nsec("1234..", &u) < 0);
        assert_se(parse_nsec("1111111111111y", &u) == -ERANGE);
        assert_se(parse_nsec("1.111111111111y", &u) >= 0);
}
 
static void test_format_timespan_one(usec_t x, usec_t accuracy) {
        char l[FORMAT_TIMESPAN_MAX];
        const char *t;
        usec_t y;
 
        log_debug(USEC_FMT"     (at accuracy "USEC_FMT")", x, accuracy);
 
        assert_se(t = format_timespan(l, sizeof l, x, accuracy));
        log_debug(" = <%s>", t);
 
        assert_se(parse_sec(t, &y) >= 0);
        log_debug(" = "USEC_FMT, y);
 
        if (accuracy <= 0)
                accuracy = 1;
 
        assert_se(x / accuracy == y / accuracy);
}
 
static void test_format_timespan_accuracy(usec_t accuracy) {
        log_info("/* %s accuracy="USEC_FMT" */", __func__, accuracy);
 
        test_format_timespan_one(0, accuracy);
        test_format_timespan_one(1, accuracy);
        test_format_timespan_one(1*USEC_PER_SEC, accuracy);
        test_format_timespan_one(999*USEC_PER_MSEC, accuracy);
        test_format_timespan_one(1234567, accuracy);
        test_format_timespan_one(12, accuracy);
        test_format_timespan_one(123, accuracy);
        test_format_timespan_one(1234, accuracy);
        test_format_timespan_one(12345, accuracy);
        test_format_timespan_one(123456, accuracy);
        test_format_timespan_one(1234567, accuracy);
        test_format_timespan_one(12345678, accuracy);
        test_format_timespan_one(1200000, accuracy);
        test_format_timespan_one(1230000, accuracy);
        test_format_timespan_one(1234000, accuracy);
        test_format_timespan_one(1234500, accuracy);
        test_format_timespan_one(1234560, accuracy);
        test_format_timespan_one(1234567, accuracy);
        test_format_timespan_one(986087, accuracy);
        test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
        test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
        test_format_timespan_one(USEC_INFINITY, accuracy);
}
 
TEST(format_timespan) {
        test_format_timespan_accuracy(1);
        test_format_timespan_accuracy(USEC_PER_MSEC);
        test_format_timespan_accuracy(USEC_PER_SEC);
 
        /* See issue #23928. */
        _cleanup_free_ char *buf = NULL;
        assert_se(buf = new(char, 5));
        assert_se(buf == format_timespan(buf, 5, 100005, 1000));
}
 
TEST(verify_timezone) {
        assert_se(verify_timezone("Europe/Berlin", LOG_DEBUG) == 0);
        assert_se(verify_timezone("Australia/Sydney", LOG_DEBUG) == 0);
        assert_se(verify_timezone("Europe/Do not exist", LOG_DEBUG) == -EINVAL);
        assert_se(verify_timezone("Europe/DoNotExist", LOG_DEBUG) == -ENOENT);
        assert_se(verify_timezone("/DoNotExist", LOG_DEBUG) == -EINVAL);
        assert_se(verify_timezone("DoNotExist/", LOG_DEBUG) == -EINVAL);
}
 
TEST(timezone_is_valid) {
        assert_se(timezone_is_valid("Europe/Berlin", LOG_ERR));
        assert_se(timezone_is_valid("Australia/Sydney", LOG_ERR));
        assert_se(!timezone_is_valid("Europe/Do not exist", LOG_ERR));
}
 
TEST(get_timezones) {
        _cleanup_strv_free_ char **zones = NULL;
        int r;
 
        r = get_timezones(&zones);
        assert_se(r == 0);
 
        STRV_FOREACH(zone, zones) {
                r = verify_timezone(*zone, LOG_ERR);
                log_debug_errno(r, "verify_timezone(\"%s\"): %m", *zone);
                assert_se(r >= 0 || r == -ENOENT);
        }
}
 
TEST(usec_add) {
        assert_se(usec_add(0, 0) == 0);
        assert_se(usec_add(1, 4) == 5);
        assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY);
        assert_se(usec_add(5, USEC_INFINITY) == USEC_INFINITY);
        assert_se(usec_add(USEC_INFINITY-5, 2) == USEC_INFINITY-3);
        assert_se(usec_add(USEC_INFINITY-2, 2) == USEC_INFINITY);
        assert_se(usec_add(USEC_INFINITY-1, 2) == USEC_INFINITY);
        assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY);
}
 
TEST(usec_sub_unsigned) {
        assert_se(usec_sub_unsigned(0, 0) == 0);
        assert_se(usec_sub_unsigned(0, 2) == 0);
        assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0);
        assert_se(usec_sub_unsigned(1, 0) == 1);
        assert_se(usec_sub_unsigned(1, 1) == 0);
        assert_se(usec_sub_unsigned(1, 2) == 0);
        assert_se(usec_sub_unsigned(1, 3) == 0);
        assert_se(usec_sub_unsigned(1, USEC_INFINITY) == 0);
        assert_se(usec_sub_unsigned(USEC_INFINITY-1, 0) == USEC_INFINITY-1);
        assert_se(usec_sub_unsigned(USEC_INFINITY-1, 1) == USEC_INFINITY-2);
        assert_se(usec_sub_unsigned(USEC_INFINITY-1, 2) == USEC_INFINITY-3);
        assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-2) == 1);
        assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-1) == 0);
        assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY) == 0);
        assert_se(usec_sub_unsigned(USEC_INFINITY, 0) == USEC_INFINITY);
        assert_se(usec_sub_unsigned(USEC_INFINITY, 1) == USEC_INFINITY);
        assert_se(usec_sub_unsigned(USEC_INFINITY, 2) == USEC_INFINITY);
        assert_se(usec_sub_unsigned(USEC_INFINITY, USEC_INFINITY) == USEC_INFINITY);
}
 
TEST(usec_sub_signed) {
        assert_se(usec_sub_signed(0, 0) == 0);
        assert_se(usec_sub_signed(4, 1) == 3);
        assert_se(usec_sub_signed(4, 4) == 0);
        assert_se(usec_sub_signed(4, 5) == 0);
 
        assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY);
        assert_se(usec_sub_signed(USEC_INFINITY-3, -4) == USEC_INFINITY);
        assert_se(usec_sub_signed(USEC_INFINITY-3, -5) == USEC_INFINITY);
        assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY);
 
        assert_se(usec_sub_signed(0, INT64_MAX) == 0);
        assert_se(usec_sub_signed(0, -INT64_MAX) == INT64_MAX);
        assert_se(usec_sub_signed(0, INT64_MIN) == (usec_t) INT64_MAX + 1);
        assert_se(usec_sub_signed(0, -(INT64_MIN+1)) == 0);
 
        assert_se(usec_sub_signed(USEC_INFINITY, INT64_MAX) == USEC_INFINITY);
        assert_se(usec_sub_signed(USEC_INFINITY, -INT64_MAX) == USEC_INFINITY);
        assert_se(usec_sub_signed(USEC_INFINITY, INT64_MIN) == USEC_INFINITY);
        assert_se(usec_sub_signed(USEC_INFINITY, -(INT64_MIN+1)) == USEC_INFINITY);
 
        assert_se(usec_sub_signed(USEC_INFINITY-1, INT64_MAX) == USEC_INFINITY-1-INT64_MAX);
        assert_se(usec_sub_signed(USEC_INFINITY-1, -INT64_MAX) == USEC_INFINITY);
        assert_se(usec_sub_signed(USEC_INFINITY-1, INT64_MIN) == USEC_INFINITY);
        assert_se(usec_sub_signed(USEC_INFINITY-1, -(INT64_MIN+1)) == USEC_INFINITY-1-((usec_t) (-(INT64_MIN+1))));
}
 
TEST(format_timestamp) {
        for (unsigned i = 0; i < TRIAL; i++) {
                char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
                usec_t x, y;
 
                x = random_u64_range(USEC_TIMESTAMP_FORMATTABLE_MAX - USEC_PER_SEC) + USEC_PER_SEC;
 
                assert_se(format_timestamp(buf, sizeof(buf), x));
                log_debug("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
                assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
 
                assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UNIX));
                log_debug("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
                assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
 
                assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UTC));
                log_debug("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
                assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
 
                assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US));
                log_debug("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
                assert_se(x == y);
 
                assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US_UTC));
                log_debug("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
                assert_se(x == y);
 
                if (x > 2 * USEC_PER_DAY) {
                        assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_DATE));
                        log_debug("%s", buf);
                        assert_se(parse_timestamp(buf, &y) >= 0);
                        assert_se(y > usec_sub_unsigned(x, 2 * USEC_PER_DAY) && y < usec_add(x, 2 * USEC_PER_DAY));
                }
 
                assert_se(format_timestamp_relative(buf, sizeof(buf), x));
                log_debug("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
 
                /* The two calls above will run with a slightly different local time. Make sure we are in the same
                 * range however, but give enough leeway that this is unlikely to explode. And of course,
                 * format_timestamp_relative() scales the accuracy with the distance from the current time up to one
                 * month, cover for that too. */
                assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY);
        }
}
 
static void test_format_timestamp_impl(usec_t x) {
        const char *xx = FORMAT_TIMESTAMP(x);
        ASSERT_NOT_NULL(xx);
 
        /* Because of the timezone change, format_timestamp() may set timezone that is currently unused.
         * E.g. Africa/Juba uses EAT since Sat Jan 15 10:00:00 2000 and until Sun Jan 31 20:59:59 2021, but
         * now CAT/CAST is used there (see zdump for more details). In such cases, format_timestamp() may set
         * the timezone used at the specified time (which happens when built with musl), but it may not match
         * the timezone currently used, thus we may not parse back the timestamp. */
 
        const char *space;
        ASSERT_NOT_NULL(space = strrchr(xx, ' '));
 
        const char *tz = space + 1;
        if (!streq_ptr(tz, get_tzname(/* dst= */ false)) &&
            !streq_ptr(tz, get_tzname(/* dst= */ true)) &&
            parse_gmtoff(tz, NULL) < 0) {
 
                log_warning("@" USEC_FMT " → %s, timezone '%s' is currently unused, ignoring.", x, xx, tz);
 
                /* Verify the generated string except for the timezone part. Of course, in most cases, parsed
                 * time does not match with the input, hence only check if it is parsable. */
                ASSERT_OK(parse_timestamp(strndupa_safe(xx, space - xx), NULL));
                return;
        }
 
        usec_t y;
        ASSERT_OK(parse_timestamp(xx, &y));
        const char *yy = FORMAT_TIMESTAMP(y);
        ASSERT_NOT_NULL(yy);
 
        usec_t x_sec = x / USEC_PER_SEC;
        usec_t y_sec = y / USEC_PER_SEC;
 
        if (x_sec == y_sec && streq(xx, yy))
                return; /* Yay! */
 
        /* There are two classes of known round-trip failures that produce exactly a 1h offset:
         *
         * 1) The formatted abbreviation doesn't match the current timezone's abbreviations - the timestamp
         *    is from a historical era (e.g. Africa/Tripoli switched between CET and EET multiple times
         *    historically, America/Cancun switched between EST/EDT and CST/CDT several times in the past,
         *    etc.), and the round-trip is inherently unreliable on platforms where parse_gmtoff() resolves
         *    such abbreviations with incorrect offsets.
         *
         * 2) Rearguard/vanguard database format differences where the abbreviation matches but the
         *    offset is still wrong (e.g. Africa/Windhoek, Europe/Kirov, Europe/Volgograd).
         *
         * See:
         *   - https://github.com/systemd/systemd/issues/28472
         *   - https://github.com/systemd/systemd/pull/35471
         *   - https://github.com/systemd/systemd/issues/37684
         */
        bool ignore =
                ((!streq_ptr(tz, get_tzname(/* dst= */ false)) &&
                  !streq_ptr(tz, get_tzname(/* dst= */ true))) ||
                 streq_ptr(getenv("TZ"), "Africa/Windhoek") ||
                 STRPTR_IN_SET(get_tzname(/* dst= */ false), "MSK", "WET")) &&
                (x_sec > y_sec ? x_sec - y_sec : y_sec - x_sec) == 3600;
 
        log_full(ignore ? LOG_WARNING : LOG_ERR,
                 "@" USEC_FMT " → %s → @" USEC_FMT " → %s%s",
                 x, xx, y, yy,
                 ignore ? ", ignoring." : "");
 
        ASSERT_TRUE(ignore);
}
 
static void test_format_timestamp_loop(void) {
        test_format_timestamp_impl(USEC_PER_DAY + USEC_PER_SEC);
        test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX_32BIT-1);
        test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX_32BIT);
        test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX-1);
        test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX);
 
        /* Specific timestamps known to cause a 1h round-trip discrepancy with certain timezones:
         *
         * Two cases which trigger https://github.com/systemd/systemd/issues/28472. */
        test_format_timestamp_impl(1504938962980066);
        test_format_timestamp_impl(1509482094632752);
        /* With tzdata-2025c, the timestamp (randomly?) fails on MSK time zone (e.g. Europe/Volgograd). */
        test_format_timestamp_impl(1414277092997572);
        /* Africa/Tripoli (Libya) switched from CET to EET multiple times in the past. */
        test_format_timestamp_impl(378687574661411);
        /* America/Cancun switched from EST/EDT to CST/CDT multiple times in the past. */
        test_format_timestamp_impl(902035565603993);
 
        for (unsigned i = 0; i < TRIAL; i++) {
                usec_t x;
 
                x = random_u64_range(USEC_TIMESTAMP_FORMATTABLE_MAX - USEC_PER_SEC) + USEC_PER_SEC;
                test_format_timestamp_impl(x);
        }
}
 
TEST(FORMAT_TIMESTAMP) {
        test_format_timestamp_loop();
}
 
static void test_format_timestamp_with_tz_one(const char *tz) {
        if (!timezone_is_valid(tz, LOG_DEBUG))
                return;
 
        SAVE_TIMEZONE;
        set_timezone(tz);
 
        test_format_timestamp_loop();
}
 
TEST(FORMAT_TIMESTAMP_with_tz) {
        _cleanup_strv_free_ char **timezones = NULL;
 
        test_format_timestamp_with_tz_one("UTC");
 
        if (!slow_tests_enabled())
                return (void) log_tests_skipped("slow tests are disabled");
 
        assert_se(get_timezones(&timezones) >= 0);
        STRV_FOREACH(tz, timezones)
                test_format_timestamp_with_tz_one(*tz);
}
 
TEST(format_timestamp_relative_full) {
        char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
        usec_t x;
 
        /* Years and months */
        x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 1*USEC_PER_MONTH);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 year 1 month ago");
 
        x = now(CLOCK_MONOTONIC) + (1*USEC_PER_YEAR + 1.5*USEC_PER_MONTH);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_MONOTONIC, false));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 year 1 month left");
 
        x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 2*USEC_PER_MONTH);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 year 2 months ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 1*USEC_PER_MONTH);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 years 1 month ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 2*USEC_PER_MONTH);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 years 2 months ago");
 
        /* Months and days */
        x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 1*USEC_PER_DAY);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 month 1 day ago");
 
        x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 2*USEC_PER_DAY);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 month 2 days ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 1*USEC_PER_DAY);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 months 1 day ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 2*USEC_PER_DAY);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 months 2 days ago");
 
        /* Weeks and days */
        x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 1*USEC_PER_DAY);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 week 1 day ago");
 
        x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 2*USEC_PER_DAY);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 week 2 days ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 1*USEC_PER_DAY);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 weeks 1 day ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 2*USEC_PER_DAY);
        assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 weeks 2 days ago");
}
 
TEST(format_timestamp_relative) {
        char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
        usec_t x;
 
        /* Only testing timestamps in the past so we don't need to add some delta to account for time passing
         * by while we are running the tests (unless we're running on potatoes and 24 hours somehow passes
         * between our call to now() and format_timestamp_relative's call to now()). */
 
        /* Years and months */
        x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 1*USEC_PER_MONTH);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 year 1 month ago");
 
        x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 2*USEC_PER_MONTH);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 year 2 months ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 1*USEC_PER_MONTH);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 years 1 month ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 2*USEC_PER_MONTH);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 years 2 months ago");
 
        /* Months and days */
        x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 1*USEC_PER_DAY);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 month 1 day ago");
 
        x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 2*USEC_PER_DAY);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 month 2 days ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 1*USEC_PER_DAY);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 months 1 day ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 2*USEC_PER_DAY);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 months 2 days ago");
 
        /* Weeks and days */
        x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 1*USEC_PER_DAY);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 week 1 day ago");
 
        x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 2*USEC_PER_DAY);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "1 week 2 days ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 1*USEC_PER_DAY);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 weeks 1 day ago");
 
        x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 2*USEC_PER_DAY);
        assert_se(format_timestamp_relative(buf, sizeof(buf), x));
        log_debug("%s", buf);
        ASSERT_STREQ(buf, "2 weeks 2 days ago");
}
 
static void test_format_timestamp_one(usec_t val, TimestampStyle style, const char *result) {
        char buf[FORMAT_TIMESTAMP_MAX];
        const char *t;
 
        t = format_timestamp_style(buf, sizeof(buf), val, style);
        ASSERT_STREQ(t, result);
}
 
TEST(format_timestamp_range) {
        test_format_timestamp_one(0, TIMESTAMP_UTC, NULL);
        test_format_timestamp_one(0, TIMESTAMP_DATE, NULL);
        test_format_timestamp_one(0, TIMESTAMP_US_UTC, NULL);
 
        test_format_timestamp_one(1, TIMESTAMP_UTC, "Thu 1970-01-01 00:00:00 UTC");
        test_format_timestamp_one(1, TIMESTAMP_DATE, "Thu 1970-01-01");
        test_format_timestamp_one(1, TIMESTAMP_US_UTC, "Thu 1970-01-01 00:00:00.000001 UTC");
 
        test_format_timestamp_one(USEC_PER_SEC, TIMESTAMP_UTC, "Thu 1970-01-01 00:00:01 UTC");
        test_format_timestamp_one(USEC_PER_SEC, TIMESTAMP_DATE, "Thu 1970-01-01");
        test_format_timestamp_one(USEC_PER_SEC, TIMESTAMP_US_UTC, "Thu 1970-01-01 00:00:01.000000 UTC");
 
#if SIZEOF_TIME_T == 8
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_UTC, "Thu 9999-12-30 23:59:59 UTC");
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_DATE, "Thu 9999-12-30");
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_UTC, "--- XXXX-XX-XX XX:XX:XX UTC");
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_US_UTC, "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC");
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_DATE, "--- XXXX-XX-XX");
#elif SIZEOF_TIME_T == 4
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_UTC, "Mon 2038-01-18 03:14:07 UTC");
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_DATE, "Mon 2038-01-18");
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_UTC, "--- XXXX-XX-XX XX:XX:XX UTC");
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_US_UTC, "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC");
        test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_DATE, "--- XXXX-XX-XX");
#endif
 
        test_format_timestamp_one(USEC_INFINITY, TIMESTAMP_UTC, NULL);
}
 
TEST(parse_gmtoff) {
        long t;
 
        ASSERT_OK(parse_gmtoff("+14", &t));
        ASSERT_EQ(t, (long) (14 * USEC_PER_HOUR / USEC_PER_SEC));
        ASSERT_OK(parse_gmtoff("-09", &t));
        ASSERT_EQ(t, - (long) (9 * USEC_PER_HOUR / USEC_PER_SEC));
        ASSERT_OK(parse_gmtoff("+1400", &t));
        ASSERT_EQ(t, (long) (14 * USEC_PER_HOUR / USEC_PER_SEC));
        ASSERT_OK(parse_gmtoff("-0900", &t));
        ASSERT_EQ(t, - (long) (9 * USEC_PER_HOUR / USEC_PER_SEC));
        ASSERT_OK(parse_gmtoff("+14:00", &t));
        ASSERT_EQ(t, (long) (14 * USEC_PER_HOUR / USEC_PER_SEC));
        ASSERT_OK(parse_gmtoff("-09:00", &t));
        ASSERT_EQ(t, - (long) (9 * USEC_PER_HOUR / USEC_PER_SEC));
 
        ASSERT_ERROR(parse_gmtoff("", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("UTC", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("09", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("0900", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("?0900", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("?0900", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("+0900abc", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("+0900 ", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("+090000", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("+0900:00", &t), EINVAL);
        ASSERT_ERROR(parse_gmtoff("+0900.00", &t), EINVAL);
}
 
static void test_parse_timestamp_one(const char *str, usec_t max_diff, usec_t expected) {
        usec_t usec = USEC_INFINITY;
        int r;
 
        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);
}
 
static bool time_is_zero(usec_t usec) {
        const char *s;
 
        s = FORMAT_TIMESTAMP(usec);
        return strstr(s, " 00:00:00 ");
}
 
static bool timezone_equal(usec_t today, usec_t target) {
        const char *s, *t, *sz, *tz;
 
        s = FORMAT_TIMESTAMP(today);
        t = FORMAT_TIMESTAMP(target);
        assert_se(sz = strrchr(s, ' '));
        assert_se(tz = strrchr(t, ' '));
        log_debug("%s("USEC_FMT", "USEC_FMT") -> %s, %s", __func__, today, target, s, t);
        return streq(sz, tz);
}
 
/* On the day DST ends 00:00:00 may exist twice (e.g. Atlantic/Azores: 00:59:59 +00 -> 00:00:00 -01), and
 * mktime() with tm_isdst=-1 returns the first instant on musl but the second on glibc. */
static bool midnight_is_unambiguous(usec_t midnight) {
        return timezone_equal(midnight, midnight - USEC_PER_HOUR) &&
                timezone_equal(midnight, midnight + USEC_PER_HOUR);
}
 
static void test_parse_timestamp_impl(const char *tz) {
        usec_t today, today2, now_usec;
 
        /* Invalid: Ensure that systemctl reboot --when=show and --when=cancel
         * will not result in ambiguities */
        assert_se(parse_timestamp("show", NULL) == -EINVAL);
        assert_se(parse_timestamp("cancel", NULL) == -EINVAL);
 
        /* UTC */
        test_parse_timestamp_one("Thu 1970-01-01 00:01 UTC", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("Thu 1970-01-01 00:00:01 UTC", 0, USEC_PER_SEC);
        test_parse_timestamp_one("Thu 1970-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("Thu 1970-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000);
 
        test_parse_timestamp_one("Thu 70-01-01 00:01 UTC", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("Thu 70-01-01 00:00:01 UTC", 0, USEC_PER_SEC);
        test_parse_timestamp_one("Thu 70-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("Thu 70-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000);
 
        test_parse_timestamp_one("1970-01-01 00:01 UTC", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("1970-01-01 00:00:01 UTC", 0, USEC_PER_SEC);
        test_parse_timestamp_one("1970-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("1970-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000);
 
        test_parse_timestamp_one("70-01-01 00:01 UTC", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("70-01-01 00:00:01 UTC", 0, USEC_PER_SEC);
        test_parse_timestamp_one("70-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("70-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000);
 
        /* Examples from RFC3339 */
        test_parse_timestamp_one("1985-04-12T23:20:50.52Z", 0, 482196050 * USEC_PER_SEC + 520000);
        test_parse_timestamp_one("1996-12-19T16:39:57-08:00", 0, 851042397 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("1996-12-20T00:39:57Z", 0, 851042397 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("1990-12-31T23:59:60Z", 0, 662688000 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("1990-12-31T15:59:60-08:00", 0, 662688000 * USEC_PER_SEC + 000000);
        assert_se(parse_timestamp("1937-01-01T12:00:27.87+00:20", NULL) == -ERANGE); /* we don't support pre-epoch timestamps */
        /* We accept timestamps without seconds as well */
        test_parse_timestamp_one("1996-12-20T00:39Z", 0, (851042397 - 57) * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("1990-12-31T15:59-08:00", 0, (662688000-60) * USEC_PER_SEC + 000000);
        /* We drop day-of-week before parsing the timestamp */
        test_parse_timestamp_one("Thu 1970-01-01T00:01 UTC", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("Thu 1970-01-01T00:00:01 UTC", 0, USEC_PER_SEC);
        test_parse_timestamp_one("Thu 1970-01-01T00:01Z", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("Thu 1970-01-01T00:00:01Z", 0, USEC_PER_SEC);
        /* RFC3339-style timezones can be welded to all formats */
        assert_se(parse_timestamp("today UTC", &today) == 0);
        assert_se(parse_timestamp("todayZ", &today2) == 0);
        assert_se(today == today2);
        assert_se(parse_timestamp("today +0200", &today) == 0);
        assert_se(parse_timestamp("today+02:00", &today2) == 0);
        assert_se(today == today2);
 
        /* https://ijmacd.github.io/rfc3339-iso8601/ */
        test_parse_timestamp_one("2023-09-06 12:49:27-00:00", 0, 1694004567 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("2023-09-06 12:49:27.284-00:00", 0, 1694004567 * USEC_PER_SEC + 284000);
        test_parse_timestamp_one("2023-09-06 12:49:27.284029Z", 0, 1694004567 * USEC_PER_SEC + 284029);
        test_parse_timestamp_one("2023-09-06 12:49:27.284Z", 0, 1694004567 * USEC_PER_SEC + 284000);
        test_parse_timestamp_one("2023-09-06 12:49:27.28Z", 0, 1694004567 * USEC_PER_SEC + 280000);
        test_parse_timestamp_one("2023-09-06 12:49:27.2Z", 0, 1694004567 * USEC_PER_SEC + 200000);
        test_parse_timestamp_one("2023-09-06 12:49:27Z", 0, 1694004567 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("2023-09-06 14:49:27+02:00", 0, 1694004567 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("2023-09-06 14:49:27.2+02:00", 0, 1694004567 * USEC_PER_SEC + 200000);
        test_parse_timestamp_one("2023-09-06 14:49:27.28+02:00", 0, 1694004567 * USEC_PER_SEC + 280000);
        test_parse_timestamp_one("2023-09-06 14:49:27.284+02:00", 0, 1694004567 * USEC_PER_SEC + 284000);
        test_parse_timestamp_one("2023-09-06 14:49:27.284029+02:00", 0, 1694004567 * USEC_PER_SEC + 284029);
        test_parse_timestamp_one("2023-09-06T12:49:27+00:00", 0, 1694004567 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("2023-09-06T12:49:27-00:00", 0, 1694004567 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("2023-09-06T12:49:27.284+00:00", 0, 1694004567 * USEC_PER_SEC + 284000);
        test_parse_timestamp_one("2023-09-06T12:49:27.284-00:00", 0, 1694004567 * USEC_PER_SEC + 284000);
        test_parse_timestamp_one("2023-09-06T12:49:27.284029Z", 0, 1694004567 * USEC_PER_SEC + 284029);
        test_parse_timestamp_one("2023-09-06T12:49:27.284Z", 0, 1694004567 * USEC_PER_SEC + 284000);
        test_parse_timestamp_one("2023-09-06T12:49:27.28Z", 0, 1694004567 * USEC_PER_SEC + 280000);
        test_parse_timestamp_one("2023-09-06T12:49:27.2Z", 0, 1694004567 * USEC_PER_SEC + 200000);
        test_parse_timestamp_one("2023-09-06T12:49:27Z", 0, 1694004567 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("2023-09-06T14:49:27+02:00", 0, 1694004567 * USEC_PER_SEC + 000000);
        test_parse_timestamp_one("2023-09-06T14:49:27.284+02:00", 0, 1694004567 * USEC_PER_SEC + 284000);
        test_parse_timestamp_one("2023-09-06T14:49:27.284029+02:00", 0, 1694004567 * USEC_PER_SEC + 284029);
        test_parse_timestamp_one("2023-09-06T21:34:27+08:45", 0, 1694004567 * USEC_PER_SEC + 000000);
 
        if (timezone_is_valid("Asia/Tokyo", LOG_DEBUG)) {
                /* Asia/Tokyo (+0900) */
                test_parse_timestamp_one("Thu 1970-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("Thu 1970-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC);
                test_parse_timestamp_one("Thu 1970-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Thu 1970-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("Thu 70-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("Thu 70-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC);
                test_parse_timestamp_one("Thu 70-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Thu 70-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("1970-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("1970-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC);
                test_parse_timestamp_one("1970-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("1970-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("70-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE);
                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);
        }
 
        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);
                test_parse_timestamp_one("Thu 1970-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Thu 1970-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("Thu 70-01-01 09:01 JST", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("Thu 70-01-01 09:00:01 JST", 0, USEC_PER_SEC);
                test_parse_timestamp_one("Thu 70-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Thu 70-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("1970-01-01 09:01 JST", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("1970-01-01 09:00:01 JST", 0, USEC_PER_SEC);
                test_parse_timestamp_one("1970-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("1970-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("70-01-01 09:01 JST", 0, USEC_PER_MINUTE);
                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);
        }
 
        if (timezone_is_valid("America/New_York", LOG_DEBUG)) {
                /* America/New_York (-0500) */
                test_parse_timestamp_one("Wed 1969-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("Wed 1969-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC);
                test_parse_timestamp_one("Wed 1969-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Wed 1969-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("Wed 69-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("Wed 69-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC);
                test_parse_timestamp_one("Wed 69-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Wed 69-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("1969-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("1969-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC);
                test_parse_timestamp_one("1969-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("1969-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("69-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE);
                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);
        }
 
        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);
                test_parse_timestamp_one("Wed 1969-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Wed 1969-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("Wed 69-12-31 19:01 EST", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("Wed 69-12-31 19:00:01 EST", 0, USEC_PER_SEC);
                test_parse_timestamp_one("Wed 69-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Wed 69-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("1969-12-31 19:01 EST", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("1969-12-31 19:00:01 EST", 0, USEC_PER_SEC);
                test_parse_timestamp_one("1969-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("1969-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("69-12-31 19:01 EST", 0, USEC_PER_MINUTE);
                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);
        }
 
        if (timezone_is_valid("NZ", LOG_DEBUG)) {
                /* NZ (+1200) */
                test_parse_timestamp_one("Thu 1970-01-01 12:01 NZ", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("Thu 1970-01-01 12:00:01 NZ", 0, USEC_PER_SEC);
                test_parse_timestamp_one("Thu 1970-01-01 12:00:01.001 NZ", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Thu 1970-01-01 12:00:01.0010 NZ", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("Thu 70-01-01 12:01 NZ", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("Thu 70-01-01 12:00:01 NZ", 0, USEC_PER_SEC);
                test_parse_timestamp_one("Thu 70-01-01 12:00:01.001 NZ", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("Thu 70-01-01 12:00:01.0010 NZ", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("1970-01-01 12:01 NZ", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("1970-01-01 12:00:01 NZ", 0, USEC_PER_SEC);
                test_parse_timestamp_one("1970-01-01 12:00:01.001 NZ", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("1970-01-01 12:00:01.0010 NZ", 0, USEC_PER_SEC + 1000);
 
                test_parse_timestamp_one("70-01-01 12:01 NZ", 0, USEC_PER_MINUTE);
                test_parse_timestamp_one("70-01-01 12:00:01 NZ", 0, USEC_PER_SEC);
                test_parse_timestamp_one("70-01-01 12:00:01.001 NZ", 0, USEC_PER_SEC + 1000);
                test_parse_timestamp_one("70-01-01 12:00:01.0010 NZ", 0, USEC_PER_SEC + 1000);
        }
 
        /* -06 */
        test_parse_timestamp_one("Wed 1969-12-31 18:01 -06", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("Wed 1969-12-31 18:00:01 -06", 0, USEC_PER_SEC);
        test_parse_timestamp_one("Wed 1969-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("Wed 1969-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000);
 
        test_parse_timestamp_one("Wed 69-12-31 18:01 -06", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("Wed 69-12-31 18:00:01 -06", 0, USEC_PER_SEC);
        test_parse_timestamp_one("Wed 69-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("Wed 69-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000);
 
        test_parse_timestamp_one("1969-12-31 18:01 -06", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("1969-12-31 18:00:01 -06", 0, USEC_PER_SEC);
        test_parse_timestamp_one("1969-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("1969-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000);
 
        test_parse_timestamp_one("69-12-31 18:01 -06", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("69-12-31 18:00:01 -06", 0, USEC_PER_SEC);
        test_parse_timestamp_one("69-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("69-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000);
 
        /* -0600 */
        test_parse_timestamp_one("Wed 1969-12-31 18:01 -0600", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("Wed 1969-12-31 18:00:01 -0600", 0, USEC_PER_SEC);
        test_parse_timestamp_one("Wed 1969-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("Wed 1969-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000);
 
        test_parse_timestamp_one("Wed 69-12-31 18:01 -0600", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("Wed 69-12-31 18:00:01 -0600", 0, USEC_PER_SEC);
        test_parse_timestamp_one("Wed 69-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("Wed 69-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000);
 
        test_parse_timestamp_one("1969-12-31 18:01 -0600", 0, USEC_PER_MINUTE);
        test_parse_timestamp_one("1969-12-31 18:00:01 -0600", 0, USEC_PER_SEC);
        test_parse_timestamp_one("1969-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000);
        test_parse_timestamp_one("1969-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000);
 
        test_parse_timestamp_one("69-12-31 18:01 -0600", 0, USEC_PER_MINUTE);