Skip to content

Commit

Permalink
Make locale-independent and C locale formats consistent among platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed Oct 30, 2021
1 parent 91c454b commit cc20c47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
10 changes: 0 additions & 10 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -1590,11 +1590,6 @@ template <typename OutputIt, typename Char> class tm_writer {

void on_datetime(numeric_system ns) {
if (is_classic_) {
#ifdef _WIN32
on_us_date();
*out_++ = ' ';
on_iso_time();
#else
on_abbr_weekday();
*out_++ = ' ';
on_abbr_month();
Expand All @@ -1604,7 +1599,6 @@ template <typename OutputIt, typename Char> class tm_writer {
on_iso_time();
*out_++ = ' ';
on_year(numeric_system::standard);
#endif
} else {
format_localized('c', ns == numeric_system::standard ? '\0' : 'E');
}
Expand Down Expand Up @@ -1735,16 +1729,12 @@ template <typename OutputIt, typename Char> class tm_writer {

void on_12_hour_time() {
if (is_classic_) {
#ifdef _WIN32
on_iso_time();
#else
char buf[8];
write_digit2_separated(buf, to_unsigned(tm_hour12()),
to_unsigned(tm_min()), to_unsigned(tm_sec()), ':');
out_ = copy_str<Char>(std::begin(buf), std::end(buf), out_);
*out_++ = ' ';
on_am_pm();
#endif
} else {
format_localized('r');
}
Expand Down
17 changes: 11 additions & 6 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,18 @@ TEST(chrono_test, time_point) {
EXPECT_EQ(strftime_full(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));

std::vector<std::string> spec_list = {
"%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C", "%EC",
"%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U", "%OU", "%W",
"%OW", "%V", "%OV", "%j", "%d", "%Od", "%e", "%Oe", "%a", "%A",
"%w", "%Ow", "%u", "%Ou", "%H", "%OH", "%I", "%OI", "%M", "%OM",
"%S", "%OS", "%c", "%Ec", "%x", "%Ex", "%X", "%EX", "%D", "%F",
"%r", "%R", "%T", "%p", "%z", "%Z"};
"%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C",
"%EC", "%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U",
"%OU", "%W", "%OW", "%V", "%OV", "%j", "%d", "%Od", "%e",
"%Oe", "%a", "%A", "%w", "%Ow", "%u", "%Ou", "%H", "%OH",
"%I", "%OI", "%M", "%OM", "%S", "%OS", "%x", "%Ex", "%X",
"%EX", "%D", "%F", "%R", "%T", "%p", "%z", "%Z"};
spec_list.push_back("%Y-%m-%d %H:%M:%S");
#ifndef _WIN32
// Disabled on Windows, because this formats is not consistent among platforms
spec_list.insert(spec_list.end(), {"%c", "%Ec", "%r"});
#endif

for (const auto& spec : spec_list) {
auto t = std::chrono::system_clock::to_time_t(t1);
auto tm = *std::localtime(&t);
Expand Down

0 comments on commit cc20c47

Please sign in to comment.