diff options
author | Victor Zverovich <viz@meta.com> | 2023-12-28 16:14:32 -0800 |
---|---|---|
committer | Victor Zverovich <viz@meta.com> | 2023-12-28 16:36:18 -0800 |
commit | be57ec7ec0c24a0fcfef774bf1e586ef10686070 (patch) | |
tree | 6611d38b41988246478713d8f2ec7b208d21ce74 | |
parent | 305747d4402a2cd48f48477ee06c171e7a70f286 (diff) | |
download | fmtlib-be57ec7ec0c24a0fcfef774bf1e586ef10686070.tar.gz |
Fix chrono-test on platforms with 32-bit time_t
-rw-r--r-- | test/chrono-test.cc | 48 |
1 files changed, 21 insertions, 27 deletions
diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 56fbd382..fea296e8 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -17,6 +17,9 @@ using fmt::runtime; using testing::Contains; +template <typename Duration> +using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>; + #if defined(__MINGW32__) && !defined(_UCRT) // Only C89 conversion specifiers when using MSVCRT instead of UCRT # define FMT_HAS_C99_STRFTIME 0 @@ -858,42 +861,33 @@ TEST(chrono_test, utc_clock) { } #endif -TEST(chrono_test, timestamps_ratios) { - std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> - t1(std::chrono::milliseconds(67890)); - +TEST(chrono_test, timestamp_ratios) { + auto t1 = sys_time<std::chrono::milliseconds>(std::chrono::milliseconds(67890)); EXPECT_EQ(fmt::format("{:%M:%S}", t1), "01:07.890"); - std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes> t2( - std::chrono::minutes(7)); - + auto t2 = sys_time<std::chrono::minutes>(std::chrono::minutes(7)); EXPECT_EQ(fmt::format("{:%M:%S}", t2), "07:00"); - std::chrono::time_point<std::chrono::system_clock, - std::chrono::duration<int, std::ratio<9>>> - t3(std::chrono::duration<int, std::ratio<9>>(7)); - + auto t3 = sys_time<std::chrono::duration<int, std::ratio<9>>>( + std::chrono::duration<int, std::ratio<9>>(7)); EXPECT_EQ(fmt::format("{:%M:%S}", t3), "01:03"); - std::chrono::time_point<std::chrono::system_clock, - std::chrono::duration<int, std::ratio<63>>> - t4(std::chrono::duration<int, std::ratio<63>>(1)); - + auto t4 = sys_time<std::chrono::duration<int, std::ratio<63>>>( + std::chrono::duration<int, std::ratio<63>>(1)); EXPECT_EQ(fmt::format("{:%M:%S}", t4), "01:03"); - std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> - t5(std::chrono::seconds(32503680000)); - - EXPECT_EQ(fmt::format("{:%Y-%m-%d}", t5), "3000-01-01"); - -#if FMT_SAFE_DURATION_CAST - using years = std::chrono::duration<std::int64_t, std::ratio<31556952>>; - std::chrono::time_point<std::chrono::system_clock, years> t6( - (years(std::numeric_limits<std::int64_t>::max()))); + if (sizeof(time_t) > 4) { + auto tp = sys_time<std::chrono::milliseconds>( + std::chrono::seconds(32503680000)); + EXPECT_EQ(fmt::format("{:%Y-%m-%d}", tp), "3000-01-01"); + } - EXPECT_THROW_MSG((void)fmt::format("{:%Y-%m-%d}", t6), fmt::format_error, - "cannot format duration"); -#endif + if (FMT_SAFE_DURATION_CAST) { + using years = std::chrono::duration<std::int64_t, std::ratio<31556952>>; + auto tp = sys_time<years>(years(std::numeric_limits<std::int64_t>::max())); + EXPECT_THROW_MSG((void)fmt::format("{:%Y-%m-%d}", tp), fmt::format_error, + "cannot format duration"); + } } TEST(chrono_test, timestamps_sub_seconds) { |