1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

time-util: fix parsing timestamp with NZ timezone

Fixes a bug caused by ef658a63f8.
This commit is contained in:
Yu Watanabe 2024-10-08 18:59:37 +09:00
parent bf34d642bc
commit eb87d3e1e9
2 changed files with 37 additions and 2 deletions

View File

@ -994,8 +994,12 @@ int parse_timestamp(const char *t, usec_t *ret) {
assert(t);
t_len = strlen(t);
if (t_len > 2 && t[t_len - 1] == 'Z' && t[t_len - 2] != ' ') /* RFC3339-style welded UTC: "1985-04-12T23:20:50.52Z" */
return parse_timestamp_impl(t, t_len - 1, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ 0, ret);
if (t_len > 2 && t[t_len - 1] == 'Z') {
/* Try to parse as RFC3339-style welded UTC: "1985-04-12T23:20:50.52Z" */
r = parse_timestamp_impl(t, t_len - 1, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ 0, ret);
if (r >= 0)
return r;
}
if (t_len > 7 && IN_SET(t[t_len - 6], '+', '-') && t[t_len - 7] != ' ') { /* RFC3339-style welded offset: "1990-12-31T15:59:60-08:00" */
k = strptime(&t[t_len - 6], "%z", &tm);

View File

@ -857,6 +857,29 @@ static void test_parse_timestamp_impl(const char *tz) {
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);
@ -934,6 +957,14 @@ static void test_parse_timestamp_impl(const char *tz) {
test_parse_timestamp_one("yesterday", 0, today - USEC_PER_DAY);
}
/* with timezone */
if (tz) {
_cleanup_free_ char *s = NULL;
ASSERT_NOT_NULL(s = strjoin("Fri 2012-11-23 23:02:15 ", tz));
ASSERT_OK(parse_timestamp(s, NULL));
}
/* relative */
assert_se(parse_timestamp("now", &now_usec) == 0);
test_parse_timestamp_one("+5hours", USEC_PER_MINUTE, now_usec + 5 * USEC_PER_HOUR);