Make locale-independent and C locale formats consistent among platforms

This commit is contained in:
Vladislav Shchapov 2021-10-30 19:32:21 +05:00
parent 91c454b7ff
commit a9df99f385
3 changed files with 24 additions and 23 deletions

View File

@ -1590,11 +1590,6 @@ template <typename OutputIt, typename Char> class tm_writer {
void on_datetime(numeric_system ns) { void on_datetime(numeric_system ns) {
if (is_classic_) { if (is_classic_) {
#ifdef _WIN32
on_us_date();
*out_++ = ' ';
on_iso_time();
#else
on_abbr_weekday(); on_abbr_weekday();
*out_++ = ' '; *out_++ = ' ';
on_abbr_month(); on_abbr_month();
@ -1604,7 +1599,6 @@ template <typename OutputIt, typename Char> class tm_writer {
on_iso_time(); on_iso_time();
*out_++ = ' '; *out_++ = ' ';
on_year(numeric_system::standard); on_year(numeric_system::standard);
#endif
} else { } else {
format_localized('c', ns == numeric_system::standard ? '\0' : 'E'); format_localized('c', ns == numeric_system::standard ? '\0' : 'E');
} }
@ -1735,16 +1729,12 @@ template <typename OutputIt, typename Char> class tm_writer {
void on_12_hour_time() { void on_12_hour_time() {
if (is_classic_) { if (is_classic_) {
#ifdef _WIN32
on_iso_time();
#else
char buf[8]; char buf[8];
write_digit2_separated(buf, to_unsigned(tm_hour12()), write_digit2_separated(buf, to_unsigned(tm_hour12()),
to_unsigned(tm_min()), to_unsigned(tm_sec()), ':'); to_unsigned(tm_min()), to_unsigned(tm_sec()), ':');
out_ = copy_str<Char>(std::begin(buf), std::end(buf), out_); out_ = copy_str<Char>(std::begin(buf), std::end(buf), out_);
*out_++ = ' '; *out_++ = ' ';
on_am_pm(); on_am_pm();
#endif
} else { } else {
format_localized('r'); format_localized('r');
} }

View File

@ -248,13 +248,19 @@ TEST(chrono_test, time_point) {
EXPECT_EQ(strftime_full(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2)); EXPECT_EQ(strftime_full(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
std::vector<std::string> spec_list = { std::vector<std::string> spec_list = {
"%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C", "%EC", "%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C",
"%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U", "%OU", "%W", "%EC", "%G", "%g", "%b", "%h", "%B", "%m", "%Om", "%U",
"%OW", "%V", "%OV", "%j", "%d", "%Od", "%e", "%Oe", "%a", "%A", "%OU", "%W", "%OW", "%V", "%OV", "%j", "%d", "%Od", "%e",
"%w", "%Ow", "%u", "%Ou", "%H", "%OH", "%I", "%OI", "%M", "%OM", "%Oe", "%a", "%A", "%w", "%Ow", "%u", "%Ou", "%H", "%OH",
"%S", "%OS", "%c", "%Ec", "%x", "%Ex", "%X", "%EX", "%D", "%F", "%I", "%OI", "%M", "%OM", "%S", "%OS", "%x", "%Ex", "%X",
"%r", "%R", "%T", "%p", "%z", "%Z"}; "%EX", "%D", "%F", "%R", "%T", "%p", "%z", "%Z"};
spec_list.push_back("%Y-%m-%d %H:%M:%S"); 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) { for (const auto& spec : spec_list) {
auto t = std::chrono::system_clock::to_time_t(t1); auto t = std::chrono::system_clock::to_time_t(t1);
auto tm = *std::localtime(&t); auto tm = *std::localtime(&t);

View File

@ -284,14 +284,19 @@ TEST(chrono_test, time_point) {
auto t1 = std::chrono::system_clock::now(); auto t1 = std::chrono::system_clock::now();
std::vector<std::wstring> spec_list = { std::vector<std::wstring> spec_list = {
L"%%", L"%n", L"%t", L"%Y", L"%EY", L"%y", L"%Oy", L"%Ey", L"%%", L"%n", L"%t", L"%Y", L"%EY", L"%y", L"%Oy", L"%Ey", L"%C",
L"%C", L"%EC", L"%G", L"%g", L"%b", L"%h", L"%B", L"%m", L"%EC", L"%G", L"%g", L"%b", L"%h", L"%B", L"%m", L"%Om", L"%U",
L"%Om", L"%U", L"%OU", L"%W", L"%OW", L"%V", L"%OV", L"%j", L"%OU", L"%W", L"%OW", L"%V", L"%OV", L"%j", L"%d", L"%Od", L"%e",
L"%d", L"%Od", L"%e", L"%Oe", L"%a", L"%A", L"%w", L"%Ow", L"%Oe", L"%a", L"%A", L"%w", L"%Ow", L"%u", L"%Ou", L"%H", L"%OH",
L"%u", L"%Ou", L"%H", L"%OH", L"%I", L"%OI", L"%M", L"%OM", L"%I", L"%OI", L"%M", L"%OM", L"%S", L"%OS", L"%x", L"%Ex", L"%X",
L"%S", L"%OS", L"%c", L"%Ec", L"%x", L"%Ex", L"%X", L"%EX", L"%EX", L"%D", L"%F", L"%R", L"%T", L"%p", L"%z", L"%Z"};
L"%D", L"%F", L"%r", L"%R", L"%T", L"%p", L"%z", L"%Z"};
spec_list.push_back(L"%Y-%m-%d %H:%M:%S"); 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) { for (const auto& spec : spec_list) {
auto t = std::chrono::system_clock::to_time_t(t1); auto t = std::chrono::system_clock::to_time_t(t1);
auto tm = *std::localtime(&t); auto tm = *std::localtime(&t);