string(TIMESTAMP: inconsistent handling of local vs UTC time zones
0 reputation · 22 Jan 2023, 03:20 UTC
0 reputation · 22 Jan 2023, 03:20 UTC
The string(TIMESTAMP) command formats the current date and time. Without the UTC flag it relies on the build machine’s local time zone, which is derived from the TZ environment variable or the system clock. When UTC is specified the output is deterministic, but CMake offers no way to target an arbitrary region or to apply daylight‑saving rules beyond the host’s current setting.
Because the underlying strftime implementation varies between POSIX systems and Windows, the same format string can yield different results (e.g., %Z may be empty on Windows). Moreover, changing TZ after CMake has already configured a project has no effect on timestamps that were already computed.
These differences raise the following unresolved questions:
string(TIMESTAMP) without external tooling?tzdata versions?TZ environment variable have on Windows builds, and does it provide the same level of control as on POSIX platforms?33275 reputation · 22 Jan 2023, 10:13 UTC
1. Can you request a specific time zone directly from string(TIMESTAMP)? No. The command only accepts an optional UTC flag. To get another zone you must set the TZ environment variable before CMake runs or compute the value externally.
2. How does CMake handle daylight‑saving transitions? It delegates to the host’s C runtime (localtime/strftime), so DST rules come from the OS’s tzdata database. The result is deterministic only if the same tzdata version is present on every machine.
3. What about Windows? The MSVC runtime honors TZ only in a very limited form and does not use the IANA tz database. MinGW/Posix‑style builds on Windows behave more like Linux, but the set of supported zone names is narrower.
Because string(TIMESTAMP) relies on the host’s local‑time machinery, any change to TZ, the system clock, or the tzdata database after configure time will not affect already‑computed timestamps. This is why reproducible builds often embed a deterministic UTC stamp or an externally supplied value instead of using string(TIMESTAMP) directly.
string(TIMESTAMP <var> <format> [UTC]) – no zone name or offset option.UTC is omitted, the C runtime’s localtime is used, pulling the current local zone from the OS.TZ can be set to a TZ string (e.g., America/New_York) or an offset (e.g., +0200) before invoking CMake.TZ or the clock have no effect until CMake is rerun.UTC flag to the string(TIMESTAMP) call.string(TIMESTAMP BUILD_TIME "%Y-%m-%d %H:%M:%S UTC" UTC)
TZ before configuring. For example, in a shell script:export TZ=America/Los_Angeles
cmake -S . -B build
date or a language runtime that supports IANA zones, then pass the result to CMake via a cache variable or an environment variable.SOURCE_DATE_EPOCH (if available) or a CI‑provided timestamp.To tailor the recommendation to your environment, could you tell me which CMake generator (e.g., Unix Makefiles, Visual Studio 17 2022, MinGW Makefiles) and operating system you are using? The behavior of TZ and the available zone names differ across these contexts.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.