Skip to content

Commit 2a9839b

Browse files
committed
zoned_time
1 parent 0e078f6 commit 2a9839b

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

CMakeLists.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,50 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
265265
set(WERROR_FLAG -Werror)
266266
endif ()
267267

268+
include(CheckCXXSourceCompiles)
269+
270+
# Check if compiler supports C++20
271+
check_cxx_compiler_flag("-std=c++20" HAVE_CXX20_FLAG)
272+
273+
if (HAVE_CXX20_FLAG)
274+
set(CMAKE_REQUIRED_FLAGS "-std=c++20")
275+
set(FMT_HAVE_STD_ZONED_TIME_TEST
276+
"#include <chrono>
277+
#if !defined(__cpp_lib_chrono) || __cpp_lib_chrono < 201907L
278+
#error \"zoned_time not available\"
279+
#endif
280+
int main() {
281+
using namespace std::chrono;
282+
zoned_time<std::chrono::seconds> zt;
283+
return 0;
284+
}
285+
")
286+
check_cxx_source_compiles("${FMT_HAVE_STD_ZONED_TIME_TEST}" FMT_HAS_STD_ZONED_TIME)
287+
else ()
288+
set(FMT_HAS_STD_ZONED_TIME OFF)
289+
message(STATUS "C++20 not supported, zoned_time disabled")
290+
endif ()
291+
292+
check_cxx_source_compiles("${FMT_HAVE_STD_ZONED_TIME_TEST}" FMT_HAS_STD_ZONED_TIME)
293+
294+
if (FMT_HAS_STD_ZONED_TIME)
295+
# Ensure we set the macro for both the library target and the header-only target.
296+
foreach (tgt IN LISTS INSTALL_TARGETS)
297+
if (TARGET ${tgt})
298+
if ("${tgt}" STREQUAL "fmt-header-only")
299+
# header-only target: INTERFACE definition is propagated to consumers
300+
target_compile_definitions(${tgt} INTERFACE FMT_HAS_STD_ZONED_TIME=1)
301+
else ()
302+
# library target: PUBLIC so it's visible to dependents as well
303+
target_compile_definitions(${tgt} PUBLIC FMT_HAS_STD_ZONED_TIME=1)
304+
endif ()
305+
endif ()
306+
endforeach ()
307+
else ()
308+
# Optional: provide a global fallback so that builds relying on a global define still see it.
309+
# add_compile_definitions(FMT_HAS_STD_ZONED_TIME=1) # usually not needed; prefer target-specific defs
310+
endif ()
311+
268312
if (MSVC)
269313
set(PEDANTIC_COMPILE_FLAGS /W3)
270314
set(WERROR_FLAG /WX)

include/fmt/chrono.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ using utc_time = std::chrono::time_point<detail::utc_clock, Duration>;
265265
template <class Duration>
266266
using local_time = std::chrono::time_point<detail::local_t, Duration>;
267267

268+
#if defined(FMT_HAS_STD_ZONED_TIME)
269+
template <typename Duration, typename TimeZonePtr>
270+
using zoned_time = std::chrono::zoned_time<Duration, TimeZonePtr>;
271+
#endif
272+
268273
namespace detail {
269274

270275
// Prevents expansion of a preceding token as a function-style macro.
@@ -2240,6 +2245,42 @@ struct formatter<local_time<Duration>, Char>
22402245
}
22412246
};
22422247

2248+
#if defined(FMT_HAS_STD_ZONED_TIME)
2249+
template <typename Duration, typename TimeZonePtr, typename Char>
2250+
struct formatter<zoned_time<Duration, TimeZonePtr>, Char,
2251+
std::enable_if_t<std::is_pointer_v<TimeZonePtr>>>
2252+
: private formatter<std::tm, Char> {
2253+
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
2254+
return this->do_parse(ctx, true);
2255+
}
2256+
2257+
template <typename FormatContext>
2258+
auto format(const std::chrono::zoned_time<Duration, TimeZonePtr>& val,
2259+
FormatContext& ctx) const -> decltype(ctx.out()) {
2260+
auto time_info = val.get_info();
2261+
auto time_since_epoch = val.get_local_time().time_since_epoch();
2262+
auto seconds_since_epoch =
2263+
detail::duration_cast<std::chrono::seconds>(time_since_epoch);
2264+
// Use gmtime to prevent time zone conversion since local_time has an
2265+
// unspecified time zone.
2266+
std::tm t = gmtime(seconds_since_epoch.count());
2267+
// Create a custom tm with timezone info if supported
2268+
if constexpr (detail::has_tm_zone<std::tm>::value) {
2269+
t.tm_zone = time_info.abbrev.c_str();
2270+
t.tm_gmtoff = time_info.offset.count();
2271+
}
2272+
using period = typename Duration::period;
2273+
if (period::num == 1 && period::den == 1 &&
2274+
!std::is_floating_point<typename Duration::rep>::value) {
2275+
return formatter<std::tm, Char>::format(t, ctx);
2276+
}
2277+
auto subsecs =
2278+
detail::duration_cast<Duration>(time_since_epoch - seconds_since_epoch);
2279+
return formatter<std::tm, Char>::do_format(t, ctx, &subsecs);
2280+
}
2281+
};
2282+
#endif
2283+
22432284
FMT_END_EXPORT
22442285
FMT_END_NAMESPACE
22452286

0 commit comments

Comments
 (0)