Skip to content

Commit 7ac97cb

Browse files
committed
Enable some local_time tests and make them deterministic
1 parent 1789879 commit 7ac97cb

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

test/chrono-test.cc

+30-43
Original file line numberDiff line numberDiff line change
@@ -350,77 +350,64 @@ TEST(chrono_test, system_clock_time_point) {
350350
}
351351
}
352352

353-
#if FMT_USE_LOCAL_TIME
354-
355-
template <typename Duration>
356-
auto strftime_full_local(fmt::local_time<Duration> t) -> std::string {
357-
auto sys_time = std::chrono::system_clock::to_time_t(
358-
std::chrono::current_zone()->to_sys(t));
359-
auto tm = *std::localtime(&sys_time);
360-
return system_strftime("%Y-%m-%d %H:%M:%S", &tm);
361-
}
362-
363-
TEST(chrono_test, local_system_clock_time_point) {
364-
# ifdef _WIN32
365-
return; // Not supported on Windows.
366-
# endif
367-
auto t1 = std::chrono::time_point_cast<std::chrono::seconds>(
368-
std::chrono::current_zone()->to_local(std::chrono::system_clock::now()));
369-
EXPECT_EQ(strftime_full_local(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
370-
EXPECT_EQ(strftime_full_local(t1), fmt::format("{}", t1));
371-
EXPECT_EQ(strftime_full_local(t1), fmt::format("{:}", t1));
372-
using time_point = fmt::local_time<std::chrono::seconds>;
373-
auto t2 = time_point(std::chrono::seconds(86400 + 42));
374-
EXPECT_EQ(strftime_full_local(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
353+
TEST(chrono_test, local_time) {
354+
auto t =
355+
fmt::local_time<std::chrono::seconds>(std::chrono::seconds(290088000));
356+
EXPECT_EQ(fmt::format("{:%Y-%m-%d %H:%M:%S}", t), "1979-03-12 12:00:00");
357+
EXPECT_EQ(fmt::format("{}", t), "1979-03-12 12:00:00");
358+
EXPECT_EQ(fmt::format("{:}", t), "1979-03-12 12:00:00");
375359

376-
std::vector<std::string> spec_list = {
360+
std::vector<std::string> specs = {
377361
"%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C",
378362
"%EC", "%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U",
379363
"%OU", "%W", "%OW", "%V", "%OV", "%j", "%d", "%Od", "%e",
380364
"%Oe", "%a", "%A", "%w", "%Ow", "%u", "%Ou", "%H", "%OH",
381365
"%I", "%OI", "%M", "%OM", "%S", "%OS", "%x", "%Ex", "%X",
382366
"%EX", "%D", "%F", "%R", "%T", "%p", "%z", "%Z"};
383-
# ifndef _WIN32
367+
#ifndef _WIN32
384368
// Disabled on Windows because these formats are not consistent among
385369
// platforms.
386-
spec_list.insert(spec_list.end(), {"%c", "%Ec", "%r"});
387-
# elif !FMT_HAS_C99_STRFTIME
370+
specs.insert(specs.end(), {"%c", "%Ec", "%r"});
371+
#elif !FMT_HAS_C99_STRFTIME
388372
// Only C89 conversion specifiers when using MSVCRT instead of UCRT
389-
spec_list = {"%%", "%Y", "%y", "%b", "%B", "%m", "%U", "%W", "%j", "%d", "%a",
390-
"%A", "%w", "%H", "%I", "%M", "%S", "%x", "%X", "%p", "%Z"};
373+
specs = {"%%", "%Y", "%y", "%b", "%B", "%m", "%U", "%W", "%j", "%d", "%a",
374+
"%A", "%w", "%H", "%I", "%M", "%S", "%x", "%X", "%p", "%Z"};
375+
#endif
376+
specs.push_back("%Y-%m-%d %H:%M:%S");
377+
378+
#if FMT_USE_LOCAL_TIME
379+
# ifdef _WIN32
380+
return; // Not supported on Windows.
391381
# endif
392-
spec_list.push_back("%Y-%m-%d %H:%M:%S");
393382

394-
for (const auto& spec : spec_list) {
395-
auto t = std::chrono::system_clock::to_time_t(
396-
std::chrono::current_zone()->to_sys(t1));
397-
auto tm = *std::localtime(&t);
383+
for (const auto& spec : specs) {
384+
auto sys_time = std::chrono::system_clock::to_time_t(
385+
std::chrono::current_zone()->to_sys(t));
386+
auto tm = *std::localtime(&sys_time);
398387

399388
auto sys_output = system_strftime(spec, &tm);
400389

401390
auto fmt_spec = fmt::format("{{:{}}}", spec);
402-
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1));
391+
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t));
403392
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
404393
}
405394

406-
if (std::find(spec_list.cbegin(), spec_list.cend(), "%z") !=
407-
spec_list.cend()) {
408-
auto t = std::chrono::system_clock::to_time_t(
409-
std::chrono::current_zone()->to_sys(t1));
410-
auto tm = *std::localtime(&t);
395+
if (std::find(specs.cbegin(), specs.cend(), "%z") != specs.cend()) {
396+
auto sys_time = std::chrono::system_clock::to_time_t(
397+
std::chrono::current_zone()->to_sys(t));
398+
auto tm = *std::localtime(&sys_time);
411399

412400
auto sys_output = system_strftime("%z", &tm);
413401
sys_output.insert(sys_output.end() - 2, 1, ':');
414402

415-
EXPECT_EQ(sys_output, fmt::format("{:%Ez}", t1));
403+
EXPECT_EQ(sys_output, fmt::format("{:%Ez}", t));
416404
EXPECT_EQ(sys_output, fmt::format("{:%Ez}", tm));
417405

418-
EXPECT_EQ(sys_output, fmt::format("{:%Oz}", t1));
406+
EXPECT_EQ(sys_output, fmt::format("{:%Oz}", t));
419407
EXPECT_EQ(sys_output, fmt::format("{:%Oz}", tm));
420408
}
421-
}
422-
423409
#endif // FMT_USE_LOCAL_TIME
410+
}
424411

425412
TEST(chrono_test, daylight_savings_time_end) {
426413
// 2024-10-27 03:05 as the number of seconds since epoch in Europe/Kyiv time.

0 commit comments

Comments
 (0)