mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 02:37:36 +02:00
Improve chrono formatting
This commit is contained in:
@ -2206,7 +2206,8 @@ template <typename Char> struct formatter<std::tm, Char> {
|
|||||||
detail::arg_ref<Char> width_ref_;
|
detail::arg_ref<Char> width_ref_;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
basic_string_view<Char> fmt_;
|
basic_string_view<Char> fmt_ =
|
||||||
|
detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>();
|
||||||
|
|
||||||
template <typename Duration, typename FormatContext>
|
template <typename Duration, typename FormatContext>
|
||||||
auto do_format(const std::tm& tm, FormatContext& ctx,
|
auto do_format(const std::tm& tm, FormatContext& ctx,
|
||||||
@ -2261,10 +2262,6 @@ template <typename Char> struct formatter<std::tm, Char> {
|
|||||||
|
|
||||||
template <typename Char, typename Duration>
|
template <typename Char, typename Duration>
|
||||||
struct formatter<sys_time<Duration>, Char> : formatter<std::tm, Char> {
|
struct formatter<sys_time<Duration>, Char> : formatter<std::tm, Char> {
|
||||||
FMT_CONSTEXPR formatter() {
|
|
||||||
this->fmt_ = detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(sys_time<Duration> val, FormatContext& ctx) const
|
auto format(sys_time<Duration> val, FormatContext& ctx) const
|
||||||
-> decltype(ctx.out()) {
|
-> decltype(ctx.out()) {
|
||||||
@ -2303,10 +2300,6 @@ struct formatter<utc_time<Duration>, Char>
|
|||||||
|
|
||||||
template <typename Duration, typename Char>
|
template <typename Duration, typename Char>
|
||||||
struct formatter<local_time<Duration>, Char> : formatter<std::tm, Char> {
|
struct formatter<local_time<Duration>, Char> : formatter<std::tm, Char> {
|
||||||
FMT_CONSTEXPR formatter() {
|
|
||||||
this->fmt_ = detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>();
|
|
||||||
}
|
|
||||||
|
|
||||||
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
||||||
return this->do_parse(ctx, true);
|
return this->do_parse(ctx, true);
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
using fmt::runtime;
|
using fmt::runtime;
|
||||||
using fmt::sys_time;
|
using fmt::sys_time;
|
||||||
|
using fmt::sys_time;
|
||||||
using testing::Contains;
|
using testing::Contains;
|
||||||
|
|
||||||
#if defined(__MINGW32__) && !defined(_UCRT)
|
#if defined(__MINGW32__) && !defined(_UCRT)
|
||||||
@ -238,8 +239,6 @@ TEST(chrono_test, format_to_empty_container) {
|
|||||||
EXPECT_EQ(s, "42");
|
EXPECT_EQ(s, "42");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(chrono_test, empty_result) { EXPECT_EQ(fmt::format("{}", std::tm()), ""); }
|
|
||||||
|
|
||||||
TEST(chrono_test, gmtime) {
|
TEST(chrono_test, gmtime) {
|
||||||
auto t = std::time(nullptr);
|
auto t = std::time(nullptr);
|
||||||
auto expected = *std::gmtime(&t);
|
auto expected = *std::gmtime(&t);
|
||||||
@ -337,6 +336,11 @@ TEST(chrono_test, local_time) {
|
|||||||
fmt::format_error, "no timezone");
|
fmt::format_error, "no timezone");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(chrono_test, tm) {
|
||||||
|
auto time = fmt::gmtime(290088000);
|
||||||
|
test_time(time);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(chrono_test, daylight_savings_time_end) {
|
TEST(chrono_test, daylight_savings_time_end) {
|
||||||
// 2024-10-27 03:05 as the number of seconds since epoch in Europe/Kyiv time.
|
// 2024-10-27 03:05 as the number of seconds since epoch in Europe/Kyiv time.
|
||||||
// It is slightly after the DST end and passing it to to_sys will result in
|
// It is slightly after the DST end and passing it to to_sys will result in
|
||||||
|
Reference in New Issue
Block a user