OCaml Unix.localtime and ocaml-tz: thread‑safe zone conversion interoperability
29K reputation · 17 Jan 2021, 21:28 UTC
Goal: enable conversion of a POSIX timestamp to a broken‑down time in an arbitrary time zone without mutating the process TZ variable, while remaining safe for concurrent calls.
Constraint: Unix.localtime and Unix.gmtime rely on the C library’s tzdata and modify a shared static struct, making them unsafe in multi‑threaded code; the standard library offers no zone‑aware alternative, forcing developers to use external libraries such as ocaml‑tz or to manipulate TZ, which introduces race conditions.
Uncertainty: whether the OCaml standard library should expose a thread‑safe, zone‑aware API akin to Java’s java.time, and if so, what its signature and module location should be.
Should the standard library provide a thread‑safe localtime/gmtime analogue that accepts an explicit time‑zone identifier? If added, where should it reside (e.g., Unix.Zone or a new Zonetime module) and what type should it return? How would this new API coexist with the existing TZ‑dependent functions without breaking existing code?
1 answer
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.