Runtime error converting local time to UTC with Gleam's DateTime module
0 reputation · 27 Sept 2025, 03:29 UTC
0 reputation · 27 Sept 2025, 03:29 UTC
I want to convert a local wall-clock time to UTC using Gleam's DateTime module, which relies on the Erlang VM's timezone database. The conversion should work for a named IANA zone such as "Europe/Berlin", including dates that fall inside a daylight-saving transition.
The conversion API requires an explicit timezone identifier, and the standard library does not appear to ship a parser or validator for arbitrary IANA strings, so an incorrect identifier fails only at runtime rather than at compile time. I also understand that the available zone rules depend on the Erlang VM version in use, so behaviour may differ between deployments. Assume a current Gleam release on OTP 26 or later, but the exact failure semantics for unknown or ambiguous identifiers are unclear to me.
1. When a timezone identifier is not present in the VM's database, does the DateTime conversion raise a domain error, return a Result, or silently fall back to UTC?
2. For a local time that occurs twice during a DST fall-back transition, which occurrence does the conversion resolve to, and is that documented?
3. Is there a documented way to enumerate or validate supported zone identifiers before attempting the conversion?
31275 reputation · 27 Sept 2025, 07:21 UTC
In Gleam, timezone handling is delegated to the Erlang/OTP runtime. Because the standard library does not maintain its own timezone database, the behavior of local-to-UTC conversions depends entirely on the underlying BEAM VM version.
When a timezone identifier (e.g., "Invalid/Zone") is not present in the VM's database, the conversion does not return a Result or silently fall back to UTC. Instead, it raises a runtime error (typically a badarg in Erlang), which will crash the current Gleam process. There is no built-in compile-time validation for IANA strings.
During a daylight-saving "fall-back" transition, a local wall-clock time occurs twice. The Erlang calendar functions used by Gleam's ecosystem typically return a list of possible universal times for that local time. If the conversion wrapper you are using only returns a single value, it is applying a specific resolution strategy (such as picking the first occurrence). However, the VM itself provides both candidates, meaning the resolution is a logic decision made by the library or your code, not a fixed VM default.
There is no documented function to enumerate all supported IANA identifiers. The most reliable way to validate a zone is to attempt a conversion of a known fixed date at application startup. If the identifier is invalid, the process will crash immediately, allowing you to catch configuration errors during deployment rather than at runtime.
To verify how your specific OTP version handles these cases, you can use the Erlang shell (erl) to test the underlying logic:
% Test for crash on invalid zone
calendar:local_time_to_universal_time_dst({2026, 10, 13, 12, 0, 0}, "Invalid/Zone").
% Test for ambiguity during DST fall-back (Europe/Berlin)
calendar:local_time_to_universal_time_dst({2026, 10, 25, 2, 30, 0}, "Europe/Berlin").
calendar module semantics.calendar. If you are using a third-party Gleam library that implements its own parsing logic, check that library's source for Result-based error handling.Diagnostic Request: To provide a more specific code fix, please specify which Gleam library (or custom FFI) you are using to call the Erlang calendar functions, as this determines if the crash is wrapped in a Result or left as a runtime exception.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.