@@ -2036,17 +2036,52 @@ struct chrono_formatter {
2036
2036
2037
2037
#if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907
2038
2038
using weekday = std::chrono::weekday;
2039
+ using day = std::chrono::day;
2040
+ using month = std::chrono::month;
2041
+ using year = std::chrono::year;
2039
2042
#else
2040
2043
// A fallback version of weekday.
2041
2044
class weekday {
2042
2045
private:
2043
- unsigned char value ;
2046
+ unsigned char value_ ;
2044
2047
2045
2048
public:
2046
2049
weekday () = default ;
2047
- explicit constexpr weekday (unsigned wd) noexcept
2048
- : value(static_cast <unsigned char >(wd != 7 ? wd : 0 )) {}
2049
- constexpr auto c_encoding () const noexcept -> unsigned { return value; }
2050
+ constexpr explicit weekday (unsigned wd) noexcept
2051
+ : value_(static_cast <unsigned char >(wd != 7 ? wd : 0 )) {}
2052
+ constexpr auto c_encoding () const noexcept -> unsigned { return value_; }
2053
+ };
2054
+
2055
+ class day {
2056
+ private:
2057
+ unsigned char value_;
2058
+
2059
+ public:
2060
+ day () = default ;
2061
+ constexpr explicit day (unsigned d) noexcept
2062
+ : value_(static_cast <unsigned char >(d)) {}
2063
+ constexpr explicit operator unsigned () const noexcept { return value_; }
2064
+ };
2065
+
2066
+ class month {
2067
+ private:
2068
+ unsigned char value_;
2069
+
2070
+ public:
2071
+ month () = default ;
2072
+ constexpr explicit month (unsigned m) noexcept
2073
+ : value_(static_cast <unsigned char >(m)) {}
2074
+ constexpr explicit operator unsigned () const noexcept { return value_; }
2075
+ };
2076
+
2077
+ class year {
2078
+ private:
2079
+ int value_;
2080
+
2081
+ public:
2082
+ year () = default ;
2083
+ constexpr explicit year (int y) noexcept : value_(y) {}
2084
+ constexpr explicit operator int () const noexcept { return value_; }
2050
2085
};
2051
2086
2052
2087
class year_month_day {};
@@ -2079,6 +2114,68 @@ template <typename Char> struct formatter<weekday, Char> {
2079
2114
}
2080
2115
};
2081
2116
2117
+ template <typename Char> struct formatter <day, Char> {
2118
+ FMT_CONSTEXPR auto parse (basic_format_parse_context<Char>& ctx)
2119
+ -> decltype(ctx.begin()) {
2120
+ return ctx.begin ();
2121
+ }
2122
+
2123
+ template <typename FormatContext>
2124
+ auto format (day d, FormatContext& ctx) const -> decltype(ctx.out()) {
2125
+ auto time = std::tm ();
2126
+ time .tm_mday = static_cast <int >(static_cast <unsigned >(d));
2127
+ detail::get_locale loc (false , ctx.locale ());
2128
+ auto w = detail::tm_writer<decltype (ctx.out ()), Char>(loc, ctx.out (), time );
2129
+ w.on_day_of_month (detail::numeric_system::standard);
2130
+ return w.out ();
2131
+ }
2132
+ };
2133
+
2134
+ template <typename Char> struct formatter <month, Char> {
2135
+ private:
2136
+ bool localized = false ;
2137
+
2138
+ public:
2139
+ FMT_CONSTEXPR auto parse (basic_format_parse_context<Char>& ctx)
2140
+ -> decltype(ctx.begin()) {
2141
+ auto begin = ctx.begin (), end = ctx.end ();
2142
+ if (begin != end && *begin == ' L' ) {
2143
+ ++begin;
2144
+ localized = true ;
2145
+ }
2146
+ return begin;
2147
+ }
2148
+
2149
+ template <typename FormatContext>
2150
+ auto format (month m, FormatContext& ctx) const -> decltype(ctx.out()) {
2151
+ auto time = std::tm ();
2152
+ // std::chrono::month has a range of 1-12, std::tm requires 0-11
2153
+ time .tm_mon = static_cast <int >(static_cast <unsigned >(m)) - 1 ;
2154
+ detail::get_locale loc (localized, ctx.locale ());
2155
+ auto w = detail::tm_writer<decltype (ctx.out ()), Char>(loc, ctx.out (), time );
2156
+ w.on_abbr_month ();
2157
+ return w.out ();
2158
+ }
2159
+ };
2160
+
2161
+ template <typename Char> struct formatter <year, Char> {
2162
+ FMT_CONSTEXPR auto parse (basic_format_parse_context<Char>& ctx)
2163
+ -> decltype(ctx.begin()) {
2164
+ return ctx.begin ();
2165
+ }
2166
+
2167
+ template <typename FormatContext>
2168
+ auto format (year y, FormatContext& ctx) const -> decltype(ctx.out()) {
2169
+ auto time = std::tm ();
2170
+ // std::tm::tm_year is years since 1900
2171
+ time .tm_year = static_cast <int >(y) - 1900 ;
2172
+ detail::get_locale loc (true , ctx.locale ());
2173
+ auto w = detail::tm_writer<decltype (ctx.out ()), Char>(loc, ctx.out (), time );
2174
+ w.on_year (detail::numeric_system::standard);
2175
+ return w.out ();
2176
+ }
2177
+ };
2178
+
2082
2179
template <typename Rep, typename Period, typename Char>
2083
2180
struct formatter <std::chrono::duration<Rep, Period>, Char> {
2084
2181
private:
0 commit comments