Enable some local_time tests and make them deterministic

This commit is contained in:
Victor Zverovich
2025-03-23 11:42:13 -07:00
parent 17898794a9
commit 7ac97cbd1d

View File

@@ -350,30 +350,14 @@ TEST(chrono_test, system_clock_time_point) {
} }
} }
#if FMT_USE_LOCAL_TIME TEST(chrono_test, local_time) {
auto t =
fmt::local_time<std::chrono::seconds>(std::chrono::seconds(290088000));
EXPECT_EQ(fmt::format("{:%Y-%m-%d %H:%M:%S}", t), "1979-03-12 12:00:00");
EXPECT_EQ(fmt::format("{}", t), "1979-03-12 12:00:00");
EXPECT_EQ(fmt::format("{:}", t), "1979-03-12 12:00:00");
template <typename Duration> std::vector<std::string> specs = {
auto strftime_full_local(fmt::local_time<Duration> t) -> std::string {
auto sys_time = std::chrono::system_clock::to_time_t(
std::chrono::current_zone()->to_sys(t));
auto tm = *std::localtime(&sys_time);
return system_strftime("%Y-%m-%d %H:%M:%S", &tm);
}
TEST(chrono_test, local_system_clock_time_point) {
# ifdef _WIN32
return; // Not supported on Windows.
# endif
auto t1 = std::chrono::time_point_cast<std::chrono::seconds>(
std::chrono::current_zone()->to_local(std::chrono::system_clock::now()));
EXPECT_EQ(strftime_full_local(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
EXPECT_EQ(strftime_full_local(t1), fmt::format("{}", t1));
EXPECT_EQ(strftime_full_local(t1), fmt::format("{:}", t1));
using time_point = fmt::local_time<std::chrono::seconds>;
auto t2 = time_point(std::chrono::seconds(86400 + 42));
EXPECT_EQ(strftime_full_local(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
std::vector<std::string> spec_list = {
"%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C", "%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C",
"%EC", "%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U", "%EC", "%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U",
"%OU", "%W", "%OW", "%V", "%OV", "%j", "%d", "%Od", "%e", "%OU", "%W", "%OW", "%V", "%OV", "%j", "%d", "%Od", "%e",
@@ -383,44 +367,47 @@ TEST(chrono_test, local_system_clock_time_point) {
#ifndef _WIN32 #ifndef _WIN32
// Disabled on Windows because these formats are not consistent among // Disabled on Windows because these formats are not consistent among
// platforms. // platforms.
spec_list.insert(spec_list.end(), {"%c", "%Ec", "%r"}); specs.insert(specs.end(), {"%c", "%Ec", "%r"});
#elif !FMT_HAS_C99_STRFTIME #elif !FMT_HAS_C99_STRFTIME
// Only C89 conversion specifiers when using MSVCRT instead of UCRT // Only C89 conversion specifiers when using MSVCRT instead of UCRT
spec_list = {"%%", "%Y", "%y", "%b", "%B", "%m", "%U", "%W", "%j", "%d", "%a", specs = {"%%", "%Y", "%y", "%b", "%B", "%m", "%U", "%W", "%j", "%d", "%a",
"%A", "%w", "%H", "%I", "%M", "%S", "%x", "%X", "%p", "%Z"}; "%A", "%w", "%H", "%I", "%M", "%S", "%x", "%X", "%p", "%Z"};
#endif #endif
spec_list.push_back("%Y-%m-%d %H:%M:%S"); specs.push_back("%Y-%m-%d %H:%M:%S");
for (const auto& spec : spec_list) { #if FMT_USE_LOCAL_TIME
auto t = std::chrono::system_clock::to_time_t( # ifdef _WIN32
std::chrono::current_zone()->to_sys(t1)); return; // Not supported on Windows.
auto tm = *std::localtime(&t); # endif
for (const auto& spec : specs) {
auto sys_time = std::chrono::system_clock::to_time_t(
std::chrono::current_zone()->to_sys(t));
auto tm = *std::localtime(&sys_time);
auto sys_output = system_strftime(spec, &tm); auto sys_output = system_strftime(spec, &tm);
auto fmt_spec = fmt::format("{{:{}}}", spec); auto fmt_spec = fmt::format("{{:{}}}", spec);
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1)); EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t));
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm)); EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
} }
if (std::find(spec_list.cbegin(), spec_list.cend(), "%z") != if (std::find(specs.cbegin(), specs.cend(), "%z") != specs.cend()) {
spec_list.cend()) { auto sys_time = std::chrono::system_clock::to_time_t(
auto t = std::chrono::system_clock::to_time_t( std::chrono::current_zone()->to_sys(t));
std::chrono::current_zone()->to_sys(t1)); auto tm = *std::localtime(&sys_time);
auto tm = *std::localtime(&t);
auto sys_output = system_strftime("%z", &tm); auto sys_output = system_strftime("%z", &tm);
sys_output.insert(sys_output.end() - 2, 1, ':'); sys_output.insert(sys_output.end() - 2, 1, ':');
EXPECT_EQ(sys_output, fmt::format("{:%Ez}", t1)); EXPECT_EQ(sys_output, fmt::format("{:%Ez}", t));
EXPECT_EQ(sys_output, fmt::format("{:%Ez}", tm)); EXPECT_EQ(sys_output, fmt::format("{:%Ez}", tm));
EXPECT_EQ(sys_output, fmt::format("{:%Oz}", t1)); EXPECT_EQ(sys_output, fmt::format("{:%Oz}", t));
EXPECT_EQ(sys_output, fmt::format("{:%Oz}", tm)); EXPECT_EQ(sys_output, fmt::format("{:%Oz}", tm));
} }
}
#endif // FMT_USE_LOCAL_TIME #endif // FMT_USE_LOCAL_TIME
}
TEST(chrono_test, daylight_savings_time_end) { TEST(chrono_test, daylight_savings_time_end) {
// 2024-10-27 03:05 as the number of seconds since epoch in Europe/Kyiv time. // 2024-10-27 03:05 as the number of seconds since epoch in Europe/Kyiv time.