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 a9df99f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 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
18 changes: 12 additions & 6 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,19 @@ 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 these 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
19 changes: 12 additions & 7 deletions test/xchar-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,19 @@ TEST(chrono_test, time_point) {
auto t1 = std::chrono::system_clock::now();

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

Please sign in to comment.