Make chrono formatting locale-independent by default
This commit is contained in:
parent
50fb0b5eae
commit
d0abe7c246
@ -863,6 +863,7 @@ struct chrono_formatter {
|
|||||||
FormatContext& context;
|
FormatContext& context;
|
||||||
OutputIt out;
|
OutputIt out;
|
||||||
int precision;
|
int precision;
|
||||||
|
bool localized = false;
|
||||||
// rep is unsigned to avoid overflow.
|
// rep is unsigned to avoid overflow.
|
||||||
using rep =
|
using rep =
|
||||||
conditional_t<std::is_integral<Rep>::value && sizeof(Rep) < sizeof(int),
|
conditional_t<std::is_integral<Rep>::value && sizeof(Rep) < sizeof(int),
|
||||||
@ -957,7 +958,8 @@ struct chrono_formatter {
|
|||||||
|
|
||||||
void format_localized(const tm& time, char format, char modifier = 0) {
|
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 = localized ? context.locale().template get<std::locale>()
|
||||||
|
: std::locale::classic();
|
||||||
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);
|
||||||
@ -1086,6 +1088,7 @@ struct formatter<std::chrono::duration<Rep, Period>, Char> {
|
|||||||
using arg_ref_type = detail::arg_ref<Char>;
|
using arg_ref_type = detail::arg_ref<Char>;
|
||||||
arg_ref_type width_ref;
|
arg_ref_type width_ref;
|
||||||
arg_ref_type precision_ref;
|
arg_ref_type precision_ref;
|
||||||
|
bool localized = false;
|
||||||
basic_string_view<Char> format_str;
|
basic_string_view<Char> format_str;
|
||||||
using duration = std::chrono::duration<Rep, Period>;
|
using duration = std::chrono::duration<Rep, Period>;
|
||||||
|
|
||||||
@ -1148,6 +1151,10 @@ struct formatter<std::chrono::duration<Rep, Period>, Char> {
|
|||||||
else
|
else
|
||||||
handler.on_error("precision not allowed for this argument type");
|
handler.on_error("precision not allowed for this argument type");
|
||||||
}
|
}
|
||||||
|
if (begin != end && *begin == 'L') {
|
||||||
|
++begin;
|
||||||
|
localized = true;
|
||||||
|
}
|
||||||
end = parse_chrono_format(begin, end, detail::chrono_format_checker());
|
end = parse_chrono_format(begin, end, detail::chrono_format_checker());
|
||||||
return {begin, end};
|
return {begin, end};
|
||||||
}
|
}
|
||||||
@ -1182,6 +1189,7 @@ struct formatter<std::chrono::duration<Rep, Period>, Char> {
|
|||||||
detail::chrono_formatter<FormatContext, decltype(out), Rep, Period> f(
|
detail::chrono_formatter<FormatContext, decltype(out), Rep, Period> f(
|
||||||
ctx, out, d);
|
ctx, out, d);
|
||||||
f.precision = precision_copy;
|
f.precision = precision_copy;
|
||||||
|
f.localized = localized;
|
||||||
parse_chrono_format(begin, end, f);
|
parse_chrono_format(begin, end, f);
|
||||||
}
|
}
|
||||||
return detail::write(
|
return detail::write(
|
||||||
|
@ -232,7 +232,7 @@ auto format_tm(const std::tm& time, fmt::string_view spec,
|
|||||||
{ \
|
{ \
|
||||||
auto jp_loc = std::locale("ja_JP.utf8"); \
|
auto jp_loc = std::locale("ja_JP.utf8"); \
|
||||||
EXPECT_EQ(format_tm(time, spec, jp_loc), \
|
EXPECT_EQ(format_tm(time, spec, jp_loc), \
|
||||||
fmt::format(loc, "{:" spec "}", duration)); \
|
fmt::format(jp_loc, "{:L" spec "}", duration)); \
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(chrono_test, locale) {
|
TEST(chrono_test, locale) {
|
||||||
|
Loading…
Reference in New Issue
Block a user