Use different overload of std::time_put<T>::put to avoid needing a format string

This commit is contained in:
zeffy 2020-01-23 11:31:31 -08:00
parent 29e93af164
commit 8f2917b59c

View File

@ -894,13 +894,13 @@ struct chrono_formatter {
void write_pinf() { std::copy_n("inf", 3, out); } void write_pinf() { std::copy_n("inf", 3, out); }
void write_ninf() { std::copy_n("-inf", 4, out); } void write_ninf() { std::copy_n("-inf", 4, out); }
void format_localized(const tm& time, const char* format) { void format_localized(const tm& time, char format, char modifier = 0) {
if (isnan(val)) return write_nan(); if (isnan(val)) return write_nan();
auto locale = context.locale().template get<std::locale>(); auto locale = context.locale().template get<std::locale>();
auto& facet = std::use_facet<std::time_put<char_type>>(locale); auto& facet = std::use_facet<std::time_put<char_type>>(locale);
std::basic_ostringstream<char_type> os; std::basic_ostringstream<char_type> os;
os.imbue(locale); os.imbue(locale);
facet.put(os, os, ' ', &time, format, format + std::char_traits<char_type>::length(format)); facet.put(os, os, ' ', &time, format, modifier);
auto str = os.str(); auto str = os.str();
std::copy(str.begin(), str.end(), out); std::copy(str.begin(), str.end(), out);
} }
@ -930,8 +930,7 @@ struct chrono_formatter {
if (ns == numeric_system::standard) return write(hour(), 2); if (ns == numeric_system::standard) return write(hour(), 2);
auto time = tm(); auto time = tm();
time.tm_hour = to_nonnegative_int(hour(), 24); time.tm_hour = to_nonnegative_int(hour(), 24);
const char_type format[] { '%', 'O', 'H', 0 }; format_localized(time, 'H', 'O');
format_localized(time, format);
} }
void on_12_hour(numeric_system ns) { void on_12_hour(numeric_system ns) {
@ -940,8 +939,7 @@ struct chrono_formatter {
if (ns == numeric_system::standard) return write(hour12(), 2); if (ns == numeric_system::standard) return write(hour12(), 2);
auto time = tm(); auto time = tm();
time.tm_hour = to_nonnegative_int(hour12(), 12); time.tm_hour = to_nonnegative_int(hour12(), 12);
const char_type format[] { '%', 'O', 'I', 0 }; format_localized(time, 'I', 'O');
format_localized(time, format);
} }
void on_minute(numeric_system ns) { void on_minute(numeric_system ns) {
@ -950,8 +948,7 @@ struct chrono_formatter {
if (ns == numeric_system::standard) return write(minute(), 2); if (ns == numeric_system::standard) return write(minute(), 2);
auto time = tm(); auto time = tm();
time.tm_min = to_nonnegative_int(minute(), 60); time.tm_min = to_nonnegative_int(minute(), 60);
const char_type format[] { '%', 'O', 'M', 0 }; format_localized(time, 'M', 'O');
format_localized(time, format);
} }
void on_second(numeric_system ns) { void on_second(numeric_system ns) {
@ -976,14 +973,12 @@ struct chrono_formatter {
} }
auto time = tm(); auto time = tm();
time.tm_sec = to_nonnegative_int(second(), 60); time.tm_sec = to_nonnegative_int(second(), 60);
const char_type format[] { '%', 'O', 'S', 0 }; format_localized(time, 'S', 'O');
format_localized(time, format);
} }
void on_12_hour_time() { void on_12_hour_time() {
if (handle_nan_inf()) return; if (handle_nan_inf()) return;
const char_type format[] { '%', 'r', 0 }; format_localized(time(), 'r');
format_localized(time(), format);
} }
void on_24_hour_time() { void on_24_hour_time() {
@ -1007,8 +1002,7 @@ struct chrono_formatter {
void on_am_pm() { void on_am_pm() {
if (handle_nan_inf()) return; if (handle_nan_inf()) return;
const char_type format[] { '%', 'p', 0 }; format_localized(time(), 'p');
format_localized(time(), format);
} }
void on_duration_value() { void on_duration_value() {