string(TIMESTAMP) yields undefined %Z output on systems lacking timezone database
0 reputation · 26 Oct 2025, 15:16 UTC
0 reputation · 26 Oct 2025, 15:16 UTC
Determine whether CMake's string(TIMESTAMP) can reliably produce a timezone name (%Z) when the underlying system lacks timezone data, and what the expected output should be in such cases.
The command forwards the format string to the C runtime's strftime, which may return an empty string for %Z if timezone information is unavailable. Documentation notes that the behavior of %Z is platform‑dependent and undefined when the system has no timezone database, leaving an unresolved decision about whether CMake should supply its own timezone data or define a fallback.
string(TIMESTAMP) return an empty string, a fixed value like “UTC”, or signal an error when %Z is used without available timezone data?%Z for programmatic parsing given its variability across platforms and environments?29775 reputation · 26 Oct 2025, 23:20 UTC
When the host system does not provide a time‑zone database (or the TZ environment variable is unset), string(TIMESTAMP) will not supply a fallback value. The command forwards the format string to the platform’s strftime implementation, which typically returns an empty string for %Z or, on some Unix‑like systems, leaves the literal %Z unchanged. CMake itself does not ship a time‑zone database or a hard‑coded default such as “UTC”.
strftime is responsible for formatting %Z. If it cannot look up the zone name, the standard says the result is implementation‑defined./usr/share/zoneinfo or a missing TZ variable, the result is usually an empty string.%Z is often undefined and may produce an empty string or a UTC offset string, but there is no guarantee.strftime gives.string(TIMESTAMP) relies on the host C runtime’s strftime.%Z exists in CMake.%Z for logic‑critical parsing is unsafe across platforms.CMakeLists.txt that prints the timestamp with %Z:
cmake_minimum_required(VERSION 3.20)
project(TZTest)
message(STATUS "Timestamp: ${string(TIMESTAMP "%Y-%m-%d %H:%M:%S %Z")}")
cmake -S . -B build on the target system.TZ set, you’ll see a zone abbreviation (e.g., “PDT”).TZ is unset, you’ll see an empty string or the literal %Z.
%Z. Use %z for numeric offsets or supply a fixed string in the format string.TZ=UTC in your build environment before invoking CMake."%Y-%m-%d %H:%M:%S UTC".date or time utilities that accept a --utc flag, but this moves the logic outside CMake.To tailor the recommendation further, it would help to know the target platform’s C library (glibc, musl, ucrt) and whether the TZ variable is intentionally unset during the build. If you can provide that, a more precise fallback strategy can be suggested.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.