os.time and os.date behavior during DST transitions
0 reputation · 01 Apr 2020, 08:38 UTC
0 reputation · 01 Apr 2020, 08:38 UTC
Lua relies on the host operating system's C runtime (CRT) to handle regional time zones and date conversions. While os.date and os.time facilitate transformations between Unix timestamps and date tables, the language does not maintain an internal timezone database.
When working with local time, the interaction between the Lua environment and the system's environment variables (such as TZ) creates uncertainty during Daylight Saving Time (DST) transitions. Because the conversion logic is delegated to the underlying platform, behavior may diverge between POSIX and Windows environments.
os.date output is reflecting a DST offset without external libraries?35275 reputation · 01 Apr 2020, 17:07 UTC
Lua does not have an internal timezone database; it delegates all time calculations to the host operating system's C runtime (CRT) via mktime() and localtime(). Consequently, during the autumn DST fallback—where the same local time occurs twice—the resolution of ambiguous timestamps is platform-dependent.
TZ environment variable. In many implementations, if the time is ambiguous, the system defaults to the first occurrence (the DST period) or the second (the standard period) based on the specific C library version (glibc vs. musl).Because Lua provides no built-in mechanism to specify the "fold" (whether to use the first or second occurrence of the hour), os.time() will return a single Unix timestamp based on the OS's internal preference for that specific local time input.
You can verify if the current time is reflecting a DST offset by using os.date with specific format strings. This allows you to check the numeric offset or the timezone abbreviation provided by the OS.
-- Check the numeric UTC offset (e.g., -0700 or -0800)
print("Numeric Offset: " .. os.date("%z"))
-- Check the timezone abbreviation (e.g., PDT vs PST)
print("Timezone Name: " .. os.date("%Z"))
os.date("%z") to see the current offset from UTC.TZ environment variable before launching the Lua interpreter to see how os.time() resolves the same local hour under different rules.Diagnostic Note: To provide a more precise prediction of your specific resolution, please specify the host operating system and the C runtime version (e.g., Ubuntu 22.04 with glibc) being used.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.