Skip to content

Add POSIX TZ support for WASI - #930

Open
abelgeorgeantony wants to merge 2 commits into
WebAssembly:mainfrom
abelgeorgeantony:tz-posix-wasi
Open

abelgeorgeantony wants to merge 2 commits into
WebAssembly:mainfrom
abelgeorgeantony:tz-posix-wasi

Conversation

@abelgeorgeantony

@abelgeorgeantony abelgeorgeantony commented Dec 4, 2026 •

Copy link
Copy Markdown

Add POSIX TZ string support to the WASI-specific timezone implementation.

Previously, the WASI-specific implementation provided only UTC timezone information. This adds support for configuring timezones through POSIX TZ strings.

The implementation supports:

  • Standard and daylight-saving timezone abbreviations and UTC offsets.
  • POSIX transition rules in Jn, n, and Mm.n.d formats.
  • Signed and extended transition times, including transitions crossing calendar-year boundaries.
  • The public timezone interfaces tzname, timezone, daylight, and tzset(), under the appropriate feature-test macros.

It also enables %Z parsing in strptime.

The existing upstream TZif implementation and WASI internal ABI are preserved.

When daylight-saving rules are omitted, the implementation uses current U.S. transition rules as its implementation-defined default. This change does not add geographical IANA timezone database support or TZif file loading.

Testing

  • Complete wasi-libc CTest suite: 234/234 tests passed.
  • Added timezone regression coverage for offsets, DST, malformed specifications, and extended transition times.
  • End-to-end GHC 9.12.4.20260402 test: TZ=UTC-5:30 correctly produces an offset of 330 minutes east of UTC.
  • GitHub Actions: all 36 required checks passed; one check was skipped.

Motivation

This change was motivated by an issue encountered while running Haskell applications under WASI. GHC's Data.Time.LocalTime.getCurrentTimeZone relies on the underlying C library's local-time support to determine the configured timezone. However, the WASI-specific timezone implementation effectively treated the local timezone as UTC and did not interpret POSIX TZ specifications.

Consequently, setting TZ=UTC-5:30 did not produce the expected UTC+05:30 offset. Supporting POSIX TZ strings addresses this limitation at the libc level, allowing applications and language runtimes to obtain the configured local timezone without implementing their own timezone handling.

AI disclosure

AI assistance (ChatGPT) was used during analysis and implementation, CI failure investigation, and preparation of this pull request description. I reviewed the changes and verified them through local testing and GitHub Actions.

@abelgeorgeantony
abelgeorgeantony marked this pull request as draft December 4, 2026 15:12
@abelgeorgeantony
abelgeorgeantony marked this pull request as ready for review December 4, 2026 15:40
@alexcrichton

Copy link
Copy Markdown
Collaborator

Thanks!

I'm not very familiar with either timezones or musl's implementation of timezones. My naive expectation here is that this PR would remove a bunch of #ifdef saying "wasi now supports this" but otherwise the bulk of the management and code would come from upstream musl. This PR, however, has what appears to be a large amount of novel new code in libc-top-half/musl/src/time/__tz.c which is not otherwise sourced from musl itself.

Could you explain a bit more why that's the case? Why isn't upstream musl code used mostly-as-is, and why is a large amount of new code introduced here?

@abelgeorgeantony

Copy link
Copy Markdown
Author

I went with a separate WASI implementation because of the filesystem dependencies in upstream musl's implementation. It can search for and load TZif files from locations such as /usr/share/zoneinfo and /etc/zoneinfo, which isn't something we can assume is available in a sandboxed WASI application, particularly in browser environments. I wanted POSIX TZ strings to work without requiring access to or bundling a timezone database.

There were also WASI-specific differences to account for, such as the __secs_to_zone() signature and locking requirements.

I hadn't fully explored whether the POSIX parsing and transition logic could be separated cleanly from TZif handling. I agree that reusing musl's existing implementation would be preferable, and I'd be happy to explore that approach.

@alexcrichton

Copy link
Copy Markdown
Collaborator

I think it's reasonable to skip probes for unix-like files because yeah they're practically guarnateed to not exist in the sandbox. That being said if upstream code could be reused as-is as much as possible I'd prefer that. For example an #ifdef to fail a file open on WASI but otherwise reusing upstream musl as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants