| 1 | // <chrono> Formatting -*- C++ -*- |
| 2 | |
| 3 | // Copyright The GNU Toolchain Authors. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file include/bits/chrono_io.h |
| 26 | * This is an internal header file, included by other library headers. |
| 27 | * Do not attempt to use it directly. @headername{chrono} |
| 28 | */ |
| 29 | |
| 30 | #ifndef _GLIBCXX_CHRONO_IO_H |
| 31 | #define _GLIBCXX_CHRONO_IO_H 1 |
| 32 | |
| 33 | #ifdef _GLIBCXX_SYSHDR |
| 34 | #pragma GCC system_header |
| 35 | #endif |
| 36 | |
| 37 | #if __cplusplus >= 202002L |
| 38 | |
| 39 | #include <sstream> // ostringstream |
| 40 | #include <iomanip> // setw, setfill |
| 41 | #include <format> |
| 42 | #include <charconv> // from_chars |
| 43 | #include <stdexcept> // __sso_string |
| 44 | |
| 45 | #include <bits/streambuf_iterator.h> |
| 46 | #include <bits/unique_ptr.h> |
| 47 | |
| 48 | namespace std _GLIBCXX_VISIBILITY(default) |
| 49 | { |
| 50 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 51 | |
| 52 | namespace chrono |
| 53 | { |
| 54 | /// @addtogroup chrono |
| 55 | /// @{ |
| 56 | |
| 57 | /// @cond undocumented |
| 58 | namespace __detail |
| 59 | { |
| 60 | #define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S) |
| 61 | #define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S) |
| 62 | |
| 63 | template<typename _Period, typename _CharT> |
| 64 | constexpr basic_string_view<_CharT> |
| 65 | __units_suffix() noexcept |
| 66 | { |
| 67 | // The standard say these are all narrow strings, which would need to |
| 68 | // be widened at run-time when inserted into a wide stream. We use |
| 69 | // STATICALLY-WIDEN to widen at compile-time. |
| 70 | #define _GLIBCXX_UNITS_SUFFIX(period, suffix) \ |
| 71 | if constexpr (is_same_v<_Period, period>) \ |
| 72 | return _GLIBCXX_WIDEN(suffix); \ |
| 73 | else |
| 74 | |
| 75 | _GLIBCXX_UNITS_SUFFIX(atto, "as" ) |
| 76 | _GLIBCXX_UNITS_SUFFIX(femto, "fs" ) |
| 77 | _GLIBCXX_UNITS_SUFFIX(pico, "ps" ) |
| 78 | _GLIBCXX_UNITS_SUFFIX(nano, "ns" ) |
| 79 | _GLIBCXX_UNITS_SUFFIX(milli, "ms" ) |
| 80 | #if _GLIBCXX_USE_ALT_MICROSECONDS_SUFFIX |
| 81 | // Deciding this at compile-time is wrong, maybe use nl_langinfo(CODESET) |
| 82 | // to check runtime environment and return u8"\u00b5s", "\xb5s", or "us". |
| 83 | _GLIBCXX_UNITS_SUFFIX(micro, "\u00b5s" ) |
| 84 | #else |
| 85 | _GLIBCXX_UNITS_SUFFIX(micro, "us" ) |
| 86 | #endif |
| 87 | _GLIBCXX_UNITS_SUFFIX(centi, "cs" ) |
| 88 | _GLIBCXX_UNITS_SUFFIX(deci, "ds" ) |
| 89 | _GLIBCXX_UNITS_SUFFIX(ratio<1>, "s" ) |
| 90 | _GLIBCXX_UNITS_SUFFIX(deca, "das" ) |
| 91 | _GLIBCXX_UNITS_SUFFIX(hecto, "hs" ) |
| 92 | _GLIBCXX_UNITS_SUFFIX(kilo, "ks" ) |
| 93 | _GLIBCXX_UNITS_SUFFIX(mega, "Ms" ) |
| 94 | _GLIBCXX_UNITS_SUFFIX(giga, "Gs" ) |
| 95 | _GLIBCXX_UNITS_SUFFIX(tera, "Ts" ) |
| 96 | _GLIBCXX_UNITS_SUFFIX(tera, "Ts" ) |
| 97 | _GLIBCXX_UNITS_SUFFIX(peta, "Ps" ) |
| 98 | _GLIBCXX_UNITS_SUFFIX(exa, "Es" ) |
| 99 | _GLIBCXX_UNITS_SUFFIX(ratio<60>, "min" ) |
| 100 | _GLIBCXX_UNITS_SUFFIX(ratio<3600>, "h" ) |
| 101 | _GLIBCXX_UNITS_SUFFIX(ratio<86400>, "d" ) |
| 102 | #undef _GLIBCXX_UNITS_SUFFIX |
| 103 | return {}; |
| 104 | } |
| 105 | |
| 106 | template<typename _Period, typename _CharT, typename _Out> |
| 107 | inline _Out |
| 108 | __fmt_units_suffix(_Out __out) noexcept |
| 109 | { |
| 110 | if (auto __s = __detail::__units_suffix<_Period, _CharT>(); __s.size()) |
| 111 | return __format::__write(std::move(__out), __s); |
| 112 | else if constexpr (_Period::den == 1) |
| 113 | return std::format_to(std::move(__out), _GLIBCXX_WIDEN("[{}]s" ), |
| 114 | (uintmax_t)_Period::num); |
| 115 | else |
| 116 | return std::format_to(std::move(__out), _GLIBCXX_WIDEN("[{}/{}]s" ), |
| 117 | (uintmax_t)_Period::num, |
| 118 | (uintmax_t)_Period::den); |
| 119 | } |
| 120 | } // namespace __detail |
| 121 | /// @endcond |
| 122 | |
| 123 | /** Write a `chrono::duration` to an ostream. |
| 124 | * |
| 125 | * @since C++20 |
| 126 | */ |
| 127 | template<typename _CharT, typename _Traits, |
| 128 | typename _Rep, typename _Period> |
| 129 | inline basic_ostream<_CharT, _Traits>& |
| 130 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
| 131 | const duration<_Rep, _Period>& __d) |
| 132 | { |
| 133 | using _Out = ostreambuf_iterator<_CharT, _Traits>; |
| 134 | using period = typename _Period::type; |
| 135 | std::basic_ostringstream<_CharT, _Traits> __s; |
| 136 | __s.flags(__os.flags()); |
| 137 | __s.imbue(__os.getloc()); |
| 138 | __s.precision(__os.precision()); |
| 139 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 140 | // 4118. How should duration formatters format custom rep types? |
| 141 | __s << +__d.count(); |
| 142 | __detail::__fmt_units_suffix<period, _CharT>(_Out(__s)); |
| 143 | __os << std::move(__s).str(); |
| 144 | return __os; |
| 145 | } |
| 146 | |
| 147 | /// @cond undocumented |
| 148 | namespace __detail |
| 149 | { |
| 150 | // An unspecified type returned by `chrono::local_time_format`. |
| 151 | // This is called `local-time-format-t` in the standard. |
| 152 | template<typename _Duration> |
| 153 | struct __local_time_fmt |
| 154 | { |
| 155 | local_time<_Duration> _M_time; |
| 156 | const string* _M_abbrev; |
| 157 | const seconds* _M_offset_sec; |
| 158 | }; |
| 159 | |
| 160 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 161 | // 4124. Cannot format zoned_time with resolution coarser than seconds |
| 162 | template<typename _Duration> |
| 163 | using __local_time_fmt_for |
| 164 | = __local_time_fmt<common_type_t<_Duration, seconds>>; |
| 165 | } |
| 166 | /// @endcond |
| 167 | |
| 168 | /** Return an object that asssociates timezone info with a local time. |
| 169 | * |
| 170 | * A `chrono::local_time` object has no timezone associated with it. This |
| 171 | * function creates an object that allows formatting a `local_time` as |
| 172 | * though it refers to a timezone with the given abbreviated name and |
| 173 | * offset from UTC. |
| 174 | * |
| 175 | * @since C++20 |
| 176 | */ |
| 177 | template<typename _Duration> |
| 178 | inline __detail::__local_time_fmt<_Duration> |
| 179 | local_time_format(local_time<_Duration> __time, |
| 180 | const string* __abbrev = nullptr, |
| 181 | const seconds* __offset_sec = nullptr) |
| 182 | { return {__time, __abbrev, __offset_sec}; } |
| 183 | |
| 184 | /// @} |
| 185 | } // namespace chrono |
| 186 | |
| 187 | /// @cond undocumented |
| 188 | namespace __format |
| 189 | { |
| 190 | [[noreturn,__gnu__::__always_inline__]] |
| 191 | inline void |
| 192 | __no_timezone_available() |
| 193 | { __throw_format_error(what: "format error: no timezone available for %Z or %z" ); } |
| 194 | |
| 195 | [[noreturn,__gnu__::__always_inline__]] |
| 196 | inline void |
| 197 | __not_valid_for_duration() |
| 198 | { __throw_format_error(what: "format error: chrono-format-spec not valid for " |
| 199 | "chrono::duration" ); } |
| 200 | |
| 201 | [[noreturn,__gnu__::__always_inline__]] |
| 202 | inline void |
| 203 | __invalid_chrono_spec() |
| 204 | { __throw_format_error(what: "format error: chrono-format-spec not valid for " |
| 205 | "argument type" ); } |
| 206 | |
| 207 | template<typename _CharT> |
| 208 | struct _ChronoSpec : _Spec<_CharT> |
| 209 | { |
| 210 | basic_string_view<_CharT> _M_chrono_specs; |
| 211 | |
| 212 | // Use one of the reserved bits in __format::_Spec<C>. |
| 213 | // This indicates that a locale-dependent conversion specifier such as |
| 214 | // %a is used in the chrono-specs. This is not the same as the |
| 215 | // _Spec<C>::_M_localized member which indicates that "L" was present |
| 216 | // in the format-spec, e.g. "{:L%a}" is localized and locale-specific, |
| 217 | // but "{:L}" is only localized and "{:%a}" is only locale-specific. |
| 218 | constexpr bool |
| 219 | _M_locale_specific() const noexcept |
| 220 | { return this->_M_reserved; } |
| 221 | |
| 222 | constexpr void |
| 223 | _M_locale_specific(bool __b) noexcept |
| 224 | { this->_M_reserved = __b; } |
| 225 | }; |
| 226 | |
| 227 | // Represents the information provided by a chrono type. |
| 228 | // e.g. month_weekday has month and weekday but no year or time of day, |
| 229 | // hh_mm_ss has time of day but no date, sys_time is time_point+timezone. |
| 230 | enum _ChronoParts { |
| 231 | _Year = 1, _Month = 2, _Day = 4, _Weekday = 8, _TimeOfDay = 16, |
| 232 | _TimeZone = 32, |
| 233 | _Date = _Year | _Month | _Day | _Weekday, |
| 234 | _DateTime = _Date | _TimeOfDay, |
| 235 | _ZonedDateTime = _DateTime | _TimeZone, |
| 236 | _Duration = 128 // special case |
| 237 | }; |
| 238 | |
| 239 | constexpr _ChronoParts |
| 240 | operator|(_ChronoParts __x, _ChronoParts __y) noexcept |
| 241 | { return static_cast<_ChronoParts>((int)__x | (int)__y); } |
| 242 | |
| 243 | constexpr _ChronoParts& |
| 244 | operator|=(_ChronoParts& __x, _ChronoParts __y) noexcept |
| 245 | { return __x = __x | __y; } |
| 246 | |
| 247 | // TODO rename this to chrono::__formatter? or chrono::__detail::__formatter? |
| 248 | template<typename _CharT> |
| 249 | struct __formatter_chrono |
| 250 | { |
| 251 | using __string_view = basic_string_view<_CharT>; |
| 252 | using __string = basic_string<_CharT>; |
| 253 | |
| 254 | template<typename _ParseContext> |
| 255 | constexpr typename _ParseContext::iterator |
| 256 | _M_parse(_ParseContext& __pc, _ChronoParts __parts) |
| 257 | { |
| 258 | auto __first = __pc.begin(); |
| 259 | auto __last = __pc.end(); |
| 260 | |
| 261 | _ChronoSpec<_CharT> __spec{}; |
| 262 | |
| 263 | auto __finalize = [this, &__spec] { |
| 264 | _M_spec = __spec; |
| 265 | }; |
| 266 | |
| 267 | auto __finished = [&] { |
| 268 | if (__first == __last || *__first == '}') |
| 269 | { |
| 270 | __finalize(); |
| 271 | return true; |
| 272 | } |
| 273 | return false; |
| 274 | }; |
| 275 | |
| 276 | if (__finished()) |
| 277 | return __first; |
| 278 | |
| 279 | __first = __spec._M_parse_fill_and_align(__first, __last); |
| 280 | if (__finished()) |
| 281 | return __first; |
| 282 | |
| 283 | __first = __spec._M_parse_width(__first, __last, __pc); |
| 284 | if (__finished()) |
| 285 | return __first; |
| 286 | |
| 287 | if (__parts & _ChronoParts::_Duration) |
| 288 | { |
| 289 | __first = __spec._M_parse_precision(__first, __last, __pc); |
| 290 | if (__finished()) |
| 291 | return __first; |
| 292 | } |
| 293 | |
| 294 | __first = __spec._M_parse_locale(__first, __last); |
| 295 | if (__finished()) |
| 296 | return __first; |
| 297 | |
| 298 | // Everything up to the end of the string or the first '}' is a |
| 299 | // chrono-specs string. Check it is valid. |
| 300 | { |
| 301 | __string_view __str(__first, __last - __first); |
| 302 | auto __end = __str.find('}'); |
| 303 | if (__end != __str.npos) |
| 304 | { |
| 305 | __str.remove_suffix(__str.length() - __end); |
| 306 | __last = __first + __end; |
| 307 | } |
| 308 | if (__str.find('{') != __str.npos) |
| 309 | __throw_format_error(what: "chrono format error: '{' in chrono-specs" ); |
| 310 | } |
| 311 | |
| 312 | // Parse chrono-specs in [first,last), checking each conversion-spec |
| 313 | // against __parts (so fail for %Y if no year in parts). |
| 314 | // Save range in __spec._M_chrono_specs. |
| 315 | |
| 316 | const auto __chrono_specs = __first++; // Skip leading '%' |
| 317 | if (*__chrono_specs != '%') |
| 318 | __throw_format_error(what: "chrono format error: no '%' at start of " |
| 319 | "chrono-specs" ); |
| 320 | |
| 321 | _CharT __mod{}; |
| 322 | bool __conv = true; |
| 323 | int __needed = 0; |
| 324 | bool __locale_specific = false; |
| 325 | |
| 326 | while (__first != __last) |
| 327 | { |
| 328 | enum _Mods { _Mod_none, _Mod_E, _Mod_O, _Mod_E_O }; |
| 329 | _Mods __allowed_mods = _Mod_none; |
| 330 | |
| 331 | _CharT __c = *__first++; |
| 332 | switch (__c) |
| 333 | { |
| 334 | case 'a': |
| 335 | case 'A': |
| 336 | __needed = _Weekday; |
| 337 | __locale_specific = true; |
| 338 | break; |
| 339 | case 'b': |
| 340 | case 'h': |
| 341 | case 'B': |
| 342 | __needed = _Month; |
| 343 | __locale_specific = true; |
| 344 | break; |
| 345 | case 'c': |
| 346 | __needed = _DateTime; |
| 347 | __allowed_mods = _Mod_E; |
| 348 | __locale_specific = true; |
| 349 | break; |
| 350 | case 'C': |
| 351 | __needed = _Year; |
| 352 | __allowed_mods = _Mod_E; |
| 353 | break; |
| 354 | case 'd': |
| 355 | case 'e': |
| 356 | __needed = _Day; |
| 357 | __allowed_mods = _Mod_O; |
| 358 | break; |
| 359 | case 'D': |
| 360 | case 'F': |
| 361 | __needed = _Date; |
| 362 | break; |
| 363 | case 'g': |
| 364 | case 'G': |
| 365 | __needed = _Date; |
| 366 | break; |
| 367 | case 'H': |
| 368 | case 'I': |
| 369 | __needed = _TimeOfDay; |
| 370 | __allowed_mods = _Mod_O; |
| 371 | break; |
| 372 | case 'j': |
| 373 | if (!(__parts & _Duration)) |
| 374 | __needed = _Date; |
| 375 | break; |
| 376 | case 'm': |
| 377 | __needed = _Month; |
| 378 | __allowed_mods = _Mod_O; |
| 379 | break; |
| 380 | case 'M': |
| 381 | __needed = _TimeOfDay; |
| 382 | __allowed_mods = _Mod_O; |
| 383 | break; |
| 384 | case 'p': |
| 385 | case 'r': |
| 386 | __locale_specific = true; |
| 387 | [[fallthrough]]; |
| 388 | case 'R': |
| 389 | case 'T': |
| 390 | __needed = _TimeOfDay; |
| 391 | break; |
| 392 | case 'q': |
| 393 | case 'Q': |
| 394 | __needed = _Duration; |
| 395 | break; |
| 396 | case 'S': |
| 397 | __needed = _TimeOfDay; |
| 398 | __allowed_mods = _Mod_O; |
| 399 | break; |
| 400 | case 'u': |
| 401 | case 'w': |
| 402 | __needed = _Weekday; |
| 403 | __allowed_mods = _Mod_O; |
| 404 | break; |
| 405 | case 'U': |
| 406 | case 'V': |
| 407 | case 'W': |
| 408 | __needed = _Date; |
| 409 | __allowed_mods = _Mod_O; |
| 410 | break; |
| 411 | case 'x': |
| 412 | __needed = _Date; |
| 413 | __locale_specific = true; |
| 414 | __allowed_mods = _Mod_E; |
| 415 | break; |
| 416 | case 'X': |
| 417 | __needed = _TimeOfDay; |
| 418 | __locale_specific = true; |
| 419 | __allowed_mods = _Mod_E; |
| 420 | break; |
| 421 | case 'y': |
| 422 | __needed = _Year; |
| 423 | __allowed_mods = _Mod_E_O; |
| 424 | break; |
| 425 | case 'Y': |
| 426 | __needed = _Year; |
| 427 | __allowed_mods = _Mod_E; |
| 428 | break; |
| 429 | case 'z': |
| 430 | __needed = _TimeZone; |
| 431 | __allowed_mods = _Mod_E_O; |
| 432 | break; |
| 433 | case 'Z': |
| 434 | __needed = _TimeZone; |
| 435 | break; |
| 436 | case 'n': |
| 437 | case 't': |
| 438 | case '%': |
| 439 | break; |
| 440 | case 'O': |
| 441 | case 'E': |
| 442 | if (__mod) [[unlikely]] |
| 443 | { |
| 444 | __allowed_mods = _Mod_none; |
| 445 | break; |
| 446 | } |
| 447 | __mod = __c; |
| 448 | continue; |
| 449 | default: |
| 450 | __throw_format_error(what: "chrono format error: invalid " |
| 451 | " specifier in chrono-specs" ); |
| 452 | } |
| 453 | |
| 454 | if ((__mod == 'E' && !(__allowed_mods & _Mod_E)) |
| 455 | || (__mod == 'O' && !(__allowed_mods & _Mod_O))) |
| 456 | __throw_format_error(what: "chrono format error: invalid " |
| 457 | " modifier in chrono-specs" ); |
| 458 | if (__mod && __c != 'z') |
| 459 | __locale_specific = true; |
| 460 | __mod = _CharT(); |
| 461 | |
| 462 | if ((__parts & __needed) != __needed) |
| 463 | __throw_format_error(what: "chrono format error: format argument " |
| 464 | "does not contain the information " |
| 465 | "required by the chrono-specs" ); |
| 466 | |
| 467 | // Scan for next '%', ignoring literal-chars before it. |
| 468 | size_t __pos = __string_view(__first, __last - __first).find('%'); |
| 469 | if (__pos == 0) |
| 470 | ++__first; |
| 471 | else |
| 472 | { |
| 473 | if (__pos == __string_view::npos) |
| 474 | { |
| 475 | __first = __last; |
| 476 | __conv = false; |
| 477 | } |
| 478 | else |
| 479 | __first += __pos + 1; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // Check for a '%' conversion-spec without a type. |
| 484 | if (__conv || __mod != _CharT()) |
| 485 | __throw_format_error(what: "chrono format error: unescaped '%' in " |
| 486 | "chrono-specs" ); |
| 487 | |
| 488 | _M_spec = __spec; |
| 489 | _M_spec._M_chrono_specs |
| 490 | = __string_view(__chrono_specs, __first - __chrono_specs); |
| 491 | _M_spec._M_locale_specific(__locale_specific); |
| 492 | |
| 493 | return __first; |
| 494 | } |
| 495 | |
| 496 | // TODO this function template is instantiated for every different _Tp. |
| 497 | // Consider creating a polymorphic interface for calendar types so |
| 498 | // that we instantiate fewer different specializations. Similar to |
| 499 | // _Sink_iter for std::format. Replace each _S_year, _S_day etc. with |
| 500 | // member functions of that type. |
| 501 | template<typename _Tp, typename _FormatContext> |
| 502 | typename _FormatContext::iterator |
| 503 | _M_format(const _Tp& __t, _FormatContext& __fc, |
| 504 | bool __is_neg = false) const |
| 505 | { |
| 506 | auto __first = _M_spec._M_chrono_specs.begin(); |
| 507 | const auto __last = _M_spec._M_chrono_specs.end(); |
| 508 | if (__first == __last) |
| 509 | return _M_format_to_ostream(__t, __fc, __is_neg); |
| 510 | |
| 511 | #if defined _GLIBCXX_USE_NL_LANGINFO_L && __CHAR_BIT__ == 8 |
| 512 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 513 | // 3565. Handling of encodings in localized formatting |
| 514 | // of chrono types is underspecified |
| 515 | if constexpr (is_same_v<_CharT, char>) |
| 516 | if constexpr (__unicode::__literal_encoding_is_utf8()) |
| 517 | if (_M_spec._M_localized && _M_spec._M_locale_specific()) |
| 518 | { |
| 519 | extern locale __with_encoding_conversion(const locale&); |
| 520 | |
| 521 | // Allocate and cache the necessary state to convert strings |
| 522 | // in the locale's encoding to UTF-8. |
| 523 | locale __loc = __fc.locale(); |
| 524 | if (__loc != locale::classic()) |
| 525 | __fc._M_loc = __with_encoding_conversion(__loc); |
| 526 | } |
| 527 | #endif |
| 528 | |
| 529 | _Sink_iter<_CharT> __out; |
| 530 | __format::_Str_sink<_CharT> __sink; |
| 531 | bool __write_direct = false; |
| 532 | if constexpr (is_same_v<typename _FormatContext::iterator, |
| 533 | _Sink_iter<_CharT>>) |
| 534 | { |
| 535 | if (_M_spec._M_width_kind == __format::_WP_none) |
| 536 | { |
| 537 | __out = __fc.out(); |
| 538 | __write_direct = true; |
| 539 | } |
| 540 | else |
| 541 | __out = __sink.out(); |
| 542 | } |
| 543 | else |
| 544 | __out = __sink.out(); |
| 545 | |
| 546 | // formatter<duration> passes the correct value of __is_neg |
| 547 | // for durations but for hh_mm_ss we decide it here. |
| 548 | if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>) |
| 549 | __is_neg = __t.is_negative(); |
| 550 | |
| 551 | auto __print_sign = [&__is_neg, &__out] { |
| 552 | if constexpr (chrono::__is_duration_v<_Tp> |
| 553 | || __is_specialization_of<_Tp, chrono::hh_mm_ss>) |
| 554 | if (__is_neg) |
| 555 | { |
| 556 | *__out++ = _S_plus_minus[1]; |
| 557 | __is_neg = false; |
| 558 | } |
| 559 | return std::move(__out); |
| 560 | }; |
| 561 | |
| 562 | // Characters to output for "%n", "%t" and "%%" specifiers. |
| 563 | constexpr const _CharT* __literals = _GLIBCXX_WIDEN("\n\t%" ); |
| 564 | |
| 565 | ++__first; // Skip leading '%' at start of chrono-specs. |
| 566 | |
| 567 | _CharT __mod{}; |
| 568 | do |
| 569 | { |
| 570 | _CharT __c = *__first++; |
| 571 | switch (__c) |
| 572 | { |
| 573 | case 'a': |
| 574 | case 'A': |
| 575 | __out = _M_a_A(__t, std::move(__out), __fc, __c == 'A'); |
| 576 | break; |
| 577 | case 'b': |
| 578 | case 'h': |
| 579 | case 'B': |
| 580 | __out = _M_b_B(__t, std::move(__out), __fc, __c == 'B'); |
| 581 | break; |
| 582 | case 'c': |
| 583 | __out = _M_c(__t, std::move(__out), __fc, __mod == 'E'); |
| 584 | break; |
| 585 | case 'C': |
| 586 | case 'y': |
| 587 | case 'Y': |
| 588 | __out = _M_C_y_Y(__t, std::move(__out), __fc, __c, __mod); |
| 589 | break; |
| 590 | case 'd': |
| 591 | case 'e': |
| 592 | __out = _M_d_e(__t, std::move(__out), __fc, __c, __mod == 'O'); |
| 593 | break; |
| 594 | case 'D': |
| 595 | __out = _M_D(__t, std::move(__out), __fc); |
| 596 | break; |
| 597 | case 'F': |
| 598 | __out = _M_F(__t, std::move(__out), __fc); |
| 599 | break; |
| 600 | case 'g': |
| 601 | case 'G': |
| 602 | __out = _M_g_G(__t, std::move(__out), __fc, __c == 'G'); |
| 603 | break; |
| 604 | case 'H': |
| 605 | case 'I': |
| 606 | __out = _M_H_I(__t, __print_sign(), __fc, __c, __mod == 'O'); |
| 607 | break; |
| 608 | case 'j': |
| 609 | __out = _M_j(__t, __print_sign(), __fc); |
| 610 | break; |
| 611 | case 'm': |
| 612 | __out = _M_m(__t, std::move(__out), __fc, __mod == 'O'); |
| 613 | break; |
| 614 | case 'M': |
| 615 | __out = _M_M(__t, __print_sign(), __fc, __mod == 'O'); |
| 616 | break; |
| 617 | case 'p': |
| 618 | __out = _M_p(__t, std::move(__out), __fc); |
| 619 | break; |
| 620 | case 'q': |
| 621 | __out = _M_q(__t, std::move(__out), __fc); |
| 622 | break; |
| 623 | case 'Q': |
| 624 | // %Q The duration's numeric value. |
| 625 | if constexpr (chrono::__is_duration_v<_Tp>) |
| 626 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 627 | // 4118. How should duration formatters format custom rep? |
| 628 | __out = std::format_to(__print_sign(), _S_empty_spec, |
| 629 | +__t.count()); |
| 630 | else |
| 631 | __throw_format_error(what: "chrono format error: argument is " |
| 632 | "not a duration" ); |
| 633 | break; |
| 634 | case 'r': |
| 635 | __out = _M_r(__t, __print_sign(), __fc); |
| 636 | break; |
| 637 | case 'R': |
| 638 | case 'T': |
| 639 | __out = _M_R_T(__t, __print_sign(), __fc, __c == 'T'); |
| 640 | break; |
| 641 | case 'S': |
| 642 | __out = _M_S(__t, __print_sign(), __fc, __mod == 'O'); |
| 643 | break; |
| 644 | case 'u': |
| 645 | case 'w': |
| 646 | __out = _M_u_w(__t, std::move(__out), __fc, __c, __mod == 'O'); |
| 647 | break; |
| 648 | case 'U': |
| 649 | case 'V': |
| 650 | case 'W': |
| 651 | __out = _M_U_V_W(__t, std::move(__out), __fc, __c, |
| 652 | __mod == 'O'); |
| 653 | break; |
| 654 | case 'x': |
| 655 | __out = _M_x(__t, std::move(__out), __fc, __mod == 'E'); |
| 656 | break; |
| 657 | case 'X': |
| 658 | __out = _M_X(__t, __print_sign(), __fc, __mod == 'E'); |
| 659 | break; |
| 660 | case 'z': |
| 661 | __out = _M_z(__t, std::move(__out), __fc, (bool)__mod); |
| 662 | break; |
| 663 | case 'Z': |
| 664 | __out = _M_Z(__t, std::move(__out), __fc); |
| 665 | break; |
| 666 | case 'n': |
| 667 | *__out++ = __literals[0]; |
| 668 | break; |
| 669 | case 't': |
| 670 | *__out++ = __literals[1]; |
| 671 | break; |
| 672 | case '%': |
| 673 | *__out++ = __literals[2]; |
| 674 | break; |
| 675 | case 'O': |
| 676 | case 'E': |
| 677 | __mod = __c; |
| 678 | continue; |
| 679 | case '}': |
| 680 | __first = __last; |
| 681 | break; |
| 682 | } |
| 683 | __mod = _CharT(); |
| 684 | // Scan for next '%' and write out everything before it. |
| 685 | __string_view __str(__first, __last - __first); |
| 686 | size_t __pos = __str.find('%'); |
| 687 | if (__pos == 0) |
| 688 | ++__first; |
| 689 | else |
| 690 | { |
| 691 | if (__pos == __str.npos) |
| 692 | __first = __last; |
| 693 | else |
| 694 | { |
| 695 | __str.remove_suffix(__str.length() - __pos); |
| 696 | __first += __pos + 1; |
| 697 | } |
| 698 | __out = __format::__write(std::move(__out), __str); |
| 699 | } |
| 700 | } |
| 701 | while (__first != __last); |
| 702 | |
| 703 | if constexpr (is_same_v<typename _FormatContext::iterator, |
| 704 | _Sink_iter<_CharT>>) |
| 705 | if (__write_direct) |
| 706 | return __out; |
| 707 | |
| 708 | auto __str = std::move(__sink).get(); |
| 709 | return __format::__write_padded_as_spec(__str, __str.size(), |
| 710 | __fc, _M_spec); |
| 711 | } |
| 712 | |
| 713 | _ChronoSpec<_CharT> _M_spec; |
| 714 | |
| 715 | private: |
| 716 | // Return the formatting locale. |
| 717 | template<typename _FormatContext> |
| 718 | std::locale |
| 719 | _M_locale(_FormatContext& __fc) const |
| 720 | { |
| 721 | if (!_M_spec._M_localized) |
| 722 | return std::locale::classic(); |
| 723 | else |
| 724 | return __fc.locale(); |
| 725 | } |
| 726 | |
| 727 | // Format for empty chrono-specs, e.g. "{}" (C++20 [time.format] p6). |
| 728 | // TODO: consider moving body of every operator<< into this function |
| 729 | // and use std::format("{}", t) to implement those operators. That |
| 730 | // would avoid std::format("{}", t) calling operator<< which calls |
| 731 | // std::format again. |
| 732 | template<typename _Tp, typename _FormatContext> |
| 733 | typename _FormatContext::iterator |
| 734 | _M_format_to_ostream(const _Tp& __t, _FormatContext& __fc, |
| 735 | bool __is_neg) const |
| 736 | { |
| 737 | using ::std::chrono::__detail::__utc_leap_second; |
| 738 | using ::std::chrono::__detail::__local_time_fmt; |
| 739 | |
| 740 | basic_ostringstream<_CharT> __os; |
| 741 | __os.imbue(_M_locale(__fc)); |
| 742 | |
| 743 | if constexpr (__is_specialization_of<_Tp, __local_time_fmt>) |
| 744 | { |
| 745 | // Format as "{:L%F %T}" |
| 746 | auto __days = chrono::floor<chrono::days>(__t._M_time); |
| 747 | __os << chrono::year_month_day(__days) << ' ' |
| 748 | << chrono::hh_mm_ss(__t._M_time - __days); |
| 749 | |
| 750 | // For __local_time_fmt the __is_neg flags says whether to |
| 751 | // append " %Z" to the result. |
| 752 | if (__is_neg) |
| 753 | { |
| 754 | if (!__t._M_abbrev) [[unlikely]] |
| 755 | __format::__no_timezone_available(); |
| 756 | else if constexpr (is_same_v<_CharT, char>) |
| 757 | __os << ' ' << *__t._M_abbrev; |
| 758 | else |
| 759 | { |
| 760 | __os << L' '; |
| 761 | for (char __c : *__t._M_abbrev) |
| 762 | __os << __c; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | else |
| 767 | { |
| 768 | if constexpr (__is_specialization_of<_Tp, __utc_leap_second>) |
| 769 | __os << __t._M_date << ' ' << __t._M_time; |
| 770 | else if constexpr (chrono::__is_time_point_v<_Tp>) |
| 771 | { |
| 772 | // Need to be careful here because not all specializations |
| 773 | // of chrono::sys_time can be written to an ostream. |
| 774 | // For the specializations of time_point that can be |
| 775 | // formatted with an empty chrono-specs, either it's a |
| 776 | // sys_time with period greater or equal to days: |
| 777 | if constexpr (is_convertible_v<_Tp, chrono::sys_days>) |
| 778 | __os << _S_date(__t); |
| 779 | else // Or it's formatted as "{:L%F %T}": |
| 780 | { |
| 781 | auto __days = chrono::floor<chrono::days>(__t); |
| 782 | __os << chrono::year_month_day(__days) << ' ' |
| 783 | << chrono::hh_mm_ss(__t - __days); |
| 784 | } |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | if constexpr (chrono::__is_duration_v<_Tp>) |
| 789 | if (__is_neg) [[unlikely]] |
| 790 | __os << _S_plus_minus[1]; |
| 791 | __os << __t; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | auto __str = std::move(__os).str(); |
| 796 | return __format::__write_padded_as_spec(__str, __str.size(), |
| 797 | __fc, _M_spec); |
| 798 | } |
| 799 | |
| 800 | static constexpr const _CharT* _S_chars |
| 801 | = _GLIBCXX_WIDEN("0123456789+-:/ {}" ); |
| 802 | static constexpr const _CharT* _S_plus_minus = _S_chars + 10; |
| 803 | static constexpr _CharT _S_colon = _S_chars[12]; |
| 804 | static constexpr _CharT _S_slash = _S_chars[13]; |
| 805 | static constexpr _CharT _S_space = _S_chars[14]; |
| 806 | static constexpr const _CharT* _S_empty_spec = _S_chars + 15; |
| 807 | |
| 808 | template<typename _OutIter> |
| 809 | _OutIter |
| 810 | _M_write(_OutIter __out, const locale& __loc, __string_view __s) const |
| 811 | { |
| 812 | #if defined _GLIBCXX_USE_NL_LANGINFO_L && __CHAR_BIT__ == 8 |
| 813 | __sso_string __buf; |
| 814 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 815 | // 3565. Handling of encodings in localized formatting |
| 816 | // of chrono types is underspecified |
| 817 | if constexpr (is_same_v<_CharT, char>) |
| 818 | if constexpr (__unicode::__literal_encoding_is_utf8()) |
| 819 | if (_M_spec._M_localized && _M_spec._M_locale_specific() |
| 820 | && __loc != locale::classic()) |
| 821 | { |
| 822 | extern string_view |
| 823 | __locale_encoding_to_utf8(const locale&, string_view, void*); |
| 824 | |
| 825 | __s = __locale_encoding_to_utf8(__loc, __s, &__buf); |
| 826 | } |
| 827 | #endif |
| 828 | return __format::__write(std::move(__out), __s); |
| 829 | } |
| 830 | |
| 831 | template<typename _Tp, typename _FormatContext> |
| 832 | typename _FormatContext::iterator |
| 833 | _M_a_A(const _Tp& __t, typename _FormatContext::iterator __out, |
| 834 | _FormatContext& __ctx, bool __full) const |
| 835 | { |
| 836 | // %a Locale's abbreviated weekday name. |
| 837 | // %A Locale's full weekday name. |
| 838 | chrono::weekday __wd = _S_weekday(__t); |
| 839 | if (!__wd.ok()) |
| 840 | __throw_format_error(what: "format error: invalid weekday" ); |
| 841 | |
| 842 | locale __loc = _M_locale(__ctx); |
| 843 | const auto& __tp = use_facet<__timepunct<_CharT>>(__loc); |
| 844 | const _CharT* __days[7]; |
| 845 | if (__full) |
| 846 | __tp._M_days(__days); |
| 847 | else |
| 848 | __tp._M_days_abbreviated(__days); |
| 849 | __string_view __str(__days[__wd.c_encoding()]); |
| 850 | return _M_write(std::move(__out), __loc, __str); |
| 851 | } |
| 852 | |
| 853 | template<typename _Tp, typename _FormatContext> |
| 854 | typename _FormatContext::iterator |
| 855 | _M_b_B(const _Tp& __t, typename _FormatContext::iterator __out, |
| 856 | _FormatContext& __ctx, bool __full) const |
| 857 | { |
| 858 | // %b Locale's abbreviated month name. |
| 859 | // %B Locale's full month name. |
| 860 | chrono::month __m = _S_month(__t); |
| 861 | if (!__m.ok()) |
| 862 | __throw_format_error(what: "format error: invalid month" ); |
| 863 | locale __loc = _M_locale(__ctx); |
| 864 | const auto& __tp = use_facet<__timepunct<_CharT>>(__loc); |
| 865 | const _CharT* __months[12]; |
| 866 | if (__full) |
| 867 | __tp._M_months(__months); |
| 868 | else |
| 869 | __tp._M_months_abbreviated(__months); |
| 870 | __string_view __str(__months[(unsigned)__m - 1]); |
| 871 | return _M_write(std::move(__out), __loc, __str); |
| 872 | } |
| 873 | |
| 874 | template<typename _Tp, typename _FormatContext> |
| 875 | typename _FormatContext::iterator |
| 876 | _M_c(const _Tp& __t, typename _FormatContext::iterator __out, |
| 877 | _FormatContext& __ctx, bool __mod = false) const |
| 878 | { |
| 879 | // %c Locale's date and time representation. |
| 880 | // %Ec Locale's alternate date and time representation. |
| 881 | |
| 882 | using namespace chrono; |
| 883 | using ::std::chrono::__detail::__utc_leap_second; |
| 884 | using ::std::chrono::__detail::__local_time_fmt; |
| 885 | |
| 886 | struct tm __tm{}; |
| 887 | |
| 888 | // Some locales use %Z in their %c format but we don't want strftime |
| 889 | // to use the system's local time zone (from /etc/localtime or $TZ) |
| 890 | // as the output for %Z. Setting tm_isdst to -1 says there is no |
| 891 | // time zone info available for the time in __tm. |
| 892 | __tm.tm_isdst = -1; |
| 893 | |
| 894 | #ifdef _GLIBCXX_USE_STRUCT_TM_TM_ZONE |
| 895 | // POSIX.1-2024 adds tm.tm_zone which will be used for %Z. |
| 896 | // BSD has had tm_zone since 1987 but as char* so cast away const. |
| 897 | if constexpr (__is_time_point_v<_Tp>) |
| 898 | { |
| 899 | // One of sys_time, utc_time, or local_time. |
| 900 | if constexpr (!is_same_v<typename _Tp::clock, local_t>) |
| 901 | __tm.tm_zone = const_cast<char*>("UTC" ); |
| 902 | } |
| 903 | else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>) |
| 904 | { |
| 905 | // local-time-format-t is used to provide time zone info for |
| 906 | // one of zoned_time, tai_time, gps_time, or local_time. |
| 907 | if (__t._M_abbrev) |
| 908 | __tm.tm_zone = const_cast<char*>(__t._M_abbrev->c_str()); |
| 909 | } |
| 910 | else |
| 911 | __tm.tm_zone = const_cast<char*>("UTC" ); |
| 912 | #endif |
| 913 | |
| 914 | auto __d = _S_days(__t); // Either sys_days or local_days. |
| 915 | using _TDays = decltype(__d); |
| 916 | const year_month_day __ymd(__d); |
| 917 | const auto __y = __ymd.year(); |
| 918 | const auto __hms = _S_hms(__t); |
| 919 | |
| 920 | __tm.tm_year = (int)__y - 1900; |
| 921 | __tm.tm_yday = (__d - _TDays(__y/January/1)).count(); |
| 922 | __tm.tm_mon = (unsigned)__ymd.month() - 1; |
| 923 | __tm.tm_mday = (unsigned)__ymd.day(); |
| 924 | __tm.tm_wday = weekday(__d).c_encoding(); |
| 925 | __tm.tm_hour = __hms.hours().count(); |
| 926 | __tm.tm_min = __hms.minutes().count(); |
| 927 | __tm.tm_sec = __hms.seconds().count(); |
| 928 | |
| 929 | return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm, 'c', |
| 930 | __mod ? 'E' : '\0'); |
| 931 | } |
| 932 | |
| 933 | template<typename _Tp, typename _FormatContext> |
| 934 | typename _FormatContext::iterator |
| 935 | _M_C_y_Y(const _Tp& __t, typename _FormatContext::iterator __out, |
| 936 | _FormatContext& __ctx, _CharT __conv, _CharT __mod = 0) const |
| 937 | { |
| 938 | // %C Year divided by 100 using floored division. |
| 939 | // %EC Locale's alternative preresentation of the century (era name). |
| 940 | // %y Last two decimal digits of the year. |
| 941 | // %Oy Locale's alternative representation. |
| 942 | // %Ey Locale's alternative representation of offset from %EC. |
| 943 | // %Y Year as a decimal number. |
| 944 | // %EY Locale's alternative full year representation. |
| 945 | |
| 946 | chrono::year __y = _S_year(__t); |
| 947 | |
| 948 | if (__mod && _M_spec._M_localized) [[unlikely]] |
| 949 | if (auto __loc = __ctx.locale(); __loc != locale::classic()) |
| 950 | { |
| 951 | struct tm __tm{}; |
| 952 | __tm.tm_year = (int)__y - 1900; |
| 953 | return _M_locale_fmt(std::move(__out), __loc, __tm, |
| 954 | __conv, __mod); |
| 955 | } |
| 956 | |
| 957 | basic_string<_CharT> __s; |
| 958 | int __yi = (int)__y; |
| 959 | const bool __is_neg = __yi < 0; |
| 960 | __yi = __builtin_abs(__yi); |
| 961 | |
| 962 | if (__conv == 'Y' || __conv == 'C') |
| 963 | { |
| 964 | int __ci = __yi / 100; |
| 965 | if (__is_neg) [[unlikely]] |
| 966 | { |
| 967 | __s.assign(1, _S_plus_minus[1]); |
| 968 | // For floored division -123//100 is -2 and -100//100 is -1 |
| 969 | if (__conv == 'C' && (__ci * 100) != __yi) |
| 970 | ++__ci; |
| 971 | } |
| 972 | if (__ci >= 100) [[unlikely]] |
| 973 | { |
| 974 | __s += std::format(_S_empty_spec, __ci / 100); |
| 975 | __ci %= 100; |
| 976 | } |
| 977 | __s += _S_two_digits(n: __ci); |
| 978 | } |
| 979 | |
| 980 | if (__conv == 'Y' || __conv == 'y') |
| 981 | __s += _S_two_digits(n: __yi % 100); |
| 982 | |
| 983 | return __format::__write(std::move(__out), __string_view(__s)); |
| 984 | } |
| 985 | |
| 986 | template<typename _Tp, typename _FormatContext> |
| 987 | typename _FormatContext::iterator |
| 988 | _M_D(const _Tp& __t, typename _FormatContext::iterator __out, |
| 989 | _FormatContext&) const |
| 990 | { |
| 991 | auto __ymd = _S_date(__t); |
| 992 | basic_string<_CharT> __s; |
| 993 | #if ! _GLIBCXX_USE_CXX11_ABI |
| 994 | __s.reserve(8); |
| 995 | #endif |
| 996 | __s = _S_two_digits(n: (unsigned)__ymd.month()); |
| 997 | __s += _S_slash; |
| 998 | __s += _S_two_digits(n: (unsigned)__ymd.day()); |
| 999 | __s += _S_slash; |
| 1000 | __s += _S_two_digits(n: __builtin_abs((int)__ymd.year()) % 100); |
| 1001 | return __format::__write(std::move(__out), __string_view(__s)); |
| 1002 | } |
| 1003 | |
| 1004 | template<typename _Tp, typename _FormatContext> |
| 1005 | typename _FormatContext::iterator |
| 1006 | _M_d_e(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1007 | _FormatContext& __ctx, _CharT __conv, bool __mod = false) const |
| 1008 | { |
| 1009 | // %d The day of month as a decimal number. |
| 1010 | // %Od Locale's alternative representation. |
| 1011 | // %e Day of month as decimal number, padded with space. |
| 1012 | // %Oe Locale's alternative digits. |
| 1013 | |
| 1014 | chrono::day __d = _S_day(__t); |
| 1015 | unsigned __i = (unsigned)__d; |
| 1016 | |
| 1017 | if (__mod && _M_spec._M_localized) [[unlikely]] |
| 1018 | if (auto __loc = __ctx.locale(); __loc != locale::classic()) |
| 1019 | { |
| 1020 | struct tm __tm{}; |
| 1021 | __tm.tm_mday = __i; |
| 1022 | return _M_locale_fmt(std::move(__out), __loc, __tm, |
| 1023 | (char)__conv, 'O'); |
| 1024 | } |
| 1025 | |
| 1026 | auto __sv = _S_two_digits(n: __i); |
| 1027 | _CharT __buf[2]; |
| 1028 | if (__conv == _CharT('e') && __i < 10) |
| 1029 | { |
| 1030 | __buf[0] = _S_space; |
| 1031 | __buf[1] = __sv[1]; |
| 1032 | __sv = {__buf, 2}; |
| 1033 | } |
| 1034 | return __format::__write(std::move(__out), __sv); |
| 1035 | } |
| 1036 | |
| 1037 | template<typename _Tp, typename _FormatContext> |
| 1038 | typename _FormatContext::iterator |
| 1039 | _M_F(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1040 | _FormatContext&) const |
| 1041 | { |
| 1042 | auto __ymd = _S_date(__t); |
| 1043 | auto __s = std::format(_GLIBCXX_WIDEN("{:04d}- - " ), |
| 1044 | (int)__ymd.year()); |
| 1045 | auto __sv = _S_two_digits(n: (unsigned)__ymd.month()); |
| 1046 | __s[__s.size() - 5] = __sv[0]; |
| 1047 | __s[__s.size() - 4] = __sv[1]; |
| 1048 | __sv = _S_two_digits(n: (unsigned)__ymd.day()); |
| 1049 | __s[__s.size() - 2] = __sv[0]; |
| 1050 | __s[__s.size() - 1] = __sv[1]; |
| 1051 | __sv = __s; |
| 1052 | return __format::__write(std::move(__out), __sv); |
| 1053 | } |
| 1054 | |
| 1055 | template<typename _Tp, typename _FormatContext> |
| 1056 | typename _FormatContext::iterator |
| 1057 | _M_g_G(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1058 | _FormatContext& __ctx, bool __full) const |
| 1059 | { |
| 1060 | // %g last two decimal digits of the ISO week-based year. |
| 1061 | // %G ISO week-based year. |
| 1062 | using namespace chrono; |
| 1063 | auto __d = _S_days(__t); |
| 1064 | // Move to nearest Thursday: |
| 1065 | __d -= (weekday(__d) - Monday) - days(3); |
| 1066 | // ISO week-based year is the year that contains that Thursday: |
| 1067 | year __y = year_month_day(__d).year(); |
| 1068 | return _M_C_y_Y(__y, std::move(__out), __ctx, "yY" [__full]); |
| 1069 | } |
| 1070 | |
| 1071 | template<typename _Tp, typename _FormatContext> |
| 1072 | typename _FormatContext::iterator |
| 1073 | _M_H_I(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1074 | _FormatContext& __ctx, _CharT __conv, bool __mod = false) const |
| 1075 | { |
| 1076 | // %H The hour (24-hour clock) as a decimal number. |
| 1077 | // %OH Locale's alternative representation. |
| 1078 | // %I The hour (12-hour clock) as a decimal number. |
| 1079 | // %OI Locale's alternative representation. |
| 1080 | |
| 1081 | const auto __hms = _S_hms(__t); |
| 1082 | int __i = __hms.hours().count(); |
| 1083 | |
| 1084 | if (__mod && _M_spec._M_localized) [[unlikely]] |
| 1085 | if (auto __loc = __ctx.locale(); __loc != locale::classic()) |
| 1086 | { |
| 1087 | struct tm __tm{}; |
| 1088 | __tm.tm_hour = __i; |
| 1089 | return _M_locale_fmt(std::move(__out), __loc, __tm, |
| 1090 | (char)__conv, 'O'); |
| 1091 | } |
| 1092 | |
| 1093 | if (__conv == _CharT('I')) |
| 1094 | { |
| 1095 | if (__i == 0) |
| 1096 | __i = 12; |
| 1097 | else if (__i > 12) |
| 1098 | __i -= 12; |
| 1099 | } |
| 1100 | return __format::__write(std::move(__out), _S_two_digits(n: __i)); |
| 1101 | } |
| 1102 | |
| 1103 | template<typename _Tp, typename _FormatContext> |
| 1104 | typename _FormatContext::iterator |
| 1105 | _M_j(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1106 | _FormatContext&) const |
| 1107 | { |
| 1108 | if constexpr (chrono::__is_duration_v<_Tp>) |
| 1109 | { |
| 1110 | // Decimal number of days, without padding. |
| 1111 | unsigned __d = chrono::duration_cast<chrono::days>(__t).count(); |
| 1112 | return std::format_to(std::move(__out), _S_empty_spec, __d); |
| 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | // Day of the year as a decimal number, padding with zero. |
| 1117 | using namespace chrono; |
| 1118 | auto __day = _S_days(__t); |
| 1119 | auto __ymd = _S_date(__t); |
| 1120 | days __d; |
| 1121 | // See "Calculating Ordinal Dates" at |
| 1122 | // https://github.com/HowardHinnant/date/wiki/Examples-and-Recipes |
| 1123 | if constexpr (is_same_v<typename decltype(__day)::clock, local_t>) |
| 1124 | __d = __day - local_days(__ymd.year()/January/0); |
| 1125 | else |
| 1126 | __d = __day - sys_days(__ymd.year()/January/0); |
| 1127 | return std::format_to(std::move(__out), _GLIBCXX_WIDEN("{:03d}" ), |
| 1128 | __d.count()); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | template<typename _Tp, typename _FormatContext> |
| 1133 | typename _FormatContext::iterator |
| 1134 | _M_m(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1135 | _FormatContext& __ctx, bool __mod) const |
| 1136 | { |
| 1137 | // %m month as a decimal number. |
| 1138 | // %Om Locale's alternative representation. |
| 1139 | |
| 1140 | auto __m = _S_month(__t); |
| 1141 | auto __i = (unsigned)__m; |
| 1142 | |
| 1143 | if (__mod && _M_spec._M_localized) [[unlikely]] // %Om |
| 1144 | if (auto __loc = __ctx.locale(); __loc != locale::classic()) |
| 1145 | { |
| 1146 | struct tm __tm{}; |
| 1147 | __tm.tm_mon = __i - 1; |
| 1148 | return _M_locale_fmt(std::move(__out), __loc, __tm, |
| 1149 | 'm', 'O'); |
| 1150 | } |
| 1151 | |
| 1152 | return __format::__write(std::move(__out), _S_two_digits(n: __i)); |
| 1153 | } |
| 1154 | |
| 1155 | template<typename _Tp, typename _FormatContext> |
| 1156 | typename _FormatContext::iterator |
| 1157 | _M_M(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1158 | _FormatContext& __ctx, bool __mod) const |
| 1159 | { |
| 1160 | // %M The minute as a decimal number. |
| 1161 | // %OM Locale's alternative representation. |
| 1162 | |
| 1163 | auto __m = _S_hms(__t).minutes(); |
| 1164 | auto __i = __m.count(); |
| 1165 | |
| 1166 | if (__mod && _M_spec._M_localized) [[unlikely]] // %OM |
| 1167 | if (auto __loc = __ctx.locale(); __loc != locale::classic()) |
| 1168 | { |
| 1169 | struct tm __tm{}; |
| 1170 | __tm.tm_min = __i; |
| 1171 | return _M_locale_fmt(std::move(__out), __loc, __tm, |
| 1172 | 'M', 'O'); |
| 1173 | } |
| 1174 | |
| 1175 | return __format::__write(std::move(__out), _S_two_digits(n: __i)); |
| 1176 | } |
| 1177 | |
| 1178 | template<typename _Tp, typename _FormatContext> |
| 1179 | typename _FormatContext::iterator |
| 1180 | _M_p(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1181 | _FormatContext& __ctx) const |
| 1182 | { |
| 1183 | // %p The locale's equivalent of the AM/PM designations. |
| 1184 | auto __hms = _S_hms(__t); |
| 1185 | locale __loc = _M_locale(__ctx); |
| 1186 | const auto& __tp = use_facet<__timepunct<_CharT>>(__loc); |
| 1187 | const _CharT* __ampm[2]; |
| 1188 | __tp._M_am_pm(__ampm); |
| 1189 | return _M_write(std::move(__out), __loc, |
| 1190 | __ampm[__hms.hours().count() >= 12]); |
| 1191 | } |
| 1192 | |
| 1193 | template<typename _Tp, typename _FormatContext> |
| 1194 | typename _FormatContext::iterator |
| 1195 | _M_q(const _Tp&, typename _FormatContext::iterator __out, |
| 1196 | _FormatContext&) const |
| 1197 | { |
| 1198 | // %q The duration's unit suffix |
| 1199 | if constexpr (!chrono::__is_duration_v<_Tp>) |
| 1200 | __throw_format_error(what: "format error: argument is not a duration" ); |
| 1201 | else |
| 1202 | { |
| 1203 | namespace __d = chrono::__detail; |
| 1204 | using period = typename _Tp::period; |
| 1205 | return __d::__fmt_units_suffix<period, _CharT>(std::move(__out)); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | // %Q handled in _M_format |
| 1210 | |
| 1211 | template<typename _Tp, typename _FormatContext> |
| 1212 | typename _FormatContext::iterator |
| 1213 | _M_r(const _Tp& __tt, typename _FormatContext::iterator __out, |
| 1214 | _FormatContext& __ctx) const |
| 1215 | { |
| 1216 | // %r locale's 12-hour clock time. |
| 1217 | auto __t = _S_floor_seconds(__tt); |
| 1218 | locale __loc = _M_locale(__ctx); |
| 1219 | const auto& __tp = use_facet<__timepunct<_CharT>>(__loc); |
| 1220 | const _CharT* __ampm_fmt; |
| 1221 | __tp._M_am_pm_format(&__ampm_fmt); |
| 1222 | basic_string<_CharT> __fmt(_S_empty_spec); |
| 1223 | __fmt.insert(1u, 1u, _S_colon); |
| 1224 | __fmt.insert(2u, __ampm_fmt); |
| 1225 | using _FmtStr = _Runtime_format_string<_CharT>; |
| 1226 | return _M_write(std::move(__out), __loc, |
| 1227 | std::format(__loc, _FmtStr(__fmt), __t)); |
| 1228 | } |
| 1229 | |
| 1230 | template<typename _Tp, typename _FormatContext> |
| 1231 | typename _FormatContext::iterator |
| 1232 | _M_R_T(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1233 | _FormatContext& __ctx, bool __secs) const |
| 1234 | { |
| 1235 | // %R Equivalent to %H:%M |
| 1236 | // %T Equivalent to %H:%M:%S |
| 1237 | auto __hms = _S_hms(__t); |
| 1238 | |
| 1239 | auto __s = std::format(_GLIBCXX_WIDEN("{:02d}:00" ), |
| 1240 | __hms.hours().count()); |
| 1241 | auto __sv = _S_two_digits(n: __hms.minutes().count()); |
| 1242 | __s[__s.size() - 2] = __sv[0]; |
| 1243 | __s[__s.size() - 1] = __sv[1]; |
| 1244 | __sv = __s; |
| 1245 | __out = __format::__write(std::move(__out), __sv); |
| 1246 | if (__secs) |
| 1247 | { |
| 1248 | *__out++ = _S_colon; |
| 1249 | __out = _M_S(__hms, std::move(__out), __ctx); |
| 1250 | } |
| 1251 | return __out; |
| 1252 | } |
| 1253 | |
| 1254 | template<typename _Tp, typename _FormatContext> |
| 1255 | typename _FormatContext::iterator |
| 1256 | _M_S(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1257 | _FormatContext& __ctx, bool __mod = false) const |
| 1258 | { |
| 1259 | // %S Seconds as a decimal number. |
| 1260 | // %OS The locale's alternative representation. |
| 1261 | auto __hms = _S_hms(__t); |
| 1262 | auto __s = __hms.seconds(); |
| 1263 | |
| 1264 | if (__mod) [[unlikely]] // %OS |
| 1265 | { |
| 1266 | if (_M_spec._M_localized) |
| 1267 | if (auto __loc = __ctx.locale(); __loc != locale::classic()) |
| 1268 | { |
| 1269 | struct tm __tm{}; |
| 1270 | __tm.tm_sec = (int)__s.count(); |
| 1271 | return _M_locale_fmt(std::move(__out), __loc, __tm, |
| 1272 | 'S', 'O'); |
| 1273 | } |
| 1274 | |
| 1275 | // %OS formats don't include subseconds, so just format that: |
| 1276 | return __format::__write(std::move(__out), |
| 1277 | _S_two_digits(n: __s.count())); |
| 1278 | } |
| 1279 | |
| 1280 | if constexpr (__hms.fractional_width == 0) |
| 1281 | __out = __format::__write(std::move(__out), |
| 1282 | _S_two_digits(n: __s.count())); |
| 1283 | else |
| 1284 | { |
| 1285 | locale __loc = _M_locale(__ctx); |
| 1286 | auto __ss = __hms.subseconds(); |
| 1287 | using rep = typename decltype(__ss)::rep; |
| 1288 | if constexpr (is_floating_point_v<rep>) |
| 1289 | { |
| 1290 | chrono::duration<rep> __fs = __s + __ss; |
| 1291 | __out = std::format_to(std::move(__out), __loc, |
| 1292 | _GLIBCXX_WIDEN("{:#0{}.{}Lf}" ), |
| 1293 | __fs.count(), |
| 1294 | 3 + __hms.fractional_width, |
| 1295 | __hms.fractional_width); |
| 1296 | } |
| 1297 | else |
| 1298 | { |
| 1299 | const auto& __np |
| 1300 | = use_facet<numpunct<_CharT>>(__loc); |
| 1301 | __out = __format::__write(std::move(__out), |
| 1302 | _S_two_digits(n: __s.count())); |
| 1303 | *__out++ = __np.decimal_point(); |
| 1304 | if constexpr (is_integral_v<rep>) |
| 1305 | __out = std::format_to(std::move(__out), |
| 1306 | _GLIBCXX_WIDEN("{:0{}}" ), |
| 1307 | __ss.count(), |
| 1308 | __hms.fractional_width); |
| 1309 | else |
| 1310 | { |
| 1311 | auto __str = std::format(_S_empty_spec, __ss.count()); |
| 1312 | __out = std::format_to(_GLIBCXX_WIDEN("{:0>{}s}" ), |
| 1313 | __str, |
| 1314 | __hms.fractional_width); |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | return __out; |
| 1319 | } |
| 1320 | |
| 1321 | // %t handled in _M_format |
| 1322 | |
| 1323 | template<typename _Tp, typename _FormatContext> |
| 1324 | typename _FormatContext::iterator |
| 1325 | _M_u_w(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1326 | _FormatContext& __ctx, _CharT __conv, bool __mod = false) const |
| 1327 | { |
| 1328 | // %u ISO weekday as a decimal number (1-7), where Monday is 1. |
| 1329 | // %Ou Locale's alternative numeric rep. |
| 1330 | // %w Weekday as a decimal number (0-6), where Sunday is 0. |
| 1331 | // %Ow Locale's alternative numeric rep. |
| 1332 | |
| 1333 | chrono::weekday __wd = _S_weekday(__t); |
| 1334 | |
| 1335 | if (__mod && _M_spec._M_localized) [[unlikely]] |
| 1336 | if (auto __loc = __ctx.locale(); __loc != locale::classic()) |
| 1337 | { |
| 1338 | struct tm __tm{}; |
| 1339 | __tm.tm_wday = __wd.c_encoding(); |
| 1340 | return _M_locale_fmt(std::move(__out), __loc, __tm, |
| 1341 | (char)__conv, 'O'); |
| 1342 | } |
| 1343 | |
| 1344 | unsigned __wdi = __conv == 'u' ? __wd.iso_encoding() |
| 1345 | : __wd.c_encoding(); |
| 1346 | const _CharT __d = _S_digit(n: __wdi); |
| 1347 | return __format::__write(std::move(__out), __string_view(&__d, 1)); |
| 1348 | } |
| 1349 | |
| 1350 | template<typename _Tp, typename _FormatContext> |
| 1351 | typename _FormatContext::iterator |
| 1352 | _M_U_V_W(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1353 | _FormatContext& __ctx, _CharT __conv, bool __mod = false) const |
| 1354 | { |
| 1355 | // %U Week number of the year as a decimal number, from first Sunday. |
| 1356 | // %OU Locale's alternative numeric rep. |
| 1357 | // %V ISO week-based week number as a decimal number. |
| 1358 | // %OV Locale's alternative numeric rep. |
| 1359 | // %W Week number of the year as a decimal number, from first Monday. |
| 1360 | // %OW Locale's alternative numeric rep. |
| 1361 | using namespace chrono; |
| 1362 | auto __d = _S_days(__t); |
| 1363 | using _TDays = decltype(__d); // Either sys_days or local_days. |
| 1364 | |
| 1365 | if (__mod && _M_spec._M_localized) [[unlikely]] |
| 1366 | if (auto __loc = __ctx.locale(); __loc != locale::classic()) |
| 1367 | { |
| 1368 | const year_month_day __ymd(__d); |
| 1369 | const year __y = __ymd.year(); |
| 1370 | struct tm __tm{}; |
| 1371 | __tm.tm_year = (int)__y - 1900; |
| 1372 | __tm.tm_yday = (__d - _TDays(__y/January/1)).count(); |
| 1373 | __tm.tm_wday = weekday(__d).c_encoding(); |
| 1374 | return _M_locale_fmt(std::move(__out), __loc, __tm, |
| 1375 | (char)__conv, 'O'); |
| 1376 | } |
| 1377 | |
| 1378 | _TDays __first; // First day of week 1. |
| 1379 | if (__conv == 'V') // W01 begins on Monday before first Thursday. |
| 1380 | { |
| 1381 | // Move to nearest Thursday: |
| 1382 | __d -= (weekday(__d) - Monday) - days(3); |
| 1383 | // ISO week of __t is number of weeks since January 1 of the |
| 1384 | // same year as that nearest Thursday. |
| 1385 | __first = _TDays(year_month_day(__d).year()/January/1); |
| 1386 | } |
| 1387 | else |
| 1388 | { |
| 1389 | year __y; |
| 1390 | if constexpr (requires { __t.year(); }) |
| 1391 | __y = __t.year(); |
| 1392 | else |
| 1393 | __y = year_month_day(__d).year(); |
| 1394 | const weekday __weekstart = __conv == 'U' ? Sunday : Monday; |
| 1395 | __first = _TDays(__y/January/__weekstart[1]); |
| 1396 | } |
| 1397 | auto __weeks = chrono::floor<weeks>(__d - __first); |
| 1398 | __string_view __sv = _S_two_digits(n: __weeks.count() + 1); |
| 1399 | return __format::__write(std::move(__out), __sv); |
| 1400 | } |
| 1401 | |
| 1402 | template<typename _Tp, typename _FormatContext> |
| 1403 | typename _FormatContext::iterator |
| 1404 | _M_x(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1405 | _FormatContext& __ctx, bool __mod = false) const |
| 1406 | { |
| 1407 | // %x Locale's date rep |
| 1408 | // %Ex Locale's alternative date representation. |
| 1409 | locale __loc = _M_locale(__ctx); |
| 1410 | const auto& __tp = use_facet<__timepunct<_CharT>>(__loc); |
| 1411 | const _CharT* __date_reps[2]; |
| 1412 | __tp._M_date_formats(__date_reps); |
| 1413 | const _CharT* __rep = __date_reps[__mod]; |
| 1414 | if (!*__rep) |
| 1415 | return _M_D(__t, std::move(__out), __ctx); |
| 1416 | |
| 1417 | basic_string<_CharT> __fmt(_S_empty_spec); |
| 1418 | __fmt.insert(1u, 1u, _S_colon); |
| 1419 | __fmt.insert(2u, __rep); |
| 1420 | using _FmtStr = _Runtime_format_string<_CharT>; |
| 1421 | return _M_write(std::move(__out), __loc, |
| 1422 | std::format(__loc, _FmtStr(__fmt), __t)); |
| 1423 | } |
| 1424 | |
| 1425 | template<typename _Tp, typename _FormatContext> |
| 1426 | typename _FormatContext::iterator |
| 1427 | _M_X(const _Tp& __tt, typename _FormatContext::iterator __out, |
| 1428 | _FormatContext& __ctx, bool __mod = false) const |
| 1429 | { |
| 1430 | // %X Locale's time rep |
| 1431 | // %EX Locale's alternative time representation. |
| 1432 | auto __t = _S_floor_seconds(__tt); |
| 1433 | locale __loc = _M_locale(__ctx); |
| 1434 | const auto& __tp = use_facet<__timepunct<_CharT>>(__loc); |
| 1435 | const _CharT* __time_reps[2]; |
| 1436 | __tp._M_time_formats(__time_reps); |
| 1437 | const _CharT* __rep = __time_reps[__mod]; |
| 1438 | if (!*__rep) |
| 1439 | return _M_R_T(__t, std::move(__out), __ctx, true); |
| 1440 | |
| 1441 | basic_string<_CharT> __fmt(_S_empty_spec); |
| 1442 | __fmt.insert(1u, 1u, _S_colon); |
| 1443 | __fmt.insert(2u, __rep); |
| 1444 | using _FmtStr = _Runtime_format_string<_CharT>; |
| 1445 | return _M_write(std::move(__out), __loc, |
| 1446 | std::format(__loc, _FmtStr(__fmt), __t)); |
| 1447 | } |
| 1448 | |
| 1449 | template<typename _Tp, typename _FormatContext> |
| 1450 | typename _FormatContext::iterator |
| 1451 | _M_z(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1452 | _FormatContext&, bool __mod = false) const |
| 1453 | { |
| 1454 | using ::std::chrono::__detail::__utc_leap_second; |
| 1455 | using ::std::chrono::__detail::__local_time_fmt; |
| 1456 | |
| 1457 | auto __utc = __mod ? __string_view(_GLIBCXX_WIDEN("+00:00" ), 6) |
| 1458 | : __string_view(_GLIBCXX_WIDEN("+0000" ), 5); |
| 1459 | |
| 1460 | if constexpr (chrono::__is_time_point_v<_Tp>) |
| 1461 | { |
| 1462 | if constexpr (is_same_v<typename _Tp::clock, |
| 1463 | chrono::system_clock>) |
| 1464 | return __format::__write(std::move(__out), __utc); |
| 1465 | } |
| 1466 | else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>) |
| 1467 | { |
| 1468 | if (__t._M_offset_sec) |
| 1469 | { |
| 1470 | auto __sv = __utc; |
| 1471 | basic_string<_CharT> __s; |
| 1472 | if (*__t._M_offset_sec != 0s) |
| 1473 | { |
| 1474 | chrono:: hh_mm_ss __hms(*__t._M_offset_sec); |
| 1475 | __s = _S_plus_minus[__hms.is_negative()]; |
| 1476 | __s += _S_two_digits(n: __hms.hours().count()); |
| 1477 | if (__mod) |
| 1478 | __s += _S_colon; |
| 1479 | __s += _S_two_digits(n: __hms.minutes().count()); |
| 1480 | __sv = __s; |
| 1481 | } |
| 1482 | return __format::__write(std::move(__out), __sv); |
| 1483 | } |
| 1484 | } |
| 1485 | else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>) |
| 1486 | return __format::__write(std::move(__out), __utc); |
| 1487 | |
| 1488 | __no_timezone_available(); |
| 1489 | } |
| 1490 | |
| 1491 | template<typename _Tp, typename _FormatContext> |
| 1492 | typename _FormatContext::iterator |
| 1493 | _M_Z(const _Tp& __t, typename _FormatContext::iterator __out, |
| 1494 | _FormatContext& __ctx) const |
| 1495 | { |
| 1496 | using ::std::chrono::__detail::__utc_leap_second; |
| 1497 | using ::std::chrono::__detail::__local_time_fmt; |
| 1498 | |
| 1499 | __string_view __utc(_GLIBCXX_WIDEN("UTC" ), 3); |
| 1500 | if constexpr (chrono::__is_time_point_v<_Tp>) |
| 1501 | { |
| 1502 | if constexpr (is_same_v<typename _Tp::clock, |
| 1503 | chrono::system_clock>) |
| 1504 | return __format::__write(std::move(__out), __utc); |
| 1505 | } |
| 1506 | else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>) |
| 1507 | { |
| 1508 | if (__t._M_abbrev) |
| 1509 | { |
| 1510 | string_view __sv = *__t._M_abbrev; |
| 1511 | if constexpr (is_same_v<_CharT, char>) |
| 1512 | return __format::__write(std::move(__out), __sv); |
| 1513 | else |
| 1514 | { |
| 1515 | // TODO use resize_and_overwrite |
| 1516 | basic_string<_CharT> __ws(__sv.size(), _CharT()); |
| 1517 | auto& __ct = use_facet<ctype<_CharT>>(_M_locale(__ctx)); |
| 1518 | __ct.widen(__sv.begin(), __sv.end(), __ws.data()); |
| 1519 | __string_view __wsv = __ws; |
| 1520 | return __format::__write(std::move(__out), __wsv); |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>) |
| 1525 | return __format::__write(std::move(__out), __utc); |
| 1526 | |
| 1527 | __no_timezone_available(); |
| 1528 | } |
| 1529 | |
| 1530 | // %% handled in _M_format |
| 1531 | |
| 1532 | // A single digit character in the range '0'..'9'. |
| 1533 | static _CharT |
| 1534 | _S_digit(int __n) noexcept |
| 1535 | { |
| 1536 | // Extra 9s avoid past-the-end read on bad input. |
| 1537 | return _GLIBCXX_WIDEN("0123456789999999" )[__n & 0xf]; |
| 1538 | } |
| 1539 | |
| 1540 | // A string view of two digit characters, "00".."99". |
| 1541 | static basic_string_view<_CharT> |
| 1542 | _S_two_digits(int __n) noexcept |
| 1543 | { |
| 1544 | return { |
| 1545 | _GLIBCXX_WIDEN("0001020304050607080910111213141516171819" |
| 1546 | "2021222324252627282930313233343536373839" |
| 1547 | "4041424344454647484950515253545556575859" |
| 1548 | "6061626364656667686970717273747576777879" |
| 1549 | "8081828384858687888990919293949596979899" |
| 1550 | "9999999999999999999999999999999999999999" |
| 1551 | "9999999999999999" ) + 2 * (__n & 0x7f), |
| 1552 | 2 |
| 1553 | }; |
| 1554 | } |
| 1555 | |
| 1556 | // Accessors for the components of chrono types: |
| 1557 | |
| 1558 | // Returns a hh_mm_ss. |
| 1559 | template<typename _Tp> |
| 1560 | static decltype(auto) |
| 1561 | _S_hms(const _Tp& __t) |
| 1562 | { |
| 1563 | using ::std::chrono::__detail::__utc_leap_second; |
| 1564 | using ::std::chrono::__detail::__local_time_fmt; |
| 1565 | |
| 1566 | if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>) |
| 1567 | return __t; |
| 1568 | else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>) |
| 1569 | return __t._M_time; |
| 1570 | else if constexpr (chrono::__is_duration_v<_Tp>) |
| 1571 | return chrono::hh_mm_ss<_Tp>(__t); |
| 1572 | else if constexpr (chrono::__is_time_point_v<_Tp>) |
| 1573 | return chrono::hh_mm_ss(__t - chrono::floor<chrono::days>(__t)); |
| 1574 | else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>) |
| 1575 | return _S_hms(__t._M_time); |
| 1576 | else |
| 1577 | { |
| 1578 | __invalid_chrono_spec(); |
| 1579 | return chrono::hh_mm_ss<chrono::seconds>(); |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | // Returns a sys_days or local_days. |
| 1584 | template<typename _Tp> |
| 1585 | static auto |
| 1586 | _S_days(const _Tp& __t) |
| 1587 | { |
| 1588 | using namespace chrono; |
| 1589 | using ::std::chrono::__detail::__utc_leap_second; |
| 1590 | using ::std::chrono::__detail::__local_time_fmt; |
| 1591 | |
| 1592 | if constexpr (__is_time_point_v<_Tp>) |
| 1593 | return chrono::floor<days>(__t); |
| 1594 | else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>) |
| 1595 | return __t._M_date; |
| 1596 | else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>) |
| 1597 | return chrono::floor<days>(__t._M_time); |
| 1598 | else if constexpr (is_same_v<_Tp, year_month_day> |
| 1599 | || is_same_v<_Tp, year_month_day_last> |
| 1600 | || is_same_v<_Tp, year_month_weekday> |
| 1601 | || is_same_v<_Tp, year_month_weekday_last>) |
| 1602 | return sys_days(__t); |
| 1603 | else |
| 1604 | { |
| 1605 | if constexpr (__is_duration_v<_Tp>) |
| 1606 | __not_valid_for_duration(); |
| 1607 | else |
| 1608 | __invalid_chrono_spec(); |
| 1609 | return chrono::sys_days(); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | // Returns a year_month_day. |
| 1614 | template<typename _Tp> |
| 1615 | static chrono::year_month_day |
| 1616 | _S_date(const _Tp& __t) |
| 1617 | { |
| 1618 | if constexpr (is_same_v<_Tp, chrono::year_month_day>) |
| 1619 | return __t; |
| 1620 | else |
| 1621 | return chrono::year_month_day(_S_days(__t)); |
| 1622 | } |
| 1623 | |
| 1624 | template<typename _Tp> |
| 1625 | static chrono::day |
| 1626 | _S_day(const _Tp& __t) |
| 1627 | { |
| 1628 | using namespace chrono; |
| 1629 | |
| 1630 | if constexpr (is_same_v<_Tp, day>) |
| 1631 | return __t; |
| 1632 | else if constexpr (requires { __t.day(); }) |
| 1633 | return __t.day(); |
| 1634 | else |
| 1635 | return _S_date(__t).day(); |
| 1636 | } |
| 1637 | |
| 1638 | template<typename _Tp> |
| 1639 | static chrono::month |
| 1640 | _S_month(const _Tp& __t) |
| 1641 | { |
| 1642 | using namespace chrono; |
| 1643 | |
| 1644 | if constexpr (is_same_v<_Tp, month>) |
| 1645 | return __t; |
| 1646 | else if constexpr (requires { __t.month(); }) |
| 1647 | return __t.month(); |
| 1648 | else |
| 1649 | return _S_date(__t).month(); |
| 1650 | } |
| 1651 | |
| 1652 | template<typename _Tp> |
| 1653 | static chrono::year |
| 1654 | _S_year(const _Tp& __t) |
| 1655 | { |
| 1656 | using namespace chrono; |
| 1657 | |
| 1658 | if constexpr (is_same_v<_Tp, year>) |
| 1659 | return __t; |
| 1660 | else if constexpr (requires { __t.year(); }) |
| 1661 | return __t.year(); |
| 1662 | else |
| 1663 | return _S_date(__t).year(); |
| 1664 | } |
| 1665 | |
| 1666 | template<typename _Tp> |
| 1667 | static chrono::weekday |
| 1668 | _S_weekday(const _Tp& __t) |
| 1669 | { |
| 1670 | using namespace ::std::chrono; |
| 1671 | using ::std::chrono::__detail::__local_time_fmt; |
| 1672 | |
| 1673 | if constexpr (is_same_v<_Tp, weekday>) |
| 1674 | return __t; |
| 1675 | else if constexpr (requires { __t.weekday(); }) |
| 1676 | return __t.weekday(); |
| 1677 | else if constexpr (is_same_v<_Tp, month_weekday>) |
| 1678 | return __t.weekday_indexed().weekday(); |
| 1679 | else if constexpr (is_same_v<_Tp, month_weekday_last>) |
| 1680 | return __t.weekday_last().weekday(); |
| 1681 | else |
| 1682 | return weekday(_S_days(__t)); |
| 1683 | } |
| 1684 | |
| 1685 | // Remove subsecond precision from a time_point. |
| 1686 | template<typename _Tp> |
| 1687 | static auto |
| 1688 | _S_floor_seconds(const _Tp& __t) |
| 1689 | { |
| 1690 | using chrono::__detail::__local_time_fmt; |
| 1691 | if constexpr (chrono::__is_time_point_v<_Tp> |
| 1692 | || chrono::__is_duration_v<_Tp>) |
| 1693 | { |
| 1694 | if constexpr (_Tp::period::den != 1) |
| 1695 | return chrono::floor<chrono::seconds>(__t); |
| 1696 | else |
| 1697 | return __t; |
| 1698 | } |
| 1699 | else if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>) |
| 1700 | { |
| 1701 | if constexpr (_Tp::fractional_width != 0) |
| 1702 | return chrono::floor<chrono::seconds>(__t.to_duration()); |
| 1703 | else |
| 1704 | return __t; |
| 1705 | } |
| 1706 | else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>) |
| 1707 | return _S_floor_seconds(__t._M_time); |
| 1708 | else |
| 1709 | return __t; |
| 1710 | } |
| 1711 | |
| 1712 | // Use the formatting locale's std::time_put facet to produce |
| 1713 | // a locale-specific representation. |
| 1714 | template<typename _Iter> |
| 1715 | _Iter |
| 1716 | _M_locale_fmt(_Iter __out, const locale& __loc, const struct tm& __tm, |
| 1717 | char __fmt, char __mod) const |
| 1718 | { |
| 1719 | basic_ostringstream<_CharT> __os; |
| 1720 | __os.imbue(__loc); |
| 1721 | const auto& __tp = use_facet<time_put<_CharT>>(__loc); |
| 1722 | __tp.put(__os, __os, _S_space, &__tm, __fmt, __mod); |
| 1723 | if (__os) |
| 1724 | __out = _M_write(std::move(__out), __loc, __os.view()); |
| 1725 | return __out; |
| 1726 | } |
| 1727 | }; |
| 1728 | |
| 1729 | } // namespace __format |
| 1730 | /// @endcond |
| 1731 | |
| 1732 | template<typename _Rep, typename _Period, typename _CharT> |
| 1733 | requires __format::__formattable_impl<_Rep, _CharT> |
| 1734 | struct formatter<chrono::duration<_Rep, _Period>, _CharT> |
| 1735 | { |
| 1736 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1737 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1738 | { |
| 1739 | using namespace __format; |
| 1740 | auto __it = _M_f._M_parse(__pc, _Duration|_TimeOfDay); |
| 1741 | if constexpr (!is_floating_point_v<_Rep>) |
| 1742 | if (_M_f._M_spec._M_prec_kind != __format::_WP_none) |
| 1743 | __throw_format_error(what: "format error: invalid precision for duration" ); |
| 1744 | return __it; |
| 1745 | } |
| 1746 | |
| 1747 | template<typename _Out> |
| 1748 | typename basic_format_context<_Out, _CharT>::iterator |
| 1749 | format(const chrono::duration<_Rep, _Period>& __d, |
| 1750 | basic_format_context<_Out, _CharT>& __fc) const |
| 1751 | { |
| 1752 | if constexpr (numeric_limits<_Rep>::is_signed) |
| 1753 | if (__d < __d.zero()) [[unlikely]] |
| 1754 | { |
| 1755 | if constexpr (is_integral_v<_Rep>) |
| 1756 | { |
| 1757 | // -d is undefined for the most negative integer. |
| 1758 | // Convert duration to corresponding unsigned rep. |
| 1759 | using _URep = make_unsigned_t<_Rep>; |
| 1760 | auto __ucnt = -static_cast<_URep>(__d.count()); |
| 1761 | auto __ud = chrono::duration<_URep, _Period>(__ucnt); |
| 1762 | return _M_f._M_format(__ud, __fc, true); |
| 1763 | } |
| 1764 | else |
| 1765 | return _M_f._M_format(-__d, __fc, true); |
| 1766 | } |
| 1767 | return _M_f._M_format(__d, __fc, false); |
| 1768 | } |
| 1769 | |
| 1770 | private: |
| 1771 | __format::__formatter_chrono<_CharT> _M_f; |
| 1772 | }; |
| 1773 | |
| 1774 | template<__format::__char _CharT> |
| 1775 | struct formatter<chrono::day, _CharT> |
| 1776 | { |
| 1777 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1778 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1779 | { return _M_f._M_parse(__pc, __format::_Day); } |
| 1780 | |
| 1781 | template<typename _Out> |
| 1782 | typename basic_format_context<_Out, _CharT>::iterator |
| 1783 | format(const chrono::day& __t, |
| 1784 | basic_format_context<_Out, _CharT>& __fc) const |
| 1785 | { return _M_f._M_format(__t, __fc); } |
| 1786 | |
| 1787 | private: |
| 1788 | __format::__formatter_chrono<_CharT> _M_f; |
| 1789 | }; |
| 1790 | |
| 1791 | template<__format::__char _CharT> |
| 1792 | struct formatter<chrono::month, _CharT> |
| 1793 | { |
| 1794 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1795 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1796 | { return _M_f._M_parse(__pc, __format::_Month); } |
| 1797 | |
| 1798 | template<typename _Out> |
| 1799 | typename basic_format_context<_Out, _CharT>::iterator |
| 1800 | format(const chrono::month& __t, |
| 1801 | basic_format_context<_Out, _CharT>& __fc) const |
| 1802 | { return _M_f._M_format(__t, __fc); } |
| 1803 | |
| 1804 | private: |
| 1805 | __format::__formatter_chrono<_CharT> _M_f; |
| 1806 | }; |
| 1807 | |
| 1808 | template<__format::__char _CharT> |
| 1809 | struct formatter<chrono::year, _CharT> |
| 1810 | { |
| 1811 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1812 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1813 | { return _M_f._M_parse(__pc, __format::_Year); } |
| 1814 | |
| 1815 | template<typename _Out> |
| 1816 | typename basic_format_context<_Out, _CharT>::iterator |
| 1817 | format(const chrono::year& __t, |
| 1818 | basic_format_context<_Out, _CharT>& __fc) const |
| 1819 | { return _M_f._M_format(__t, __fc); } |
| 1820 | |
| 1821 | private: |
| 1822 | __format::__formatter_chrono<_CharT> _M_f; |
| 1823 | }; |
| 1824 | |
| 1825 | template<__format::__char _CharT> |
| 1826 | struct formatter<chrono::weekday, _CharT> |
| 1827 | { |
| 1828 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1829 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1830 | { return _M_f._M_parse(__pc, __format::_Weekday); } |
| 1831 | |
| 1832 | template<typename _Out> |
| 1833 | typename basic_format_context<_Out, _CharT>::iterator |
| 1834 | format(const chrono::weekday& __t, |
| 1835 | basic_format_context<_Out, _CharT>& __fc) const |
| 1836 | { return _M_f._M_format(__t, __fc); } |
| 1837 | |
| 1838 | private: |
| 1839 | __format::__formatter_chrono<_CharT> _M_f; |
| 1840 | }; |
| 1841 | |
| 1842 | template<__format::__char _CharT> |
| 1843 | struct formatter<chrono::weekday_indexed, _CharT> |
| 1844 | { |
| 1845 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1846 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1847 | { return _M_f._M_parse(__pc, __format::_Weekday); } |
| 1848 | |
| 1849 | template<typename _Out> |
| 1850 | typename basic_format_context<_Out, _CharT>::iterator |
| 1851 | format(const chrono::weekday_indexed& __t, |
| 1852 | basic_format_context<_Out, _CharT>& __fc) const |
| 1853 | { return _M_f._M_format(__t, __fc); } |
| 1854 | |
| 1855 | private: |
| 1856 | __format::__formatter_chrono<_CharT> _M_f; |
| 1857 | }; |
| 1858 | |
| 1859 | template<__format::__char _CharT> |
| 1860 | struct formatter<chrono::weekday_last, _CharT> |
| 1861 | { |
| 1862 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1863 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1864 | { return _M_f._M_parse(__pc, __format::_Weekday); } |
| 1865 | |
| 1866 | template<typename _Out> |
| 1867 | typename basic_format_context<_Out, _CharT>::iterator |
| 1868 | format(const chrono::weekday_last& __t, |
| 1869 | basic_format_context<_Out, _CharT>& __fc) const |
| 1870 | { return _M_f._M_format(__t, __fc); } |
| 1871 | |
| 1872 | private: |
| 1873 | __format::__formatter_chrono<_CharT> _M_f; |
| 1874 | }; |
| 1875 | |
| 1876 | template<__format::__char _CharT> |
| 1877 | struct formatter<chrono::month_day, _CharT> |
| 1878 | { |
| 1879 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1880 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1881 | { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); } |
| 1882 | |
| 1883 | template<typename _Out> |
| 1884 | typename basic_format_context<_Out, _CharT>::iterator |
| 1885 | format(const chrono::month_day& __t, |
| 1886 | basic_format_context<_Out, _CharT>& __fc) const |
| 1887 | { return _M_f._M_format(__t, __fc); } |
| 1888 | |
| 1889 | private: |
| 1890 | __format::__formatter_chrono<_CharT> _M_f; |
| 1891 | }; |
| 1892 | |
| 1893 | template<__format::__char _CharT> |
| 1894 | struct formatter<chrono::month_day_last, _CharT> |
| 1895 | { |
| 1896 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1897 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1898 | { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); } |
| 1899 | |
| 1900 | template<typename _Out> |
| 1901 | typename basic_format_context<_Out, _CharT>::iterator |
| 1902 | format(const chrono::month_day_last& __t, |
| 1903 | basic_format_context<_Out, _CharT>& __fc) const |
| 1904 | { return _M_f._M_format(__t, __fc); } |
| 1905 | |
| 1906 | private: |
| 1907 | __format::__formatter_chrono<_CharT> _M_f; |
| 1908 | }; |
| 1909 | |
| 1910 | template<__format::__char _CharT> |
| 1911 | struct formatter<chrono::month_weekday, _CharT> |
| 1912 | { |
| 1913 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1914 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1915 | { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); } |
| 1916 | |
| 1917 | template<typename _Out> |
| 1918 | typename basic_format_context<_Out, _CharT>::iterator |
| 1919 | format(const chrono::month_weekday& __t, |
| 1920 | basic_format_context<_Out, _CharT>& __fc) const |
| 1921 | { return _M_f._M_format(__t, __fc); } |
| 1922 | |
| 1923 | private: |
| 1924 | __format::__formatter_chrono<_CharT> _M_f; |
| 1925 | }; |
| 1926 | |
| 1927 | template<__format::__char _CharT> |
| 1928 | struct formatter<chrono::month_weekday_last, _CharT> |
| 1929 | { |
| 1930 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1931 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1932 | { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); } |
| 1933 | |
| 1934 | template<typename _Out> |
| 1935 | typename basic_format_context<_Out, _CharT>::iterator |
| 1936 | format(const chrono::month_weekday_last& __t, |
| 1937 | basic_format_context<_Out, _CharT>& __fc) const |
| 1938 | { return _M_f._M_format(__t, __fc); } |
| 1939 | |
| 1940 | private: |
| 1941 | __format::__formatter_chrono<_CharT> _M_f; |
| 1942 | }; |
| 1943 | |
| 1944 | template<__format::__char _CharT> |
| 1945 | struct formatter<chrono::year_month, _CharT> |
| 1946 | { |
| 1947 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1948 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1949 | { return _M_f._M_parse(__pc, __format::_Year|__format::_Month); } |
| 1950 | |
| 1951 | template<typename _Out> |
| 1952 | typename basic_format_context<_Out, _CharT>::iterator |
| 1953 | format(const chrono::year_month& __t, |
| 1954 | basic_format_context<_Out, _CharT>& __fc) const |
| 1955 | { return _M_f._M_format(__t, __fc); } |
| 1956 | |
| 1957 | private: |
| 1958 | __format::__formatter_chrono<_CharT> _M_f; |
| 1959 | }; |
| 1960 | |
| 1961 | template<__format::__char _CharT> |
| 1962 | struct formatter<chrono::year_month_day, _CharT> |
| 1963 | { |
| 1964 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1965 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1966 | { return _M_f._M_parse(__pc, __format::_Date); } |
| 1967 | |
| 1968 | template<typename _Out> |
| 1969 | typename basic_format_context<_Out, _CharT>::iterator |
| 1970 | format(const chrono::year_month_day& __t, |
| 1971 | basic_format_context<_Out, _CharT>& __fc) const |
| 1972 | { return _M_f._M_format(__t, __fc); } |
| 1973 | |
| 1974 | private: |
| 1975 | __format::__formatter_chrono<_CharT> _M_f; |
| 1976 | }; |
| 1977 | |
| 1978 | template<__format::__char _CharT> |
| 1979 | struct formatter<chrono::year_month_day_last, _CharT> |
| 1980 | { |
| 1981 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1982 | parse(basic_format_parse_context<_CharT>& __pc) |
| 1983 | { return _M_f._M_parse(__pc, __format::_Date); } |
| 1984 | |
| 1985 | template<typename _Out> |
| 1986 | typename basic_format_context<_Out, _CharT>::iterator |
| 1987 | format(const chrono::year_month_day_last& __t, |
| 1988 | basic_format_context<_Out, _CharT>& __fc) const |
| 1989 | { return _M_f._M_format(__t, __fc); } |
| 1990 | |
| 1991 | private: |
| 1992 | __format::__formatter_chrono<_CharT> _M_f; |
| 1993 | }; |
| 1994 | |
| 1995 | template<__format::__char _CharT> |
| 1996 | struct formatter<chrono::year_month_weekday, _CharT> |
| 1997 | { |
| 1998 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 1999 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2000 | { return _M_f._M_parse(__pc, __format::_Date); } |
| 2001 | |
| 2002 | template<typename _Out> |
| 2003 | typename basic_format_context<_Out, _CharT>::iterator |
| 2004 | format(const chrono::year_month_weekday& __t, |
| 2005 | basic_format_context<_Out, _CharT>& __fc) const |
| 2006 | { return _M_f._M_format(__t, __fc); } |
| 2007 | |
| 2008 | private: |
| 2009 | __format::__formatter_chrono<_CharT> _M_f; |
| 2010 | }; |
| 2011 | |
| 2012 | template<__format::__char _CharT> |
| 2013 | struct formatter<chrono::year_month_weekday_last, _CharT> |
| 2014 | { |
| 2015 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2016 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2017 | { return _M_f._M_parse(__pc, __format::_Date); } |
| 2018 | |
| 2019 | template<typename _Out> |
| 2020 | typename basic_format_context<_Out, _CharT>::iterator |
| 2021 | format(const chrono::year_month_weekday_last& __t, |
| 2022 | basic_format_context<_Out, _CharT>& __fc) const |
| 2023 | { return _M_f._M_format(__t, __fc); } |
| 2024 | |
| 2025 | private: |
| 2026 | __format::__formatter_chrono<_CharT> _M_f; |
| 2027 | }; |
| 2028 | |
| 2029 | template<typename _Rep, typename _Period, __format::__char _CharT> |
| 2030 | struct formatter<chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>, _CharT> |
| 2031 | { |
| 2032 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2033 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2034 | { return _M_f._M_parse(__pc, __format::_TimeOfDay); } |
| 2035 | |
| 2036 | template<typename _Out> |
| 2037 | typename basic_format_context<_Out, _CharT>::iterator |
| 2038 | format(const chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>& __t, |
| 2039 | basic_format_context<_Out, _CharT>& __fc) const |
| 2040 | { return _M_f._M_format(__t, __fc); } |
| 2041 | |
| 2042 | private: |
| 2043 | __format::__formatter_chrono<_CharT> _M_f; |
| 2044 | }; |
| 2045 | |
| 2046 | #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI |
| 2047 | template<__format::__char _CharT> |
| 2048 | struct formatter<chrono::sys_info, _CharT> |
| 2049 | { |
| 2050 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2051 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2052 | { return _M_f._M_parse(__pc, __format::_ChronoParts{}); } |
| 2053 | |
| 2054 | template<typename _Out> |
| 2055 | typename basic_format_context<_Out, _CharT>::iterator |
| 2056 | format(const chrono::sys_info& __i, |
| 2057 | basic_format_context<_Out, _CharT>& __fc) const |
| 2058 | { return _M_f._M_format(__i, __fc); } |
| 2059 | |
| 2060 | private: |
| 2061 | __format::__formatter_chrono<_CharT> _M_f; |
| 2062 | }; |
| 2063 | |
| 2064 | template<__format::__char _CharT> |
| 2065 | struct formatter<chrono::local_info, _CharT> |
| 2066 | { |
| 2067 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2068 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2069 | { return _M_f._M_parse(__pc, __format::_ChronoParts{}); } |
| 2070 | |
| 2071 | template<typename _Out> |
| 2072 | typename basic_format_context<_Out, _CharT>::iterator |
| 2073 | format(const chrono::local_info& __i, |
| 2074 | basic_format_context<_Out, _CharT>& __fc) const |
| 2075 | { return _M_f._M_format(__i, __fc); } |
| 2076 | |
| 2077 | private: |
| 2078 | __format::__formatter_chrono<_CharT> _M_f; |
| 2079 | }; |
| 2080 | #endif |
| 2081 | |
| 2082 | template<typename _Duration, __format::__char _CharT> |
| 2083 | struct formatter<chrono::sys_time<_Duration>, _CharT> |
| 2084 | { |
| 2085 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2086 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2087 | { |
| 2088 | auto __next = _M_f._M_parse(__pc, __format::_ZonedDateTime); |
| 2089 | if constexpr (!__stream_insertable) |
| 2090 | if (_M_f._M_spec._M_chrono_specs.empty()) |
| 2091 | __format::__invalid_chrono_spec(); // chrono-specs can't be empty |
| 2092 | return __next; |
| 2093 | } |
| 2094 | |
| 2095 | template<typename _Out> |
| 2096 | typename basic_format_context<_Out, _CharT>::iterator |
| 2097 | format(const chrono::sys_time<_Duration>& __t, |
| 2098 | basic_format_context<_Out, _CharT>& __fc) const |
| 2099 | { return _M_f._M_format(__t, __fc); } |
| 2100 | |
| 2101 | private: |
| 2102 | static constexpr bool __stream_insertable |
| 2103 | = requires (basic_ostream<_CharT>& __os, |
| 2104 | chrono::sys_time<_Duration> __t) { __os << __t; }; |
| 2105 | |
| 2106 | __format::__formatter_chrono<_CharT> _M_f; |
| 2107 | }; |
| 2108 | |
| 2109 | template<typename _Duration, __format::__char _CharT> |
| 2110 | struct formatter<chrono::utc_time<_Duration>, _CharT> |
| 2111 | : __format::__formatter_chrono<_CharT> |
| 2112 | { |
| 2113 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2114 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2115 | { return _M_f._M_parse(__pc, __format::_ZonedDateTime); } |
| 2116 | |
| 2117 | template<typename _Out> |
| 2118 | typename basic_format_context<_Out, _CharT>::iterator |
| 2119 | format(const chrono::utc_time<_Duration>& __t, |
| 2120 | basic_format_context<_Out, _CharT>& __fc) const |
| 2121 | { |
| 2122 | // Adjust by removing leap seconds to get equivalent sys_time. |
| 2123 | // We can't just use clock_cast because we want to know if the time |
| 2124 | // falls within a leap second insertion, and format seconds as "60". |
| 2125 | using chrono::__detail::__utc_leap_second; |
| 2126 | using chrono::seconds; |
| 2127 | using chrono::sys_time; |
| 2128 | using _CDur = common_type_t<_Duration, seconds>; |
| 2129 | const auto __li = chrono::get_leap_second_info(__t); |
| 2130 | sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed}; |
| 2131 | if (!__li.is_leap_second) [[likely]] |
| 2132 | return _M_f._M_format(__s, __fc); |
| 2133 | else |
| 2134 | return _M_f._M_format(__utc_leap_second(__s), __fc); |
| 2135 | } |
| 2136 | |
| 2137 | private: |
| 2138 | friend formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>; |
| 2139 | |
| 2140 | __format::__formatter_chrono<_CharT> _M_f; |
| 2141 | }; |
| 2142 | |
| 2143 | template<typename _Duration, __format::__char _CharT> |
| 2144 | struct formatter<chrono::tai_time<_Duration>, _CharT> |
| 2145 | : __format::__formatter_chrono<_CharT> |
| 2146 | { |
| 2147 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2148 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2149 | { return _M_f._M_parse(__pc, __format::_ZonedDateTime); } |
| 2150 | |
| 2151 | template<typename _Out> |
| 2152 | typename basic_format_context<_Out, _CharT>::iterator |
| 2153 | format(const chrono::tai_time<_Duration>& __t, |
| 2154 | basic_format_context<_Out, _CharT>& __fc) const |
| 2155 | { |
| 2156 | // Convert to __local_time_fmt with abbrev "TAI" and offset 0s. |
| 2157 | // We use __local_time_fmt and not sys_time (as the standard implies) |
| 2158 | // because %Z for sys_time would print "UTC" and we want "TAI" here. |
| 2159 | |
| 2160 | // Offset is 1970y/January/1 - 1958y/January/1 |
| 2161 | constexpr chrono::days __tai_offset = chrono::days(4383); |
| 2162 | using _CDur = common_type_t<_Duration, chrono::days>; |
| 2163 | chrono::local_time<_CDur> __lt(__t.time_since_epoch() - __tai_offset); |
| 2164 | const string __abbrev("TAI" , 3); |
| 2165 | const chrono::seconds __off = 0s; |
| 2166 | const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off); |
| 2167 | return _M_f._M_format(__lf, __fc); |
| 2168 | } |
| 2169 | |
| 2170 | private: |
| 2171 | __format::__formatter_chrono<_CharT> _M_f; |
| 2172 | }; |
| 2173 | |
| 2174 | template<typename _Duration, __format::__char _CharT> |
| 2175 | struct formatter<chrono::gps_time<_Duration>, _CharT> |
| 2176 | : __format::__formatter_chrono<_CharT> |
| 2177 | { |
| 2178 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2179 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2180 | { return _M_f._M_parse(__pc, __format::_ZonedDateTime); } |
| 2181 | |
| 2182 | template<typename _Out> |
| 2183 | typename basic_format_context<_Out, _CharT>::iterator |
| 2184 | format(const chrono::gps_time<_Duration>& __t, |
| 2185 | basic_format_context<_Out, _CharT>& __fc) const |
| 2186 | { |
| 2187 | // Convert to __local_time_fmt with abbrev "GPS" and offset 0s. |
| 2188 | // We use __local_time_fmt and not sys_time (as the standard implies) |
| 2189 | // because %Z for sys_time would print "UTC" and we want "GPS" here. |
| 2190 | |
| 2191 | // Offset is 1980y/January/Sunday[1] - 1970y/January/1 |
| 2192 | constexpr chrono::days __gps_offset = chrono::days(3657); |
| 2193 | using _CDur = common_type_t<_Duration, chrono::days>; |
| 2194 | chrono::local_time<_CDur> __lt(__t.time_since_epoch() + __gps_offset); |
| 2195 | const string __abbrev("GPS" , 3); |
| 2196 | const chrono::seconds __off = 0s; |
| 2197 | const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off); |
| 2198 | return _M_f._M_format(__lf, __fc); |
| 2199 | } |
| 2200 | |
| 2201 | private: |
| 2202 | __format::__formatter_chrono<_CharT> _M_f; |
| 2203 | }; |
| 2204 | |
| 2205 | template<typename _Duration, __format::__char _CharT> |
| 2206 | struct formatter<chrono::file_time<_Duration>, _CharT> |
| 2207 | { |
| 2208 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2209 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2210 | { return _M_f._M_parse(__pc, __format::_ZonedDateTime); } |
| 2211 | |
| 2212 | template<typename _Out> |
| 2213 | typename basic_format_context<_Out, _CharT>::iterator |
| 2214 | format(const chrono::file_time<_Duration>& __t, |
| 2215 | basic_format_context<_Out, _CharT>& __fc) const |
| 2216 | { |
| 2217 | using namespace chrono; |
| 2218 | return _M_f._M_format(chrono::clock_cast<system_clock>(__t), __fc); |
| 2219 | } |
| 2220 | |
| 2221 | private: |
| 2222 | __format::__formatter_chrono<_CharT> _M_f; |
| 2223 | }; |
| 2224 | |
| 2225 | template<typename _Duration, __format::__char _CharT> |
| 2226 | struct formatter<chrono::local_time<_Duration>, _CharT> |
| 2227 | { |
| 2228 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2229 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2230 | { return _M_f._M_parse(__pc, __format::_DateTime); } |
| 2231 | |
| 2232 | template<typename _Out> |
| 2233 | typename basic_format_context<_Out, _CharT>::iterator |
| 2234 | format(const chrono::local_time<_Duration>& __t, |
| 2235 | basic_format_context<_Out, _CharT>& __fc) const |
| 2236 | { return _M_f._M_format(__t, __fc); } |
| 2237 | |
| 2238 | private: |
| 2239 | __format::__formatter_chrono<_CharT> _M_f; |
| 2240 | }; |
| 2241 | |
| 2242 | template<typename _Duration, __format::__char _CharT> |
| 2243 | struct formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT> |
| 2244 | { |
| 2245 | constexpr typename basic_format_parse_context<_CharT>::iterator |
| 2246 | parse(basic_format_parse_context<_CharT>& __pc) |
| 2247 | { return _M_f._M_parse(__pc, __format::_ZonedDateTime); } |
| 2248 | |
| 2249 | template<typename _Out> |
| 2250 | typename basic_format_context<_Out, _CharT>::iterator |
| 2251 | format(const chrono::__detail::__local_time_fmt<_Duration>& __t, |
| 2252 | basic_format_context<_Out, _CharT>& __fc) const |
| 2253 | { return _M_f._M_format(__t, __fc, /* use %Z for {} */ true); } |
| 2254 | |
| 2255 | private: |
| 2256 | __format::__formatter_chrono<_CharT> _M_f; |
| 2257 | }; |
| 2258 | |
| 2259 | #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI |
| 2260 | template<typename _Duration, typename _TimeZonePtr, __format::__char _CharT> |
| 2261 | struct formatter<chrono::zoned_time<_Duration, _TimeZonePtr>, _CharT> |
| 2262 | : formatter<chrono::__detail::__local_time_fmt_for<_Duration>, _CharT> |
| 2263 | { |
| 2264 | template<typename _Out> |
| 2265 | typename basic_format_context<_Out, _CharT>::iterator |
| 2266 | format(const chrono::zoned_time<_Duration, _TimeZonePtr>& __tp, |
| 2267 | basic_format_context<_Out, _CharT>& __fc) const |
| 2268 | { |
| 2269 | using _Ltf = chrono::__detail::__local_time_fmt_for<_Duration>; |
| 2270 | using _Base = formatter<_Ltf, _CharT>; |
| 2271 | const chrono::sys_info __info = __tp.get_info(); |
| 2272 | const auto __lf = chrono::local_time_format(__tp.get_local_time(), |
| 2273 | &__info.abbrev, |
| 2274 | &__info.offset); |
| 2275 | return _Base::format(__lf, __fc); |
| 2276 | } |
| 2277 | }; |
| 2278 | #endif |
| 2279 | |
| 2280 | // Partial specialization needed for %c formatting of __utc_leap_second. |
| 2281 | template<typename _Duration, __format::__char _CharT> |
| 2282 | struct formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT> |
| 2283 | : formatter<chrono::utc_time<_Duration>, _CharT> |
| 2284 | { |
| 2285 | template<typename _Out> |
| 2286 | typename basic_format_context<_Out, _CharT>::iterator |
| 2287 | format(const chrono::__detail::__utc_leap_second<_Duration>& __t, |
| 2288 | basic_format_context<_Out, _CharT>& __fc) const |
| 2289 | { return this->_M_f._M_format(__t, __fc); } |
| 2290 | }; |
| 2291 | |
| 2292 | namespace chrono |
| 2293 | { |
| 2294 | /// @addtogroup chrono |
| 2295 | /// @{ |
| 2296 | |
| 2297 | /// @cond undocumented |
| 2298 | namespace __detail |
| 2299 | { |
| 2300 | template<typename _Duration = seconds> |
| 2301 | struct _Parser |
| 2302 | { |
| 2303 | static_assert(is_same_v<common_type_t<_Duration, seconds>, _Duration>); |
| 2304 | |
| 2305 | explicit |
| 2306 | _Parser(__format::_ChronoParts __need) : _M_need(__need) { } |
| 2307 | |
| 2308 | _Parser(_Parser&&) = delete; |
| 2309 | void operator=(_Parser&&) = delete; |
| 2310 | |
| 2311 | _Duration _M_time{}; // since midnight |
| 2312 | sys_days _M_sys_days{}; |
| 2313 | year_month_day _M_ymd{}; |
| 2314 | weekday _M_wd{}; |
| 2315 | __format::_ChronoParts _M_need; |
| 2316 | unsigned _M_is_leap_second : 1 {}; |
| 2317 | unsigned _M_reserved : 15 {}; |
| 2318 | |
| 2319 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 2320 | basic_istream<_CharT, _Traits>& |
| 2321 | operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2322 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2323 | minutes* __offset = nullptr); |
| 2324 | |
| 2325 | private: |
| 2326 | // Read an unsigned integer from the stream and return it. |
| 2327 | // Extract no more than __n digits. Set failbit if an integer isn't read. |
| 2328 | template<typename _CharT, typename _Traits> |
| 2329 | static int_least32_t |
| 2330 | _S_read_unsigned(basic_istream<_CharT, _Traits>& __is, |
| 2331 | ios_base::iostate& __err, int __n) |
| 2332 | { |
| 2333 | int_least32_t __val = _S_try_read_digit(__is, __err); |
| 2334 | if (__val == -1) [[unlikely]] |
| 2335 | __err |= ios_base::failbit; |
| 2336 | else |
| 2337 | { |
| 2338 | int __n1 = (std::min)(a: __n, b: 9); |
| 2339 | // Cannot overflow __val unless we read more than 9 digits |
| 2340 | for (int __i = 1; __i < __n1; ++__i) |
| 2341 | if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1) |
| 2342 | { |
| 2343 | __val *= 10; |
| 2344 | __val += __dig; |
| 2345 | } |
| 2346 | |
| 2347 | while (__n1++ < __n) [[unlikely]] |
| 2348 | if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1) |
| 2349 | { |
| 2350 | if (__builtin_mul_overflow(__val, 10, &__val) |
| 2351 | || __builtin_add_overflow(__val, __dig, &__val)) |
| 2352 | { |
| 2353 | __err |= ios_base::failbit; |
| 2354 | return -1; |
| 2355 | } |
| 2356 | } |
| 2357 | } |
| 2358 | return __val; |
| 2359 | } |
| 2360 | |
| 2361 | // Read an unsigned integer from the stream and return it. |
| 2362 | // Extract no more than __n digits. Set failbit if an integer isn't read. |
| 2363 | template<typename _CharT, typename _Traits> |
| 2364 | static int_least32_t |
| 2365 | _S_read_signed(basic_istream<_CharT, _Traits>& __is, |
| 2366 | ios_base::iostate& __err, int __n) |
| 2367 | { |
| 2368 | auto __sign = __is.peek(); |
| 2369 | if (__sign == '-' || __sign == '+') |
| 2370 | (void) __is.get(); |
| 2371 | int_least32_t __val = _S_read_unsigned(__is, __err, __n); |
| 2372 | if (__err & ios_base::failbit) |
| 2373 | { |
| 2374 | if (__sign == '-') [[unlikely]] |
| 2375 | __val *= -1; |
| 2376 | } |
| 2377 | return __val; |
| 2378 | } |
| 2379 | |
| 2380 | // Read a digit from the stream and return it, or return -1. |
| 2381 | // If no digit is read eofbit will be set (but not failbit). |
| 2382 | template<typename _CharT, typename _Traits> |
| 2383 | static int_least32_t |
| 2384 | _S_try_read_digit(basic_istream<_CharT, _Traits>& __is, |
| 2385 | ios_base::iostate& __err) |
| 2386 | { |
| 2387 | int_least32_t __val = -1; |
| 2388 | auto __i = __is.peek(); |
| 2389 | if (!_Traits::eq_int_type(__i, _Traits::eof())) [[likely]] |
| 2390 | { |
| 2391 | _CharT __c = _Traits::to_char_type(__i); |
| 2392 | if (_CharT('0') <= __c && __c <= _CharT('9')) [[likely]] |
| 2393 | { |
| 2394 | (void) __is.get(); |
| 2395 | __val = __c - _CharT('0'); |
| 2396 | } |
| 2397 | } |
| 2398 | else |
| 2399 | __err |= ios_base::eofbit; |
| 2400 | return __val; |
| 2401 | } |
| 2402 | |
| 2403 | // Read the specified character and return true. |
| 2404 | // If the character is not found, set failbit and return false. |
| 2405 | template<typename _CharT, typename _Traits> |
| 2406 | static bool |
| 2407 | _S_read_chr(basic_istream<_CharT, _Traits>& __is, |
| 2408 | ios_base::iostate& __err, _CharT __c) |
| 2409 | { |
| 2410 | auto __i = __is.peek(); |
| 2411 | if (_Traits::eq_int_type(__i, _Traits::eof())) |
| 2412 | __err |= ios_base::eofbit; |
| 2413 | else if (_Traits::to_char_type(__i) == __c) [[likely]] |
| 2414 | { |
| 2415 | (void) __is.get(); |
| 2416 | return true; |
| 2417 | } |
| 2418 | __err |= ios_base::failbit; |
| 2419 | return false; |
| 2420 | } |
| 2421 | }; |
| 2422 | |
| 2423 | template<typename _Duration> |
| 2424 | using _Parser_t = _Parser<common_type_t<_Duration, seconds>>; |
| 2425 | |
| 2426 | template<typename _Duration> |
| 2427 | consteval bool |
| 2428 | __use_floor() |
| 2429 | { |
| 2430 | if constexpr (_Duration::period::den == 1) |
| 2431 | { |
| 2432 | switch (_Duration::period::num) |
| 2433 | { |
| 2434 | case minutes::period::num: |
| 2435 | case hours::period::num: |
| 2436 | case days::period::num: |
| 2437 | case weeks::period::num: |
| 2438 | case years::period::num: |
| 2439 | return true; |
| 2440 | } |
| 2441 | } |
| 2442 | return false; |
| 2443 | } |
| 2444 | |
| 2445 | // A "do the right thing" rounding function for duration and time_point |
| 2446 | // values extracted by from_stream. When treat_as_floating_point is true |
| 2447 | // we don't want to do anything, just a straightforward conversion. |
| 2448 | // When the destination type has a period of minutes, hours, days, weeks, |
| 2449 | // or years, we use chrono::floor to truncate towards negative infinity. |
| 2450 | // This ensures that an extracted timestamp such as 2024-09-05 13:00:00 |
| 2451 | // will produce 2024-09-05 when rounded to days, rather than rounding up |
| 2452 | // to 2024-09-06 (a different day). |
| 2453 | // Otherwise, use chrono::round to get the nearest value representable |
| 2454 | // in the destination type. |
| 2455 | template<typename _ToDur, typename _Tp> |
| 2456 | constexpr auto |
| 2457 | __round(const _Tp& __t) |
| 2458 | { |
| 2459 | if constexpr (__is_duration_v<_Tp>) |
| 2460 | { |
| 2461 | if constexpr (treat_as_floating_point_v<typename _Tp::rep>) |
| 2462 | return chrono::duration_cast<_ToDur>(__t); |
| 2463 | else if constexpr (__detail::__use_floor<_ToDur>()) |
| 2464 | return chrono::floor<_ToDur>(__t); |
| 2465 | else |
| 2466 | return chrono::round<_ToDur>(__t); |
| 2467 | } |
| 2468 | else |
| 2469 | { |
| 2470 | static_assert(__is_time_point_v<_Tp>); |
| 2471 | using _Tpt = time_point<typename _Tp::clock, _ToDur>; |
| 2472 | return _Tpt(__detail::__round<_ToDur>(__t.time_since_epoch())); |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | } // namespace __detail |
| 2477 | /// @endcond |
| 2478 | |
| 2479 | template<typename _CharT, typename _Traits, typename _Rep, typename _Period, |
| 2480 | typename _Alloc = allocator<_CharT>> |
| 2481 | inline basic_istream<_CharT, _Traits>& |
| 2482 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2483 | duration<_Rep, _Period>& __d, |
| 2484 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2485 | minutes* __offset = nullptr) |
| 2486 | { |
| 2487 | auto __need = __format::_ChronoParts::_TimeOfDay; |
| 2488 | __detail::_Parser_t<duration<_Rep, _Period>> __p(__need); |
| 2489 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2490 | __d = __detail::__round<duration<_Rep, _Period>>(__p._M_time); |
| 2491 | return __is; |
| 2492 | } |
| 2493 | |
| 2494 | template<typename _CharT, typename _Traits> |
| 2495 | inline basic_ostream<_CharT, _Traits>& |
| 2496 | operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) |
| 2497 | { |
| 2498 | using _Ctx = __format::__format_context<_CharT>; |
| 2499 | using _Str = basic_string_view<_CharT>; |
| 2500 | _Str __s = _GLIBCXX_WIDEN("{:02d} is not a valid day" ); |
| 2501 | if (__d.ok()) |
| 2502 | __s = __s.substr(0, 6); |
| 2503 | auto __u = (unsigned)__d; |
| 2504 | __os << std::vformat(__s, make_format_args<_Ctx>(__u)); |
| 2505 | return __os; |
| 2506 | } |
| 2507 | |
| 2508 | template<typename _CharT, typename _Traits, |
| 2509 | typename _Alloc = allocator<_CharT>> |
| 2510 | inline basic_istream<_CharT, _Traits>& |
| 2511 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2512 | day& __d, |
| 2513 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2514 | minutes* __offset = nullptr) |
| 2515 | { |
| 2516 | __detail::_Parser<> __p(__format::_ChronoParts::_Day); |
| 2517 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2518 | __d = __p._M_ymd.day(); |
| 2519 | return __is; |
| 2520 | } |
| 2521 | |
| 2522 | template<typename _CharT, typename _Traits> |
| 2523 | inline basic_ostream<_CharT, _Traits>& |
| 2524 | operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) |
| 2525 | { |
| 2526 | using _Ctx = __format::__format_context<_CharT>; |
| 2527 | using _Str = basic_string_view<_CharT>; |
| 2528 | _Str __s = _GLIBCXX_WIDEN("{:L%b}{} is not a valid month" ); |
| 2529 | if (__m.ok()) |
| 2530 | __os << std::vformat(__os.getloc(), __s.substr(0, 6), |
| 2531 | make_format_args<_Ctx>(__m)); |
| 2532 | else |
| 2533 | { |
| 2534 | auto __u = (unsigned)__m; |
| 2535 | __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__u)); |
| 2536 | } |
| 2537 | return __os; |
| 2538 | } |
| 2539 | |
| 2540 | template<typename _CharT, typename _Traits, |
| 2541 | typename _Alloc = allocator<_CharT>> |
| 2542 | inline basic_istream<_CharT, _Traits>& |
| 2543 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2544 | month& __m, |
| 2545 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2546 | minutes* __offset = nullptr) |
| 2547 | { |
| 2548 | __detail::_Parser<> __p(__format::_ChronoParts::_Month); |
| 2549 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2550 | __m = __p._M_ymd.month(); |
| 2551 | return __is; |
| 2552 | } |
| 2553 | |
| 2554 | template<typename _CharT, typename _Traits> |
| 2555 | inline basic_ostream<_CharT, _Traits>& |
| 2556 | operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) |
| 2557 | { |
| 2558 | using _Ctx = __format::__format_context<_CharT>; |
| 2559 | using _Str = basic_string_view<_CharT>; |
| 2560 | _Str __s = _GLIBCXX_WIDEN("-{:04d} is not a valid year" ); |
| 2561 | if (__y.ok()) |
| 2562 | __s = __s.substr(0, 7); |
| 2563 | int __i = (int)__y; |
| 2564 | if (__i >= 0) [[likely]] |
| 2565 | __s.remove_prefix(1); |
| 2566 | else |
| 2567 | __i = -__i; |
| 2568 | __os << std::vformat(__s, make_format_args<_Ctx>(__i)); |
| 2569 | return __os; |
| 2570 | } |
| 2571 | |
| 2572 | template<typename _CharT, typename _Traits, |
| 2573 | typename _Alloc = allocator<_CharT>> |
| 2574 | inline basic_istream<_CharT, _Traits>& |
| 2575 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2576 | year& __y, |
| 2577 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2578 | minutes* __offset = nullptr) |
| 2579 | { |
| 2580 | __detail::_Parser<> __p(__format::_ChronoParts::_Year); |
| 2581 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2582 | __y = __p._M_ymd.year(); |
| 2583 | return __is; |
| 2584 | } |
| 2585 | |
| 2586 | template<typename _CharT, typename _Traits> |
| 2587 | inline basic_ostream<_CharT, _Traits>& |
| 2588 | operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd) |
| 2589 | { |
| 2590 | using _Ctx = __format::__format_context<_CharT>; |
| 2591 | using _Str = basic_string_view<_CharT>; |
| 2592 | _Str __s = _GLIBCXX_WIDEN("{:L%a}{} is not a valid weekday" ); |
| 2593 | if (__wd.ok()) |
| 2594 | __os << std::vformat(__os.getloc(), __s.substr(0, 6), |
| 2595 | make_format_args<_Ctx>(__wd)); |
| 2596 | else |
| 2597 | { |
| 2598 | auto __c = __wd.c_encoding(); |
| 2599 | __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__c)); |
| 2600 | } |
| 2601 | return __os; |
| 2602 | } |
| 2603 | |
| 2604 | template<typename _CharT, typename _Traits, |
| 2605 | typename _Alloc = allocator<_CharT>> |
| 2606 | inline basic_istream<_CharT, _Traits>& |
| 2607 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2608 | weekday& __wd, |
| 2609 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2610 | minutes* __offset = nullptr) |
| 2611 | { |
| 2612 | __detail::_Parser<> __p(__format::_ChronoParts::_Weekday); |
| 2613 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2614 | __wd = __p._M_wd; |
| 2615 | return __is; |
| 2616 | } |
| 2617 | |
| 2618 | template<typename _CharT, typename _Traits> |
| 2619 | inline basic_ostream<_CharT, _Traits>& |
| 2620 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2621 | const weekday_indexed& __wdi) |
| 2622 | { |
| 2623 | // The standard says to format wdi.weekday() and wdi.index() using |
| 2624 | // either "{:L}[{}]" or "{:L}[{} is not a valid index]". The {:L} spec |
| 2625 | // means to format the weekday using ostringstream, so just do that. |
| 2626 | basic_stringstream<_CharT> __os2; |
| 2627 | __os2.imbue(__os.getloc()); |
| 2628 | __os2 << __wdi.weekday(); |
| 2629 | const auto __i = __wdi.index(); |
| 2630 | basic_string_view<_CharT> __s |
| 2631 | = _GLIBCXX_WIDEN("[ is not a valid index]" ); |
| 2632 | __os2 << __s[0]; |
| 2633 | __os2 << std::format(_GLIBCXX_WIDEN("{}" ), __i); |
| 2634 | if (__i >= 1 && __i <= 5) |
| 2635 | __os2 << __s.back(); |
| 2636 | else |
| 2637 | __os2 << __s.substr(1); |
| 2638 | __os << __os2.view(); |
| 2639 | return __os; |
| 2640 | } |
| 2641 | |
| 2642 | template<typename _CharT, typename _Traits> |
| 2643 | inline basic_ostream<_CharT, _Traits>& |
| 2644 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2645 | const weekday_last& __wdl) |
| 2646 | { |
| 2647 | // As above, just write straight to a stringstream, as if by "{:L}[last]" |
| 2648 | basic_stringstream<_CharT> __os2; |
| 2649 | __os2.imbue(__os.getloc()); |
| 2650 | __os2 << __wdl.weekday() << _GLIBCXX_WIDEN("[last]" ); |
| 2651 | __os << __os2.view(); |
| 2652 | return __os; |
| 2653 | } |
| 2654 | |
| 2655 | template<typename _CharT, typename _Traits> |
| 2656 | inline basic_ostream<_CharT, _Traits>& |
| 2657 | operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md) |
| 2658 | { |
| 2659 | // As above, just write straight to a stringstream, as if by "{:L}/{}" |
| 2660 | basic_stringstream<_CharT> __os2; |
| 2661 | __os2.imbue(__os.getloc()); |
| 2662 | __os2 << __md.month(); |
| 2663 | if constexpr (is_same_v<_CharT, char>) |
| 2664 | __os2 << '/'; |
| 2665 | else |
| 2666 | __os2 << L'/'; |
| 2667 | __os2 << __md.day(); |
| 2668 | __os << __os2.view(); |
| 2669 | return __os; |
| 2670 | } |
| 2671 | |
| 2672 | template<typename _CharT, typename _Traits, |
| 2673 | typename _Alloc = allocator<_CharT>> |
| 2674 | inline basic_istream<_CharT, _Traits>& |
| 2675 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2676 | month_day& __md, |
| 2677 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2678 | minutes* __offset = nullptr) |
| 2679 | { |
| 2680 | using __format::_ChronoParts; |
| 2681 | auto __need = _ChronoParts::_Month | _ChronoParts::_Day; |
| 2682 | __detail::_Parser<> __p(__need); |
| 2683 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2684 | __md = month_day(__p._M_ymd.month(), __p._M_ymd.day()); |
| 2685 | return __is; |
| 2686 | } |
| 2687 | |
| 2688 | template<typename _CharT, typename _Traits> |
| 2689 | inline basic_ostream<_CharT, _Traits>& |
| 2690 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2691 | const month_day_last& __mdl) |
| 2692 | { |
| 2693 | // As above, just write straight to a stringstream, as if by "{:L}/last" |
| 2694 | basic_stringstream<_CharT> __os2; |
| 2695 | __os2.imbue(__os.getloc()); |
| 2696 | __os2 << __mdl.month() << _GLIBCXX_WIDEN("/last" ); |
| 2697 | __os << __os2.view(); |
| 2698 | return __os; |
| 2699 | } |
| 2700 | |
| 2701 | template<typename _CharT, typename _Traits> |
| 2702 | inline basic_ostream<_CharT, _Traits>& |
| 2703 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2704 | const month_weekday& __mwd) |
| 2705 | { |
| 2706 | // As above, just write straight to a stringstream, as if by "{:L}/{:L}" |
| 2707 | basic_stringstream<_CharT> __os2; |
| 2708 | __os2.imbue(__os.getloc()); |
| 2709 | __os2 << __mwd.month(); |
| 2710 | if constexpr (is_same_v<_CharT, char>) |
| 2711 | __os2 << '/'; |
| 2712 | else |
| 2713 | __os2 << L'/'; |
| 2714 | __os2 << __mwd.weekday_indexed(); |
| 2715 | __os << __os2.view(); |
| 2716 | return __os; |
| 2717 | } |
| 2718 | |
| 2719 | template<typename _CharT, typename _Traits> |
| 2720 | inline basic_ostream<_CharT, _Traits>& |
| 2721 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2722 | const month_weekday_last& __mwdl) |
| 2723 | { |
| 2724 | // As above, just write straight to a stringstream, as if by "{:L}/{:L}" |
| 2725 | basic_stringstream<_CharT> __os2; |
| 2726 | __os2.imbue(__os.getloc()); |
| 2727 | __os2 << __mwdl.month(); |
| 2728 | if constexpr (is_same_v<_CharT, char>) |
| 2729 | __os2 << '/'; |
| 2730 | else |
| 2731 | __os2 << L'/'; |
| 2732 | __os2 << __mwdl.weekday_last(); |
| 2733 | __os << __os2.view(); |
| 2734 | return __os; |
| 2735 | } |
| 2736 | |
| 2737 | template<typename _CharT, typename _Traits> |
| 2738 | inline basic_ostream<_CharT, _Traits>& |
| 2739 | operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym) |
| 2740 | { |
| 2741 | // As above, just write straight to a stringstream, as if by "{}/{:L}" |
| 2742 | basic_stringstream<_CharT> __os2; |
| 2743 | __os2.imbue(__os.getloc()); |
| 2744 | __os2 << __ym.year(); |
| 2745 | if constexpr (is_same_v<_CharT, char>) |
| 2746 | __os2 << '/'; |
| 2747 | else |
| 2748 | __os2 << L'/'; |
| 2749 | __os2 << __ym.month(); |
| 2750 | __os << __os2.view(); |
| 2751 | return __os; |
| 2752 | } |
| 2753 | |
| 2754 | template<typename _CharT, typename _Traits, |
| 2755 | typename _Alloc = allocator<_CharT>> |
| 2756 | inline basic_istream<_CharT, _Traits>& |
| 2757 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2758 | year_month& __ym, |
| 2759 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2760 | minutes* __offset = nullptr) |
| 2761 | { |
| 2762 | using __format::_ChronoParts; |
| 2763 | auto __need = _ChronoParts::_Year | _ChronoParts::_Month; |
| 2764 | __detail::_Parser<> __p(__need); |
| 2765 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2766 | __ym = year_month(__p._M_ymd.year(), __p._M_ymd.month()); |
| 2767 | return __is; |
| 2768 | } |
| 2769 | |
| 2770 | template<typename _CharT, typename _Traits> |
| 2771 | inline basic_ostream<_CharT, _Traits>& |
| 2772 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2773 | const year_month_day& __ymd) |
| 2774 | { |
| 2775 | using _Ctx = __format::__format_context<_CharT>; |
| 2776 | using _Str = basic_string_view<_CharT>; |
| 2777 | _Str __s = _GLIBCXX_WIDEN("{:%F} is not a valid date" ); |
| 2778 | __os << std::vformat(__ymd.ok() ? __s.substr(0, 5) : __s, |
| 2779 | make_format_args<_Ctx>(__ymd)); |
| 2780 | return __os; |
| 2781 | } |
| 2782 | |
| 2783 | template<typename _CharT, typename _Traits, |
| 2784 | typename _Alloc = allocator<_CharT>> |
| 2785 | inline basic_istream<_CharT, _Traits>& |
| 2786 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2787 | year_month_day& __ymd, |
| 2788 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2789 | minutes* __offset = nullptr) |
| 2790 | { |
| 2791 | using __format::_ChronoParts; |
| 2792 | auto __need = _ChronoParts::_Year | _ChronoParts::_Month |
| 2793 | | _ChronoParts::_Day; |
| 2794 | __detail::_Parser<> __p(__need); |
| 2795 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2796 | __ymd = __p._M_ymd; |
| 2797 | return __is; |
| 2798 | } |
| 2799 | |
| 2800 | template<typename _CharT, typename _Traits> |
| 2801 | inline basic_ostream<_CharT, _Traits>& |
| 2802 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2803 | const year_month_day_last& __ymdl) |
| 2804 | { |
| 2805 | // As above, just write straight to a stringstream, as if by "{}/{:L}" |
| 2806 | basic_stringstream<_CharT> __os2; |
| 2807 | __os2.imbue(__os.getloc()); |
| 2808 | __os2 << __ymdl.year(); |
| 2809 | if constexpr (is_same_v<_CharT, char>) |
| 2810 | __os2 << '/'; |
| 2811 | else |
| 2812 | __os2 << L'/'; |
| 2813 | __os2 << __ymdl.month_day_last(); |
| 2814 | __os << __os2.view(); |
| 2815 | return __os; |
| 2816 | } |
| 2817 | |
| 2818 | template<typename _CharT, typename _Traits> |
| 2819 | inline basic_ostream<_CharT, _Traits>& |
| 2820 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2821 | const year_month_weekday& __ymwd) |
| 2822 | { |
| 2823 | // As above, just write straight to a stringstream, as if by |
| 2824 | // "{}/{:L}/{:L}" |
| 2825 | basic_stringstream<_CharT> __os2; |
| 2826 | __os2.imbue(__os.getloc()); |
| 2827 | _CharT __slash; |
| 2828 | if constexpr (is_same_v<_CharT, char>) |
| 2829 | __slash = '/'; |
| 2830 | else |
| 2831 | __slash = L'/'; |
| 2832 | __os2 << __ymwd.year() << __slash << __ymwd.month() << __slash |
| 2833 | << __ymwd.weekday_indexed(); |
| 2834 | __os << __os2.view(); |
| 2835 | return __os; |
| 2836 | } |
| 2837 | |
| 2838 | template<typename _CharT, typename _Traits> |
| 2839 | inline basic_ostream<_CharT, _Traits>& |
| 2840 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2841 | const year_month_weekday_last& __ymwdl) |
| 2842 | { |
| 2843 | // As above, just write straight to a stringstream, as if by |
| 2844 | // "{}/{:L}/{:L}" |
| 2845 | basic_stringstream<_CharT> __os2; |
| 2846 | __os2.imbue(__os.getloc()); |
| 2847 | _CharT __slash; |
| 2848 | if constexpr (is_same_v<_CharT, char>) |
| 2849 | __slash = '/'; |
| 2850 | else |
| 2851 | __slash = L'/'; |
| 2852 | __os2 << __ymwdl.year() << __slash << __ymwdl.month() << __slash |
| 2853 | << __ymwdl.weekday_last(); |
| 2854 | __os << __os2.view(); |
| 2855 | return __os; |
| 2856 | } |
| 2857 | |
| 2858 | template<typename _CharT, typename _Traits, typename _Duration> |
| 2859 | inline basic_ostream<_CharT, _Traits>& |
| 2860 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2861 | const hh_mm_ss<_Duration>& __hms) |
| 2862 | { |
| 2863 | return __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%T}" ), __hms); |
| 2864 | } |
| 2865 | |
| 2866 | #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI |
| 2867 | /// Writes a sys_info object to an ostream in an unspecified format. |
| 2868 | template<typename _CharT, typename _Traits> |
| 2869 | basic_ostream<_CharT, _Traits>& |
| 2870 | operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __i) |
| 2871 | { |
| 2872 | __os << '[' << __i.begin << ',' << __i.end |
| 2873 | << ',' << hh_mm_ss(__i.offset) << ',' << __i.save |
| 2874 | << ',' << __i.abbrev << ']'; |
| 2875 | return __os; |
| 2876 | } |
| 2877 | |
| 2878 | /// Writes a local_info object to an ostream in an unspecified format. |
| 2879 | template<typename _CharT, typename _Traits> |
| 2880 | basic_ostream<_CharT, _Traits>& |
| 2881 | operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __li) |
| 2882 | { |
| 2883 | __os << '['; |
| 2884 | if (__li.result == local_info::unique) |
| 2885 | __os << __li.first; |
| 2886 | else |
| 2887 | { |
| 2888 | if (__li.result == local_info::nonexistent) |
| 2889 | __os << "nonexistent" ; |
| 2890 | else |
| 2891 | __os << "ambiguous" ; |
| 2892 | __os << " local time between " << __li.first; |
| 2893 | __os << " and " << __li.second; |
| 2894 | } |
| 2895 | __os << ']'; |
| 2896 | return __os; |
| 2897 | } |
| 2898 | |
| 2899 | template<typename _CharT, typename _Traits, typename _Duration, |
| 2900 | typename _TimeZonePtr> |
| 2901 | inline basic_ostream<_CharT, _Traits>& |
| 2902 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2903 | const zoned_time<_Duration, _TimeZonePtr>& __t) |
| 2904 | { |
| 2905 | __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T %Z}" ), __t); |
| 2906 | return __os; |
| 2907 | } |
| 2908 | #endif |
| 2909 | |
| 2910 | template<typename _CharT, typename _Traits, typename _Duration> |
| 2911 | requires (!treat_as_floating_point_v<typename _Duration::rep>) |
| 2912 | && ratio_less_v<typename _Duration::period, days::period> |
| 2913 | inline basic_ostream<_CharT, _Traits>& |
| 2914 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2915 | const sys_time<_Duration>& __tp) |
| 2916 | { |
| 2917 | __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}" ), __tp); |
| 2918 | return __os; |
| 2919 | } |
| 2920 | |
| 2921 | template<typename _CharT, typename _Traits> |
| 2922 | inline basic_ostream<_CharT, _Traits>& |
| 2923 | operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp) |
| 2924 | { |
| 2925 | __os << year_month_day{__dp}; |
| 2926 | return __os; |
| 2927 | } |
| 2928 | |
| 2929 | template<typename _CharT, typename _Traits, typename _Duration, |
| 2930 | typename _Alloc = allocator<_CharT>> |
| 2931 | basic_istream<_CharT, _Traits>& |
| 2932 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2933 | sys_time<_Duration>& __tp, |
| 2934 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2935 | minutes* __offset = nullptr) |
| 2936 | { |
| 2937 | minutes __off{}; |
| 2938 | if (!__offset) |
| 2939 | __offset = &__off; |
| 2940 | using __format::_ChronoParts; |
| 2941 | auto __need = _ChronoParts::_Year | _ChronoParts::_Month |
| 2942 | | _ChronoParts::_Day | _ChronoParts::_TimeOfDay; |
| 2943 | __detail::_Parser_t<_Duration> __p(__need); |
| 2944 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2945 | { |
| 2946 | if (__p._M_is_leap_second) |
| 2947 | __is.setstate(ios_base::failbit); |
| 2948 | else |
| 2949 | { |
| 2950 | auto __st = __p._M_sys_days + __p._M_time - *__offset; |
| 2951 | __tp = __detail::__round<_Duration>(__st); |
| 2952 | } |
| 2953 | } |
| 2954 | return __is; |
| 2955 | } |
| 2956 | |
| 2957 | template<typename _CharT, typename _Traits, typename _Duration> |
| 2958 | inline basic_ostream<_CharT, _Traits>& |
| 2959 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2960 | const utc_time<_Duration>& __t) |
| 2961 | { |
| 2962 | __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}" ), __t); |
| 2963 | return __os; |
| 2964 | } |
| 2965 | |
| 2966 | template<typename _CharT, typename _Traits, typename _Duration, |
| 2967 | typename _Alloc = allocator<_CharT>> |
| 2968 | inline basic_istream<_CharT, _Traits>& |
| 2969 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 2970 | utc_time<_Duration>& __tp, |
| 2971 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 2972 | minutes* __offset = nullptr) |
| 2973 | { |
| 2974 | minutes __off{}; |
| 2975 | if (!__offset) |
| 2976 | __offset = &__off; |
| 2977 | using __format::_ChronoParts; |
| 2978 | auto __need = _ChronoParts::_Year | _ChronoParts::_Month |
| 2979 | | _ChronoParts::_Day | _ChronoParts::_TimeOfDay; |
| 2980 | __detail::_Parser_t<_Duration> __p(__need); |
| 2981 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 2982 | { |
| 2983 | // Converting to utc_time before adding _M_time is necessary for |
| 2984 | // "23:59:60" to correctly produce a time within a leap second. |
| 2985 | auto __ut = utc_clock::from_sys(__p._M_sys_days) + __p._M_time |
| 2986 | - *__offset; |
| 2987 | __tp = __detail::__round<_Duration>(__ut); |
| 2988 | } |
| 2989 | return __is; |
| 2990 | } |
| 2991 | |
| 2992 | template<typename _CharT, typename _Traits, typename _Duration> |
| 2993 | inline basic_ostream<_CharT, _Traits>& |
| 2994 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2995 | const tai_time<_Duration>& __t) |
| 2996 | { |
| 2997 | __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}" ), __t); |
| 2998 | return __os; |
| 2999 | } |
| 3000 | |
| 3001 | template<typename _CharT, typename _Traits, typename _Duration, |
| 3002 | typename _Alloc = allocator<_CharT>> |
| 3003 | inline basic_istream<_CharT, _Traits>& |
| 3004 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 3005 | tai_time<_Duration>& __tp, |
| 3006 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 3007 | minutes* __offset = nullptr) |
| 3008 | { |
| 3009 | minutes __off{}; |
| 3010 | if (!__offset) |
| 3011 | __offset = &__off; |
| 3012 | using __format::_ChronoParts; |
| 3013 | auto __need = _ChronoParts::_Year | _ChronoParts::_Month |
| 3014 | | _ChronoParts::_Day | _ChronoParts::_TimeOfDay; |
| 3015 | __detail::_Parser_t<_Duration> __p(__need); |
| 3016 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 3017 | { |
| 3018 | if (__p._M_is_leap_second) |
| 3019 | __is.setstate(ios_base::failbit); |
| 3020 | else |
| 3021 | { |
| 3022 | constexpr sys_days __epoch(-days(4383)); // 1958y/1/1 |
| 3023 | auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset; |
| 3024 | tai_time<common_type_t<_Duration, seconds>> __tt(__d); |
| 3025 | __tp = __detail::__round<_Duration>(__tt); |
| 3026 | } |
| 3027 | } |
| 3028 | return __is; |
| 3029 | } |
| 3030 | |
| 3031 | template<typename _CharT, typename _Traits, typename _Duration> |
| 3032 | inline basic_ostream<_CharT, _Traits>& |
| 3033 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3034 | const gps_time<_Duration>& __t) |
| 3035 | { |
| 3036 | __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}" ), __t); |
| 3037 | return __os; |
| 3038 | } |
| 3039 | |
| 3040 | template<typename _CharT, typename _Traits, typename _Duration, |
| 3041 | typename _Alloc = allocator<_CharT>> |
| 3042 | inline basic_istream<_CharT, _Traits>& |
| 3043 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 3044 | gps_time<_Duration>& __tp, |
| 3045 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 3046 | minutes* __offset = nullptr) |
| 3047 | { |
| 3048 | minutes __off{}; |
| 3049 | if (!__offset) |
| 3050 | __offset = &__off; |
| 3051 | using __format::_ChronoParts; |
| 3052 | auto __need = _ChronoParts::_Year | _ChronoParts::_Month |
| 3053 | | _ChronoParts::_Day | _ChronoParts::_TimeOfDay; |
| 3054 | __detail::_Parser_t<_Duration> __p(__need); |
| 3055 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 3056 | { |
| 3057 | if (__p._M_is_leap_second) |
| 3058 | __is.setstate(ios_base::failbit); |
| 3059 | else |
| 3060 | { |
| 3061 | constexpr sys_days __epoch(days(3657)); // 1980y/1/Sunday[1] |
| 3062 | auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset; |
| 3063 | gps_time<common_type_t<_Duration, seconds>> __gt(__d); |
| 3064 | __tp = __detail::__round<_Duration>(__gt); |
| 3065 | } |
| 3066 | } |
| 3067 | return __is; |
| 3068 | } |
| 3069 | |
| 3070 | template<typename _CharT, typename _Traits, typename _Duration> |
| 3071 | inline basic_ostream<_CharT, _Traits>& |
| 3072 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3073 | const file_time<_Duration>& __t) |
| 3074 | { |
| 3075 | __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}" ), __t); |
| 3076 | return __os; |
| 3077 | } |
| 3078 | |
| 3079 | template<typename _CharT, typename _Traits, typename _Duration, |
| 3080 | typename _Alloc = allocator<_CharT>> |
| 3081 | inline basic_istream<_CharT, _Traits>& |
| 3082 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 3083 | file_time<_Duration>& __tp, |
| 3084 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 3085 | minutes* __offset = nullptr) |
| 3086 | { |
| 3087 | sys_time<_Duration> __st; |
| 3088 | if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset)) |
| 3089 | __tp = __detail::__round<_Duration>(file_clock::from_sys(__st)); |
| 3090 | return __is; |
| 3091 | } |
| 3092 | |
| 3093 | template<typename _CharT, typename _Traits, typename _Duration> |
| 3094 | inline basic_ostream<_CharT, _Traits>& |
| 3095 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3096 | const local_time<_Duration>& __lt) |
| 3097 | { |
| 3098 | __os << sys_time<_Duration>{__lt.time_since_epoch()}; |
| 3099 | return __os; |
| 3100 | } |
| 3101 | |
| 3102 | template<typename _CharT, typename _Traits, typename _Duration, |
| 3103 | typename _Alloc = allocator<_CharT>> |
| 3104 | basic_istream<_CharT, _Traits>& |
| 3105 | from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 3106 | local_time<_Duration>& __tp, |
| 3107 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 3108 | minutes* __offset = nullptr) |
| 3109 | { |
| 3110 | using __format::_ChronoParts; |
| 3111 | auto __need = _ChronoParts::_Year | _ChronoParts::_Month |
| 3112 | | _ChronoParts::_Day | _ChronoParts::_TimeOfDay; |
| 3113 | __detail::_Parser_t<_Duration> __p(__need); |
| 3114 | if (__p(__is, __fmt, __abbrev, __offset)) |
| 3115 | { |
| 3116 | days __d = __p._M_sys_days.time_since_epoch(); |
| 3117 | auto __t = local_days(__d) + __p._M_time; // ignore offset |
| 3118 | __tp = __detail::__round<_Duration>(__t); |
| 3119 | } |
| 3120 | return __is; |
| 3121 | } |
| 3122 | |
| 3123 | // [time.parse] parsing |
| 3124 | |
| 3125 | namespace __detail |
| 3126 | { |
| 3127 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 3128 | // 3956. chrono::parse uses from_stream as a customization point |
| 3129 | void from_stream() = delete; |
| 3130 | |
| 3131 | template<typename _Parsable, typename _CharT, |
| 3132 | typename _Traits = std::char_traits<_CharT>, |
| 3133 | typename... _OptArgs> |
| 3134 | concept __parsable = requires (basic_istream<_CharT, _Traits>& __is, |
| 3135 | const _CharT* __fmt, _Parsable& __tp, |
| 3136 | _OptArgs*... __args) |
| 3137 | { from_stream(__is, __fmt, __tp, __args...); }; |
| 3138 | |
| 3139 | template<typename _Parsable, typename _CharT, |
| 3140 | typename _Traits = char_traits<_CharT>, |
| 3141 | typename _Alloc = allocator<_CharT>> |
| 3142 | struct _Parse |
| 3143 | { |
| 3144 | private: |
| 3145 | using __string_type = basic_string<_CharT, _Traits, _Alloc>; |
| 3146 | |
| 3147 | public: |
| 3148 | _Parse(const _CharT* __fmt, _Parsable& __tp, |
| 3149 | basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr, |
| 3150 | minutes* __offset = nullptr) |
| 3151 | : _M_fmt(__fmt), _M_tp(std::__addressof(__tp)), |
| 3152 | _M_abbrev(__abbrev), _M_offset(__offset) |
| 3153 | { } |
| 3154 | |
| 3155 | _Parse(_Parse&&) = delete; |
| 3156 | _Parse& operator=(_Parse&&) = delete; |
| 3157 | |
| 3158 | private: |
| 3159 | using __stream_type = basic_istream<_CharT, _Traits>; |
| 3160 | |
| 3161 | const _CharT* const _M_fmt; |
| 3162 | _Parsable* const _M_tp; |
| 3163 | __string_type* const _M_abbrev; |
| 3164 | minutes* const _M_offset; |
| 3165 | |
| 3166 | friend __stream_type& |
| 3167 | operator>>(__stream_type& __is, _Parse&& __p) |
| 3168 | { |
| 3169 | if (__p._M_offset) |
| 3170 | from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev, |
| 3171 | __p._M_offset); |
| 3172 | else if (__p._M_abbrev) |
| 3173 | from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev); |
| 3174 | else |
| 3175 | from_stream(__is, __p._M_fmt, *__p._M_tp); |
| 3176 | return __is; |
| 3177 | } |
| 3178 | |
| 3179 | friend void operator>>(__stream_type&, _Parse&) = delete; |
| 3180 | friend void operator>>(__stream_type&, const _Parse&) = delete; |
| 3181 | }; |
| 3182 | } // namespace __detail |
| 3183 | |
| 3184 | template<typename _CharT, __detail::__parsable<_CharT> _Parsable> |
| 3185 | [[nodiscard, __gnu__::__access__(__read_only__, 1)]] |
| 3186 | inline auto |
| 3187 | parse(const _CharT* __fmt, _Parsable& __tp) |
| 3188 | { return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp); } |
| 3189 | |
| 3190 | template<typename _CharT, typename _Traits, typename _Alloc, |
| 3191 | __detail::__parsable<_CharT, _Traits> _Parsable> |
| 3192 | [[nodiscard]] |
| 3193 | inline auto |
| 3194 | parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp) |
| 3195 | { |
| 3196 | return __detail::_Parse<_Parsable, _CharT, _Traits>(__fmt.c_str(), __tp); |
| 3197 | } |
| 3198 | |
| 3199 | template<typename _CharT, typename _Traits, typename _Alloc, |
| 3200 | typename _StrT = basic_string<_CharT, _Traits, _Alloc>, |
| 3201 | __detail::__parsable<_CharT, _Traits, _StrT> _Parsable> |
| 3202 | [[nodiscard, __gnu__::__access__(__read_only__, 1)]] |
| 3203 | inline auto |
| 3204 | parse(const _CharT* __fmt, _Parsable& __tp, |
| 3205 | basic_string<_CharT, _Traits, _Alloc>& __abbrev) |
| 3206 | { |
| 3207 | auto __pa = std::__addressof(__abbrev); |
| 3208 | return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp, |
| 3209 | __pa); |
| 3210 | } |
| 3211 | |
| 3212 | template<typename _CharT, typename _Traits, typename _Alloc, |
| 3213 | typename _StrT = basic_string<_CharT, _Traits, _Alloc>, |
| 3214 | __detail::__parsable<_CharT, _Traits, _StrT> _Parsable> |
| 3215 | [[nodiscard]] |
| 3216 | inline auto |
| 3217 | parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp, |
| 3218 | basic_string<_CharT, _Traits, _Alloc>& __abbrev) |
| 3219 | { |
| 3220 | auto __pa = std::__addressof(__abbrev); |
| 3221 | return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(), |
| 3222 | __tp, __pa); |
| 3223 | } |
| 3224 | |
| 3225 | template<typename _CharT, typename _Traits = char_traits<_CharT>, |
| 3226 | typename _StrT = basic_string<_CharT, _Traits>, |
| 3227 | __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable> |
| 3228 | [[nodiscard, __gnu__::__access__(__read_only__, 1)]] |
| 3229 | inline auto |
| 3230 | parse(const _CharT* __fmt, _Parsable& __tp, minutes& __offset) |
| 3231 | { |
| 3232 | return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp, nullptr, |
| 3233 | &__offset); |
| 3234 | } |
| 3235 | |
| 3236 | template<typename _CharT, typename _Traits, typename _Alloc, |
| 3237 | typename _StrT = basic_string<_CharT, _Traits>, |
| 3238 | __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable> |
| 3239 | [[nodiscard]] |
| 3240 | inline auto |
| 3241 | parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp, |
| 3242 | minutes& __offset) |
| 3243 | { |
| 3244 | return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(), |
| 3245 | __tp, nullptr, |
| 3246 | &__offset); |
| 3247 | } |
| 3248 | |
| 3249 | template<typename _CharT, typename _Traits, typename _Alloc, |
| 3250 | typename _StrT = basic_string<_CharT, _Traits, _Alloc>, |
| 3251 | __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable> |
| 3252 | [[nodiscard, __gnu__::__access__(__read_only__, 1)]] |
| 3253 | inline auto |
| 3254 | parse(const _CharT* __fmt, _Parsable& __tp, |
| 3255 | basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset) |
| 3256 | { |
| 3257 | auto __pa = std::__addressof(__abbrev); |
| 3258 | return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp, |
| 3259 | __pa, |
| 3260 | &__offset); |
| 3261 | } |
| 3262 | |
| 3263 | template<typename _CharT, typename _Traits, typename _Alloc, |
| 3264 | typename _StrT = basic_string<_CharT, _Traits, _Alloc>, |
| 3265 | __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable> |
| 3266 | [[nodiscard]] |
| 3267 | inline auto |
| 3268 | parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp, |
| 3269 | basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset) |
| 3270 | { |
| 3271 | auto __pa = std::__addressof(__abbrev); |
| 3272 | return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(), |
| 3273 | __tp, __pa, |
| 3274 | &__offset); |
| 3275 | } |
| 3276 | |
| 3277 | /// @cond undocumented |
| 3278 | template<typename _Duration> |
| 3279 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3280 | basic_istream<_CharT, _Traits>& |
| 3281 | __detail::_Parser<_Duration>:: |
| 3282 | operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt, |
| 3283 | basic_string<_CharT, _Traits, _Alloc>* __abbrev, |
| 3284 | minutes* __offset) |
| 3285 | { |
| 3286 | using sentry = typename basic_istream<_CharT, _Traits>::sentry; |
| 3287 | ios_base::iostate __err = ios_base::goodbit; |
| 3288 | if (sentry __cerb(__is, true); __cerb) |
| 3289 | { |
| 3290 | locale __loc = __is.getloc(); |
| 3291 | auto& __tmget = std::use_facet<std::time_get<_CharT>>(__loc); |
| 3292 | auto& __tmpunct = std::use_facet<std::__timepunct<_CharT>>(__loc); |
| 3293 | |
| 3294 | // RAII type to save and restore stream state. |
| 3295 | struct _Stream_state |
| 3296 | { |
| 3297 | explicit |
| 3298 | _Stream_state(basic_istream<_CharT, _Traits>& __i) |
| 3299 | : _M_is(__i), |
| 3300 | _M_flags(__i.flags(ios_base::skipws | ios_base::dec)), |
| 3301 | _M_w(__i.width(0)) |
| 3302 | { } |
| 3303 | |
| 3304 | ~_Stream_state() |
| 3305 | { |
| 3306 | _M_is.flags(_M_flags); |
| 3307 | _M_is.width(_M_w); |
| 3308 | } |
| 3309 | |
| 3310 | _Stream_state(_Stream_state&&) = delete; |
| 3311 | |
| 3312 | basic_istream<_CharT, _Traits>& _M_is; |
| 3313 | ios_base::fmtflags _M_flags; |
| 3314 | streamsize _M_w; |
| 3315 | }; |
| 3316 | |
| 3317 | auto __is_failed = [](ios_base::iostate __e) { |
| 3318 | return static_cast<bool>(__e & ios_base::failbit); |
| 3319 | }; |
| 3320 | |
| 3321 | // Read an unsigned integer from the stream and return it. |
| 3322 | // Extract no more than __n digits. Set __err on error. |
| 3323 | auto __read_unsigned = [&] (int __n) { |
| 3324 | return _S_read_unsigned(__is, __err, __n); |
| 3325 | }; |
| 3326 | |
| 3327 | // Read a signed integer from the stream and return it. |
| 3328 | // Extract no more than __n digits. Set __err on error. |
| 3329 | auto __read_signed = [&] (int __n) { |
| 3330 | return _S_read_signed(__is, __err, __n); |
| 3331 | }; |
| 3332 | |
| 3333 | // Read an expected character from the stream. |
| 3334 | auto __read_chr = [&__is, &__err] (_CharT __c) { |
| 3335 | return _S_read_chr(__is, __err, __c); |
| 3336 | }; |
| 3337 | |
| 3338 | using __format::_ChronoParts; |
| 3339 | _ChronoParts __parts{}; |
| 3340 | |
| 3341 | const year __bad_y = --year::min(); // SHRT_MIN |
| 3342 | const month __bad_mon(255); |
| 3343 | const day __bad_day(255); |
| 3344 | const weekday __bad_wday(255); |
| 3345 | const hours __bad_h(-1); |
| 3346 | const minutes __bad_min(-9999); |
| 3347 | const seconds __bad_sec(-1); |
| 3348 | |
| 3349 | year __y = __bad_y, __yy = __bad_y; // %Y, %yy |
| 3350 | year __iso_y = __bad_y, __iso_yy = __bad_y; // %G, %g |
| 3351 | month __m = __bad_mon; // %m |
| 3352 | day __d = __bad_day; // %d |
| 3353 | weekday __wday = __bad_wday; // %a %A %u %w |
| 3354 | hours __h = __bad_h, __h12 = __bad_h; // %H, %I |
| 3355 | minutes __min = __bad_min; // %M |
| 3356 | _Duration __s = __bad_sec; // %S |
| 3357 | int __ampm = 0; // %p |
| 3358 | int __iso_wk = -1, __sunday_wk = -1, __monday_wk = -1; // %V, %U, %W |
| 3359 | int __century = -1; // %C |
| 3360 | int __dayofyear = -1; // %j (for non-duration) |
| 3361 | |
| 3362 | minutes __tz_offset = __bad_min; |
| 3363 | basic_string<_CharT, _Traits> __tz_abbr; |
| 3364 | |
| 3365 | if ((_M_need & _ChronoParts::_TimeOfDay) |
| 3366 | && (_M_need & _ChronoParts::_Year)) |
| 3367 | { |
| 3368 | // For time_points assume "00:00:00" is implicitly present, |
| 3369 | // so we don't fail to parse if it's not (PR libstdc++/114240). |
| 3370 | // We will still fail to parse if there's no year+month+day. |
| 3371 | __h = hours(0); |
| 3372 | __parts = _ChronoParts::_TimeOfDay; |
| 3373 | } |
| 3374 | |
| 3375 | // bool __is_neg = false; // TODO: how is this handled for parsing? |
| 3376 | |
| 3377 | _CharT __mod{}; // One of 'E' or 'O' or nul. |
| 3378 | unsigned __num = 0; // Non-zero for N modifier. |
| 3379 | bool __is_flag = false; // True if we're processing a % flag. |
| 3380 | |
| 3381 | constexpr bool __is_floating |
| 3382 | = treat_as_floating_point_v<typename _Duration::rep>; |
| 3383 | |
| 3384 | // If an out-of-range value is extracted (e.g. 61min for %M), |
| 3385 | // do not set failbit immediately because we might not need it |
| 3386 | // (e.g. parsing chrono::year doesn't care about invalid %M values). |
| 3387 | // Instead set the variable back to its initial 'bad' state, |
| 3388 | // and also set related variables corresponding to the same field |
| 3389 | // (e.g. a bad %M value for __min should also reset __h and __s). |
| 3390 | // If a valid value is needed later the bad value will cause failure. |
| 3391 | |
| 3392 | // For some fields we don't know the correct range when parsing and |
| 3393 | // we have to be liberal in what we accept, e.g. we allow 366 for |
| 3394 | // day-of-year because that's valid in leap years, and we allow 31 |
| 3395 | // for day-of-month. If those values are needed to determine the |
| 3396 | // result then we can do a correct range check at the end when we |
| 3397 | // know the how many days the relevant year or month actually has. |
| 3398 | |
| 3399 | while (*__fmt) |
| 3400 | { |
| 3401 | _CharT __c = *__fmt++; |
| 3402 | if (!__is_flag) |
| 3403 | { |
| 3404 | if (__c == '%') |
| 3405 | __is_flag = true; // This is the start of a flag. |
| 3406 | else if (std::isspace(__c, __loc)) |
| 3407 | std::ws(__is); // Match zero or more whitespace characters. |
| 3408 | else if (!__read_chr(__c)) [[unlikely]] |
| 3409 | break; // Failed to match the expected character. |
| 3410 | |
| 3411 | continue; // Process next character in the format string. |
| 3412 | } |
| 3413 | |
| 3414 | // Now processing a flag. |
| 3415 | switch (__c) |
| 3416 | { |
| 3417 | case 'a': // Locale's weekday name |
| 3418 | case 'A': // (full or abbreviated, matched case-insensitively). |
| 3419 | if (__mod || __num) [[unlikely]] |
| 3420 | __err = ios_base::failbit; |
| 3421 | else |
| 3422 | { |
| 3423 | struct tm __tm{}; |
| 3424 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3425 | __fmt - 2, __fmt); |
| 3426 | if (!__is_failed(__err)) |
| 3427 | __wday = weekday(__tm.tm_wday); |
| 3428 | } |
| 3429 | __parts |= _ChronoParts::_Weekday; |
| 3430 | break; |
| 3431 | |
| 3432 | case 'b': // Locale's month name |
| 3433 | case 'h': // (full or abbreviated, matched case-insensitively). |
| 3434 | case 'B': |
| 3435 | if (__mod || __num) [[unlikely]] |
| 3436 | __err = ios_base::failbit; |
| 3437 | else |
| 3438 | { |
| 3439 | // strptime behaves differently for %b and %B, |
| 3440 | // but chrono::parse says they're equivalent. |
| 3441 | // Luckily libstdc++ std::time_get works as needed. |
| 3442 | struct tm __tm{}; |
| 3443 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3444 | __fmt - 2, __fmt); |
| 3445 | if (!__is_failed(__err)) |
| 3446 | __m = month(__tm.tm_mon + 1); |
| 3447 | } |
| 3448 | __parts |= _ChronoParts::_Month; |
| 3449 | break; |
| 3450 | |
| 3451 | case 'c': // Locale's date and time representation. |
| 3452 | if (__mod == 'O' || __num) [[unlikely]] |
| 3453 | __err |= ios_base::failbit; |
| 3454 | else |
| 3455 | { |
| 3456 | struct tm __tm{}; |
| 3457 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3458 | __fmt - 2 - (__mod == 'E'), __fmt); |
| 3459 | if (!__is_failed(__err)) |
| 3460 | { |
| 3461 | __y = year(__tm.tm_year + 1900); |
| 3462 | __m = month(__tm.tm_mon + 1); |
| 3463 | __d = day(__tm.tm_mday); |
| 3464 | __h = hours(__tm.tm_hour); |
| 3465 | __min = minutes(__tm.tm_min); |
| 3466 | __s = seconds(__tm.tm_sec); |
| 3467 | } |
| 3468 | } |
| 3469 | __parts |= _ChronoParts::_DateTime; |
| 3470 | break; |
| 3471 | |
| 3472 | case 'C': // Century |
| 3473 | if (!__mod) [[likely]] |
| 3474 | { |
| 3475 | auto __v = __read_signed(__num ? __num : 2); |
| 3476 | if (!__is_failed(__err)) |
| 3477 | { |
| 3478 | int __cmin = (int)year::min() / 100; |
| 3479 | int __cmax = (int)year::max() / 100; |
| 3480 | if (__cmin <= __v && __v <= __cmax) |
| 3481 | __century = __v * 100; |
| 3482 | else |
| 3483 | __century = -2; // This prevents guessing century. |
| 3484 | } |
| 3485 | } |
| 3486 | else if (__mod == 'E') |
| 3487 | { |
| 3488 | struct tm __tm{}; |
| 3489 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3490 | __fmt - 3, __fmt); |
| 3491 | if (!__is_failed(__err)) |
| 3492 | __century = __tm.tm_year; |
| 3493 | } |
| 3494 | else [[unlikely]] |
| 3495 | __err |= ios_base::failbit; |
| 3496 | // N.B. don't set this here: __parts |= _ChronoParts::_Year; |
| 3497 | break; |
| 3498 | |
| 3499 | case 'd': // Day of month (1-31) |
| 3500 | case 'e': |
| 3501 | if (!__mod) [[likely]] |
| 3502 | { |
| 3503 | auto __v = __read_unsigned(__num ? __num : 2); |
| 3504 | if (!__is_failed(__err)) |
| 3505 | __d = day(__v); |
| 3506 | } |
| 3507 | else if (__mod == 'O') |
| 3508 | { |
| 3509 | struct tm __tm{}; |
| 3510 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3511 | __fmt - 3, __fmt); |
| 3512 | if (!__is_failed(__err)) |
| 3513 | __d = day(__tm.tm_mday); |
| 3514 | } |
| 3515 | else [[unlikely]] |
| 3516 | __err |= ios_base::failbit; |
| 3517 | __parts |= _ChronoParts::_Day; |
| 3518 | break; |
| 3519 | |
| 3520 | case 'D': // %m/%d/%y |
| 3521 | if (__mod || __num) [[unlikely]] |
| 3522 | __err |= ios_base::failbit; |
| 3523 | else |
| 3524 | { |
| 3525 | auto __month = __read_unsigned(2); // %m |
| 3526 | __read_chr('/'); |
| 3527 | auto __day = __read_unsigned(2); // %d |
| 3528 | __read_chr('/'); |
| 3529 | auto __year = __read_unsigned(2); // %y |
| 3530 | if (__is_failed(__err)) |
| 3531 | break; |
| 3532 | __y = year(__year + 1900 + 100 * int(__year < 69)); |
| 3533 | __m = month(__month); |
| 3534 | __d = day(__day); |
| 3535 | if (!year_month_day(__y, __m, __d).ok()) |
| 3536 | { |
| 3537 | __y = __yy = __iso_y = __iso_yy = __bad_y; |
| 3538 | __m = __bad_mon; |
| 3539 | __d = __bad_day; |
| 3540 | break; |
| 3541 | } |
| 3542 | } |
| 3543 | __parts |= _ChronoParts::_Date; |
| 3544 | break; |
| 3545 | |
| 3546 | case 'F': // %Y-%m-%d - any N modifier only applies to %Y. |
| 3547 | if (__mod) [[unlikely]] |
| 3548 | __err |= ios_base::failbit; |
| 3549 | else |
| 3550 | { |
| 3551 | auto __year = __read_signed(__num ? __num : 4); // %Y |
| 3552 | __read_chr('-'); |
| 3553 | auto __month = __read_unsigned(2); // %m |
| 3554 | __read_chr('-'); |
| 3555 | auto __day = __read_unsigned(2); // %d |
| 3556 | if (__is_failed(__err)) |
| 3557 | break; |
| 3558 | __y = year(__year); |
| 3559 | __m = month(__month); |
| 3560 | __d = day(__day); |
| 3561 | if (!year_month_day(__y, __m, __d).ok()) |
| 3562 | { |
| 3563 | __y = __yy = __iso_y = __iso_yy = __bad_y; |
| 3564 | __m = __bad_mon; |
| 3565 | __d = __bad_day; |
| 3566 | break; |
| 3567 | } |
| 3568 | } |
| 3569 | __parts |= _ChronoParts::_Date; |
| 3570 | break; |
| 3571 | |
| 3572 | case 'g': // Last two digits of ISO week-based year. |
| 3573 | if (__mod) [[unlikely]] |
| 3574 | __err |= ios_base::failbit; |
| 3575 | else |
| 3576 | { |
| 3577 | auto __val = __read_unsigned(__num ? __num : 2); |
| 3578 | if (__val >= 0 && __val <= 99) |
| 3579 | { |
| 3580 | __iso_yy = year(__val); |
| 3581 | if (__century == -1) // No %C has been parsed yet. |
| 3582 | __century = 2000; |
| 3583 | } |
| 3584 | else |
| 3585 | __iso_yy = __iso_y = __y = __yy = __bad_y; |
| 3586 | } |
| 3587 | __parts |= _ChronoParts::_Year; |
| 3588 | break; |
| 3589 | |
| 3590 | case 'G': // ISO week-based year. |
| 3591 | if (__mod) [[unlikely]] |
| 3592 | __err |= ios_base::failbit; |
| 3593 | else |
| 3594 | __iso_y = year(__read_unsigned(__num ? __num : 4)); |
| 3595 | __parts |= _ChronoParts::_Year; |
| 3596 | break; |
| 3597 | |
| 3598 | case 'H': // 24-hour (00-23) |
| 3599 | case 'I': // 12-hour (1-12) |
| 3600 | if (__mod == 'E') [[unlikely]] |
| 3601 | __err |= ios_base::failbit; |
| 3602 | else if (__mod == 'O') |
| 3603 | { |
| 3604 | #if 0 |
| 3605 | struct tm __tm{}; |
| 3606 | __tm.tm_ampm = 1; |
| 3607 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3608 | __fmt - 3, __fmt); |
| 3609 | if (!__is_failed(__err)) |
| 3610 | { |
| 3611 | if (__c == 'I') |
| 3612 | { |
| 3613 | __h12 = hours(__tm.tm_hour); |
| 3614 | __h = __bad_h; |
| 3615 | } |
| 3616 | else |
| 3617 | __h = hours(__tm.tm_hour); |
| 3618 | } |
| 3619 | #else |
| 3620 | // XXX %OI seems to be unimplementable. |
| 3621 | __err |= ios_base::failbit; |
| 3622 | #endif |
| 3623 | } |
| 3624 | else |
| 3625 | { |
| 3626 | auto __val = __read_unsigned(__num ? __num : 2); |
| 3627 | if (__c == 'I' && __val >= 1 && __val <= 12) |
| 3628 | { |
| 3629 | __h12 = hours(__val); |
| 3630 | __h = __bad_h; |
| 3631 | } |
| 3632 | else if (__c == 'H' && __val >= 0 && __val <= 23) |
| 3633 | { |
| 3634 | __h = hours(__val); |
| 3635 | __h12 = __bad_h; |
| 3636 | } |
| 3637 | else |
| 3638 | { |
| 3639 | if (_M_need & _ChronoParts::_TimeOfDay) |
| 3640 | __err |= ios_base::failbit; |
| 3641 | break; |
| 3642 | } |
| 3643 | } |
| 3644 | __parts |= _ChronoParts::_TimeOfDay; |
| 3645 | break; |
| 3646 | |
| 3647 | case 'j': // For duration, count of days, otherwise day of year |
| 3648 | if (__mod) [[unlikely]] |
| 3649 | __err |= ios_base::failbit; |
| 3650 | else if (_M_need == _ChronoParts::_TimeOfDay) // duration |
| 3651 | { |
| 3652 | auto __val = __read_signed(__num ? __num : 3); |
| 3653 | if (!__is_failed(__err)) |
| 3654 | { |
| 3655 | __h = days(__val); // __h will get added to _M_time |
| 3656 | __parts |= _ChronoParts::_TimeOfDay; |
| 3657 | } |
| 3658 | } |
| 3659 | else |
| 3660 | { |
| 3661 | __dayofyear = __read_unsigned(__num ? __num : 3); |
| 3662 | // N.B. do not alter __parts here, done after loop. |
| 3663 | // No need for range checking here either. |
| 3664 | } |
| 3665 | break; |
| 3666 | |
| 3667 | case 'm': // Month (1-12) |
| 3668 | if (__mod == 'E') [[unlikely]] |
| 3669 | __err |= ios_base::failbit; |
| 3670 | else if (__mod == 'O') |
| 3671 | { |
| 3672 | struct tm __tm{}; |
| 3673 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3674 | __fmt - 2, __fmt); |
| 3675 | if (!__is_failed(__err)) |
| 3676 | __m = month(__tm.tm_mon + 1); |
| 3677 | } |
| 3678 | else |
| 3679 | { |
| 3680 | auto __val = __read_unsigned(__num ? __num : 2); |
| 3681 | if (__val >= 1 && __val <= 12) |
| 3682 | __m = month(__val); |
| 3683 | else |
| 3684 | __m = __bad_mon; |
| 3685 | } |
| 3686 | __parts |= _ChronoParts::_Month; |
| 3687 | break; |
| 3688 | |
| 3689 | case 'M': // Minutes |
| 3690 | if (__mod == 'E') [[unlikely]] |
| 3691 | __err |= ios_base::failbit; |
| 3692 | else if (__mod == 'O') |
| 3693 | { |
| 3694 | struct tm __tm{}; |
| 3695 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3696 | __fmt - 2, __fmt); |
| 3697 | if (!__is_failed(__err)) |
| 3698 | __min = minutes(__tm.tm_min); |
| 3699 | } |
| 3700 | else |
| 3701 | { |
| 3702 | auto __val = __read_unsigned(__num ? __num : 2); |
| 3703 | if (0 <= __val && __val < 60) |
| 3704 | __min = minutes(__val); |
| 3705 | else |
| 3706 | { |
| 3707 | if (_M_need & _ChronoParts::_TimeOfDay) |
| 3708 | __err |= ios_base::failbit; |
| 3709 | break; |
| 3710 | } |
| 3711 | } |
| 3712 | __parts |= _ChronoParts::_TimeOfDay; |
| 3713 | break; |
| 3714 | |
| 3715 | case 'p': // Locale's AM/PM designation for 12-hour clock. |
| 3716 | if (__mod || __num) |
| 3717 | __err |= ios_base::failbit; |
| 3718 | else |
| 3719 | { |
| 3720 | // Can't use std::time_get here as it can't parse %p |
| 3721 | // in isolation without %I. This might be faster anyway. |
| 3722 | const _CharT* __ampms[2]; |
| 3723 | __tmpunct._M_am_pm(__ampms); |
| 3724 | int __n = 0, __which = 3; |
| 3725 | while (__which != 0) |
| 3726 | { |
| 3727 | auto __i = __is.peek(); |
| 3728 | if (_Traits::eq_int_type(__i, _Traits::eof())) |
| 3729 | { |
| 3730 | __err |= ios_base::eofbit | ios_base::failbit; |
| 3731 | break; |
| 3732 | } |
| 3733 | __i = std::toupper(_Traits::to_char_type(__i), __loc); |
| 3734 | if (__which & 1) |
| 3735 | { |
| 3736 | if (__i != std::toupper(__ampms[0][__n], __loc)) |
| 3737 | __which ^= 1; |
| 3738 | else if (__ampms[0][__n + 1] == _CharT()) |
| 3739 | { |
| 3740 | __which = 1; |
| 3741 | (void) __is.get(); |
| 3742 | break; |
| 3743 | } |
| 3744 | } |
| 3745 | if (__which & 2) |
| 3746 | { |
| 3747 | if (__i != std::toupper(__ampms[1][__n], __loc)) |
| 3748 | __which ^= 2; |
| 3749 | else if (__ampms[1][__n + 1] == _CharT()) |
| 3750 | { |
| 3751 | __which = 2; |
| 3752 | (void) __is.get(); |
| 3753 | break; |
| 3754 | } |
| 3755 | } |
| 3756 | if (__which) |
| 3757 | (void) __is.get(); |
| 3758 | ++__n; |
| 3759 | } |
| 3760 | if (__which == 0 || __which == 3) |
| 3761 | __err |= ios_base::failbit; |
| 3762 | else |
| 3763 | __ampm = __which; |
| 3764 | } |
| 3765 | break; |
| 3766 | |
| 3767 | case 'r': // Locale's 12-hour time. |
| 3768 | if (__mod || __num) |
| 3769 | __err |= ios_base::failbit; |
| 3770 | else |
| 3771 | { |
| 3772 | struct tm __tm{}; |
| 3773 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3774 | __fmt - 2, __fmt); |
| 3775 | if (!__is_failed(__err)) |
| 3776 | { |
| 3777 | __h = hours(__tm.tm_hour); |
| 3778 | __min = minutes(__tm.tm_min); |
| 3779 | __s = seconds(__tm.tm_sec); |
| 3780 | } |
| 3781 | } |
| 3782 | __parts |= _ChronoParts::_TimeOfDay; |
| 3783 | break; |
| 3784 | |
| 3785 | case 'R': // %H:%M |
| 3786 | case 'T': // %H:%M:%S |
| 3787 | if (__mod || __num) [[unlikely]] |
| 3788 | { |
| 3789 | __err |= ios_base::failbit; |
| 3790 | break; |
| 3791 | } |
| 3792 | else |
| 3793 | { |
| 3794 | auto __val = __read_unsigned(2); |
| 3795 | if (__val == -1 || __val > 23) [[unlikely]] |
| 3796 | { |
| 3797 | if (_M_need & _ChronoParts::_TimeOfDay) |
| 3798 | __err |= ios_base::failbit; |
| 3799 | break; |
| 3800 | } |
| 3801 | if (!__read_chr(':')) [[unlikely]] |
| 3802 | break; |
| 3803 | __h = hours(__val); |
| 3804 | |
| 3805 | __val = __read_unsigned(2); |
| 3806 | if (__val == -1 || __val > 60) [[unlikely]] |
| 3807 | { |
| 3808 | if (_M_need & _ChronoParts::_TimeOfDay) |
| 3809 | __err |= ios_base::failbit; |
| 3810 | break; |
| 3811 | } |
| 3812 | __min = minutes(__val); |
| 3813 | |
| 3814 | if (__c == 'R') |
| 3815 | { |
| 3816 | __parts |= _ChronoParts::_TimeOfDay; |
| 3817 | break; |
| 3818 | } |
| 3819 | else if (!__read_chr(':')) [[unlikely]] |
| 3820 | break; |
| 3821 | } |
| 3822 | [[fallthrough]]; |
| 3823 | |
| 3824 | case 'S': // Seconds |
| 3825 | if (__mod == 'E') [[unlikely]] |
| 3826 | __err |= ios_base::failbit; |
| 3827 | else if (__mod == 'O') |
| 3828 | { |
| 3829 | struct tm __tm{}; |
| 3830 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3831 | __fmt - 3, __fmt); |
| 3832 | if (!__is_failed(__err)) |
| 3833 | __s = seconds(__tm.tm_sec); |
| 3834 | } |
| 3835 | else if constexpr (_Duration::period::den == 1 |
| 3836 | && !__is_floating) |
| 3837 | { |
| 3838 | auto __val = __read_unsigned(__num ? __num : 2); |
| 3839 | if (0 <= __val && __val <= 59) [[likely]] |
| 3840 | __s = seconds(__val); |
| 3841 | else |
| 3842 | { |
| 3843 | if (_M_need & _ChronoParts::_TimeOfDay) |
| 3844 | __err |= ios_base::failbit; |
| 3845 | break; |
| 3846 | } |
| 3847 | } |
| 3848 | else // Read fractional seconds |
| 3849 | { |
| 3850 | basic_stringstream<_CharT> __buf; |
| 3851 | auto __digit = _S_try_read_digit(__is, __err); |
| 3852 | if (__digit != -1) |
| 3853 | { |
| 3854 | __buf.put(_CharT('0') + __digit); |
| 3855 | __digit = _S_try_read_digit(__is, __err); |
| 3856 | if (__digit != -1) |
| 3857 | __buf.put(_CharT('0') + __digit); |
| 3858 | } |
| 3859 | |
| 3860 | auto __i = __is.peek(); |
| 3861 | if (_Traits::eq_int_type(__i, _Traits::eof())) |
| 3862 | __err |= ios_base::eofbit; |
| 3863 | else |
| 3864 | { |
| 3865 | _CharT __dp = '.'; |
| 3866 | if (__loc != locale::classic()) |
| 3867 | { |
| 3868 | auto& __np = use_facet<numpunct<_CharT>>(__loc); |
| 3869 | __dp = __np.decimal_point(); |
| 3870 | } |
| 3871 | _CharT __c = _Traits::to_char_type(__i); |
| 3872 | if (__c == __dp) |
| 3873 | { |
| 3874 | (void) __is.get(); |
| 3875 | __buf.put('.'); |
| 3876 | int __prec |
| 3877 | = hh_mm_ss<_Duration>::fractional_width; |
| 3878 | do |
| 3879 | { |
| 3880 | __digit = _S_try_read_digit(__is, __err); |
| 3881 | if (__digit != -1) |
| 3882 | __buf.put(_CharT('0') + __digit); |
| 3883 | else |
| 3884 | break; |
| 3885 | } |
| 3886 | while (--__prec); |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | if (!__is_failed(__err)) [[likely]] |
| 3891 | { |
| 3892 | long double __val{}; |
| 3893 | #if __cpp_lib_to_chars |
| 3894 | string __str = std::move(__buf).str(); |
| 3895 | auto __first = __str.data(); |
| 3896 | auto __last = __first + __str.size(); |
| 3897 | using enum chars_format; |
| 3898 | auto [ptr, ec] = std::from_chars(__first, __last, |
| 3899 | value&: __val, fmt: fixed); |
| 3900 | if ((bool)ec || ptr != __last) [[unlikely]] |
| 3901 | __err |= ios_base::failbit; |
| 3902 | else |
| 3903 | #else |
| 3904 | if (__buf >> __val) |
| 3905 | #endif |
| 3906 | { |
| 3907 | duration<long double> __fs(__val); |
| 3908 | if constexpr (__is_floating) |
| 3909 | __s = __fs; |
| 3910 | else |
| 3911 | __s = chrono::round<_Duration>(__fs); |
| 3912 | } |
| 3913 | } |
| 3914 | } |
| 3915 | __parts |= _ChronoParts::_TimeOfDay; |
| 3916 | break; |
| 3917 | |
| 3918 | case 'u': // ISO weekday (1-7) |
| 3919 | case 'w': // Weekday (0-6) |
| 3920 | if (__mod == 'E') [[unlikely]] |
| 3921 | __err |= ios_base::failbit; |
| 3922 | else if (__mod == 'O') |
| 3923 | { |
| 3924 | if (__c == 'w') |
| 3925 | { |
| 3926 | struct tm __tm{}; |
| 3927 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3928 | __fmt - 3, __fmt); |
| 3929 | if (!__is_failed(__err)) |
| 3930 | __wday = weekday(__tm.tm_wday); |
| 3931 | } |
| 3932 | else |
| 3933 | __err |= ios_base::failbit; |
| 3934 | } |
| 3935 | else |
| 3936 | { |
| 3937 | const int __lo = __c == 'u' ? 1 : 0; |
| 3938 | const int __hi = __lo + 6; |
| 3939 | auto __val = __read_unsigned(__num ? __num : 1); |
| 3940 | if (__lo <= __val && __val <= __hi) |
| 3941 | __wday = weekday(__val); |
| 3942 | else |
| 3943 | { |
| 3944 | __wday = __bad_wday; |
| 3945 | break; |
| 3946 | } |
| 3947 | } |
| 3948 | __parts |= _ChronoParts::_Weekday; |
| 3949 | break; |
| 3950 | |
| 3951 | case 'U': // Week number of the year (from first Sunday). |
| 3952 | case 'V': // ISO week-based week number. |
| 3953 | case 'W': // Week number of the year (from first Monday). |
| 3954 | if (__mod == 'E') [[unlikely]] |
| 3955 | __err |= ios_base::failbit; |
| 3956 | else if (__mod == 'O') |
| 3957 | { |
| 3958 | if (__c == 'V') [[unlikely]] |
| 3959 | __err |= ios_base::failbit; |
| 3960 | else |
| 3961 | { |
| 3962 | // TODO nl_langinfo_l(ALT_DIGITS) ? |
| 3963 | // Not implementable using std::time_get. |
| 3964 | } |
| 3965 | } |
| 3966 | else |
| 3967 | { |
| 3968 | const int __lo = __c == 'V' ? 1 : 0; |
| 3969 | const int __hi = 53; |
| 3970 | auto __val = __read_unsigned(__num ? __num : 2); |
| 3971 | if (__lo <= __val && __val <= __hi) |
| 3972 | { |
| 3973 | switch (__c) |
| 3974 | { |
| 3975 | case 'U': |
| 3976 | __sunday_wk = __val; |
| 3977 | break; |
| 3978 | case 'V': |
| 3979 | __iso_wk = __val; |
| 3980 | break; |
| 3981 | case 'W': |
| 3982 | __monday_wk = __val; |
| 3983 | break; |
| 3984 | } |
| 3985 | } |
| 3986 | else |
| 3987 | __iso_wk = __sunday_wk = __monday_wk = -1; |
| 3988 | } |
| 3989 | // N.B. do not alter __parts here, done after loop. |
| 3990 | break; |
| 3991 | |
| 3992 | case 'x': // Locale's date representation. |
| 3993 | if (__mod == 'O' || __num) [[unlikely]] |
| 3994 | __err |= ios_base::failbit; |
| 3995 | else |
| 3996 | { |
| 3997 | struct tm __tm{}; |
| 3998 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 3999 | __fmt - 2 - (__mod == 'E'), __fmt); |
| 4000 | if (!__is_failed(__err)) |
| 4001 | { |
| 4002 | __y = year(__tm.tm_year + 1900); |
| 4003 | __m = month(__tm.tm_mon + 1); |
| 4004 | __d = day(__tm.tm_mday); |
| 4005 | } |
| 4006 | } |
| 4007 | __parts |= _ChronoParts::_Date; |
| 4008 | break; |
| 4009 | |
| 4010 | case 'X': // Locale's time representation. |
| 4011 | if (__mod == 'O' || __num) [[unlikely]] |
| 4012 | __err |= ios_base::failbit; |
| 4013 | else |
| 4014 | { |
| 4015 | struct tm __tm{}; |
| 4016 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 4017 | __fmt - 2 - (__mod == 'E'), __fmt); |
| 4018 | if (!__is_failed(__err)) |
| 4019 | { |
| 4020 | __h = hours(__tm.tm_hour); |
| 4021 | __min = minutes(__tm.tm_min); |
| 4022 | __s = seconds(__tm.tm_sec); |
| 4023 | } |
| 4024 | } |
| 4025 | __parts |= _ChronoParts::_TimeOfDay; |
| 4026 | break; |
| 4027 | |
| 4028 | case 'y': // Last two digits of year. |
| 4029 | if (__mod) [[unlikely]] |
| 4030 | { |
| 4031 | struct tm __tm{}; |
| 4032 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 4033 | __fmt - 3, __fmt); |
| 4034 | if (!__is_failed(__err)) |
| 4035 | { |
| 4036 | int __cent = __tm.tm_year < 2000 ? 1900 : 2000; |
| 4037 | __yy = year(__tm.tm_year - __cent); |
| 4038 | if (__century == -1) // No %C has been parsed yet. |
| 4039 | __century = __cent; |
| 4040 | } |
| 4041 | } |
| 4042 | else |
| 4043 | { |
| 4044 | auto __val = __read_unsigned(__num ? __num : 2); |
| 4045 | if (__val >= 0 && __val <= 99) |
| 4046 | { |
| 4047 | __yy = year(__val); |
| 4048 | if (__century == -1) // No %C has been parsed yet. |
| 4049 | __century = __val < 69 ? 2000 : 1900; |
| 4050 | } |
| 4051 | else |
| 4052 | __y = __yy = __iso_yy = __iso_y = __bad_y; |
| 4053 | } |
| 4054 | __parts |= _ChronoParts::_Year; |
| 4055 | break; |
| 4056 | |
| 4057 | case 'Y': // Year |
| 4058 | if (__mod == 'O') [[unlikely]] |
| 4059 | __err |= ios_base::failbit; |
| 4060 | else if (__mod == 'E') |
| 4061 | { |
| 4062 | struct tm __tm{}; |
| 4063 | __tmget.get(__is, {}, __is, __err, &__tm, |
| 4064 | __fmt - 3, __fmt); |
| 4065 | if (!__is_failed(__err)) |
| 4066 | __y = year(__tm.tm_year); |
| 4067 | } |
| 4068 | else |
| 4069 | { |
| 4070 | auto __val = __read_unsigned(__num ? __num : 4); |
| 4071 | if (!__is_failed(__err)) |
| 4072 | __y = year(__val); |
| 4073 | } |
| 4074 | __parts |= _ChronoParts::_Year; |
| 4075 | break; |
| 4076 | |
| 4077 | case 'z': |
| 4078 | if (__num) [[unlikely]] |
| 4079 | __err |= ios_base::failbit; |
| 4080 | else |
| 4081 | { |
| 4082 | // For %Ez and %Oz read [+|-][h]h[:mm]. |
| 4083 | // For %z read [+|-]hh[mm]. |
| 4084 | |
| 4085 | auto __i = __is.peek(); |
| 4086 | if (_Traits::eq_int_type(__i, _Traits::eof())) |
| 4087 | { |
| 4088 | __err |= ios_base::eofbit | ios_base::failbit; |
| 4089 | break; |
| 4090 | } |
| 4091 | _CharT __ic = _Traits::to_char_type(__i); |
| 4092 | const bool __neg = __ic == _CharT('-'); |
| 4093 | if (__ic == _CharT('-') || __ic == _CharT('+')) |
| 4094 | (void) __is.get(); |
| 4095 | |
| 4096 | int_least32_t __hh; |
| 4097 | if (__mod) |
| 4098 | { |
| 4099 | // Read h[h] |
| 4100 | __hh = __read_unsigned(2); |
| 4101 | } |
| 4102 | else |
| 4103 | { |
| 4104 | // Read hh |
| 4105 | __hh = 10 * _S_try_read_digit(__is, __err); |
| 4106 | __hh += _S_try_read_digit(__is, __err); |
| 4107 | } |
| 4108 | |
| 4109 | if (__is_failed(__err)) |
| 4110 | break; |
| 4111 | |
| 4112 | __i = __is.peek(); |
| 4113 | if (_Traits::eq_int_type(__i, _Traits::eof())) |
| 4114 | { |
| 4115 | __err |= ios_base::eofbit; |
| 4116 | __tz_offset = minutes(__hh * (__neg ? -60 : 60)); |
| 4117 | break; |
| 4118 | } |
| 4119 | __ic = _Traits::to_char_type(__i); |
| 4120 | |
| 4121 | bool __read_mm = false; |
| 4122 | if (__mod) |
| 4123 | { |
| 4124 | if (__ic == _GLIBCXX_WIDEN(":" )[0]) |
| 4125 | { |
| 4126 | // Read [:mm] part. |
| 4127 | (void) __is.get(); |
| 4128 | __read_mm = true; |
| 4129 | } |
| 4130 | } |
| 4131 | else if (_CharT('0') <= __ic && __ic <= _CharT('9')) |
| 4132 | { |
| 4133 | // Read [mm] part. |
| 4134 | __read_mm = true; |
| 4135 | } |
| 4136 | |
| 4137 | int_least32_t __mm = 0; |
| 4138 | if (__read_mm) |
| 4139 | { |
| 4140 | __mm = 10 * _S_try_read_digit(__is, __err); |
| 4141 | __mm += _S_try_read_digit(__is, __err); |
| 4142 | } |
| 4143 | |
| 4144 | if (!__is_failed(__err)) |
| 4145 | { |
| 4146 | auto __z = __hh * 60 + __mm; |
| 4147 | __tz_offset = minutes(__neg ? -__z : __z); |
| 4148 | } |
| 4149 | } |
| 4150 | break; |
| 4151 | |
| 4152 | case 'Z': |
| 4153 | if (__mod || __num) [[unlikely]] |
| 4154 | __err |= ios_base::failbit; |
| 4155 | else |
| 4156 | { |
| 4157 | basic_string_view<_CharT> __x = _GLIBCXX_WIDEN("_/-+" ); |
| 4158 | __tz_abbr.clear(); |
| 4159 | while (true) |
| 4160 | { |
| 4161 | auto __i = __is.peek(); |
| 4162 | if (!_Traits::eq_int_type(__i, _Traits::eof())) |
| 4163 | { |
| 4164 | _CharT __a = _Traits::to_char_type(__i); |
| 4165 | if (std::isalnum(__a, __loc) |
| 4166 | || __x.find(__a) != __x.npos) |
| 4167 | { |
| 4168 | __tz_abbr.push_back(__a); |
| 4169 | (void) __is.get(); |
| 4170 | continue; |
| 4171 | } |
| 4172 | } |
| 4173 | else |
| 4174 | __err |= ios_base::eofbit; |
| 4175 | break; |
| 4176 | } |
| 4177 | if (__tz_abbr.empty()) |
| 4178 | __err |= ios_base::failbit; |
| 4179 | } |
| 4180 | break; |
| 4181 | |
| 4182 | case 'n': // Exactly one whitespace character. |
| 4183 | if (__mod || __num) [[unlikely]] |
| 4184 | __err |= ios_base::failbit; |
| 4185 | else |
| 4186 | { |
| 4187 | _CharT __i = __is.peek(); |
| 4188 | if (_Traits::eq_int_type(__i, _Traits::eof())) |
| 4189 | __err |= ios_base::eofbit | ios_base::failbit; |
| 4190 | else if (std::isspace(_Traits::to_char_type(__i), __loc)) |
| 4191 | (void) __is.get(); |
| 4192 | else |
| 4193 | __err |= ios_base::failbit; |
| 4194 | } |
| 4195 | break; |
| 4196 | |
| 4197 | case 't': // Zero or one whitespace characters. |
| 4198 | if (__mod || __num) [[unlikely]] |
| 4199 | __err |= ios_base::failbit; |
| 4200 | else |
| 4201 | { |
| 4202 | _CharT __i = __is.peek(); |
| 4203 | if (_Traits::eq_int_type(__i, _Traits::eof())) |
| 4204 | __err |= ios_base::eofbit; |
| 4205 | else if (std::isspace(_Traits::to_char_type(__i), __loc)) |
| 4206 | (void) __is.get(); |
| 4207 | } |
| 4208 | break; |
| 4209 | |
| 4210 | case '%': // A % character. |
| 4211 | if (__mod || __num) [[unlikely]] |
| 4212 | __err |= ios_base::failbit; |
| 4213 | else |
| 4214 | __read_chr('%'); |
| 4215 | break; |
| 4216 | |
| 4217 | case 'O': // Modifiers |
| 4218 | case 'E': |
| 4219 | if (__mod || __num) [[unlikely]] |
| 4220 | { |
| 4221 | __err |= ios_base::failbit; |
| 4222 | break; |
| 4223 | } |
| 4224 | __mod = __c; |
| 4225 | continue; |
| 4226 | |
| 4227 | default: |
| 4228 | if (_CharT('1') <= __c && __c <= _CharT('9')) |
| 4229 | { |
| 4230 | if (!__mod) [[likely]] |
| 4231 | { |
| 4232 | // %Nx - extract positive decimal integer N |
| 4233 | auto __end = __fmt + _Traits::length(__fmt); |
| 4234 | auto [__v, __ptr] |
| 4235 | = __format::__parse_integer(__fmt - 1, __end); |
| 4236 | if (__ptr) [[likely]] |
| 4237 | { |
| 4238 | __num = __v; |
| 4239 | __fmt = __ptr; |
| 4240 | continue; |
| 4241 | } |
| 4242 | } |
| 4243 | } |
| 4244 | __err |= ios_base::failbit; |
| 4245 | } |
| 4246 | |
| 4247 | if (__is_failed(__err)) [[unlikely]] |
| 4248 | break; |
| 4249 | |
| 4250 | __is_flag = false; |
| 4251 | __num = 0; |
| 4252 | __mod = _CharT(); |
| 4253 | } |
| 4254 | |
| 4255 | if (__century >= 0) |
| 4256 | { |
| 4257 | if (__yy != __bad_y && __y == __bad_y) |
| 4258 | __y = years(__century) + __yy; // Use %y instead of %Y |
| 4259 | if (__iso_yy != __bad_y && __iso_y == __bad_y) |
| 4260 | __iso_y = years(__century) + __iso_yy; // Use %g instead of %G |
| 4261 | } |
| 4262 | |
| 4263 | bool __can_use_doy = false; |
| 4264 | bool __can_use_iso_wk = false; |
| 4265 | bool __can_use_sun_wk = false; |
| 4266 | bool __can_use_mon_wk = false; |
| 4267 | |
| 4268 | // A year + day-of-year can be converted to a full date. |
| 4269 | if (__y != __bad_y && __dayofyear >= 0) |
| 4270 | { |
| 4271 | __can_use_doy = true; |
| 4272 | __parts |= _ChronoParts::_Date; |
| 4273 | } |
| 4274 | else if (__y != __bad_y && __wday != __bad_wday && __sunday_wk >= 0) |
| 4275 | { |
| 4276 | __can_use_sun_wk = true; |
| 4277 | __parts |= _ChronoParts::_Date; |
| 4278 | } |
| 4279 | else if (__y != __bad_y && __wday != __bad_wday && __monday_wk >= 0) |
| 4280 | { |
| 4281 | __can_use_mon_wk = true; |
| 4282 | __parts |= _ChronoParts::_Date; |
| 4283 | } |
| 4284 | else if (__iso_y != __bad_y && __wday != __bad_wday && __iso_wk > 0) |
| 4285 | { |
| 4286 | // An ISO week date can be converted to a full date. |
| 4287 | __can_use_iso_wk = true; |
| 4288 | __parts |= _ChronoParts::_Date; |
| 4289 | } |
| 4290 | |
| 4291 | if (__is_failed(__err)) [[unlikely]] |
| 4292 | ; // Don't bother doing any more work. |
| 4293 | else if (__is_flag) [[unlikely]] // incomplete format flag |
| 4294 | __err |= ios_base::failbit; |
| 4295 | else if ((_M_need & __parts) == _M_need) [[likely]] |
| 4296 | { |
| 4297 | // We try to avoid calculating _M_sys_days and _M_ymd unless |
| 4298 | // necessary, because converting sys_days to year_month_day |
| 4299 | // (or vice versa) requires non-trivial calculations. |
| 4300 | // If we have y/m/d values then use them to populate _M_ymd |
| 4301 | // and only convert it to _M_sys_days if the caller needs that. |
| 4302 | // But if we don't have y/m/d and need to calculate the date |
| 4303 | // from the day-of-year or a week+weekday then we set _M_sys_days |
| 4304 | // and only convert it to _M_ymd if the caller needs that. |
| 4305 | |
| 4306 | // We do more error checking here, but only for the fields that |
| 4307 | // we actually need to use. For example, we will not diagnose |
| 4308 | // an invalid dayofyear==366 for non-leap years unless actually |
| 4309 | // using __dayofyear. This should mean we never produce invalid |
| 4310 | // results, but it means not all invalid inputs are diagnosed, |
| 4311 | // e.g. "2023-01-01 366" >> "%F %j" ignores the invalid 366. |
| 4312 | // We also do not diagnose inconsistent values for the same |
| 4313 | // field, e.g. "2021 2022 2023" >> "%C%y %Y %Y" just uses 2023. |
| 4314 | |
| 4315 | // Whether the caller wants _M_wd. |
| 4316 | // The _Weekday bit is only set for chrono::weekday. |
| 4317 | const bool __need_wday = _M_need & _ChronoParts::_Weekday; |
| 4318 | |
| 4319 | // Whether the caller wants _M_sys_days and _M_time. |
| 4320 | // Only true for durations and time_points. |
| 4321 | const bool __need_time = _M_need & _ChronoParts::_TimeOfDay; |
| 4322 | |
| 4323 | if (__need_wday && __wday != __bad_wday) |
| 4324 | _M_wd = __wday; // Caller only wants a weekday and we have one. |
| 4325 | else if (_M_need & _ChronoParts::_Date) // subsumes __need_wday |
| 4326 | { |
| 4327 | // Whether the caller wants _M_ymd. |
| 4328 | // True for chrono::year etc., false for time_points. |
| 4329 | const bool __need_ymd = !__need_wday && !__need_time; |
| 4330 | |
| 4331 | if ((_M_need & _ChronoParts::_Year && __y == __bad_y) |
| 4332 | || (_M_need & _ChronoParts::_Month && __m == __bad_mon) |
| 4333 | || (_M_need & _ChronoParts::_Day && __d == __bad_day)) |
| 4334 | { |
| 4335 | // Missing at least one of y/m/d so calculate sys_days |
| 4336 | // from the other data we have available. |
| 4337 | |
| 4338 | if (__can_use_doy) |
| 4339 | { |
| 4340 | if ((0 < __dayofyear && __dayofyear <= 365) |
| 4341 | || (__dayofyear == 366 && __y.is_leap())) |
| 4342 | [[likely]] |
| 4343 | { |
| 4344 | _M_sys_days = sys_days(__y/January/1) |
| 4345 | + days(__dayofyear - 1); |
| 4346 | if (__need_ymd) |
| 4347 | _M_ymd = year_month_day(_M_sys_days); |
| 4348 | } |
| 4349 | else |
| 4350 | __err |= ios_base::failbit; |
| 4351 | } |
| 4352 | else if (__can_use_iso_wk) |
| 4353 | { |
| 4354 | // Calculate y/m/d from ISO week date. |
| 4355 | |
| 4356 | if (__iso_wk == 53) |
| 4357 | { |
| 4358 | // A year has 53 weeks iff Jan 1st is a Thursday |
| 4359 | // or Jan 1 is a Wednesday and it's a leap year. |
| 4360 | const sys_days __jan4(__iso_y/January/4); |
| 4361 | weekday __wd1(__jan4 - days(3)); |
| 4362 | if (__wd1 != Thursday) |
| 4363 | if (__wd1 != Wednesday || !__iso_y.is_leap()) |
| 4364 | __err |= ios_base::failbit; |
| 4365 | } |
| 4366 | |
| 4367 | if (!__is_failed(__err)) [[likely]] |
| 4368 | { |
| 4369 | // First Thursday is always in week one: |
| 4370 | sys_days __w(Thursday[1]/January/__iso_y); |
| 4371 | // First day of week-based year: |
| 4372 | __w -= Thursday - Monday; |
| 4373 | __w += days(weeks(__iso_wk - 1)); |
| 4374 | __w += __wday - Monday; |
| 4375 | _M_sys_days = __w; |
| 4376 | |
| 4377 | if (__need_ymd) |
| 4378 | _M_ymd = year_month_day(_M_sys_days); |
| 4379 | } |
| 4380 | } |
| 4381 | else if (__can_use_sun_wk) |
| 4382 | { |
| 4383 | // Calculate y/m/d from week number + weekday. |
| 4384 | sys_days __wk1(__y/January/Sunday[1]); |
| 4385 | _M_sys_days = __wk1 + weeks(__sunday_wk - 1) |
| 4386 | + days(__wday.c_encoding()); |
| 4387 | _M_ymd = year_month_day(_M_sys_days); |
| 4388 | if (_M_ymd.year() != __y) [[unlikely]] |
| 4389 | __err |= ios_base::failbit; |
| 4390 | } |
| 4391 | else if (__can_use_mon_wk) |
| 4392 | { |
| 4393 | // Calculate y/m/d from week number + weekday. |
| 4394 | sys_days __wk1(__y/January/Monday[1]); |
| 4395 | _M_sys_days = __wk1 + weeks(__monday_wk - 1) |
| 4396 | + days(__wday.c_encoding() - 1); |
| 4397 | _M_ymd = year_month_day(_M_sys_days); |
| 4398 | if (_M_ymd.year() != __y) [[unlikely]] |
| 4399 | __err |= ios_base::failbit; |
| 4400 | } |
| 4401 | else // Should not be able to get here. |
| 4402 | __err |= ios_base::failbit; |
| 4403 | } |
| 4404 | else |
| 4405 | { |
| 4406 | // We know that all fields the caller needs are present, |
| 4407 | // but check that their values are in range. |
| 4408 | // Make unwanted fields valid so that _M_ymd.ok() is true. |
| 4409 | |
| 4410 | if (_M_need & _ChronoParts::_Year) |
| 4411 | { |
| 4412 | if (!__y.ok()) [[unlikely]] |
| 4413 | __err |= ios_base::failbit; |
| 4414 | } |
| 4415 | else if (__y == __bad_y) |
| 4416 | __y = 1972y; // Leap year so that Feb 29 is valid. |
| 4417 | |
| 4418 | if (_M_need & _ChronoParts::_Month) |
| 4419 | { |
| 4420 | if (!__m.ok()) [[unlikely]] |
| 4421 | __err |= ios_base::failbit; |
| 4422 | } |
| 4423 | else if (__m == __bad_mon) |
| 4424 | __m = January; |
| 4425 | |
| 4426 | if (_M_need & _ChronoParts::_Day) |
| 4427 | { |
| 4428 | if (__d < day(1) || __d > (__y/__m/last).day()) |
| 4429 | __err |= ios_base::failbit; |
| 4430 | } |
| 4431 | else if (__d == __bad_day) |
| 4432 | __d = 1d; |
| 4433 | |
| 4434 | if (year_month_day __ymd(__y, __m, __d); __ymd.ok()) |
| 4435 | { |
| 4436 | _M_ymd = __ymd; |
| 4437 | if (__need_wday || __need_time) |
| 4438 | _M_sys_days = sys_days(_M_ymd); |
| 4439 | } |
| 4440 | else [[unlikely]] |
| 4441 | __err |= ios_base::failbit; |
| 4442 | } |
| 4443 | |
| 4444 | if (__need_wday) |
| 4445 | _M_wd = weekday(_M_sys_days); |
| 4446 | } |
| 4447 | |
| 4448 | // Need to set _M_time for both durations and time_points. |
| 4449 | if (__need_time) |
| 4450 | { |
| 4451 | if (__h == __bad_h && __h12 != __bad_h) |
| 4452 | { |
| 4453 | if (__ampm == 1) |
| 4454 | __h = __h12 == hours(12) ? hours(0) : __h12; |
| 4455 | else if (__ampm == 2) |
| 4456 | __h = __h12 == hours(12) ? __h12 : __h12 + hours(12); |
| 4457 | else [[unlikely]] |
| 4458 | __err |= ios_base::failbit; |
| 4459 | } |
| 4460 | |
| 4461 | auto __t = _M_time.zero(); |
| 4462 | bool __ok = false; |
| 4463 | |
| 4464 | if (__h != __bad_h) |
| 4465 | { |
| 4466 | __ok = true; |
| 4467 | __t += __h; |
| 4468 | } |
| 4469 | |
| 4470 | if (__min != __bad_min) |
| 4471 | { |
| 4472 | __ok = true; |
| 4473 | __t += __min; |
| 4474 | } |
| 4475 | |
| 4476 | if (__s != __bad_sec) |
| 4477 | { |
| 4478 | __ok = true; |
| 4479 | __t += __s; |
| 4480 | _M_is_leap_second = __s >= seconds(60); |
| 4481 | } |
| 4482 | |
| 4483 | if (__ok) |
| 4484 | _M_time = __t; |
| 4485 | else |
| 4486 | __err |= ios_base::failbit; |
| 4487 | } |
| 4488 | |
| 4489 | if (!__is_failed(__err)) [[likely]] |
| 4490 | { |
| 4491 | if (__offset && __tz_offset != __bad_min) |
| 4492 | *__offset = __tz_offset; |
| 4493 | if (__abbrev && !__tz_abbr.empty()) |
| 4494 | *__abbrev = std::move(__tz_abbr); |
| 4495 | } |
| 4496 | } |
| 4497 | else |
| 4498 | __err |= ios_base::failbit; |
| 4499 | } |
| 4500 | if (__err) |
| 4501 | __is.setstate(__err); |
| 4502 | return __is; |
| 4503 | } |
| 4504 | /// @endcond |
| 4505 | #undef _GLIBCXX_WIDEN |
| 4506 | |
| 4507 | /// @} group chrono |
| 4508 | } // namespace chrono |
| 4509 | |
| 4510 | _GLIBCXX_END_NAMESPACE_VERSION |
| 4511 | } // namespace std |
| 4512 | |
| 4513 | #endif // C++20 |
| 4514 | |
| 4515 | #endif //_GLIBCXX_CHRONO_IO_H |
| 4516 | |