From 5ab2da3dcacd162a04455f82abf6d1bd1f194463 Mon Sep 17 00:00:00 2001 From: Abel George Antony Date: Fri, 4 Dec 2026 18:21:58 +0530 Subject: [PATCH 1/2] Add POSIX TZ support for WASI --- libc-top-half/musl/include/time.h | 8 +- libc-top-half/musl/src/time/__tz.c | 584 ++++++++++++++++++++++- libc-top-half/musl/src/time/strptime.c | 2 - test/CMakeLists.txt | 1 + test/src/timezone.c | 636 +++++++++++++++++++++++++ 5 files changed, 1214 insertions(+), 17 deletions(-) create mode 100644 test/src/timezone.c diff --git a/libc-top-half/musl/include/time.h b/libc-top-half/musl/include/time.h index f53414878..d617f2099 100644 --- a/libc-top-half/musl/include/time.h +++ b/libc-top-half/musl/include/time.h @@ -97,9 +97,7 @@ struct tm *localtime_r (const time_t *__restrict, struct tm *__restrict); char *asctime_r (const struct tm *__restrict, char *__restrict); char *ctime_r (const time_t *, char *); -#ifdef __wasilibc_unmodified_upstream /* WASI has no timezone tables */ void tzset (void); -#endif struct itimerspec { struct timespec it_interval; @@ -143,19 +141,17 @@ int timer_gettime (timer_t, struct itimerspec *); int timer_getoverrun (timer_t); #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no timezone tables */ extern char *tzname[2]; -#endif #endif #if defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE) char *strptime (const char *__restrict, const char *__restrict, struct tm *__restrict); -#ifdef __wasilibc_unmodified_upstream /* WASI has no timezone tables */ + extern int daylight; extern long timezone; -#endif + extern int getdate_err; struct tm *getdate (const char *); #endif diff --git a/libc-top-half/musl/src/time/__tz.c b/libc-top-half/musl/src/time/__tz.c index 7f2e196d4..6a6fa27f2 100644 --- a/libc-top-half/musl/src/time/__tz.c +++ b/libc-top-half/musl/src/time/__tz.c @@ -5,8 +5,8 @@ #include #ifdef __wasilibc_unmodified_upstream // timezone data #include -#include #endif +#include #include "libc.h" #include "lock.h" #include "fork_impl.h" @@ -430,18 +430,561 @@ static void __tzset() weak_alias(__tzset, tzset); #else -void __secs_to_zone(long long t, int local, int *isdst, int *offset, long *oppoff, const char **zonename) +long __timezone = 0; +int __daylight = 0; +char *__tzname[2] = { 0, 0 }; + +weak_alias(__timezone, timezone); +weak_alias(__daylight, daylight); +weak_alias(__tzname, tzname); + +static char std_name[TZNAME_MAX + 1]; +static char dst_name[TZNAME_MAX + 1]; + +static int dst_off; +static int r0[5], r1[5]; + +static char old_tz_buf[32]; +static char *old_tz = old_tz_buf; +static size_t old_tz_size = sizeof old_tz_buf; + +/* + * Timezone state is shared by localtime_r(), mktime(), and tzset(). + * A strong lock also protects configurations where an operation inside + * the critical section can cause a context switch. + */ +DECLARE_STRONG_LOCK(tz_lock, static); + + + +/* + * Parse POSIX proleptic TZ specifications. + * + * The parser builds a temporary timezone state. The global timezone + * variables are updated only after the entire specification is valid. + */ + +struct tz_state { + char std_name[TZNAME_MAX + 1]; + char dst_name[TZNAME_MAX + 1]; + long timezone; + int daylight; + int dst_off; + int r0[5]; + int r1[5]; +}; + +static int is_ascii_digit(unsigned char c) +{ + return c >= '0' && c <= '9'; +} + +static int is_ascii_alpha(unsigned char c) { - // Minimalist implementation for now. + return (c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z'); +} + +/* + * Parse a decimal number containing one or more digits. + * Leave the input pointer at the first character after the number. + * Reject values that cannot be represented by int. + */ +static int parse_uint(const char **p, int *value) +{ + int n = 0; + int digits = 0; + + while (is_ascii_digit((unsigned char)**p)) { + int digit = **p - '0'; + + if (n > (INT_MAX - digit) / 10) + return 0; + + n = n * 10 + digit; + digits++; + (*p)++; + } + + if (!digits) + return 0; + + *value = n; + return 1; +} + +/* + * Parse an offset or transition time in hh[:mm[:ss]] form. + * + * Standard UTC offsets use hours 0 through 24. + * POSIX.1-2024 transition times use signed hours -167 through 167. + * + * The returned value is signed seconds. For UTC offsets, positive means + * seconds west of UTC. For transition times, positive means later than + * midnight on the specified transition date. + */ +static int parse_hms( + const char **p, + int max_hour, + int allow_sign, + int *value +) +{ + int sign = 1; + int hour, minute = 0, second = 0; + + if (**p == '+' || **p == '-') { + if (!allow_sign) + return 0; + + if (**p == '-') + sign = -1; + + (*p)++; + } + + if (!parse_uint(p, &hour) || hour > max_hour) + return 0; + + if (**p == ':') { + (*p)++; + + if (!parse_uint(p, &minute) || minute > 59) + return 0; + + if (**p == ':') { + (*p)++; + + if (!parse_uint(p, &second) || second > 59) + return 0; + } + } + + *value = sign * (hour * 3600 + minute * 60 + second); + return 1; +} + +/* + * Parse an unquoted alphabetic or quoted POSIX timezone abbreviation. + * + * Quoted abbreviations may contain ASCII letters, digits, '+' and '-'. + * The complete name is consumed, but stored names are limited to + * Names longer than TZNAME_MAX are rejected. + */ +static int parse_name(char *dst, const char **p) +{ + const char *start; + size_t len, copy_len; + + if (**p == '<') { + (*p)++; + start = *p; + + while (**p && **p != '>') { + unsigned char c = **p; + + if (!is_ascii_alpha(c) && + !is_ascii_digit(c) && + c != '+' && c != '-') + return 0; + + (*p)++; + } + + if (**p != '>') + return 0; + + len = *p - start; + (*p)++; + } else { + start = *p; + + while (is_ascii_alpha((unsigned char)**p)) + (*p)++; + + len = *p - start; + } + + if (len < 3 || len > TZNAME_MAX) + return 0; + + copy_len = len; + memcpy(dst, start, copy_len); + dst[copy_len] = '\0'; + + return 1; +} + +/* + * Parse one POSIX DST date rule: + * + * Jn 1 <= n <= 365 + * n 0 <= n <= 365 + * Mm.n.d 1 <= m <= 12, 1 <= n <= 5, 0 <= d <= 6 + * + * An optional /time suffix specifies the transition time. + * The default transition time is 02:00:00. + */ +static int parse_rule(const char **p, int rule[5]) +{ + int a, b, c; + + rule[4] = 7200; + + if (**p == 'J') { + rule[0] = 'J'; + (*p)++; + + if (!parse_uint(p, &rule[1]) || + rule[1] < 1 || rule[1] > 365) + return 0; + } else if (**p == 'M') { + rule[0] = 'M'; + (*p)++; + + if (!parse_uint(p, &a) || + a < 1 || a > 12 || **p != '.') + return 0; + + (*p)++; + + if (!parse_uint(p, &b) || + b < 1 || b > 5 || **p != '.') + return 0; + + (*p)++; + + if (!parse_uint(p, &c) || + c < 0 || c > 6) + return 0; + + rule[1] = a; + rule[2] = b; + rule[3] = c; + } else { + rule[0] = 0; + + if (!parse_uint(p, &rule[1]) || + rule[1] < 0 || rule[1] > 365) + return 0; + } + + if (**p == '/') { + (*p)++; + + if (!parse_hms(p, 167, 1, &rule[4])) + return 0; + } + + return 1; +} + +/* + * Parse a complete POSIX TZ specification into temporary state. + * + * The special bare strings UTC and GMT are accepted as zero-offset + * shorthand for compatibility with the existing WASI implementation. + */ +static int parse_posix_tz(const char *s, struct tz_state *tz) +{ + const char *p = s; + int offset; + + memset(tz, 0, sizeof *tz); + + if (!strcmp(s, "UTC") || !strcmp(s, "GMT")) { + memcpy(tz->std_name, s, strlen(s) + 1); + return 1; + } + + if (!parse_name(tz->std_name, &p)) + return 0; + + if (!parse_hms(&p, 24, 1, &offset)) + return 0; + + tz->timezone = offset; + tz->dst_off = offset; + + /* A standard-time-only specification is complete here. */ + if (!*p) + return 1; + + if (!parse_name(tz->dst_name, &p)) + return 0; + + tz->daylight = 1; + + /* + * An explicit DST offset is optional. When omitted, DST is + * one hour ahead of standard time. + */ + if (*p == '+' || *p == '-' || + is_ascii_digit((unsigned char)*p)) { + if (!parse_hms(&p, 24, 1, &tz->dst_off)) + return 0; + } else { + tz->dst_off = offset - 3600; + } + + /* + * POSIX leaves the default DST rules implementation-defined. + * Use the current U.S. rules when the TZ string omits explicit + * transition rules. Explicit rules below override these defaults. + */ + tz->r0[0] = 'M'; + tz->r0[1] = 3; + tz->r0[2] = 2; + tz->r0[3] = 0; + tz->r0[4] = 7200; + + tz->r1[0] = 'M'; + tz->r1[1] = 11; + tz->r1[2] = 1; + tz->r1[3] = 0; + tz->r1[4] = 7200; + + if (*p == ',') { + p++; + + if (!parse_rule(&p, tz->r0) || *p != ',') + return 0; + + p++; + + if (!parse_rule(&p, tz->r1)) + return 0; + } + + /* Reject every unconsumed character. */ + return *p == '\0'; +} + +static void set_utc_state(struct tz_state *tz) +{ + memset(tz, 0, sizeof *tz); + memcpy(tz->std_name, "UTC", sizeof "UTC"); +} + +static void do_tzset(void) +{ + const char *s; + size_t i; + struct tz_state next; + + s = getenv("TZ"); + + if (!s || !*s) + s = __utc; + + if (old_tz && !strcmp(s, old_tz)) + return; + + /* + * Avoid accepting arbitrarily long environment values. + * Oversized values retain the established WASI UTC fallback. + */ + i = strlen(s); + + if (i > PATH_MAX + 1) { + s = __utc; + i = 3; + } + + /* + * Cache the last processed TZ string. Avoid free so this does + * not pull deallocation into otherwise small static programs. + */ + if (i >= old_tz_size) { + old_tz_size *= 2; + + if (i >= old_tz_size) + old_tz_size = i + 1; + + if (old_tz_size > PATH_MAX + 2) + old_tz_size = PATH_MAX + 2; + + old_tz = malloc(old_tz_size); + } + + if (old_tz) + memcpy(old_tz, s, i + 1); + + if (!parse_posix_tz(s, &next)) + set_utc_state(&next); + + /* + * Commit only after parsing has completed successfully, or + * after the complete UTC fallback state has been constructed. + */ + memcpy(std_name, next.std_name, sizeof std_name); + memcpy(dst_name, next.dst_name, sizeof dst_name); + + __tzname[0] = std_name; + __tzname[1] = next.daylight ? dst_name : std_name; + + __timezone = next.timezone; + __daylight = next.daylight; + dst_off = next.dst_off; + + memcpy(r0, next.r0, sizeof r0); + memcpy(r1, next.r1, sizeof r1); +} + + + +static int days_in_month(int m, int is_leap) +{ + if (m == 2) + return 28 + is_leap; + + return 30 + ((0xad5 >> (m - 1)) & 1); +} + +/* Convert a POSIX DST rule and year to seconds since the epoch. */ +static long long rule_to_secs(const int *rule, int year) +{ + int is_leap; + long long t = __year_to_secs(year, &is_leap); + int x, m, n, d; + + if (rule[0] != 'M') { + x = rule[1]; + + if (rule[0] == 'J' && (x < 60 || !is_leap)) + x--; + + t += 86400 * x; + } else { + m = rule[1]; + n = rule[2]; + d = rule[3]; + + t += __month_to_secs(m - 1, is_leap); + + long long weekday_seconds = + (t + 4LL * 86400) % (7LL * 86400); + if (weekday_seconds < 0) + weekday_seconds += 7LL * 86400; + int wday = weekday_seconds / 86400; + int days = d - wday; + + if (days < 0) + days += 7; + + if (n == 5 && days + 28 >= days_in_month(m, is_leap)) + n = 4; + + t += 86400 * (days + 7 * (n - 1)); + } + + t += rule[4]; + return t; +} + +/* + * Return the timezone applicable to t. + * + * The internal WASI ABI deliberately uses int * for offset because + * WASI's struct tm stores tm_gmtoff as an int. oppoff remains a long. + */ +void __secs_to_zone( + long long t, + int local, + int *isdst, + int *offset, + long *oppoff, + const char **zonename +) +{ + int is_dst = 0; + int active_offset; + long opposite_offset; + const char *active_name; + + STRONG_LOCK(tz_lock); + + do_tzset(); + + if (__daylight) { + long long y = t / 31556952 + 70; + + while (__year_to_secs(y, 0) > t) + y--; + + while (__year_to_secs(y + 1, 0) < t) + y++; + + /* + * Extended transition times can move events across calendar-year + * boundaries. Consider nearby rule-years and select the latest + * transition that has occurred by t. + * + * The range y-2 through y+1 accounts for transitions displaced + * by the rule time and, for UTC timestamps, the UTC offset. + */ + long long last_transition = 0; + int have_transition = 0; + + for (int delta = -2; delta <= 1; delta++) { + long long rule_year = y + delta; + long long start = rule_to_secs(r0, rule_year); + long long end = rule_to_secs(r1, rule_year); + + if (!local) { + start += __timezone; + end += dst_off; + } + + if (start <= t && + (!have_transition || start > last_transition)) { + last_transition = start; + have_transition = 1; + is_dst = 1; + } + + if (end <= t && + (!have_transition || end >= last_transition)) { + last_transition = end; + have_transition = 1; + is_dst = 0; + } + } + } + + if (is_dst) { + active_offset = (int)-dst_off; + opposite_offset = -__timezone; + active_name = __tzname[1]; + } else { + active_offset = (int)-__timezone; + opposite_offset = -dst_off; + active_name = __tzname[0]; + } + if (isdst) - *isdst = 0; + *isdst = is_dst; + if (offset) - *offset = 0; + *offset = active_offset; + if (oppoff) - *oppoff = 0; + *oppoff = opposite_offset; + if (zonename) - *zonename = __utc; + *zonename = active_name; + + STRONG_UNLOCK(tz_lock); +} + +static void __tzset(void) +{ + STRONG_LOCK(tz_lock); + do_tzset(); + STRONG_UNLOCK(tz_lock); } + +weak_alias(__tzset, tzset); #endif const char *__tm_to_tzname(const struct tm *tm) @@ -454,6 +997,12 @@ const char *__tm_to_tzname(const struct tm *tm) (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs)) p = ""; UNLOCK(lock); +#else + STRONG_LOCK(tz_lock); + do_tzset(); + if (p != __utc && p != __tzname[0] && p != __tzname[1]) + p = ""; + STRONG_UNLOCK(tz_lock); #endif return p; } @@ -467,14 +1016,31 @@ int __tzname_to_isdst(const char *restrict *s) if (tzname[0] && !strncmp(*s, tzname[0], len = strlen(tzname[0]))) { isdst = 0; *s += len; - } else if (tzname[1] && !strncmp(*s, tzname[1], len=strlen(tzname[1]))) { + } else if (tzname[1] && !strncmp(*s, tzname[1], len = strlen(tzname[1]))) { isdst = 1; *s += len; } else { /* FIXME: is this supposed to be an error? */ - while (isalpha(**s)) ++*s; + while (isalpha(**s)) + ++*s; } UNLOCK(lock); +#else + STRONG_LOCK(tz_lock); + do_tzset(); + if (__tzname[0] && + !strncmp(*s, __tzname[0], len = strlen(__tzname[0]))) { + isdst = 0; + *s += len; + } else if (__tzname[1] && + !strncmp(*s, __tzname[1], len = strlen(__tzname[1]))) { + isdst = 1; + *s += len; + } else { + while (isalpha((unsigned char)**s)) + ++*s; + } + STRONG_UNLOCK(tz_lock); #endif return isdst; } diff --git a/libc-top-half/musl/src/time/strptime.c b/libc-top-half/musl/src/time/strptime.c index 70ff24497..40bb37afe 100644 --- a/libc-top-half/musl/src/time/strptime.c +++ b/libc-top-half/musl/src/time/strptime.c @@ -207,12 +207,10 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri if (neg) tm->__tm_gmtoff = -tm->__tm_gmtoff; s += 5; break; -#ifdef __wasilibc_unmodified_upstream /* WASI has no timezone tables */ case 'Z': i = __tzname_to_isdst(&s); if (i>=0) tm->tm_isdst = i; break; -#endif case '%': if (*s++ != '%') return 0; break; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 11a209ef7..601e21088 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -690,6 +690,7 @@ add_wasilibc_test(time_and_times.c CFLAGS -D_WASI_EMULATED_PROCESS_CLOCKS LDFLAGS -lwasi-emulated-process-clocks SHARED_LIBS "${SYSROOT_LIB}/libwasi-emulated-process-clocks.so") +add_wasilibc_test(timezone.c) add_wasilibc_test(time.c) add_wasilibc_test(utime.c FS) add_wasilibc_test(rewinddir.c FS) diff --git a/test/src/timezone.c b/test/src/timezone.c new file mode 100644 index 000000000..7623976fd --- /dev/null +++ b/test/src/timezone.c @@ -0,0 +1,636 @@ + +#define _GNU_SOURCE + +#include +#include +#include +#include + +static int check_utc_default(const char *description) +{ + time_t instant = 0; + struct tm local; + char formatted[64] = { 0 }; + + if (localtime_r(&instant, &local) == NULL) { + fprintf(stderr, "%s: localtime_r failed\n", description); + return 1; + } + + if (local.tm_year != 70 || + local.tm_mon != 0 || + local.tm_mday != 1 || + local.tm_hour != 0 || + local.tm_min != 0 || + local.tm_isdst != 0 || + (long)local.tm_gmtoff != 0) { + fprintf(stderr, + "%s: expected UTC, got %04d-%02d-%02d %02d:%02d " + "offset=%ld DST=%d\n", + description, + local.tm_year + 1900, + local.tm_mon + 1, + local.tm_mday, + local.tm_hour, + local.tm_min, + (long)local.tm_gmtoff, + local.tm_isdst); + return 1; + } + + if (tzname[0] == NULL || + tzname[1] == NULL || + strcmp(tzname[0], "UTC") != 0 || + strcmp(tzname[1], "UTC") != 0 || + timezone != 0 || + daylight != 0) { + fprintf(stderr, + "%s: incorrect public UTC timezone state\n", + description); + return 1; + } + + if (strftime(formatted, sizeof formatted, "%z %Z", &local) == 0 || + strcmp(formatted, "+0000 UTC") != 0) { + fprintf(stderr, + "%s: incorrect timezone formatting: '%s'\n", + description, formatted); + return 1; + } + + struct tm roundtrip = local; + if (mktime(&roundtrip) != instant) { + fprintf(stderr, "%s: mktime roundtrip failed\n", description); + return 1; + } + + return 0; +} + +static int check_invalid_timezone(const char *tz) +{ + if (setenv("TZ", tz, 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_utc_default(tz)) + return 1; + + if (timezone != 0 || daylight != 0 || + tzname[0] == NULL || strcmp(tzname[0], "UTC") != 0) { + fprintf(stderr, + "Invalid TZ did not fall back to UTC: %s\n", tz); + return 1; + } + + return 0; +} + +static int check_timezone( + const char *tz, + int year, + int month, + int day, + int hour, + int minute, + long offset, + const char *formatted_offset, + const char *expected_name +) { + if (setenv("TZ", tz, 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (tzname[0] == NULL || + tzname[1] == NULL || + strcmp(tzname[0], expected_name) != 0 || + strcmp(tzname[1], expected_name) != 0 || + timezone != -offset || + daylight != 0) { + fprintf(stderr, + "Wrong public timezone state for TZ=%s: " + "tzname={%s,%s}, timezone=%ld, daylight=%d\n", + tz, + tzname[0] ? tzname[0] : "(null)", + tzname[1] ? tzname[1] : "(null)", + timezone, daylight); + return 1; + } + + time_t instant = 0; + struct tm local; + + if (localtime_r(&instant, &local) == NULL) { + fprintf(stderr, "localtime_r failed for TZ=%s\n", tz); + return 1; + } + + if (local.tm_year != year - 1900 || + local.tm_mon != month - 1 || + local.tm_mday != day || + local.tm_hour != hour || + local.tm_min != minute || + local.tm_isdst != 0) { + fprintf(stderr, + "Wrong local time for TZ=%s: " + "%04d-%02d-%02d %02d:%02d DST=%d\n", + tz, + local.tm_year + 1900, + local.tm_mon + 1, + local.tm_mday, + local.tm_hour, + local.tm_min, + local.tm_isdst); + return 1; + } + + if ((long)local.tm_gmtoff != offset) { + fprintf(stderr, + "Wrong UTC offset for TZ=%s: got %ld, expected %ld\n", + tz, (long)local.tm_gmtoff, offset); + return 1; + } + + char formatted[64] = { 0 }; + + if (strftime(formatted, sizeof formatted, "%z %Z", &local) == 0) { + fprintf(stderr, "strftime failed for TZ=%s\n", tz); + return 1; + } + + char expected[32]; + snprintf(expected, sizeof expected, "%s %s", formatted_offset, expected_name); + + if (strcmp(formatted, expected) != 0) { + fprintf(stderr, + "Wrong strftime result for TZ=%s: got '%s', expected '%s'\n", + tz, formatted, expected); + return 1; + } + + struct tm roundtrip = local; + time_t converted = mktime(&roundtrip); + + if (converted != instant) { + fprintf(stderr, + "mktime roundtrip failed for TZ=%s: got %lld, expected 0\n", + tz, (long long)converted); + return 1; + } + + return 0; +} + + +static int check_dst_instant( + time_t instant, + int year, + int month, + int day, + int hour, + int minute, + int second, + int isdst, + long offset, + const char *expected_format +) { + struct tm local; + char formatted[64] = { 0 }; + + if (localtime_r(&instant, &local) == NULL) { + fprintf(stderr, "localtime_r failed at %lld\n", + (long long)instant); + return 1; + } + + if (local.tm_year != year - 1900 || + local.tm_mon != month - 1 || + local.tm_mday != day || + local.tm_hour != hour || + local.tm_min != minute || + local.tm_sec != second || + local.tm_isdst != isdst || + (long)local.tm_gmtoff != offset) { + fprintf(stderr, + "DST transition mismatch at %lld: " + "%04d-%02d-%02d %02d:%02d:%02d " + "DST=%d offset=%ld\n", + (long long)instant, + local.tm_year + 1900, + local.tm_mon + 1, + local.tm_mday, + local.tm_hour, + local.tm_min, + local.tm_sec, + local.tm_isdst, + (long)local.tm_gmtoff); + return 1; + } + + if (strftime(formatted, sizeof formatted, + "%z %Z", &local) == 0 || + strcmp(formatted, expected_format) != 0) { + fprintf(stderr, + "Wrong DST formatting: got '%s', expected '%s'\n", + formatted, expected_format); + return 1; + } + + struct tm roundtrip = local; + + if (mktime(&roundtrip) != instant) { + fprintf(stderr, + "DST mktime roundtrip failed at %lld\n", + (long long)instant); + return 1; + } + + return 0; +} + + +static int check_public_timezone_interfaces(void) +{ + struct tm parsed; + char *end; + + if (tzname[0] == NULL || + tzname[1] == NULL || + strcmp(tzname[0], "EST") != 0 || + strcmp(tzname[1], "EDT") != 0 || + timezone != 18000 || + daylight != 1) { + fprintf(stderr, + "Incorrect public timezone variables: " + "tzname={%s,%s}, timezone=%ld, daylight=%d\n", + tzname[0] ? tzname[0] : "(null)", + tzname[1] ? tzname[1] : "(null)", + timezone, daylight); + return 1; + } + + memset(&parsed, 0, sizeof parsed); + parsed.tm_isdst = -1; + + end = strptime("EST", "%Z", &parsed); + + if (end == NULL || *end != '\0' || parsed.tm_isdst != 0) { + fprintf(stderr, "strptime failed to recognise EST\n"); + return 1; + } + + memset(&parsed, 0, sizeof parsed); + parsed.tm_isdst = -1; + + end = strptime("EDT", "%Z", &parsed); + + if (end == NULL || *end != '\0' || parsed.tm_isdst != 1) { + fprintf(stderr, "strptime failed to recognise EDT\n"); + return 1; + } + + return 0; +} + + +int main(void) +{ + if (check_timezone( + "UTC", 1970, 1, 1, 0, 0, 0, "+0000", "UTC")) + return 1; + + if (check_timezone( + "UTC-5:30", 1970, 1, 1, 5, 30, 19800, "+0530", "UTC")) + return 1; + + if (check_timezone( + "UTC-5:3", 1970, 1, 1, 5, 3, 18180, "+0503", "UTC")) + return 1; + + if (check_timezone( + "UTC+4", 1969, 12, 31, 20, 0, -14400, "-0400", "UTC")) + return 1; + + /* + * POSIX quoted timezone abbreviation. + * <+05> is the abbreviation; -5 means five hours east of UTC. + */ + if (check_timezone( + "<+05>-5", 1970, 1, 1, 5, 0, 18000, "+0500", "+05")) + return 1; + + + /* Changing TZ must reconfigure the timezone. */ + if (check_timezone( + "UTC-5:30", 1970, 1, 1, 5, 30, 19800, "+0530", "UTC")) + return 1; + + /* + * US Eastern timezone: + * Standard UTC-05:00 + * Daylight UTC-04:00 + * + * Second Sunday in March, 02:00: + * clocks advance from 01:59:59 to 03:00:00. + * + * First Sunday in November, 02:00: + * clocks return from 01:59:59 to 01:00:00. + */ + if (setenv( + "TZ", + "EST5EDT,M3.2.0/2,M11.1.0/2", + 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_public_timezone_interfaces()) + return 1; + + /* Immediately before spring transition. */ + if (check_dst_instant( + 1710053999, + 2024, 3, 10, 1, 59, 59, + 0, -18000, "-0500 EST")) + return 1; + + /* Immediately after spring transition. */ + if (check_dst_instant( + 1710054000, + 2024, 3, 10, 3, 0, 0, + 1, -14400, "-0400 EDT")) + return 1; + + /* Immediately before autumn transition. */ + if (check_dst_instant( + 1730613599, + 2024, 11, 3, 1, 59, 59, + 1, -14400, "-0400 EDT")) + return 1; + + /* Immediately after autumn transition. */ + if (check_dst_instant( + 1730613600, + 2024, 11, 3, 1, 0, 0, + 0, -18000, "-0500 EST")) + return 1; + + + /* + * POSIX extended transition times: + * + * Start: -2 hours means 22:00 on the preceding day. + * End: 26 hours means 02:00 on the following day. + */ + if (setenv( + "TZ", + "EST5EDT,M3.2.0/-2,M11.1.0/26", + 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + /* Immediately before the early spring transition. */ + if (check_dst_instant( + 1710039599, + 2024, 3, 9, 21, 59, 59, + 0, -18000, "-0500 EST")) + return 1; + + /* Immediately after the early spring transition. */ + if (check_dst_instant( + 1710039600, + 2024, 3, 9, 23, 0, 0, + 1, -14400, "-0400 EDT")) + return 1; + + /* Immediately before the delayed autumn transition. */ + if (check_dst_instant( + 1730699999, + 2024, 11, 4, 1, 59, 59, + 1, -14400, "-0400 EDT")) + return 1; + + /* Immediately after the delayed autumn transition. */ + if (check_dst_instant( + 1730700000, + 2024, 11, 4, 1, 0, 0, + 0, -18000, "-0500 EST")) + return 1; + + + /* + * Extended transition times can move a transition into the + * previous calendar year. + * + * The first Sunday of January 1999 is January 3: + * -167 hours places the start at December 27, 1998, 01:00 EST. + * -48 hours places the end at January 1, 1999, 00:00 EDT. + */ + if (setenv( + "TZ", + "EST5EDT,M1.1.0/-167,M1.1.0/-48", + 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + /* Immediately before the December 1998 start transition. */ + if (check_dst_instant( + 914738399, + 1998, 12, 27, 0, 59, 59, + 0, -18000, "-0500 EST")) + return 1; + + /* Immediately after the December 1998 start transition. */ + if (check_dst_instant( + 914738400, + 1998, 12, 27, 2, 0, 0, + 1, -14400, "-0400 EDT")) + return 1; + + /* Immediately before the January 1999 end transition. */ + if (check_dst_instant( + 915163199, + 1998, 12, 31, 23, 59, 59, + 1, -14400, "-0400 EDT")) + return 1; + + /* Immediately after the January 1999 end transition. */ + if (check_dst_instant( + 915163200, + 1998, 12, 31, 23, 0, 0, + 0, -18000, "-0500 EST")) + return 1; + + /* + * Positive extended transition times can move an event into the + * following calendar year. December 27, 1998 at 00:00 plus + * 167 hours is January 2, 1999 at 23:00 standard time. + */ + if (setenv( + "TZ", + "EST5EDT,M12.5.0/167,M3.2.0/2", + 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_dst_instant( + 915335999, + 1999, 1, 2, 22, 59, 59, + 0, -18000, "-0500 EST")) + return 1; + + if (check_dst_instant( + 915336000, + 1999, 1, 3, 0, 0, 0, + 1, -14400, "-0400 EDT")) + return 1; + + /* + * When DST rules are omitted, WASI uses its implementation-defined + * default: the current U.S. transition dates. + */ + if (setenv("TZ", "EST5EDT", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_public_timezone_interfaces()) + return 1; + + if (check_dst_instant( + 1710053999, + 2024, 3, 10, 1, 59, 59, + 0, -18000, "-0500 EST")) + return 1; + + if (check_dst_instant( + 1710054000, + 2024, 3, 10, 3, 0, 0, + 1, -14400, "-0400 EDT")) + return 1; + + if (check_dst_instant( + 1730613599, + 2024, 11, 3, 1, 59, 59, + 1, -14400, "-0400 EDT")) + return 1; + + if (check_dst_instant( + 1730613600, + 2024, 11, 3, 1, 0, 0, + 0, -18000, "-0500 EST")) + return 1; + + /* + * M.m.w rules must also work before the Unix epoch. In 1969, + * March 1 and November 1 were Saturdays; these rules select the + * second Saturday in March and first Saturday in November. + */ + if (setenv("TZ", "EST5EDT,M3.2.6/2,M11.1.6/2", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_dst_instant( + -25808401, + 1969, 3, 8, 1, 59, 59, + 0, -18000, "-0500 EST")) + return 1; + + if (check_dst_instant( + -25808400, + 1969, 3, 8, 3, 0, 0, + 1, -14400, "-0400 EDT")) + return 1; + + if (check_dst_instant( + -5248801, + 1969, 11, 1, 1, 59, 59, + 1, -14400, "-0400 EDT")) + return 1; + + if (check_dst_instant( + -5248800, + 1969, 11, 1, 1, 0, 0, + 0, -18000, "-0500 EST")) + return 1; + + /* + * Invalid POSIX TZ specifications must fall back to UTC. + */ + static const char *invalid_timezones[] = { + "AB0", /* Short standard name */ + "EST", /* Missing standard offset */ + "EST25", /* Invalid offset hour */ + "EST5:60", /* Invalid offset minute */ + "EST5:", /* Missing offset minute */ + "EST5ED", /* Short DST name */ + "EST5-5", /* Invalid quoted abbreviation */ + }; + + size_t i; + + for (i = 0; + i < sizeof invalid_timezones / sizeof invalid_timezones[0]; + i++) { + if (check_invalid_timezone(invalid_timezones[i])) + return 1; + } + + + + /* Unset TZ: WASI must retain its UTC default. */ + if (unsetenv("TZ") != 0) { + perror("unsetenv"); + return 1; + } + + tzset(); + + if (check_utc_default("unset TZ")) + return 1; + + /* Empty TZ must also select UTC. */ + if (setenv("TZ", "", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_utc_default("empty TZ")) + return 1; + + puts("Timezone tests passed"); + return 0; +} From 28fff04ed50ba07ce6551e1d86241b0e585564b8 Mon Sep 17 00:00:00 2001 From: Abel George Antony Date: Fri, 4 Dec 2026 20:53:07 +0530 Subject: [PATCH 2/2] Fix POSIX timezone CI regressions --- .../wasm32-wasip1-threads/defined-symbols.txt | 7 + expected/wasm32-wasip1/defined-symbols.txt | 7 + expected/wasm32-wasip2/defined-symbols.txt | 7 + .../wasm32-wasip3-coop/defined-symbols.txt | 7 + expected/wasm32-wasip3/defined-symbols.txt | 7 + expected/wasm32-webp2/defined-symbols.txt | 7 + libc-top-half/musl/src/time/__tz.c | 6 - test/src/timezone.c | 1030 +++++++---------- 8 files changed, 484 insertions(+), 594 deletions(-) diff --git a/expected/wasm32-wasip1-threads/defined-symbols.txt b/expected/wasm32-wasip1-threads/defined-symbols.txt index b1a8e8d8d..a49b77f3c 100644 --- a/expected/wasm32-wasip1-threads/defined-symbols.txt +++ b/expected/wasm32-wasip1-threads/defined-symbols.txt @@ -37,6 +37,7 @@ __ctype_tolower_loc __ctype_toupper_loc __cxa_atexit __cxa_finalize +__daylight __default_guardsize __default_stacksize __des_setkey @@ -282,6 +283,7 @@ __testcancel __thread_list_lock __timedwait __timedwait_cp +__timezone __tl_lock __tl_sync __tl_unlock @@ -300,6 +302,7 @@ __tre_mem_alloc_impl __tre_mem_destroy __tre_mem_new_impl __tsearch_balance +__tzname __tzname_to_isdst __uflow __unlist_locked_file @@ -582,6 +585,7 @@ ctanhl ctanl ctime ctime_r +daylight difftime dirfd dirname @@ -1307,6 +1311,7 @@ time timegm times timespec_get +timezone toascii tolower tolower_l @@ -1325,6 +1330,8 @@ truncl tsearch tss_get twalk +tzname +tzset uname ungetc ungetwc diff --git a/expected/wasm32-wasip1/defined-symbols.txt b/expected/wasm32-wasip1/defined-symbols.txt index c94d5d8fa..2f5818c5f 100644 --- a/expected/wasm32-wasip1/defined-symbols.txt +++ b/expected/wasm32-wasip1/defined-symbols.txt @@ -35,6 +35,7 @@ __ctype_tolower_loc __ctype_toupper_loc __cxa_atexit __cxa_finalize +__daylight __default_guardsize __default_stacksize __des_setkey @@ -277,6 +278,7 @@ __tanl __testcancel __timedwait __timedwait_cp +__timezone __tl_lock __tl_unlock __tm_to_secs @@ -294,6 +296,7 @@ __tre_mem_alloc_impl __tre_mem_destroy __tre_mem_new_impl __tsearch_balance +__tzname __tzname_to_isdst __uflow __unlist_locked_file @@ -569,6 +572,7 @@ ctanhl ctanl ctime ctime_r +daylight difftime dirfd dirname @@ -1294,6 +1298,7 @@ time timegm times timespec_get +timezone toascii tolower tolower_l @@ -1312,6 +1317,8 @@ truncl tsearch tss_get twalk +tzname +tzset uname ungetc ungetwc diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index e1c0b6ec5..fa34b15f2 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -37,6 +37,7 @@ __ctype_tolower_loc __ctype_toupper_loc __cxa_atexit __cxa_finalize +__daylight __default_guardsize __default_stacksize __des_setkey @@ -279,6 +280,7 @@ __tanl __testcancel __timedwait __timedwait_cp +__timezone __tl_lock __tl_unlock __tm_to_secs @@ -296,6 +298,7 @@ __tre_mem_alloc_impl __tre_mem_destroy __tre_mem_new_impl __tsearch_balance +__tzname __tzname_to_isdst __uflow __unlist_locked_file @@ -552,6 +555,7 @@ ctanhl ctanl ctime ctime_r +daylight descriptor_table_clear descriptor_table_dup descriptor_table_get @@ -1471,6 +1475,7 @@ time timegm times timespec_get +timezone toascii tolower tolower_l @@ -1489,6 +1494,8 @@ truncl tsearch tss_get twalk +tzname +tzset udp_borrow_incoming_datagram_stream udp_borrow_outgoing_datagram_stream udp_borrow_udp_socket diff --git a/expected/wasm32-wasip3-coop/defined-symbols.txt b/expected/wasm32-wasip3-coop/defined-symbols.txt index 429a4334b..17115a4a8 100644 --- a/expected/wasm32-wasip3-coop/defined-symbols.txt +++ b/expected/wasm32-wasip3-coop/defined-symbols.txt @@ -38,6 +38,7 @@ __ctype_tolower_loc __ctype_toupper_loc __cxa_atexit __cxa_finalize +__daylight __default_guardsize __default_stacksize __des_setkey @@ -304,6 +305,7 @@ __tanl __testcancel __timedwait __timedwait_cp +__timezone __tl_lock __tl_unlock __tm_to_secs @@ -321,6 +323,7 @@ __tre_mem_alloc_impl __tre_mem_destroy __tre_mem_new_impl __tsearch_balance +__tzname __tzname_to_isdst __uflow __unlist_locked_file @@ -592,6 +595,7 @@ ctanhl ctanl ctime ctime_r +daylight descriptor_table_clear descriptor_table_dup descriptor_table_get @@ -1553,6 +1557,7 @@ time timegm times timespec_get +timezone toascii tolower tolower_l @@ -1571,6 +1576,8 @@ truncl tsearch tss_get twalk +tzname +tzset uname ungetc ungetwc diff --git a/expected/wasm32-wasip3/defined-symbols.txt b/expected/wasm32-wasip3/defined-symbols.txt index a5fcf44bb..ec8b1398f 100644 --- a/expected/wasm32-wasip3/defined-symbols.txt +++ b/expected/wasm32-wasip3/defined-symbols.txt @@ -37,6 +37,7 @@ __ctype_tolower_loc __ctype_toupper_loc __cxa_atexit __cxa_finalize +__daylight __default_guardsize __default_stacksize __des_setkey @@ -280,6 +281,7 @@ __tanl __testcancel __timedwait __timedwait_cp +__timezone __tl_lock __tl_unlock __tm_to_secs @@ -297,6 +299,7 @@ __tre_mem_alloc_impl __tre_mem_destroy __tre_mem_new_impl __tsearch_balance +__tzname __tzname_to_isdst __uflow __unlist_locked_file @@ -566,6 +569,7 @@ ctanhl ctanl ctime ctime_r +daylight descriptor_table_clear descriptor_table_dup descriptor_table_get @@ -1525,6 +1529,7 @@ time timegm times timespec_get +timezone toascii tolower tolower_l @@ -1543,6 +1548,8 @@ truncl tsearch tss_get twalk +tzname +tzset uname ungetc ungetwc diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index dfc2fe7a4..af1565114 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -37,6 +37,7 @@ __ctype_tolower_loc __ctype_toupper_loc __cxa_atexit __cxa_finalize +__daylight __default_guardsize __default_stacksize __des_setkey @@ -279,6 +280,7 @@ __tanl __testcancel __timedwait __timedwait_cp +__timezone __tl_lock __tl_unlock __tm_to_secs @@ -296,6 +298,7 @@ __tre_mem_alloc_impl __tre_mem_destroy __tre_mem_new_impl __tsearch_balance +__tzname __tzname_to_isdst __uflow __unlist_locked_file @@ -528,6 +531,7 @@ ctanhl ctanl ctime ctime_r +daylight descriptor_table_clear descriptor_table_dup descriptor_table_get @@ -1265,6 +1269,7 @@ time timegm times timespec_get +timezone toascii tolower tolower_l @@ -1283,6 +1288,8 @@ truncl tsearch tss_get twalk +tzname +tzset uname ungetc ungetwc diff --git a/libc-top-half/musl/src/time/__tz.c b/libc-top-half/musl/src/time/__tz.c index 6a6fa27f2..addf9ed7c 100644 --- a/libc-top-half/musl/src/time/__tz.c +++ b/libc-top-half/musl/src/time/__tz.c @@ -997,12 +997,6 @@ const char *__tm_to_tzname(const struct tm *tm) (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs)) p = ""; UNLOCK(lock); -#else - STRONG_LOCK(tz_lock); - do_tzset(); - if (p != __utc && p != __tzname[0] && p != __tzname[1]) - p = ""; - STRONG_UNLOCK(tz_lock); #endif return p; } diff --git a/test/src/timezone.c b/test/src/timezone.c index 7623976fd..326b95f11 100644 --- a/test/src/timezone.c +++ b/test/src/timezone.c @@ -6,631 +6,485 @@ #include #include -static int check_utc_default(const char *description) -{ - time_t instant = 0; - struct tm local; - char formatted[64] = { 0 }; - - if (localtime_r(&instant, &local) == NULL) { - fprintf(stderr, "%s: localtime_r failed\n", description); - return 1; - } - - if (local.tm_year != 70 || - local.tm_mon != 0 || - local.tm_mday != 1 || - local.tm_hour != 0 || - local.tm_min != 0 || - local.tm_isdst != 0 || - (long)local.tm_gmtoff != 0) { - fprintf(stderr, +static int check_utc_default(const char *description) { + time_t instant = 0; + struct tm local; + char formatted[64] = {0}; + + if (localtime_r(&instant, &local) == NULL) { + fprintf(stderr, "%s: localtime_r failed\n", description); + return 1; + } + + if (local.tm_year != 70 || local.tm_mon != 0 || local.tm_mday != 1 || + local.tm_hour != 0 || local.tm_min != 0 || local.tm_isdst != 0 || + (long)local.tm_gmtoff != 0) { + fprintf(stderr, "%s: expected UTC, got %04d-%02d-%02d %02d:%02d " "offset=%ld DST=%d\n", - description, - local.tm_year + 1900, - local.tm_mon + 1, - local.tm_mday, - local.tm_hour, - local.tm_min, - (long)local.tm_gmtoff, - local.tm_isdst); - return 1; - } - - if (tzname[0] == NULL || - tzname[1] == NULL || - strcmp(tzname[0], "UTC") != 0 || - strcmp(tzname[1], "UTC") != 0 || - timezone != 0 || - daylight != 0) { - fprintf(stderr, - "%s: incorrect public UTC timezone state\n", - description); - return 1; - } - - if (strftime(formatted, sizeof formatted, "%z %Z", &local) == 0 || - strcmp(formatted, "+0000 UTC") != 0) { - fprintf(stderr, - "%s: incorrect timezone formatting: '%s'\n", - description, formatted); - return 1; - } - - struct tm roundtrip = local; - if (mktime(&roundtrip) != instant) { - fprintf(stderr, "%s: mktime roundtrip failed\n", description); - return 1; - } - - return 0; + description, local.tm_year + 1900, local.tm_mon + 1, local.tm_mday, + local.tm_hour, local.tm_min, (long)local.tm_gmtoff, local.tm_isdst); + return 1; + } + + if (tzname[0] == NULL || tzname[1] == NULL || strcmp(tzname[0], "UTC") != 0 || + strcmp(tzname[1], "UTC") != 0 || timezone != 0 || daylight != 0) { + fprintf(stderr, "%s: incorrect public UTC timezone state\n", description); + return 1; + } + + if (strftime(formatted, sizeof formatted, "%z %Z", &local) == 0 || + strcmp(formatted, "+0000 UTC") != 0) { + fprintf(stderr, "%s: incorrect timezone formatting: '%s'\n", description, + formatted); + return 1; + } + + struct tm roundtrip = local; + if (mktime(&roundtrip) != instant) { + fprintf(stderr, "%s: mktime roundtrip failed\n", description); + return 1; + } + + return 0; } -static int check_invalid_timezone(const char *tz) -{ - if (setenv("TZ", tz, 1) != 0) { - perror("setenv"); - return 1; - } +static int check_invalid_timezone(const char *tz) { + if (setenv("TZ", tz, 1) != 0) { + perror("setenv"); + return 1; + } - tzset(); + tzset(); - if (check_utc_default(tz)) - return 1; + if (check_utc_default(tz)) + return 1; - if (timezone != 0 || daylight != 0 || - tzname[0] == NULL || strcmp(tzname[0], "UTC") != 0) { - fprintf(stderr, - "Invalid TZ did not fall back to UTC: %s\n", tz); - return 1; - } + if (timezone != 0 || daylight != 0 || tzname[0] == NULL || + strcmp(tzname[0], "UTC") != 0) { + fprintf(stderr, "Invalid TZ did not fall back to UTC: %s\n", tz); + return 1; + } - return 0; + return 0; } -static int check_timezone( - const char *tz, - int year, - int month, - int day, - int hour, - int minute, - long offset, - const char *formatted_offset, - const char *expected_name -) { - if (setenv("TZ", tz, 1) != 0) { - perror("setenv"); - return 1; - } - - tzset(); - - if (tzname[0] == NULL || - tzname[1] == NULL || - strcmp(tzname[0], expected_name) != 0 || - strcmp(tzname[1], expected_name) != 0 || - timezone != -offset || - daylight != 0) { - fprintf(stderr, - "Wrong public timezone state for TZ=%s: " - "tzname={%s,%s}, timezone=%ld, daylight=%d\n", - tz, - tzname[0] ? tzname[0] : "(null)", - tzname[1] ? tzname[1] : "(null)", - timezone, daylight); - return 1; - } - - time_t instant = 0; - struct tm local; - - if (localtime_r(&instant, &local) == NULL) { - fprintf(stderr, "localtime_r failed for TZ=%s\n", tz); - return 1; - } - - if (local.tm_year != year - 1900 || - local.tm_mon != month - 1 || - local.tm_mday != day || - local.tm_hour != hour || - local.tm_min != minute || - local.tm_isdst != 0) { - fprintf(stderr, +static int check_timezone(const char *tz, int year, int month, int day, + int hour, int minute, long offset, + const char *formatted_offset, + const char *expected_name) { + if (setenv("TZ", tz, 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (tzname[0] == NULL || tzname[1] == NULL || + strcmp(tzname[0], expected_name) != 0 || + strcmp(tzname[1], expected_name) != 0 || timezone != -offset || + daylight != 0) { + fprintf(stderr, + "Wrong public timezone state for TZ=%s: " + "tzname={%s,%s}, timezone=%ld, daylight=%d\n", + tz, tzname[0] ? tzname[0] : "(null)", + tzname[1] ? tzname[1] : "(null)", timezone, daylight); + return 1; + } + + time_t instant = 0; + struct tm local; + + if (localtime_r(&instant, &local) == NULL) { + fprintf(stderr, "localtime_r failed for TZ=%s\n", tz); + return 1; + } + + if (local.tm_year != year - 1900 || local.tm_mon != month - 1 || + local.tm_mday != day || local.tm_hour != hour || local.tm_min != minute || + local.tm_isdst != 0) { + fprintf(stderr, "Wrong local time for TZ=%s: " "%04d-%02d-%02d %02d:%02d DST=%d\n", - tz, - local.tm_year + 1900, - local.tm_mon + 1, - local.tm_mday, - local.tm_hour, - local.tm_min, - local.tm_isdst); - return 1; - } - - if ((long)local.tm_gmtoff != offset) { - fprintf(stderr, - "Wrong UTC offset for TZ=%s: got %ld, expected %ld\n", - tz, (long)local.tm_gmtoff, offset); - return 1; - } - - char formatted[64] = { 0 }; - - if (strftime(formatted, sizeof formatted, "%z %Z", &local) == 0) { - fprintf(stderr, "strftime failed for TZ=%s\n", tz); - return 1; - } - - char expected[32]; - snprintf(expected, sizeof expected, "%s %s", formatted_offset, expected_name); - - if (strcmp(formatted, expected) != 0) { - fprintf(stderr, - "Wrong strftime result for TZ=%s: got '%s', expected '%s'\n", - tz, formatted, expected); - return 1; - } - - struct tm roundtrip = local; - time_t converted = mktime(&roundtrip); - - if (converted != instant) { - fprintf(stderr, - "mktime roundtrip failed for TZ=%s: got %lld, expected 0\n", + tz, local.tm_year + 1900, local.tm_mon + 1, local.tm_mday, + local.tm_hour, local.tm_min, local.tm_isdst); + return 1; + } + + if ((long)local.tm_gmtoff != offset) { + fprintf(stderr, "Wrong UTC offset for TZ=%s: got %ld, expected %ld\n", tz, + (long)local.tm_gmtoff, offset); + return 1; + } + + char formatted[64] = {0}; + + if (strftime(formatted, sizeof formatted, "%z %Z", &local) == 0) { + fprintf(stderr, "strftime failed for TZ=%s\n", tz); + return 1; + } + + char expected[32]; + snprintf(expected, sizeof expected, "%s %s", formatted_offset, expected_name); + + if (strcmp(formatted, expected) != 0) { + fprintf(stderr, + "Wrong strftime result for TZ=%s: got '%s', expected '%s'\n", tz, + formatted, expected); + return 1; + } + + struct tm roundtrip = local; + time_t converted = mktime(&roundtrip); + + if (converted != instant) { + fprintf(stderr, "mktime roundtrip failed for TZ=%s: got %lld, expected 0\n", tz, (long long)converted); - return 1; - } + return 1; + } - return 0; + return 0; } - -static int check_dst_instant( - time_t instant, - int year, - int month, - int day, - int hour, - int minute, - int second, - int isdst, - long offset, - const char *expected_format -) { - struct tm local; - char formatted[64] = { 0 }; - - if (localtime_r(&instant, &local) == NULL) { - fprintf(stderr, "localtime_r failed at %lld\n", - (long long)instant); - return 1; - } - - if (local.tm_year != year - 1900 || - local.tm_mon != month - 1 || - local.tm_mday != day || - local.tm_hour != hour || - local.tm_min != minute || - local.tm_sec != second || - local.tm_isdst != isdst || - (long)local.tm_gmtoff != offset) { - fprintf(stderr, +static int check_dst_instant(time_t instant, int year, int month, int day, + int hour, int minute, int second, int isdst, + long offset, const char *expected_format) { + struct tm local; + char formatted[64] = {0}; + + if (localtime_r(&instant, &local) == NULL) { + fprintf(stderr, "localtime_r failed at %lld\n", (long long)instant); + return 1; + } + + if (local.tm_year != year - 1900 || local.tm_mon != month - 1 || + local.tm_mday != day || local.tm_hour != hour || local.tm_min != minute || + local.tm_sec != second || local.tm_isdst != isdst || + (long)local.tm_gmtoff != offset) { + fprintf(stderr, "DST transition mismatch at %lld: " "%04d-%02d-%02d %02d:%02d:%02d " "DST=%d offset=%ld\n", - (long long)instant, - local.tm_year + 1900, - local.tm_mon + 1, - local.tm_mday, - local.tm_hour, - local.tm_min, - local.tm_sec, - local.tm_isdst, - (long)local.tm_gmtoff); - return 1; - } - - if (strftime(formatted, sizeof formatted, - "%z %Z", &local) == 0 || - strcmp(formatted, expected_format) != 0) { - fprintf(stderr, - "Wrong DST formatting: got '%s', expected '%s'\n", + (long long)instant, local.tm_year + 1900, local.tm_mon + 1, + local.tm_mday, local.tm_hour, local.tm_min, local.tm_sec, + local.tm_isdst, (long)local.tm_gmtoff); + return 1; + } + + if (strftime(formatted, sizeof formatted, "%z %Z", &local) == 0 || + strcmp(formatted, expected_format) != 0) { + fprintf(stderr, "Wrong DST formatting: got '%s', expected '%s'\n", formatted, expected_format); - return 1; - } + return 1; + } - struct tm roundtrip = local; + struct tm roundtrip = local; - if (mktime(&roundtrip) != instant) { - fprintf(stderr, - "DST mktime roundtrip failed at %lld\n", + if (mktime(&roundtrip) != instant) { + fprintf(stderr, "DST mktime roundtrip failed at %lld\n", (long long)instant); - return 1; - } + return 1; + } - return 0; + return 0; } +static int check_public_timezone_interfaces(void) { + struct tm parsed; + char *end; -static int check_public_timezone_interfaces(void) -{ - struct tm parsed; - char *end; - - if (tzname[0] == NULL || - tzname[1] == NULL || - strcmp(tzname[0], "EST") != 0 || - strcmp(tzname[1], "EDT") != 0 || - timezone != 18000 || - daylight != 1) { - fprintf(stderr, + if (tzname[0] == NULL || tzname[1] == NULL || strcmp(tzname[0], "EST") != 0 || + strcmp(tzname[1], "EDT") != 0 || timezone != 18000 || daylight != 1) { + fprintf(stderr, "Incorrect public timezone variables: " "tzname={%s,%s}, timezone=%ld, daylight=%d\n", - tzname[0] ? tzname[0] : "(null)", - tzname[1] ? tzname[1] : "(null)", + tzname[0] ? tzname[0] : "(null)", tzname[1] ? tzname[1] : "(null)", timezone, daylight); - return 1; - } + return 1; + } - memset(&parsed, 0, sizeof parsed); - parsed.tm_isdst = -1; + memset(&parsed, 0, sizeof parsed); + parsed.tm_isdst = -1; - end = strptime("EST", "%Z", &parsed); + end = strptime("EST", "%Z", &parsed); - if (end == NULL || *end != '\0' || parsed.tm_isdst != 0) { - fprintf(stderr, "strptime failed to recognise EST\n"); - return 1; - } + if (end == NULL || *end != '\0' || parsed.tm_isdst != 0) { + fprintf(stderr, "strptime failed to recognise EST\n"); + return 1; + } - memset(&parsed, 0, sizeof parsed); - parsed.tm_isdst = -1; + memset(&parsed, 0, sizeof parsed); + parsed.tm_isdst = -1; - end = strptime("EDT", "%Z", &parsed); + end = strptime("EDT", "%Z", &parsed); - if (end == NULL || *end != '\0' || parsed.tm_isdst != 1) { - fprintf(stderr, "strptime failed to recognise EDT\n"); - return 1; - } + if (end == NULL || *end != '\0' || parsed.tm_isdst != 1) { + fprintf(stderr, "strptime failed to recognise EDT\n"); + return 1; + } - return 0; + return 0; } - -int main(void) -{ - if (check_timezone( - "UTC", 1970, 1, 1, 0, 0, 0, "+0000", "UTC")) - return 1; - - if (check_timezone( - "UTC-5:30", 1970, 1, 1, 5, 30, 19800, "+0530", "UTC")) - return 1; - - if (check_timezone( - "UTC-5:3", 1970, 1, 1, 5, 3, 18180, "+0503", "UTC")) - return 1; - - if (check_timezone( - "UTC+4", 1969, 12, 31, 20, 0, -14400, "-0400", "UTC")) - return 1; - - /* - * POSIX quoted timezone abbreviation. - * <+05> is the abbreviation; -5 means five hours east of UTC. - */ - if (check_timezone( - "<+05>-5", 1970, 1, 1, 5, 0, 18000, "+0500", "+05")) - return 1; - - - /* Changing TZ must reconfigure the timezone. */ - if (check_timezone( - "UTC-5:30", 1970, 1, 1, 5, 30, 19800, "+0530", "UTC")) - return 1; - - /* - * US Eastern timezone: - * Standard UTC-05:00 - * Daylight UTC-04:00 - * - * Second Sunday in March, 02:00: - * clocks advance from 01:59:59 to 03:00:00. - * - * First Sunday in November, 02:00: - * clocks return from 01:59:59 to 01:00:00. - */ - if (setenv( - "TZ", - "EST5EDT,M3.2.0/2,M11.1.0/2", - 1) != 0) { - perror("setenv"); - return 1; - } - - tzset(); - - if (check_public_timezone_interfaces()) - return 1; - - /* Immediately before spring transition. */ - if (check_dst_instant( - 1710053999, - 2024, 3, 10, 1, 59, 59, - 0, -18000, "-0500 EST")) - return 1; - - /* Immediately after spring transition. */ - if (check_dst_instant( - 1710054000, - 2024, 3, 10, 3, 0, 0, - 1, -14400, "-0400 EDT")) - return 1; - - /* Immediately before autumn transition. */ - if (check_dst_instant( - 1730613599, - 2024, 11, 3, 1, 59, 59, - 1, -14400, "-0400 EDT")) - return 1; - - /* Immediately after autumn transition. */ - if (check_dst_instant( - 1730613600, - 2024, 11, 3, 1, 0, 0, - 0, -18000, "-0500 EST")) - return 1; - - - /* - * POSIX extended transition times: - * - * Start: -2 hours means 22:00 on the preceding day. - * End: 26 hours means 02:00 on the following day. - */ - if (setenv( - "TZ", - "EST5EDT,M3.2.0/-2,M11.1.0/26", - 1) != 0) { - perror("setenv"); - return 1; - } - - tzset(); - - /* Immediately before the early spring transition. */ - if (check_dst_instant( - 1710039599, - 2024, 3, 9, 21, 59, 59, - 0, -18000, "-0500 EST")) - return 1; - - /* Immediately after the early spring transition. */ - if (check_dst_instant( - 1710039600, - 2024, 3, 9, 23, 0, 0, - 1, -14400, "-0400 EDT")) - return 1; - - /* Immediately before the delayed autumn transition. */ - if (check_dst_instant( - 1730699999, - 2024, 11, 4, 1, 59, 59, - 1, -14400, "-0400 EDT")) - return 1; - - /* Immediately after the delayed autumn transition. */ - if (check_dst_instant( - 1730700000, - 2024, 11, 4, 1, 0, 0, - 0, -18000, "-0500 EST")) - return 1; - - - /* - * Extended transition times can move a transition into the - * previous calendar year. - * - * The first Sunday of January 1999 is January 3: - * -167 hours places the start at December 27, 1998, 01:00 EST. - * -48 hours places the end at January 1, 1999, 00:00 EDT. - */ - if (setenv( - "TZ", - "EST5EDT,M1.1.0/-167,M1.1.0/-48", - 1) != 0) { - perror("setenv"); - return 1; - } - - tzset(); - - /* Immediately before the December 1998 start transition. */ - if (check_dst_instant( - 914738399, - 1998, 12, 27, 0, 59, 59, - 0, -18000, "-0500 EST")) - return 1; - - /* Immediately after the December 1998 start transition. */ - if (check_dst_instant( - 914738400, - 1998, 12, 27, 2, 0, 0, - 1, -14400, "-0400 EDT")) - return 1; - - /* Immediately before the January 1999 end transition. */ - if (check_dst_instant( - 915163199, - 1998, 12, 31, 23, 59, 59, - 1, -14400, "-0400 EDT")) - return 1; - - /* Immediately after the January 1999 end transition. */ - if (check_dst_instant( - 915163200, - 1998, 12, 31, 23, 0, 0, - 0, -18000, "-0500 EST")) - return 1; - - /* - * Positive extended transition times can move an event into the - * following calendar year. December 27, 1998 at 00:00 plus - * 167 hours is January 2, 1999 at 23:00 standard time. - */ - if (setenv( - "TZ", - "EST5EDT,M12.5.0/167,M3.2.0/2", - 1) != 0) { - perror("setenv"); - return 1; - } - - tzset(); - - if (check_dst_instant( - 915335999, - 1999, 1, 2, 22, 59, 59, - 0, -18000, "-0500 EST")) - return 1; - - if (check_dst_instant( - 915336000, - 1999, 1, 3, 0, 0, 0, - 1, -14400, "-0400 EDT")) - return 1; - - /* - * When DST rules are omitted, WASI uses its implementation-defined - * default: the current U.S. transition dates. - */ - if (setenv("TZ", "EST5EDT", 1) != 0) { - perror("setenv"); - return 1; - } - - tzset(); - - if (check_public_timezone_interfaces()) - return 1; - - if (check_dst_instant( - 1710053999, - 2024, 3, 10, 1, 59, 59, - 0, -18000, "-0500 EST")) - return 1; - - if (check_dst_instant( - 1710054000, - 2024, 3, 10, 3, 0, 0, - 1, -14400, "-0400 EDT")) - return 1; - - if (check_dst_instant( - 1730613599, - 2024, 11, 3, 1, 59, 59, - 1, -14400, "-0400 EDT")) - return 1; - - if (check_dst_instant( - 1730613600, - 2024, 11, 3, 1, 0, 0, - 0, -18000, "-0500 EST")) - return 1; - - /* - * M.m.w rules must also work before the Unix epoch. In 1969, - * March 1 and November 1 were Saturdays; these rules select the - * second Saturday in March and first Saturday in November. - */ - if (setenv("TZ", "EST5EDT,M3.2.6/2,M11.1.6/2", 1) != 0) { - perror("setenv"); - return 1; - } - - tzset(); - - if (check_dst_instant( - -25808401, - 1969, 3, 8, 1, 59, 59, - 0, -18000, "-0500 EST")) - return 1; - - if (check_dst_instant( - -25808400, - 1969, 3, 8, 3, 0, 0, - 1, -14400, "-0400 EDT")) - return 1; - - if (check_dst_instant( - -5248801, - 1969, 11, 1, 1, 59, 59, - 1, -14400, "-0400 EDT")) - return 1; - - if (check_dst_instant( - -5248800, - 1969, 11, 1, 1, 0, 0, - 0, -18000, "-0500 EST")) - return 1; - - /* - * Invalid POSIX TZ specifications must fall back to UTC. - */ - static const char *invalid_timezones[] = { - "AB0", /* Short standard name */ - "EST", /* Missing standard offset */ - "EST25", /* Invalid offset hour */ - "EST5:60", /* Invalid offset minute */ - "EST5:", /* Missing offset minute */ - "EST5ED", /* Short DST name */ - "EST5-5", /* Invalid quoted abbreviation */ - }; - - size_t i; - - for (i = 0; - i < sizeof invalid_timezones / sizeof invalid_timezones[0]; - i++) { - if (check_invalid_timezone(invalid_timezones[i])) - return 1; - } - - - - /* Unset TZ: WASI must retain its UTC default. */ - if (unsetenv("TZ") != 0) { - perror("unsetenv"); - return 1; - } - - tzset(); - - if (check_utc_default("unset TZ")) - return 1; - - /* Empty TZ must also select UTC. */ - if (setenv("TZ", "", 1) != 0) { - perror("setenv"); - return 1; - } - - tzset(); - - if (check_utc_default("empty TZ")) - return 1; - - puts("Timezone tests passed"); - return 0; +int main(void) { + if (check_timezone("UTC", 1970, 1, 1, 0, 0, 0, "+0000", "UTC")) + return 1; + + if (check_timezone("UTC-5:30", 1970, 1, 1, 5, 30, 19800, "+0530", "UTC")) + return 1; + + if (check_timezone("UTC-5:3", 1970, 1, 1, 5, 3, 18180, "+0503", "UTC")) + return 1; + + if (check_timezone("UTC+4", 1969, 12, 31, 20, 0, -14400, "-0400", "UTC")) + return 1; + + /* + * POSIX quoted timezone abbreviation. + * <+05> is the abbreviation; -5 means five hours east of UTC. + */ + if (check_timezone("<+05>-5", 1970, 1, 1, 5, 0, 18000, "+0500", "+05")) + return 1; + + /* Changing TZ must reconfigure the timezone. */ + if (check_timezone("UTC-5:30", 1970, 1, 1, 5, 30, 19800, "+0530", "UTC")) + return 1; + + /* + * US Eastern timezone: + * Standard UTC-05:00 + * Daylight UTC-04:00 + * + * Second Sunday in March, 02:00: + * clocks advance from 01:59:59 to 03:00:00. + * + * First Sunday in November, 02:00: + * clocks return from 01:59:59 to 01:00:00. + */ + if (setenv("TZ", "EST5EDT,M3.2.0/2,M11.1.0/2", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_public_timezone_interfaces()) + return 1; + + /* Immediately before spring transition. */ + if (check_dst_instant(1710053999, 2024, 3, 10, 1, 59, 59, 0, -18000, + "-0500 EST")) + return 1; + + /* Immediately after spring transition. */ + if (check_dst_instant(1710054000, 2024, 3, 10, 3, 0, 0, 1, -14400, + "-0400 EDT")) + return 1; + + /* Immediately before autumn transition. */ + if (check_dst_instant(1730613599, 2024, 11, 3, 1, 59, 59, 1, -14400, + "-0400 EDT")) + return 1; + + /* Immediately after autumn transition. */ + if (check_dst_instant(1730613600, 2024, 11, 3, 1, 0, 0, 0, -18000, + "-0500 EST")) + return 1; + + /* + * POSIX extended transition times: + * + * Start: -2 hours means 22:00 on the preceding day. + * End: 26 hours means 02:00 on the following day. + */ + if (setenv("TZ", "EST5EDT,M3.2.0/-2,M11.1.0/26", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + /* Immediately before the early spring transition. */ + if (check_dst_instant(1710039599, 2024, 3, 9, 21, 59, 59, 0, -18000, + "-0500 EST")) + return 1; + + /* Immediately after the early spring transition. */ + if (check_dst_instant(1710039600, 2024, 3, 9, 23, 0, 0, 1, -14400, + "-0400 EDT")) + return 1; + + /* Immediately before the delayed autumn transition. */ + if (check_dst_instant(1730699999, 2024, 11, 4, 1, 59, 59, 1, -14400, + "-0400 EDT")) + return 1; + + /* Immediately after the delayed autumn transition. */ + if (check_dst_instant(1730700000, 2024, 11, 4, 1, 0, 0, 0, -18000, + "-0500 EST")) + return 1; + + /* + * Extended transition times can move a transition into the + * previous calendar year. + * + * The first Sunday of January 1999 is January 3: + * -167 hours places the start at December 27, 1998, 01:00 EST. + * -48 hours places the end at January 1, 1999, 00:00 EDT. + */ + if (setenv("TZ", "EST5EDT,M1.1.0/-167,M1.1.0/-48", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + /* Immediately before the December 1998 start transition. */ + if (check_dst_instant(914738399, 1998, 12, 27, 0, 59, 59, 0, -18000, + "-0500 EST")) + return 1; + + /* Immediately after the December 1998 start transition. */ + if (check_dst_instant(914738400, 1998, 12, 27, 2, 0, 0, 1, -14400, + "-0400 EDT")) + return 1; + + /* Immediately before the January 1999 end transition. */ + if (check_dst_instant(915163199, 1998, 12, 31, 23, 59, 59, 1, -14400, + "-0400 EDT")) + return 1; + + /* Immediately after the January 1999 end transition. */ + if (check_dst_instant(915163200, 1998, 12, 31, 23, 0, 0, 0, -18000, + "-0500 EST")) + return 1; + + /* + * Positive extended transition times can move an event into the + * following calendar year. December 27, 1998 at 00:00 plus + * 167 hours is January 2, 1999 at 23:00 standard time. + */ + if (setenv("TZ", "EST5EDT,M12.5.0/167,M3.2.0/2", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_dst_instant(915335999, 1999, 1, 2, 22, 59, 59, 0, -18000, + "-0500 EST")) + return 1; + + if (check_dst_instant(915336000, 1999, 1, 3, 0, 0, 0, 1, -14400, "-0400 EDT")) + return 1; + + /* + * When DST rules are omitted, WASI uses its implementation-defined + * default: the current U.S. transition dates. + */ + if (setenv("TZ", "EST5EDT", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_public_timezone_interfaces()) + return 1; + + if (check_dst_instant(1710053999, 2024, 3, 10, 1, 59, 59, 0, -18000, + "-0500 EST")) + return 1; + + if (check_dst_instant(1710054000, 2024, 3, 10, 3, 0, 0, 1, -14400, + "-0400 EDT")) + return 1; + + if (check_dst_instant(1730613599, 2024, 11, 3, 1, 59, 59, 1, -14400, + "-0400 EDT")) + return 1; + + if (check_dst_instant(1730613600, 2024, 11, 3, 1, 0, 0, 0, -18000, + "-0500 EST")) + return 1; + + /* + * M.m.w rules must also work before the Unix epoch. In 1969, + * March 1 and November 1 were Saturdays; these rules select the + * second Saturday in March and first Saturday in November. + */ + if (setenv("TZ", "EST5EDT,M3.2.6/2,M11.1.6/2", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_dst_instant(-25808401, 1969, 3, 8, 1, 59, 59, 0, -18000, + "-0500 EST")) + return 1; + + if (check_dst_instant(-25808400, 1969, 3, 8, 3, 0, 0, 1, -14400, "-0400 EDT")) + return 1; + + if (check_dst_instant(-5248801, 1969, 11, 1, 1, 59, 59, 1, -14400, + "-0400 EDT")) + return 1; + + if (check_dst_instant(-5248800, 1969, 11, 1, 1, 0, 0, 0, -18000, "-0500 EST")) + return 1; + + /* + * Invalid POSIX TZ specifications must fall back to UTC. + */ + static const char *invalid_timezones[] = { + "AB0", /* Short standard name */ + "EST", /* Missing standard offset */ + "EST25", /* Invalid offset hour */ + "EST5:60", /* Invalid offset minute */ + "EST5:", /* Missing offset minute */ + "EST5ED", /* Short DST name */ + "EST5-5", /* Invalid quoted abbreviation */ + }; + + size_t i; + + for (i = 0; i < sizeof invalid_timezones / sizeof invalid_timezones[0]; i++) { + if (check_invalid_timezone(invalid_timezones[i])) + return 1; + } + + /* Unset TZ: WASI must retain its UTC default. */ + if (unsetenv("TZ") != 0) { + perror("unsetenv"); + return 1; + } + + tzset(); + + if (check_utc_default("unset TZ")) + return 1; + + /* Empty TZ must also select UTC. */ + if (setenv("TZ", "", 1) != 0) { + perror("setenv"); + return 1; + } + + tzset(); + + if (check_utc_default("empty TZ")) + return 1; + + puts("Timezone tests passed"); + return 0; }