diff options
author | Brad King <brad.king@kitware.com> | 2022-10-28 14:04:28 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2022-10-28 14:04:47 (GMT) |
commit | 94983f1d969f227c5343e291926a20599c0f5411 (patch) | |
tree | d27fd3eda159c210590966fba8f94cefc5f07ac2 /Source | |
parent | 3469d53b3f9ddabe067044ed2cac92215fbe825c (diff) | |
parent | 9da542d5c1c983b9ea608b7fc04d75826278564f (diff) | |
download | CMake-94983f1d969f227c5343e291926a20599c0f5411.zip CMake-94983f1d969f227c5343e291926a20599c0f5411.tar.gz CMake-94983f1d969f227c5343e291926a20599c0f5411.tar.bz2 |
Merge topic 'timestamp-timezone'
9da542d5c1 string(TIMESTAMP): Add %z and %Z for adding time zone string
Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !7788
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmTimestamp.cxx | 63 | ||||
-rw-r--r-- | Source/cmTimestamp.h | 4 |
2 files changed, 62 insertions, 5 deletions
diff --git a/Source/cmTimestamp.cxx b/Source/cmTimestamp.cxx index 677fdb6..7e47b4e 100644 --- a/Source/cmTimestamp.cxx +++ b/Source/cmTimestamp.cxx @@ -128,8 +128,8 @@ std::string cmTimestamp::CreateTimestampFromTimeT(time_t timeT, : static_cast<char>(0); if (c1 == '%' && c2 != 0) { - result += - this->AddTimestampComponent(c2, timeStruct, timeT, microseconds); + result += this->AddTimestampComponent(c2, timeStruct, timeT, utcFlag, + microseconds); ++i; } else { result += c1; @@ -179,7 +179,7 @@ time_t cmTimestamp::CreateUtcTimeTFromTm(struct tm& tm) const } std::string cmTimestamp::AddTimestampComponent( - char flag, struct tm& timeStruct, const time_t timeT, + char flag, struct tm& timeStruct, const time_t timeT, const bool utcFlag, const uint32_t microseconds) const { std::string formatString = cmStrCat('%', flag); @@ -203,6 +203,63 @@ std::string cmTimestamp::AddTimestampComponent( case 'Y': case '%': break; + case 'Z': +#if defined(__GLIBC__) + // 'struct tm' has the time zone, so strftime can honor UTC. + static_cast<void>(utcFlag); +#else + // 'struct tm' may not have the time zone, so strftime may + // use local time. Hard-code the UTC result. + if (utcFlag) { + return std::string("GMT"); + } +#endif + break; + case 'z': { +#if defined(__GLIBC__) + // 'struct tm' has the time zone, so strftime can honor UTC. + static_cast<void>(utcFlag); +#else + // 'struct tm' may not have the time zone, so strftime may + // use local time. Hard-code the UTC result. + if (utcFlag) { + return std::string("+0000"); + } +#endif +#ifndef _AIX + break; +#else + std::string xpg_sus_old; + bool const xpg_sus_was_set = + cmSystemTools::GetEnv("XPG_SUS_ENV", xpg_sus_old); + if (xpg_sus_was_set && xpg_sus_old == "ON") { + break; + } + xpg_sus_old = "XPG_SUS_ENV=" + xpg_sus_old; + + // On AIX systems, %z requires XPG_SUS_ENV=ON to work as desired. + cmSystemTools::PutEnv("XPG_SUS_ENV=ON"); + tzset(); + + char buffer[16]; + size_t size = strftime(buffer, sizeof(buffer), "%z", &timeStruct); + +# ifndef CMAKE_BOOTSTRAP + if (xpg_sus_was_set) { + cmSystemTools::PutEnv(xpg_sus_old); + } else { + cmSystemTools::UnsetEnv("XPG_SUS_ENV"); + } +# else + // No UnsetEnv during bootstrap. This is good enough for CMake itself. + cmSystemTools::PutEnv(xpg_sus_old); + static_cast<void>(xpg_sus_was_set); +# endif + tzset(); + + return std::string(buffer, size); +#endif + } case 's': // Seconds since UNIX epoch (midnight 1-jan-1970) { // Build a time_t for UNIX epoch and subtract from the input "timeT": diff --git a/Source/cmTimestamp.h b/Source/cmTimestamp.h index ada5006..05c6342 100644 --- a/Source/cmTimestamp.h +++ b/Source/cmTimestamp.h @@ -32,6 +32,6 @@ private: time_t CreateUtcTimeTFromTm(struct tm& timeStruct) const; std::string AddTimestampComponent(char flag, struct tm& timeStruct, - time_t timeT, - uint32_t microseconds = 0) const; + time_t timeT, bool utcFlag, + uint32_t microseconds) const; }; |