25 Time library [time]

25.1 General [time.general]

This Clause describes the chrono library ([time.syn]) and various C functions ([ctime.syn]) that provide generally useful time utilities, as summarized in Table 86.
Table 86 — Time library summary
Subclause
Header(s)
Cpp17Clock requirements
Time-related traits
<chrono>
Class template duration
Class template time_­point
Clocks
Civil calendar
Class template time_­of_­day
Time zones
Formatting
Parsing
C library time utilities
<ctime>

25.2 Header <chrono> synopsis [time.syn]

namespace std {
  namespace chrono {
    // [time.duration], class template duration
    template<class Rep, class Period = ratio<1>> class duration;

    // [time.point], class template time_­point
    template<class Clock, class Duration = typename Clock::duration> class time_point;
  }

  // [time.traits.specializations], common_­type specializations
  template<class Rep1, class Period1, class Rep2, class Period2>
    struct common_type<chrono::duration<Rep1, Period1>,
                       chrono::duration<Rep2, Period2>>;

  template<class Clock, class Duration1, class Duration2>
    struct common_type<chrono::time_point<Clock, Duration1>,
                       chrono::time_point<Clock, Duration2>>;

  namespace chrono {
    // [time.traits], customization traits
    template<class Rep> struct treat_as_floating_point;
    template<class Rep> struct duration_values;
    template<class Rep>
      inline constexpr bool treat_as_floating_point_v = treat_as_floating_point<Rep>::value;

    template<class T> struct is_clock;
    template<class T> inline constexpr bool is_clock_v = is_clock<T>::value;

    // [time.duration.nonmember], duration arithmetic
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
        operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
        operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
    template<class Rep1, class Period, class Rep2>
      constexpr duration<common_type_t<Rep1, Rep2>, Period>
        operator*(const duration<Rep1, Period>& d, const Rep2& s);
    template<class Rep1, class Rep2, class Period>
      constexpr duration<common_type_t<Rep1, Rep2>, Period>
        operator*(const Rep1& s, const duration<Rep2, Period>& d);
    template<class Rep1, class Period, class Rep2>
      constexpr duration<common_type_t<Rep1, Rep2>, Period>
        operator/(const duration<Rep1, Period>& d, const Rep2& s);
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr common_type_t<Rep1, Rep2>
        operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
    template<class Rep1, class Period, class Rep2>
      constexpr duration<common_type_t<Rep1, Rep2>, Period>
        operator%(const duration<Rep1, Period>& d, const Rep2& s);
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
        operator%(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);

    // [time.duration.comparisons], duration comparisons
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr bool operator==(const duration<Rep1, Period1>& lhs,
                                const duration<Rep2, Period2>& rhs);
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr bool operator!=(const duration<Rep1, Period1>& lhs,
                                const duration<Rep2, Period2>& rhs);
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr bool operator< (const duration<Rep1, Period1>& lhs,
                                const duration<Rep2, Period2>& rhs);
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr bool operator> (const duration<Rep1, Period1>& lhs,
                                const duration<Rep2, Period2>& rhs);
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr bool operator<=(const duration<Rep1, Period1>& lhs,
                                const duration<Rep2, Period2>& rhs);
    template<class Rep1, class Period1, class Rep2, class Period2>
      constexpr bool operator>=(const duration<Rep1, Period1>& lhs,
                                const duration<Rep2, Period2>& rhs);

    // [time.duration.cast], duration_­cast
    template<class ToDuration, class Rep, class Period>
      constexpr ToDuration duration_cast(const duration<Rep, Period>& d);
    template<class ToDuration, class Rep, class Period>
      constexpr ToDuration floor(const duration<Rep, Period>& d);
    template<class ToDuration, class Rep, class Period>
      constexpr ToDuration ceil(const duration<Rep, Period>& d);
    template<class ToDuration, class Rep, class Period>
      constexpr ToDuration round(const duration<Rep, Period>& d);

    // [time.duration.io], duration I/O
    template<class charT, class traits, class Rep, class Period>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os,
                   const duration<Rep, Period>& d);
    template<class charT, class traits, class Rep, class Period>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt,
                  const duration<Rep, Period>& d);
    template<class charT, class traits, class Rep, class Period, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    duration<Rep, Period>& d,
                    basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // convenience typedefs
    using nanoseconds  = duration<signed integer type of at least 64 bits, nano>;
    using microseconds = duration<signed integer type of at least 55 bits, micro>;
    using milliseconds = duration<signed integer type of at least 45 bits, milli>;
    using seconds      = duration<signed integer type of at least 35 bits>;
    using minutes      = duration<signed integer type of at least 29 bits, ratio<  60>>;
    using hours        = duration<signed integer type of at least 23 bits, ratio<3600>>;
    using days         = duration<signed integer type of at least 25 bits,
                                  ratio_multiply<ratio<24>, hours::period>>;
    using weeks        = duration<signed integer type of at least 22 bits,
                                  ratio_multiply<ratio<7>, days::period>>;
    using years        = duration<signed integer type of at least 17 bits,
                                  ratio_multiply<ratio<146097, 400>, days::period>>;
    using months       = duration<signed integer type of at least 20 bits,
                                  ratio_divide<years::period, ratio<12>>>;

    // [time.point.nonmember], time_­point arithmetic
    template<class Clock, class Duration1, class Rep2, class Period2>
      constexpr time_point<Clock, common_type_t<Duration1, duration<Rep2, Period2>>>
        operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
    template<class Rep1, class Period1, class Clock, class Duration2>
      constexpr time_point<Clock, common_type_t<duration<Rep1, Period1>, Duration2>>
        operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs);
    template<class Clock, class Duration1, class Rep2, class Period2>
      constexpr time_point<Clock, common_type_t<Duration1, duration<Rep2, Period2>>>
        operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
    template<class Clock, class Duration1, class Duration2>
      constexpr common_type_t<Duration1, Duration2>
        operator-(const time_point<Clock, Duration1>& lhs,
                  const time_point<Clock, Duration2>& rhs);

    // [time.point.comparisons], time_­point comparisons
    template<class Clock, class Duration1, class Duration2>
       constexpr bool operator==(const time_point<Clock, Duration1>& lhs,
                                 const time_point<Clock, Duration2>& rhs);
    template<class Clock, class Duration1, class Duration2>
       constexpr bool operator!=(const time_point<Clock, Duration1>& lhs,
                                 const time_point<Clock, Duration2>& rhs);
    template<class Clock, class Duration1, class Duration2>
       constexpr bool operator< (const time_point<Clock, Duration1>& lhs,
                                 const time_point<Clock, Duration2>& rhs);
    template<class Clock, class Duration1, class Duration2>
       constexpr bool operator> (const time_point<Clock, Duration1>& lhs,
                                 const time_point<Clock, Duration2>& rhs);
    template<class Clock, class Duration1, class Duration2>
       constexpr bool operator<=(const time_point<Clock, Duration1>& lhs,
                                 const time_point<Clock, Duration2>& rhs);
    template<class Clock, class Duration1, class Duration2>
       constexpr bool operator>=(const time_point<Clock, Duration1>& lhs,
                                 const time_point<Clock, Duration2>& rhs);

    // [time.point.cast], time_­point_­cast
    template<class ToDuration, class Clock, class Duration>
      constexpr time_point<Clock, ToDuration>
        time_point_cast(const time_point<Clock, Duration>& t);
    template<class ToDuration, class Clock, class Duration>
      constexpr time_point<Clock, ToDuration> floor(const time_point<Clock, Duration>& tp);
    template<class ToDuration, class Clock, class Duration>
      constexpr time_point<Clock, ToDuration> ceil(const time_point<Clock, Duration>& tp);
    template<class ToDuration, class Clock, class Duration>
      constexpr time_point<Clock, ToDuration> round(const time_point<Clock, Duration>& tp);

    // [time.duration.alg], specialized algorithms
    template<class Rep, class Period>
      constexpr duration<Rep, Period> abs(duration<Rep, Period> d);

    // [time.clock.system], class system_­clock
    class system_clock;

    template<class Duration>
      using sys_time  = time_point<system_clock, Duration>;
    using sys_seconds = sys_time<seconds>;
    using sys_days    = sys_time<days>;

    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const sys_time<Duration>& tp);

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const sys_days& dp);

    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt,
                  const sys_time<Duration>& tp);

    template<class charT, class traits, class Duration, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    sys_time<Duration>& tp,
                    basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.clock.utc], class utc_­clock
    class utc_clock;

    template<class Duration>
      using utc_time  = time_point<utc_clock, Duration>;
    using utc_seconds = utc_time<seconds>;

    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const utc_time<Duration>& t);
    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt,
                  const utc_time<Duration>& tp);
    template<class charT, class traits, class Duration, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    utc_time<Duration>& tp,
                    basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.clock.tai], class tai_­clock
    class tai_clock;

    template<class Duration>
      using tai_time  = time_point<tai_clock, Duration>;
    using tai_seconds = tai_time<seconds>;

    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const tai_time<Duration>& t);
    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt,
                  const tai_time<Duration>& tp);
    template<class charT, class traits, class Duration, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    tai_time<Duration>& tp,
                    basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.clock.gps], class gps_­clock
    class gps_clock;

    template<class Duration>
      using gps_time  = time_point<gps_clock, Duration>;
    using gps_seconds = gps_time<seconds>;

    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const gps_time<Duration>& t);
    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt,
                  const gps_time<Duration>& tp);
    template<class charT, class traits, class Duration, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    gps_time<Duration>& tp,
                    basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.clock.file], class file_­clock
    class file_clock;

    template<class Duration>
      using file_time = time_point<file_clock, Duration>;

    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const file_time<Duration>& tp);
    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt,
                  const file_time<Duration>& tp);
    template<class charT, class traits, class Duration, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    file_time<Duration>& tp,
                    basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.clock.steady], class steady_­clock
    class steady_clock;

    // [time.clock.hires], class high_­resolution_­clock
    class high_resolution_clock;

    // [time.clock.local], local time
    struct local_t {};
    template<class Duration>
      using local_time  = time_point<local_t, Duration>;
    using local_seconds = local_time<seconds>;
    using local_days    = local_time<days>;

    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const local_time<Duration>& tp);
    template<class charT, class traits, class Duration>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt,
                  const local_time<Duration>& tp,
                  const string* abbrev = nullptr, const seconds* offset_sec = nullptr);
    template<class charT, class traits, class Duration, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    local_time<Duration>& tp,
                    basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.clock.cast], time_­point conversions
    template<class DestClock, class SourceClock>
      struct clock_time_conversion;

    template<class DestClock, class SourceClock, class Duration>
      auto clock_cast(const time_point<SourceClock, Duration>& t);

    // [time.cal.last], class last_­spec
    struct last_spec;

    // [time.cal.day], class day
    class day;

    constexpr bool operator==(const day& x, const day& y) noexcept;
    constexpr bool operator!=(const day& x, const day& y) noexcept;
    constexpr bool operator< (const day& x, const day& y) noexcept;
    constexpr bool operator> (const day& x, const day& y) noexcept;
    constexpr bool operator<=(const day& x, const day& y) noexcept;
    constexpr bool operator>=(const day& x, const day& y) noexcept;

    constexpr day  operator+(const day&  x, const days& y) noexcept;
    constexpr day  operator+(const days& x, const day&  y) noexcept;
    constexpr day  operator-(const day&  x, const days& y) noexcept;
    constexpr days operator-(const day&  x, const day&  y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const day& d);
    template<class charT, class traits>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const day& d);
    template<class charT, class traits, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    day& d, basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.cal.month], class month
    class month;

    constexpr bool operator==(const month& x, const month& y) noexcept;
    constexpr bool operator!=(const month& x, const month& y) noexcept;
    constexpr bool operator< (const month& x, const month& y) noexcept;
    constexpr bool operator> (const month& x, const month& y) noexcept;
    constexpr bool operator<=(const month& x, const month& y) noexcept;
    constexpr bool operator>=(const month& x, const month& y) noexcept;

    constexpr month  operator+(const month&  x, const months& y) noexcept;
    constexpr month  operator+(const months& x,  const month& y) noexcept;
    constexpr month  operator-(const month&  x, const months& y) noexcept;
    constexpr months operator-(const month&  x,  const month& y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const month& m);
    template<class charT, class traits>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const month& m);
    template<class charT, class traits, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    month& m, basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.cal.year], class year
    class year;

    constexpr bool operator==(const year& x, const year& y) noexcept;
    constexpr bool operator!=(const year& x, const year& y) noexcept;
    constexpr bool operator< (const year& x, const year& y) noexcept;
    constexpr bool operator> (const year& x, const year& y) noexcept;
    constexpr bool operator<=(const year& x, const year& y) noexcept;
    constexpr bool operator>=(const year& x, const year& y) noexcept;

    constexpr year  operator+(const year&  x, const years& y) noexcept;
    constexpr year  operator+(const years& x, const year&  y) noexcept;
    constexpr year  operator-(const year&  x, const years& y) noexcept;
    constexpr years operator-(const year&  x, const year&  y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const year& y);

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const year& y);

    template<class charT, class traits, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    year& y, basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.cal.wd], class weekday
    class weekday;

    constexpr bool operator==(const weekday& x, const weekday& y) noexcept;
    constexpr bool operator!=(const weekday& x, const weekday& y) noexcept;

    constexpr weekday operator+(const weekday& x, const days&    y) noexcept;
    constexpr weekday operator+(const days&    x, const weekday& y) noexcept;
    constexpr weekday operator-(const weekday& x, const days&    y) noexcept;
    constexpr days    operator-(const weekday& x, const weekday& y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const weekday& wd);

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const weekday& wd);

    template<class charT, class traits, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    weekday& wd, basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.cal.wdidx], class weekday_­indexed
    class weekday_indexed;

    constexpr bool operator==(const weekday_indexed& x, const weekday_indexed& y) noexcept;
    constexpr bool operator!=(const weekday_indexed& x, const weekday_indexed& y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const weekday_indexed& wdi);

    // [time.cal.wdlast], class weekday_­last
    class weekday_last;

    constexpr bool operator==(const weekday_last& x, const weekday_last& y) noexcept;
    constexpr bool operator!=(const weekday_last& x, const weekday_last& y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const weekday_last& wdl);

    // [time.cal.md], class month_­day
    class month_day;

    constexpr bool operator==(const month_day& x, const month_day& y) noexcept;
    constexpr bool operator!=(const month_day& x, const month_day& y) noexcept;
    constexpr bool operator< (const month_day& x, const month_day& y) noexcept;
    constexpr bool operator> (const month_day& x, const month_day& y) noexcept;
    constexpr bool operator<=(const month_day& x, const month_day& y) noexcept;
    constexpr bool operator>=(const month_day& x, const month_day& y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const month_day& md);

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const month_day& md);

    template<class charT, class traits, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    month_day& md, basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.cal.mdlast], class month_­day_­last
    class month_day_last;

    constexpr bool operator==(const month_day_last& x, const month_day_last& y) noexcept;
    constexpr bool operator!=(const month_day_last& x, const month_day_last& y) noexcept;
    constexpr bool operator< (const month_day_last& x, const month_day_last& y) noexcept;
    constexpr bool operator> (const month_day_last& x, const month_day_last& y) noexcept;
    constexpr bool operator<=(const month_day_last& x, const month_day_last& y) noexcept;
    constexpr bool operator>=(const month_day_last& x, const month_day_last& y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const month_day_last& mdl);

    // [time.cal.mwd], class month_­weekday
    class month_weekday;

    constexpr bool operator==(const month_weekday& x, const month_weekday& y) noexcept;
    constexpr bool operator!=(const month_weekday& x, const month_weekday& y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const month_weekday& mwd);

    // [time.cal.mwdlast], class month_­weekday_­last
    class month_weekday_last;

    constexpr bool operator==(const month_weekday_last& x, const month_weekday_last& y) noexcept;
    constexpr bool operator!=(const month_weekday_last& x, const month_weekday_last& y) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const month_weekday_last& mwdl);

    // [time.cal.ym], class year_­month
    class year_month;

    constexpr bool operator==(const year_month& x, const year_month& y) noexcept;
    constexpr bool operator!=(const year_month& x, const year_month& y) noexcept;
    constexpr bool operator< (const year_month& x, const year_month& y) noexcept;
    constexpr bool operator> (const year_month& x, const year_month& y) noexcept;
    constexpr bool operator<=(const year_month& x, const year_month& y) noexcept;
    constexpr bool operator>=(const year_month& x, const year_month& y) noexcept;

    constexpr year_month operator+(const year_month& ym, const months& dm) noexcept;
    constexpr year_month operator+(const months& dm, const year_month& ym) noexcept;
    constexpr year_month operator-(const year_month& ym, const months& dm) noexcept;
    constexpr months operator-(const year_month& x, const year_month& y) noexcept;
    constexpr year_month operator+(const year_month& ym, const years& dy) noexcept;
    constexpr year_month operator+(const years& dy, const year_month& ym) noexcept;
    constexpr year_month operator-(const year_month& ym, const years& dy) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const year_month& ym);

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const year_month& ym);

    template<class charT, class traits, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    year_month& ym, basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.cal.ymd], class year_­month_­day
    class year_month_day;

    constexpr bool operator==(const year_month_day& x, const year_month_day& y) noexcept;
    constexpr bool operator!=(const year_month_day& x, const year_month_day& y) noexcept;
    constexpr bool operator< (const year_month_day& x, const year_month_day& y) noexcept;
    constexpr bool operator> (const year_month_day& x, const year_month_day& y) noexcept;
    constexpr bool operator<=(const year_month_day& x, const year_month_day& y) noexcept;
    constexpr bool operator>=(const year_month_day& x, const year_month_day& y) noexcept;

    constexpr year_month_day operator+(const year_month_day& ymd, const months& dm) noexcept;
    constexpr year_month_day operator+(const months& dm, const year_month_day& ymd) noexcept;
    constexpr year_month_day operator+(const year_month_day& ymd, const years& dy) noexcept;
    constexpr year_month_day operator+(const years& dy, const year_month_day& ymd) noexcept;
    constexpr year_month_day operator-(const year_month_day& ymd, const months& dm) noexcept;
    constexpr year_month_day operator-(const year_month_day& ymd, const years& dy) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const year_month_day& ymd);

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt,
                  const year_month_day& ymd);

    template<class charT, class traits, class Alloc = allocator<charT>>
      basic_istream<charT, traits>&
        from_stream(basic_istream<charT, traits>& is, const charT* fmt,
                    year_month_day& ymd,
                    basic_string<charT, traits, Alloc>* abbrev = nullptr,
                    minutes* offset = nullptr);

    // [time.cal.ymdlast], class year_­month_­day_­last
    class year_month_day_last;

    constexpr bool operator==(const year_month_day_last& x,
                              const year_month_day_last& y) noexcept;
    constexpr bool operator!=(const year_month_day_last& x,
                              const year_month_day_last& y) noexcept;
    constexpr bool operator< (const year_month_day_last& x,
                              const year_month_day_last& y) noexcept;
    constexpr bool operator> (const year_month_day_last& x,
                              const year_month_day_last& y) noexcept;
    constexpr bool operator<=(const year_month_day_last& x,
                              const year_month_day_last& y) noexcept;
    constexpr bool operator>=(const year_month_day_last& x,
                              const year_month_day_last& y) noexcept;

    constexpr year_month_day_last
      operator+(const year_month_day_last& ymdl, const months& dm) noexcept;
    constexpr year_month_day_last
      operator+(const months& dm, const year_month_day_last& ymdl) noexcept;
    constexpr year_month_day_last
      operator+(const year_month_day_last& ymdl, const years& dy) noexcept;
    constexpr year_month_day_last
      operator+(const years& dy, const year_month_day_last& ymdl) noexcept;
    constexpr year_month_day_last
      operator-(const year_month_day_last& ymdl, const months& dm) noexcept;
    constexpr year_month_day_last
      operator-(const year_month_day_last& ymdl, const years& dy) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const year_month_day_last& ymdl);

    // [time.cal.ymwd], class year_­month_­weekday
    class year_month_weekday;

    constexpr bool operator==(const year_month_weekday& x,
                              const year_month_weekday& y) noexcept;
    constexpr bool operator!=(const year_month_weekday& x,
                              const year_month_weekday& y) noexcept;

    constexpr year_month_weekday
      operator+(const year_month_weekday& ymwd, const months& dm) noexcept;
    constexpr year_month_weekday
      operator+(const months& dm, const year_month_weekday& ymwd) noexcept;
    constexpr year_month_weekday
      operator+(const year_month_weekday& ymwd, const years& dy) noexcept;
    constexpr year_month_weekday
      operator+(const years& dy, const year_month_weekday& ymwd) noexcept;
    constexpr year_month_weekday
      operator-(const year_month_weekday& ymwd, const months& dm) noexcept;
    constexpr year_month_weekday
      operator-(const year_month_weekday& ymwd, const years& dy) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const year_month_weekday& ymwdi);

    // [time.cal.ymwdlast], class year_­month_­weekday_­last
    class year_month_weekday_last;

    constexpr bool operator==(const year_month_weekday_last& x,
                              const year_month_weekday_last& y) noexcept;
    constexpr bool operator!=(const year_month_weekday_last& x,
                              const year_month_weekday_last& y) noexcept;

    constexpr year_month_weekday_last
      operator+(const year_month_weekday_last& ymwdl, const months& dm) noexcept;
    constexpr year_month_weekday_last
      operator+(const months& dm, const year_month_weekday_last& ymwdl) noexcept;
    constexpr year_month_weekday_last
      operator+(const year_month_weekday_last& ymwdl, const years& dy) noexcept;
    constexpr year_month_weekday_last
      operator+(const years& dy, const year_month_weekday_last& ymwdl) noexcept;
    constexpr year_month_weekday_last
      operator-(const year_month_weekday_last& ymwdl, const months& dm) noexcept;
    constexpr year_month_weekday_last
      operator-(const year_month_weekday_last& ymwdl, const years& dy) noexcept;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const year_month_weekday_last& ymwdl);

    // [time.cal.operators], civil calendar conventional syntax operators
    constexpr year_month
      operator/(const year& y, const month& m) noexcept;
    constexpr year_month
      operator/(const year& y, int m) noexcept;
    constexpr month_day
      operator/(const month& m, const day& d) noexcept;
    constexpr month_day
      operator/(const month& m, int d) noexcept;
    constexpr month_day
      operator/(int m, const day& d) noexcept;
    constexpr month_day
      operator/(const day& d, const month& m) noexcept;
    constexpr month_day
      operator/(const day& d, int m) noexcept;
    constexpr month_day_last
      operator/(const month& m, last_spec) noexcept;
    constexpr month_day_last
      operator/(int m, last_spec) noexcept;
    constexpr month_day_last
      operator/(last_spec, const month& m) noexcept;
    constexpr month_day_last
      operator/(last_spec, int m) noexcept;
    constexpr month_weekday
      operator/(const month& m, const weekday_indexed& wdi) noexcept;
    constexpr month_weekday
      operator/(int m, const weekday_indexed& wdi) noexcept;
    constexpr month_weekday
      operator/(const weekday_indexed& wdi, const month& m) noexcept;
    constexpr month_weekday
      operator/(const weekday_indexed& wdi, int m) noexcept;
    constexpr month_weekday_last
      operator/(const month& m, const weekday_last& wdl) noexcept;
    constexpr month_weekday_last
      operator/(int m, const weekday_last& wdl) noexcept;
    constexpr month_weekday_last
      operator/(const weekday_last& wdl, const month& m) noexcept;
    constexpr month_weekday_last
      operator/(const weekday_last& wdl, int m) noexcept;
    constexpr year_month_day
      operator/(const year_month& ym, const day& d) noexcept;
    constexpr year_month_day
      operator/(const year_month& ym, int d) noexcept;
    constexpr year_month_day
      operator/(const year& y, const month_day& md) noexcept;
    constexpr year_month_day
      operator/(int y, const month_day& md) noexcept;
    constexpr year_month_day
      operator/(const month_day& md, const year& y) noexcept;
    constexpr year_month_day
      operator/(const month_day& md, int y) noexcept;
    constexpr year_month_day_last
      operator/(const year_month& ym, last_spec) noexcept;
    constexpr year_month_day_last
      operator/(const year& y, const month_day_last& mdl) noexcept;
    constexpr year_month_day_last
      operator/(int y, const month_day_last& mdl) noexcept;
    constexpr year_month_day_last
      operator/(const month_day_last& mdl, const year& y) noexcept;
    constexpr year_month_day_last
      operator/(const month_day_last& mdl, int y) noexcept;
    constexpr year_month_weekday
      operator/(const year_month& ym, const weekday_indexed& wdi) noexcept;
    constexpr year_month_weekday
      operator/(const year& y, const month_weekday& mwd) noexcept;
    constexpr year_month_weekday
      operator/(int y, const month_weekday& mwd) noexcept;
    constexpr year_month_weekday
      operator/(const month_weekday& mwd, const year& y) noexcept;
    constexpr year_month_weekday
      operator/(const month_weekday& mwd, int y) noexcept;
    constexpr year_month_weekday_last
      operator/(const year_month& ym, const weekday_last& wdl) noexcept;
    constexpr year_month_weekday_last
      operator/(const year& y, const month_weekday_last& mwdl) noexcept;
    constexpr year_month_weekday_last
      operator/(int y, const month_weekday_last& mwdl) noexcept;
    constexpr year_month_weekday_last
      operator/(const month_weekday_last& mwdl, const year& y) noexcept;
    constexpr year_month_weekday_last
      operator/(const month_weekday_last& mwdl, int y) noexcept;

    // [time.tod], class template time_­of_­day
    template<class Duration> class time_of_day;
    template<> class time_of_day<hours>;
    template<> class time_of_day<minutes>;
    template<> class time_of_day<seconds>;
    template<class Rep, class Period> class time_of_day<duration<Rep, Period>>;

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const time_of_day<hours>& t);

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const time_of_day<minutes>& t);

    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const time_of_day<seconds>& t);

    template<class charT, class traits, class Rep, class Period>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os,
                   const time_of_day<duration<Rep, Period>>& t);

    // [time.zone.db], time zone database
    struct tzdb;
    class tzdb_list;

    // [time.zone.db.access], time zone database access
    const tzdb& get_tzdb();
    tzdb_list& get_tzdb_list();
    const time_zone* locate_zone(string_view tz_name);
    const time_zone* current_zone();

    // [time.zone.db.remote], remote time zone database support
    const tzdb& reload_tzdb();
    string remote_version();

    // [time.zone.exception], exception classes
    class nonexistent_local_time;
    class ambiguous_local_time;

    // [time.zone.info], information classes
    struct sys_info;
    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const sys_info& si);

    struct local_info;
    template<class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const local_info& li);

    // [time.zone.timezone], class time_­zone
    enum class choose {earliest, latest};
    class time_zone;

    bool operator==(const time_zone& x, const time_zone& y) noexcept;
    bool operator!=(const time_zone& x, const time_zone& y) noexcept;

    bool operator<(const time_zone& x, const time_zone& y) noexcept;
    bool operator>(const time_zone& x, const time_zone& y) noexcept;
    bool operator<=(const time_zone& x, const time_zone& y) noexcept;
    bool operator>=(const time_zone& x, const time_zone& y) noexcept;

    // [time.zone.zonedtraits], class template zoned_­traits
    template<class T> struct zoned_traits;

    // [time.zone.zonedtime], class template zoned_­time
    template<class Duration, class TimeZonePtr = const time_zone*> class zoned_time;

    using zoned_seconds = zoned_time<seconds>;

    template<class Duration1, class Duration2, class TimeZonePtr>
      bool operator==(const zoned_time<Duration1, TimeZonePtr>& x,
                      const zoned_time<Duration2, TimeZonePtr>& y);

    template<class Duration1, class Duration2, class TimeZonePtr>
      bool operator!=(const zoned_time<Duration1, TimeZonePtr>& x,
                      const zoned_time<Duration2, TimeZonePtr>& y);

    template<class charT, class traits, class Duration, class TimeZonePtr>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os,
                   const zoned_time<Duration, TimeZonePtr>& t);

    template<class charT, class traits, class Duration, class TimeZonePtr>
      basic_ostream<charT, traits>&
        to_stream(basic_ostream<charT, traits>& os, const charT* fmt,
                  const zoned_time<Duration, TimeZonePtr>& tp);

    // [time.zone.leap], leap second support
    class leap;

    bool operator==(const leap& x, const leap& y);
    bool operator!=(const leap& x, const leap& y);
    bool operator< (const leap& x, const leap& y);
    bool operator> (const leap& x, const leap& y);
    bool operator<=(const leap& x, const leap& y);
    bool operator>=(const leap& x, const leap& y);

    template<class Duration>
      bool operator==(const leap& x, const sys_time<Duration>& y);
    template<class Duration>
      bool operator==(const sys_time<Duration>& x, const leap& y);
    template<class Duration>
      bool operator!=(const leap& x, const sys_time<Duration>& y);
    template<class Duration>
      bool operator!=(const sys_time<Duration>& x, const leap& y);
    template<class Duration>
      bool operator< (const leap& x, const sys_time<Duration>& y);
    template<class Duration>
      bool operator< (const sys_time<Duration>& x, const leap& y);
    template<class Duration>
      bool operator> (const leap& x, const sys_time<Duration>& y);
    template<class Duration>
      bool operator> (const sys_time<Duration>& x, const leap& y);
    template<class Duration>
      bool operator<=(const leap& x, const sys_time<Duration>& y);
    template<class Duration>
      bool operator<=(const sys_time<Duration>& x, const leap& y);
    template<class Duration>
      bool operator>=(const leap& x, const sys_time<Duration>& y);
    template<class Duration>
      bool operator>=(const sys_time<Duration>& x, const leap& y);

    // [time.zone.link], class link
    class link;

    bool operator==(const link& x, const link& y);
    bool operator!=(const link& x, const link& y);
    bool operator< (const link& x, const link& y);
    bool operator> (const link& x, const link& y);
    bool operator<=(const link& x, const link& y);
    bool operator>=(const link& x, const link& y);

    // [time.format], formatting
    template<class charT, class Streamable>
      basic_string<charT>
        format(const charT* fmt, const Streamable& s);
    template<class charT, class Streamable>
      basic_string<charT>
        format(const locale& loc, const charT* fmt, const Streamable& s);
    template<class charT, class traits, class Alloc, class Streamable>
      basic_string<charT, traits, Alloc>
        format(const basic_string<charT, traits, Alloc>& fmt, const Streamable& s);
    template<class charT, class traits, class Alloc, class Streamable>
      basic_string<charT, traits, Alloc>
        format(const locale& loc, const basic_string<charT, traits, Alloc>& fmt,
               const Streamable& s);

    // [time.parse], parsing
    template<class charT, class traits, class Alloc, class Parsable>
      unspecified
        parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp);

    template<class charT, class traits, class Alloc, class Parsable>
      unspecified
        parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp,
              basic_string<charT, traits, Alloc>& abbrev);

    template<class charT, class traits, class Alloc, class Parsable>
      unspecified
        parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp,
              minutes& offset);

    template<class charT, class traits, class Alloc, class Parsable>
      unspecified
        parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp,
              basic_string<charT, traits, Alloc>& abbrev, minutes& offset);

    // calendrical constants
    inline constexpr last_spec last{};

    inline constexpr weekday Sunday{0};
    inline constexpr weekday Monday{1};
    inline constexpr weekday Tuesday{2};
    inline constexpr weekday Wednesday{3};
    inline constexpr weekday Thursday{4};
    inline constexpr weekday Friday{5};
    inline constexpr weekday Saturday{6};

    inline constexpr month January{1};
    inline constexpr month February{2};
    inline constexpr month March{3};
    inline constexpr month April{4};
    inline constexpr month May{5};
    inline constexpr month June{6};
    inline constexpr month July{7};
    inline constexpr month August{8};
    inline constexpr month September{9};
    inline constexpr month October{10};
    inline constexpr month November{11};
    inline constexpr month December{12};
  }

  inline namespace literals {
  inline namespace chrono_literals {
    // [time.duration.literals], suffixes for duration literals
    constexpr chrono::hours                                 operator""h(unsigned long long);
    constexpr chrono::duration<unspecified, ratio<3600, 1>> operator""h(long double);

    constexpr chrono::minutes                             operator""min(unsigned long long);
    constexpr chrono::duration<unspecified, ratio<60, 1>> operator""min(long double);

    constexpr chrono::seconds               operator""s(unsigned long long);
    constexpr chrono::duration<unspecified> operator""s(long double);

    constexpr chrono::milliseconds                 operator""ms(unsigned long long);
    constexpr chrono::duration<unspecified, milli> operator""ms(long double);

    constexpr chrono::microseconds                 operator""us(unsigned long long);
    constexpr chrono::duration<unspecified, micro> operator""us(long double);

    constexpr chrono::nanoseconds                 operator""ns(unsigned long long);
    constexpr chrono::duration<unspecified, nano> operator""ns(long double);

    // [time.cal.day.nonmembers], non-member functions
    constexpr chrono::day  operator""d(unsigned long long d) noexcept;

    // [time.cal.year.nonmembers], non-member functions
    constexpr chrono::year operator""y(unsigned long long y) noexcept;
  }
  }

  namespace chrono {
    using namespace literals::chrono_literals;
  }
}

25.3 Cpp17Clock requirements [time.clock.req]

A clock is a bundle consisting of a duration, a time_­point, and a function now() to get the current time_­point.
The origin of the clock's time_­point is referred to as the clock's epoch.
A clock shall satisfy the requirements in Table 87.
In Table 87 C1 and C2 denote clock types.
t1 and t2 are values returned by C1::now() where the call returning t1 happens before ([intro.multithread]) the call returning t2 and both of these calls occur before C1::time_­point::max().
[Note
:
This means C1 did not wrap around between t1 and t2.
end note
]
Table 87Cpp17Clock requirements
Expression
Return type
Operational semantics
C1::rep
An arithmetic type or a class emulating an arithmetic type
The representation type of C1::duration.
C1::period
a specialization of ratio
The tick period of the clock in seconds.
C1::duration
chrono::duration<C1::rep, C1::period>
The duration type of the clock.
C1::time_­point
chrono::time_­point<C1> or chrono::time_­point<C2, C1::duration>
The time_­point type of the clock.
C1 and C2 shall refer to the same epoch.
C1::is_­steady
const bool
true if t1 <= t2 is always true and the time between clock ticks is constant, otherwise false.
C1::now()
C1::time_­point
Returns a time_­point object representing the current point in time.
[Note
:
The relative difference in durations between those reported by a given clock and the SI definition is a measure of the quality of implementation.
end note
]
A type TC meets the Cpp17TrivialClock requirements if:
  • TC satisfies the Cpp17Clock requirements ([time.clock.req]),
  • the types TC::rep, TC::duration, and TC::time_­point satisfy the Cpp17EqualityComparable (Table 22), Cpp17LessThanComparable (Table 23), Cpp17DefaultConstructible (Table 24), Cpp17CopyConstructible (Table 26), Cpp17CopyAssignable (Table 28), and Cpp17Destructible (Table 29) requirements and the requirements of numeric types ([numeric.requirements]).
    [Note
    :
    This means, in particular, that operations on these types will not throw exceptions.
    end note
    ]
  • lvalues of the types TC::rep, TC::duration, and TC::time_­point are swappable ([swappable.requirements]),
  • the function TC::now() does not throw exceptions, and
  • the type TC::time_­point::clock meets the Cpp17TrivialClock requirements, recursively.

25.4 Time-related traits [time.traits]

25.4.1 treat_­as_­floating_­point [time.traits.is_fp]

template<class Rep> struct treat_as_floating_point : is_floating_point<Rep> { };
The duration template uses the treat_­as_­floating_­point trait to help determine if a duration object can be converted to another duration with a different tick period.
If treat_­as_­floating_­point_­v<Rep> is true, then implicit conversions are allowed among durations.
Otherwise, the implicit convertibility depends on the tick periods of the durations.
[Note
:
The intention of this trait is to indicate whether a given class behaves like a floating-point type, and thus allows division of one value by another with acceptable loss of precision.
If treat_­as_­floating_­point_­v<Rep> is false, Rep will be treated as if it behaved like an integral type for the purpose of these conversions.
end note
]

25.4.2 duration_­values [time.traits.duration_values]

template<class Rep> struct duration_values { public: static constexpr Rep zero(); static constexpr Rep min(); static constexpr Rep max(); };
The duration template uses the duration_­values trait to construct special values of the duration's representation (Rep).
This is done because the representation might be a class type with behavior which requires some other implementation to return these special values.
In that case, the author of that class type should specialize duration_­values to return the indicated values.
static constexpr Rep zero();
Returns: Rep(0).
[Note
:
Rep(0) is specified instead of Rep() because Rep() may have some other meaning, such as an uninitialized value.
end note
]
Remarks: The value returned shall be the additive identity.
static constexpr Rep min();
Returns: numeric_­limits<Rep>::lowest().
Remarks: The value returned shall compare less than or equal to zero().
static constexpr Rep max();
Returns: numeric_­limits<Rep>::max().
Remarks: The value returned shall compare greater than zero().

25.4.3 Specializations of common_­type [time.traits.specializations]

template<class Rep1, class Period1, class Rep2, class Period2> struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>> { using type = chrono::duration<common_type_t<Rep1, Rep2>, see below>; };
The period of the duration indicated by this specialization of common_­type shall be the greatest common divisor of Period1 and Period2.
[Note
:
This can be computed by forming a ratio of the greatest common divisor of Period1::num and Period2::num and the least common multiple of Period1::den and Period2::den.
end note
]
[Note
:
The typedef name type is a synonym for the duration with the largest tick period possible where both duration arguments will convert to it without requiring a division operation.
The representation of this type is intended to be able to hold any value resulting from this conversion with no truncation error, although floating-point durations may have round-off errors.
end note
]
template<class Clock, class Duration1, class Duration2> struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> { using type = chrono::time_point<Clock, common_type_t<Duration1, Duration2>>; };
The common type of two time_­point types is a time_­point with the same clock as the two types and the common type of their two durations.

25.4.4 Class template is_­clock [time.traits.is_clock]

template<class T> struct is_clock;
is_­clock is a Cpp17UnaryTypeTrait ([meta.rqmts]) with a base characteristic of true_­type if T meets the Cpp17Clock requirements ([time.clock.req]), otherwise false_­type.
For the purposes of the specification of this trait, the extent to which an implementation determines that a type cannot meet the Cpp17Clock requirements is unspecified, except that as a minimum a type T shall not qualify as a Cpp17Clock unless it satisfies all of the following conditions:
  • the qualified-ids T::rep, T::period, T::duration, and T::time_­point are valid and each denotes a type ([temp.deduct]),
  • the expression T::is_­steady is well-formed when treated as an unevaluated operand,
  • the expression T::now() is well-formed when treated as an unevaluated operand.
The behavior of a program that adds specializations for is_­clock is undefined.

25.5 Class template duration [time.duration]

A duration type measures time between two points in time (time_­points).
A duration has a representation which holds a count of ticks and a tick period.
The tick period is the amount of time which occurs from one tick to the next, in units of seconds.
It is expressed as a rational constant using the template ratio.
namespace std::chrono {
  template<class Rep, class Period = ratio<1>>
  class duration {
  public:
    using rep    = Rep;
    using period = typename Period::type;

  private:
    rep rep_;       // exposition only

  public:
    // [time.duration.cons], construct/copy/destroy
    constexpr duration() = default;
    template<class Rep2>
      constexpr explicit duration(const Rep2& r);
    template<class Rep2, class Period2>
      constexpr duration(const duration<Rep2, Period2>& d);
    ~duration() = default;
    duration(const duration&) = default;
    duration& operator=(const duration&) = default;

    // [time.duration.observer], observer
    constexpr rep count() const;

    // [time.duration.arithmetic], arithmetic
    constexpr common_type_t<duration> operator+() const;
    constexpr common_type_t<duration> operator-() const;
    constexpr duration& operator++();
    constexpr duration  operator++(int);
    constexpr duration& operator--();
    constexpr duration  operator--(int);

    constexpr duration& operator+=(const duration& d);
    constexpr duration& operator-=(const duration& d);

    constexpr duration& operator*=(const rep& rhs);
    constexpr duration& operator/=(const rep& rhs);
    constexpr duration& operator%=(const rep& rhs);
    constexpr duration& operator%=(const duration& rhs);

    // [time.duration.special], special values
    static constexpr duration zero();
    static constexpr duration min();
    static constexpr duration max();
  };
}
Rep shall be an arithmetic type or a class emulating an arithmetic type.
If duration is instantiated with a duration type as the argument for the template parameter Rep, the program is ill-formed.
If Period is not a specialization of ratio, the program is ill-formed.
If Period::num is not positive, the program is ill-formed.
Members of duration shall not throw exceptions other than those thrown by the indicated operations on their representations.
The defaulted copy constructor of duration shall be a constexpr function if and only if the required initialization of the member rep_­ for copy and move, respectively, would satisfy the requirements for a constexpr function.
[Example
:
duration<long, ratio<60>> d0;       // holds a count of minutes using a long
duration<long long, milli> d1;      // holds a count of milliseconds using a long long
duration<double, ratio<1, 30>>  d2; // holds a count with a tick period of  of a second
                                    // (30 Hz) using a double
end example
]

25.5.1 Constructors [time.duration.cons]

template<class Rep2> constexpr explicit duration(const Rep2& r);
Remarks: This constructor shall not participate in overload resolution unless Rep2 is implicitly convertible to rep and
  • treat_­as_­floating_­point_­v<rep> is true or
  • treat_­as_­floating_­point_­v<Rep2> is false.
[Example
:
duration<int, milli> d(3);          // OK
duration<int, milli> d(3.5);        // error
end example
]
Effects: Constructs an object of type duration.
Ensures: count() == static_­cast<rep>(r).
template<class Rep2, class Period2> constexpr duration(const duration<Rep2, Period2>& d);
Remarks: This constructor shall not participate in overload resolution unless no overflow is induced in the conversion and treat_­as_­floating_­point_­v<rep> is true or both ratio_­divide<Period2, period>::den is 1 and treat_­as_­floating_­point_­v<Rep2> is false.
[Note
:
This requirement prevents implicit truncation error when converting between integral-based duration types.
Such a construction could easily lead to confusion about the value of the duration.
end note
]
[Example
:
duration<int, milli> ms(3);
duration<int, micro> us = ms;       // OK
duration<int, milli> ms2 = us;      // error
end example
]
Effects: Constructs an object of type duration, constructing rep_­ from
duration_­cast<duration>(d).count().

25.5.2 Observer [time.duration.observer]

constexpr rep count() const;
Returns: rep_­.

25.5.3 Arithmetic [time.duration.arithmetic]

constexpr common_type_t<duration> operator+() const;
Returns: common_­type_­t<duration>(*this).
constexpr common_type_t<duration> operator-() const;
Returns: common_­type_­t<duration>(-rep_­).
constexpr duration& operator++();
Effects: As if by ++rep_­.
Returns: *this.
constexpr duration operator++(int);
Returns: duration(rep_­++).
constexpr duration& operator--();
Effects: As if by --rep_­.
Returns: *this.
constexpr duration operator--(int);
Returns: duration(rep_­--).
constexpr duration& operator+=(const duration& d);
Effects: As if by: rep_­ += d.count();
Returns: *this.
constexpr duration& operator-=(const duration& d);
Effects: As if by: rep_­ -= d.count();
Returns: *this.
constexpr duration& operator*=(const rep& rhs);
Effects: As if by: rep_­ *= rhs;
Returns: *this.
constexpr duration& operator/=(const rep& rhs);
Effects: As if by: rep_­ /= rhs;
Returns: *this.
constexpr duration& operator%=(const rep& rhs);
Effects: As if by: rep_­ %= rhs;
Returns: *this.
constexpr duration& operator%=(const duration& rhs);
Effects: As if by: rep_­ %= rhs.count();
Returns: *this.

25.5.4 Special values [time.duration.special]

static constexpr duration zero();
Returns: duration(duration_­values<rep>::zero()).
static constexpr duration min();
Returns: duration(duration_­values<rep>::min()).
static constexpr duration max();
Returns: duration(duration_­values<rep>::max()).

25.5.5 Non-member arithmetic [time.duration.nonmember]

In the function descriptions that follow, unless stated otherwise, let CD represent the return type of the function.
template<class Rep1, class Period1, class Rep2, class Period2> constexpr common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>> operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CD(CD(lhs).count() + CD(rhs).count()).
template<class Rep1, class Period1, class Rep2, class Period2> constexpr common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>> operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CD(CD(lhs).count() - CD(rhs).count()).
template<class Rep1, class Period, class Rep2> constexpr duration<common_type_t<Rep1, Rep2>, Period> operator*(const duration<Rep1, Period>& d, const Rep2& s);
Remarks: This operator shall not participate in overload resolution unless Rep2 is implicitly convertible to common_­type_­t<Rep1, Rep2>.
Returns: CD(CD(d).count() * s).
template<class Rep1, class Rep2, class Period> constexpr duration<common_type_t<Rep1, Rep2>, Period> operator*(const Rep1& s, const duration<Rep2, Period>& d);
Remarks: This operator shall not participate in overload resolution unless Rep1 is implicitly convertible to common_­type_­t<Rep1, Rep2>.
Returns: d * s.
template<class Rep1, class Period, class Rep2> constexpr duration<common_type_t<Rep1, Rep2>, Period> operator/(const duration<Rep1, Period>& d, const Rep2& s);
Remarks: This operator shall not participate in overload resolution unless Rep2 is implicitly convertible to common_­type_­t<Rep1, Rep2> and Rep2 is not a specialization of duration.
Returns: CD(CD(d).count() / s).
template<class Rep1, class Period1, class Rep2, class Period2> constexpr common_type_t<Rep1, Rep2> operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Let CD be common_­type_­t<duration<Rep1, Period1>, duration<Rep2, Period2>>.
Returns: CD(lhs).count() / CD(rhs).count().
template<class Rep1, class Period, class Rep2> constexpr duration<common_type_t<Rep1, Rep2>, Period> operator%(const duration<Rep1, Period>& d, const Rep2& s);
Remarks: This operator shall not participate in overload resolution unless Rep2 is implicitly convertible to common_­type_­t<Rep1, Rep2> and Rep2 is not a specialization of duration.
Returns: CD(CD(d).count() % s).
template<class Rep1, class Period1, class Rep2, class Period2> constexpr common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>> operator%(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CD(CD(lhs).count() % CD(rhs).count()).

25.5.6 Comparisons [time.duration.comparisons]

In the function descriptions that follow, CT represents common_­type_­t<A, B>, where A and B are the types of the two arguments to the function.
template<class Rep1, class Period1, class Rep2, class Period2> constexpr bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CT(lhs).count() == CT(rhs).count().
template<class Rep1, class Period1, class Rep2, class Period2> constexpr bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: !(lhs == rhs).
template<class Rep1, class Period1, class Rep2, class Period2> constexpr bool operator<(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CT(lhs).count() < CT(rhs).count().
template<class Rep1, class Period1, class Rep2, class Period2> constexpr bool operator>(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: rhs < lhs.
template<class Rep1, class Period1, class Rep2, class Period2> constexpr bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: !(rhs < lhs).
template<class Rep1, class Period1, class Rep2, class Period2> constexpr bool operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: !(lhs < rhs).

25.5.7 duration_­cast [time.duration.cast]

template<class ToDuration, class Rep, class Period> constexpr ToDuration duration_cast(const duration<Rep, Period>& d);
Remarks: This function shall not participate in overload resolution unless ToDuration is a specialization of duration.
Returns: Let CF be ratio_­divide<Period, typename ToDuration::period>, and CR be common_­type<typename ToDuration::rep, Rep, intmax_­t>::type.
  • If CF::num == 1 and CF::den == 1, returns
    ToDuration(static_cast<typename ToDuration::rep>(d.count()))
    
  • otherwise, if CF::num != 1 and CF::den == 1, returns
    ToDuration(static_cast<typename ToDuration::rep>(
      static_cast<CR>(d.count()) * static_cast<CR>(CF::num)))
    
  • otherwise, if CF::num == 1 and CF::den != 1, returns
    ToDuration(static_cast<typename ToDuration::rep>(
      static_cast<CR>(d.count()) / static_cast<CR>(CF::den)))
    
  • otherwise, returns
    ToDuration(static_cast<typename ToDuration::rep>(
      static_cast<CR>(d.count()) * static_cast<CR>(CF::num) / static_cast<CR>(CF::den)))
    
[Note
:
This function does not use any implicit conversions; all conversions are done with static_­cast.
It avoids multiplications and divisions when it is known at compile time that one or more arguments is 1.
Intermediate computations are carried out in the widest representation and only converted to the destination representation at the final step.
end note
]
template<class ToDuration, class Rep, class Period> constexpr ToDuration floor(const duration<Rep, Period>& d);
Remarks: This function shall not participate in overload resolution unless ToDuration is a specialization of duration.
Returns: The greatest result t representable in ToDuration for which t <= d.
template<class ToDuration, class Rep, class Period> constexpr ToDuration ceil(const duration<Rep, Period>& d);
Remarks: This function shall not participate in overload resolution unless ToDuration is a specialization of duration.
Returns: The least result t representable in ToDuration for which t >= d.
template<class ToDuration, class Rep, class Period> constexpr ToDuration round(const duration<Rep, Period>& d);
Remarks: This function shall not participate in overload resolution unless ToDuration is a specialization of duration, and treat_­as_­floating_­point_­v<typename ToDuration::rep> is false.
Returns: The value of ToDuration that is closest to d.
If there are two closest values, then return the value t for which t % 2 == 0.

25.5.8 Suffixes for duration literals [time.duration.literals]

This subclause describes literal suffixes for constructing duration literals.
The suffixes h, min, s, ms, us, ns denote duration values of the corresponding types hours, minutes, seconds, milliseconds, microseconds, and nanoseconds respectively if they are applied to integral literals.
If any of these suffixes are applied to a floating-point literal the result is a chrono::duration literal with an unspecified floating-point representation.
If any of these suffixes are applied to an integer literal and the resulting chrono::duration value cannot be represented in the result type because of overflow, the program is ill-formed.
[Example
:
The following code shows some duration literals.
using namespace std::chrono_literals;
auto constexpr aday=24h;
auto constexpr lesson=45min;
auto constexpr halfanhour=0.5h;
end example
]
constexpr chrono::hours operator""h(unsigned long long hours); constexpr chrono::duration<unspecified, ratio<3600, 1>> operator""h(long double hours);
Returns: A duration literal representing hours hours.
constexpr chrono::minutes operator""min(unsigned long long minutes); constexpr chrono::duration<unspecified, ratio<60, 1>> operator""min(long double minutes);
Returns: A duration literal representing minutes minutes.
constexpr chrono::seconds operator""s(unsigned long long sec); constexpr chrono::duration<unspecified> operator""s(long double sec);
Returns: A duration literal representing sec seconds.
[Note
:
The same suffix s is used for basic_­string but there is no conflict, since duration suffixes apply to numbers and string literal suffixes apply to character array literals.
end note
]
constexpr chrono::milliseconds operator""ms(unsigned long long msec); constexpr chrono::duration<unspecified, milli> operator""ms(long double msec);
Returns: A duration literal representing msec milliseconds.
constexpr chrono::microseconds operator""us(unsigned long long usec); constexpr chrono::duration<unspecified, micro> operator""us(long double usec);
Returns: A duration literal representing usec microseconds.
constexpr chrono::nanoseconds operator""ns(unsigned long long nsec); constexpr chrono::duration<unspecified, nano> operator""ns(long double nsec);
Returns: A duration literal representing nsec nanoseconds.

25.5.9 Algorithms [time.duration.alg]

template<class Rep, class Period> constexpr duration<Rep, Period> abs(duration<Rep, Period> d);
Remarks: This function shall not participate in overload resolution unless numeric_­limits<Rep>::is_­signed is true.
Returns: If d >= d.zero(), return d, otherwise return -d.

25.5.10 I/O [time.duration.io]

template<class charT, class traits, class Rep, class Period> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const duration<Rep, Period>& d);
Requires: Rep is an integral type whose integer conversion rank ([conv.rank]) is greater than or equal to that of short, or a floating point type.
charT is char or wchar_­t.
Effects: Forms a basic_­string<charT, traits> from d.count() using to_­string if charT is char, or to_­wstring if charT is wchar_­t.
Appends the units suffix described below to the basic_­string.
Inserts the resulting basic_­string into os.
[Note
:
This specification ensures that the result of this streaming operation will obey the width and alignment properties of the stream.
end note
]
The units suffix depends on the type Period::type as follows:
  • If Period::type is atto, the suffix is "as".
  • Otherwise, if Period::type is femto, the suffix is "fs".
  • Otherwise, if Period::type is pico, the suffix is "ps".
  • Otherwise, if Period::type is nano, the suffix is "ns".
  • Otherwise, if Period::type is micro, the suffix is "μs" ("\u00b5\u0073").
  • Otherwise, if Period::type is milli, the suffix is "ms".
  • Otherwise, if Period::type is centi, the suffix is "cs".
  • Otherwise, if Period::type is deci, the suffix is "ds".
  • Otherwise, if Period::type is ratio<1>, the suffix is "s".
  • Otherwise, if Period::type is deca, the suffix is "das".
  • Otherwise, if Period::type is hecto, the suffix is "hs".
  • Otherwise, if Period::type is kilo, the suffix is "ks".
  • Otherwise, if Period::type is mega, the suffix is "Ms".
  • Otherwise, if Period::type is giga, the suffix is "Gs".
  • Otherwise, if Period::type is tera, the suffix is "Ts".
  • Otherwise, if Period::type is peta, the suffix is "Ps".
  • Otherwise, if Period::type is exa, the suffix is "Es".
  • Otherwise, if Period::type is ratio<60>, the suffix is "min".
  • Otherwise, if Period::type is ratio<3600>, the suffix is "h".
  • Otherwise, if Period::type::den == 1, the suffix is "[num]s".
  • Otherwise, the suffix is "[num/den]s".
In the list above the use of num and den refer to the static data members of Period::type, which are converted to arrays of charT using a decimal conversion with no leading zeroes.
If Period::type is micro, but the character U+00B5 cannot be represented in the encoding used for charT, the unit suffix "us" is used instead of "μs".
Returns: os.
template<class charT, class traits, class Rep, class Period> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const duration<Rep, Period>& d);
Effects: Streams d into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
Returns: os.
template<class charT, class traits, class Rep, class Period, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, duration<Rep, Period>& d, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the duration d using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse parses everything specified by the parsing format flags without error, and yet none of the flags impacts a duration, d will be assigned a zero value.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Returns: is.

25.6 Class template time_­point [time.point]

namespace std::chrono {
  template<class Clock, class Duration = typename Clock::duration>
  class time_point {
  public:
    using clock    = Clock;
    using duration = Duration;
    using rep      = typename duration::rep;
    using period   = typename duration::period;

  private:
    duration d_;                                                // exposition only

  public:
    // [time.point.cons], construct
    constexpr time_point();                                     // has value epoch
    constexpr explicit time_point(const duration& d);           // same as time_­point() + d
    template<class Duration2>
      constexpr time_point(const time_point<clock, Duration2>& t);

    // [time.point.observer], observer
    constexpr duration time_since_epoch() const;

    // [time.point.arithmetic], arithmetic
    constexpr time_point& operator++();
    constexpr time_point operator++(int);
    constexpr time_point& operator--();
    constexpr time_point operator--(int);
    constexpr time_point& operator+=(const duration& d);
    constexpr time_point& operator-=(const duration& d);

    // [time.point.special], special values
    static constexpr time_point min();
    static constexpr time_point max();
  };
}
Clock shall either satisfy the Cpp17Clock requirements ([time.clock.req]) or be the type local_­t.
If Duration is not an instance of duration, the program is ill-formed.

25.6.1 Constructors [time.point.cons]

constexpr time_point();
Effects: Constructs an object of type time_­point, initializing d_­ with duration::zero().
Such a time_­point object represents the epoch.
constexpr explicit time_point(const duration& d);
Effects: Constructs an object of type time_­point, initializing d_­ with d.
Such a time_­point object represents the epoch + d.
template<class Duration2> constexpr time_point(const time_point<clock, Duration2>& t);
Remarks: This constructor shall not participate in overload resolution unless Duration2 is implicitly convertible to duration.
Effects: Constructs an object of type time_­point, initializing d_­ with t.time_­since_­epoch().

25.6.2 Observer [time.point.observer]

constexpr duration time_since_epoch() const;
Returns: d_­.

25.6.3 Arithmetic [time.point.arithmetic]

constexpr time_point& operator++();
Effects: ++d_­.
Returns: *this.
constexpr time_point operator++(int);
Returns: time_­point{d_­++}.
constexpr time_point& operator--();
Effects: --d_­.
Returns: *this.
constexpr time_point operator--(int);
Returns: time_­point{d_­--}.
constexpr time_point& operator+=(const duration& d);
Effects: As if by: d_­ += d;
Returns: *this.
constexpr time_point& operator-=(const duration& d);
Effects: As if by: d_­ -= d;
Returns: *this.

25.6.4 Special values [time.point.special]

static constexpr time_point min();
Returns: time_­point(duration::min()).
static constexpr time_point max();
Returns: time_­point(duration::max()).

25.6.5 Non-member arithmetic [time.point.nonmember]

template<class Clock, class Duration1, class Rep2, class Period2> constexpr time_point<Clock, common_type_t<Duration1, duration<Rep2, Period2>>> operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CT(lhs.time_­since_­epoch() + rhs), where CT is the type of the return value.
template<class Rep1, class Period1, class Clock, class Duration2> constexpr time_point<Clock, common_type_t<duration<Rep1, Period1>, Duration2>> operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs);
Returns: rhs + lhs.
template<class Clock, class Duration1, class Rep2, class Period2> constexpr time_point<Clock, common_type_t<Duration1, duration<Rep2, Period2>>> operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CT(lhs.time_­since_­epoch() - rhs), where CT is the type of the return value.
template<class Clock, class Duration1, class Duration2> constexpr common_type_t<Duration1, Duration2> operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
Returns: lhs.time_­since_­epoch() - rhs.time_­since_­epoch().

25.6.6 Comparisons [time.point.comparisons]

template<class Clock, class Duration1, class Duration2> constexpr bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
Returns: lhs.time_­since_­epoch() == rhs.time_­since_­epoch().
template<class Clock, class Duration1, class Duration2> constexpr bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
Returns: !(lhs == rhs).
template<class Clock, class Duration1, class Duration2> constexpr bool operator<(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
Returns: lhs.time_­since_­epoch() < rhs.time_­since_­epoch().
template<class Clock, class Duration1, class Duration2> constexpr bool operator>(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
Returns: rhs < lhs.
template<class Clock, class Duration1, class Duration2> constexpr bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
Returns: !(rhs < lhs).
template<class Clock, class Duration1, class Duration2> constexpr bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
Returns: !(lhs < rhs).

25.6.7 time_­point_­cast [time.point.cast]

template<class ToDuration, class Clock, class Duration> constexpr time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
Remarks: This function shall not participate in overload resolution unless ToDuration is a specialization of duration.
Returns:
time_point<Clock, ToDuration>(duration_cast<ToDuration>(t.time_since_epoch()))
template<class ToDuration, class Clock, class Duration> constexpr time_point<Clock, ToDuration> floor(const time_point<Clock, Duration>& tp);
Remarks: This function shall not participate in overload resolution unless ToDuration is a specialization of duration.
Returns: time_­point<Clock, ToDuration>(floor<ToDuration>(tp.time_­since_­epoch())).
template<class ToDuration, class Clock, class Duration> constexpr time_point<Clock, ToDuration> ceil(const time_point<Clock, Duration>& tp);
Remarks: This function shall not participate in overload resolution unless ToDuration is a specialization of duration.
Returns: time_­point<Clock, ToDuration>(ceil<ToDuration>(tp.time_­since_­epoch())).
template<class ToDuration, class Clock, class Duration> constexpr time_point<Clock, ToDuration> round(const time_point<Clock, Duration>& tp);
Remarks: This function shall not participate in overload resolution unless ToDuration is a specialization of duration, and treat_­as_­floating_­point_­v<typename ToDuration::rep> is false.
Returns: time_­point<Clock, ToDuration>(round<ToDuration>(tp.time_­since_­epoch())).

25.7 Clocks [time.clock]

The types defined in this subclause shall satisfy the Cpp17TrivialClock requirements ([time.clock.req]) unless otherwise specified.

25.7.1 Class system_­clock [time.clock.system]

25.7.1.1 Overview [time.clock.system.overview]

namespace std::chrono {
  class system_clock {
  public:
    using rep        = see below;
    using period     = ratio<unspecified, unspecified>;
    using duration   = chrono::duration<rep, period>;
    using time_point = chrono::time_point<system_clock>;
    static constexpr bool is_steady = unspecified;

    static time_point now() noexcept;

    // mapping to/from C type time_­t
    static time_t      to_time_t  (const time_point& t) noexcept;
    static time_point  from_time_t(time_t t) noexcept;
  };
}
Objects of type system_­clock represent wall clock time from the system-wide realtime clock.
Objects of type sys_­time<Duration> measure time since (and before) 1970-01-01 00:00:00 UTC excluding leap seconds.
This measure is commonly referred to as Unix time.
This measure facilitates an efficient mapping between sys_­time and calendar types ([time.cal]).
[Example
:

sys_­seconds{sys_­days{1970y/January/1}}.time_­since_­epoch() is 0s.

sys_­seconds{sys_­days{2000y/January/1}}.time_­since_­epoch() is 946'684'800s, which is 10'957 * 86'400s.

end example
]

25.7.1.2 Members [time.clock.system.members]

using system_clock::rep = unspecified;
Requires: system_­clock::duration::min() < system_­clock::duration::zero() shall be true.

[Note
:
This implies that rep is a signed type.
end note
]
static time_t to_time_t(const time_point& t) noexcept;
Returns: A time_­t object that represents the same point in time as t when both values are restricted to the coarser of the precisions of time_­t and time_­point.
It is implementation-defined whether values are rounded or truncated to the required precision.
static time_point from_time_t(time_t t) noexcept;
Returns: A time_­point object that represents the same point in time as t when both values are restricted to the coarser of the precisions of time_­t and time_­point.
It is implementation-defined whether values are rounded or truncated to the required precision.

25.7.1.3 Non-member functions [time.clock.system.nonmembers]

template<class charT, class traits, class Duration> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const sys_time<Duration>& tp);
Remarks: This operator shall not participate in overload resolution if treat_­as_­floating_­point_­v<typename Duration::rep> is true, or if Duration{1} >= days{1}.
Effects:
auto const dp = floor<days>(tp);
os << year_month_day{dp} << ' ' << time_of_day{tp-dp};
Returns: os.
[Example
:
cout << sys_seconds{0s} << '\n';                // 1970-01-01 00:00:00
cout << sys_seconds{946'684'800s} << '\n';      // 2000-01-01 00:00:00
cout << sys_seconds{946'688'523s} << '\n';      // 2000-01-01 01:02:03
end example
]
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const sys_days& dp);
Effects: os << year_­month_­day{dp}.
Returns: os.
template<class charT, class traits, class Duration> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const sys_time<Duration>& tp);
Effects: Streams tp into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
If %Z is used, it will be replaced with "UTC" widened to charT.
If %z is used (or a modified variant of %z), an offset of 0min will be formatted.
Returns: os.
template<class charT, class traits, class Duration, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, sys_time<Duration>& tp, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the sys_­time tp using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid date, is.setstate(ios_­base::failbit) shall be called and tp shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Additionally, the parsed offset will be subtracted from the successfully parsed timestamp prior to assigning that difference to tp.
Returns: is.

25.7.2 Class utc_­clock [time.clock.utc]

25.7.2.1 Overview [time.clock.utc.overview]

namespace std::chrono {
  class utc_clock {
  public:
    using rep                       = a signed arithmetic type;
    using period                    = ratio<unspecified, unspecified>;
    using duration                  = chrono::duration<rep, period>;
    using time_point                = chrono::time_point<utc_clock>;
    static constexpr bool is_steady = unspecified;

    static time_point now();

    template<class Duration>
      static sys_time<common_type_t<Duration, seconds>>
        to_sys(const utc_time<Duration>& t);
    template<class Duration>
      static utc_time<common_type_t<Duration, seconds>>
        from_sys(const sys_time<Duration>& t);
  };
}
In contrast to sys_­time, which does not take leap seconds into account, utc_­clock and its associated time_­point, utc_­time, count time, including leap seconds, since 1970-01-01 00:00:00 UTC.
[Example
:

clock_­cast<utc_­clock>(sys_­seconds{sys_­days{1970y/January/1}}).time_­since_­epoch() is 0s.

clock_­cast<utc_­clock>(sys_­seconds{sys_­days{2000y/January/1}}).time_­since_­epoch()
is 946'684'822s, which is 10'957 * 86'400s + 22s.

end example
]
utc_­clock is not a Cpp17TrivialClock unless the implementation can guarantee that utc_­clock::now() does not propagate an exception.
[Note
:
noexcept(from_­sys(system_­clock::now())) is false.
end note
]

25.7.2.2 Member functions [time.clock.utc.members]

static time_point now();
Returns: from_­sys(system_­clock::now()), or a more accurate value of utc_­time.
template<typename Duration> static sys_time<common_type_t<Duration, seconds>> to_sys(const utc_time<Duration>& u);
Returns: A sys_­time t, such that from_­sys(t) == u if such a mapping exists.
Otherwise u represents a time_­point during a leap second insertion and the last representable value of sys_­time prior to the insertion of the leap second is returned.
template<typename Duration> static utc_time<common_type_t<Duration, seconds>> from_sys(const sys_time<Duration>& t);
Returns: A utc_­time u, such that u.time_­since_­epoch() - t.time_­since_­epoch() is equal to the number of leap seconds that were inserted between t and 1970-01-01.
If t is exactly the date of leap second insertion, then the conversion counts that leap second as inserted.
[Example
:
auto t = sys_days{July/1/2015} - 2ns;
auto u = utc_clock::from_sys(t);
assert(u.time_since_epoch() - t.time_since_epoch() == 25s);
t += 1ns;
u = utc_clock::from_sys(t);
assert(u.time_since_epoch() - t.time_since_epoch() == 25s);
t += 1ns;
u = utc_clock::from_sys(t);
assert(u.time_since_epoch() - t.time_since_epoch() == 26s);
t += 1ns;
u = utc_clock::from_sys(t);
assert(u.time_since_epoch() - t.time_since_epoch() == 26s);
end example
]

25.7.2.3 Non-member functions [time.clock.utc.nonmembers]

template<class charT, class traits, class Duration> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const utc_time<Duration>& t);
Effects: Calls to_­stream(os, fmt, t), where fmt is a string containing "%F %T" widened to charT.
Returns: os.
template<class charT, class traits, class Duration> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const utc_time<Duration>& tp);
Effects: Streams tp into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
If %Z is used, it will be replaced with "UTC" widened to charT.
If %z is used (or a modified variant of %z), an offset of 0min will be formatted.
If tp represents a time during a leap second insertion, and if a seconds field is formatted, the integral portion of that format shall be "60" widened to charT.
Returns: os.
[Example
:
auto t = sys_days{July/1/2015} - 500ms;
auto u = clock_cast<utc_clock>(t);
for (auto i = 0; i < 8; ++i, u += 250ms)
  cout << u << " UTC\n";
Produces this output:
2015-06-30 23:59:59.500 UTC
2015-06-30 23:59:59.750 UTC
2015-06-30 23:59:60.000 UTC
2015-06-30 23:59:60.250 UTC
2015-06-30 23:59:60.500 UTC
2015-06-30 23:59:60.750 UTC
2015-07-01 00:00:00.000 UTC
2015-07-01 00:00:00.250 UTC
end example
]
template<class charT, class traits, class Duration, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, utc_time<Duration>& tp, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the utc_­time tp using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid date, is.setstate(ios_­base::failbit) shall be called and tp shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Additionally, the parsed offset will be subtracted from the successfully parsed timestamp prior to assigning that difference to tp.
Returns: is.

25.7.3 Class tai_­clock [time.clock.tai]

25.7.3.1 Overview [time.clock.tai.overview]

namespace std::chrono {
  class tai_clock {
  public:
    using rep                       = a signed arithmetic type;
    using period                    = ratio<unspecified, unspecified>;
    using duration                  = chrono::duration<rep, period>;
    using time_point                = chrono::time_point<tai_clock>;
    static constexpr bool is_steady = unspecified;

    static time_point now();

    template<class Duration>
      static utc_time<common_type_t<Duration, seconds>>
        to_utc(const tai_time<Duration>&) noexcept;
    template<class Duration>
      static tai_time<common_type_t<Duration, seconds>>
        from_utc(const utc_time<Duration>&) noexcept;
  };
}
The clock tai_­clock measures seconds since 1958-01-01 00:00:00 and is offset 10s ahead of UTC at this date.
That is, 1958-01-01 00:00:00 TAI is equivalent to 1957-12-31 23:59:50 UTC.
Leap seconds are not inserted into TAI.
Therefore every time a leap second is inserted into UTC, UTC falls another second behind TAI.
For example by 2000-01-01 there had been 22 leap seconds inserted so 2000-01-01 00:00:00 UTC is equivalent to 2000-01-01 00:00:32 TAI (22s plus the initial 10s offset).
tai_­clock is not a Cpp17TrivialClock unless the implementation can guarantee that tai_­clock::now() does not propagate an exception.
[Note
:
noexcept(from_­utc(utc_­clock::now())) is false.
end note
]

25.7.3.2 Member functions [time.clock.tai.members]

static time_point now();
Returns: from_­utc(utc_­clock::now()), or a more accurate value of tai_­time.
template<class Duration> static utc_time<common_type_t<Duration, seconds>> to_utc(const tai_time<Duration>& t) noexcept;
Returns:
utc_time<common_type_t<Duration, seconds>>{t.time_since_epoch()} - 378691210s
[Note
:
378691210s == sys_days{1970y/January/1} - sys_days{1958y/January/1} + 10s
end note
]
template<class Duration> static tai_time<common_type_t<Duration, seconds>> from_utc(const utc_time<Duration>& t) noexcept;
Returns:
tai_time<common_type_t<Duration, seconds>>{t.time_since_epoch()} + 378691210s
[Note
:
378691210s == sys_days{1970y/January/1} - sys_days{1958y/January/1} + 10s
end note
]

25.7.3.3 Non-member functions [time.clock.tai.nonmembers]

template<class charT, class traits, class Duration> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const tai_time<Duration>& t);
Effects: Calls to_­stream(os, fmt, t), where fmt is a string containing "%F %T" widened to charT.
Returns: os.
template<class charT, class traits, class Duration> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const tai_time<Duration>& tp);
Effects: Streams tp into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
If %Z is used, it will be replaced with "TAI".
If %z is used (or a modified variant of %z), an offset of 0min will be formatted.
The date and time formatted shall be equivalent to that formatted by a sys_­time initialized with:
sys_time<Duration>{tp.time_since_epoch()} -
  (sys_days{1970y/January/1} - sys_days{1958y/January/1})
Returns: os.
[Example
:
auto st = sys_days{2000y/January/1};
auto tt = clock_cast<tai_clock>(st);
cout << format("%F %T %Z == ", st) << format("%F %T %Z\n", tt);
Produces this output:
2000-01-01 00:00:00 UTC == 2000-01-01 00:00:32 TAI
end example
]
template<class charT, class traits, class Duration, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, tai_time<Duration>& tp, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the tai_­time tp using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid date, is.setstate(ios_­base::failbit) shall be called and tp shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Additionally, the parsed offset will be subtracted from the successfully parsed timestamp prior to assigning that difference to tp.
Returns: is.

25.7.4 Class gps_­clock [time.clock.gps]

25.7.4.1 Overview [time.clock.gps.overview]

namespace std::chrono {
  class gps_clock {
  public:
    using rep                       = a signed arithmetic type;
    using period                    = ratio<unspecified, unspecified>;
    using duration                  = chrono::duration<rep, period>;
    using time_point                = chrono::time_point<gps_clock>;
    static constexpr bool is_steady = unspecified;

    static time_point now();

    template<class Duration>
      static utc_time<common_type_t<Duration, seconds>>
        to_utc(const gps_time<Duration>&) noexcept;
    template<class Duration>
      static gps_time<common_type_t<Duration, seconds>>
        from_utc(const utc_time<Duration>&) noexcept;
  };
}
The clock gps_­clock measures seconds since the first Sunday of January, 1980 00:00:00 UTC.
Leap seconds are not inserted into GPS.
Therefore every time a leap second is inserted into UTC, UTC falls another second behind GPS.
Aside from the offset from 1958y/January/1 to 1980y/January/Sunday[1], GPS is behind TAI by 19s due to the 10s offset between 1958 and 1970 and the additional 9 leap seconds inserted between 1970 and 1980.
gps_­clock is not a Cpp17TrivialClock unless the implementation can guarantee that gps_­clock::now() does not propagate an exception.
[Note
:
noexcept(from_­utc(utc_­clock::now())) is false.
end note
]

25.7.4.2 Member functions [time.clock.gps.members]

static time_point now();
Returns: from_­utc(utc_­clock::now()), or a more accurate value of gps_­time.
template<class Duration> static utc_time<common_type_t<Duration, seconds>> to_utc(const gps_time<Duration>& t) noexcept;
Returns:
gps_time<common_type_t<Duration, seconds>>{t.time_since_epoch()} + 315964809s
[Note
:
315964809s == sys_days{1980y/January/Sunday[1]} - sys_days{1970y/January/1} + 9s
end note
]
template<class Duration> static gps_time<common_type_t<Duration, seconds>> from_utc(const utc_time<Duration>& t) noexcept;
Returns:
gps_time<common_type_t<Duration, seconds>>{t.time_since_epoch()} - 315964809s
[Note
:
315964809s == sys_days{1980y/January/Sunday[1]} - sys_days{1970y/January/1} + 9s
end note
]

25.7.4.3 Non-member functions [time.clock.gps.nonmembers]

template<class charT, class traits, class Duration> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const gps_time<Duration>& t);
Effects: Calls to_­stream(os, fmt, t), where fmt is a string containing "%F %T" widened to charT.
Returns: os.
template<class charT, class traits, class Duration> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const gps_time<Duration>& tp);
Effects: Streams tp into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
If %Z is used, it will be replaced with "GPS".
If %z is used (or a modified variant of %z), an offset of 0min will be formatted.
The date and time formatted shall be equivalent to that formatted by a sys_­time initialized with:
sys_time<Duration>{tp.time_since_epoch()} +
  (sys_days{1980y/January/Sunday[1]} - sys_days{1970y/January/1})
Returns: os.
[Example
:
auto st = sys_days{2000y/January/1};
auto gt = clock_cast<gps_clock>(st);
cout << format("%F %T %Z == ", st) << format("%F %T %Z\n", gt);
Produces this output:
2000-01-01 00:00:00 UTC == 2000-01-01 00:00:13 GPS
end example
]
template<class charT, class traits, class Duration, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, gps_time<Duration>& tp, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the gps_­time tp using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid date, is.setstate(ios_­base::failbit) shall be called and tp shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Additionally, the parsed offset will be subtracted from the successfully parsed timestamp prior to assigning that difference to tp.
Returns: is.

25.7.5 Class file_­clock [time.clock.file]

25.7.5.1 Overview [time.clock.file.overview]

namespace std::chrono {
  class file_clock {
  public:
    using rep                       = a signed arithmetic type;
    using period                    = ratio<unspecified, unspecified>;
    using duration                  = chrono::duration<rep, period>;
    using time_point                = chrono::time_point<file_clock>;
    static constexpr bool is_steady = unspecified;

    static time_point now() noexcept;

    // Conversion functions, see below
  };
}
The clock file_­clock is used to create the time_­point system used for file_­time_­type ([filesystems]).
Its epoch is unspecified.

25.7.5.2 Member functions [time.clock.file.members]

static time_point now();
Returns: A file_­clock::time_­point indicating the current time.
The class file_­clock shall provide precisely one of the following two sets of static member functions:
template<class Duration>
  static sys_time<see below>
    to_sys(const file_time<Duration>&);
template<class Duration>
  static file_time<see below>
    from_sys(const sys_time<Duration>&);
or:
template<class Duration>
  static utc_time<see below>
    to_utc(const file_time<Duration>&);
template<class Duration>
  static file_time<see below>
    from_utc(const utc_time<Duration>&);
These member functions shall provide time_­point conversions consistent with those specified by utc_­clock, tai_­clock, and gps_­clock.
The Duration of the resultant time_­point is computed from the Duration of the input time_­point.

25.7.5.3 Non-member functions [time.clock.file.nonmembers]

template<class charT, class traits, class Duration> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const file_time<Duration>& t);
Effects: Calls to_­stream(os, fmt, t), where fmt is a string containing "%F %T" widened to charT.
Returns: os.
template<class charT, class traits, class Duration> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const file_time<Duration>& tp);
Effects: Streams tp into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
If %Z is used, it will be replaced with "UTC" widened to charT.
If %z is used (or a modified variant of %z), an offset of 0min will be formatted.
The date and time formatted shall be equivalent to that formatted by a sys_­time initialized with clock_­cast<system_­clock>(tp), or by a utc_­time initialized with clock_­cast<utc_­clock>(tp).
Returns: os.
template<class charT, class traits, class Duration, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, file_time<Duration>& tp, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the file_­time tp using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid date, is.setstate(ios_­base::failbit) shall be called and tp shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Additionally, the parsed offset will be subtracted from the successfully parsed timestamp prior to assigning that difference to tp.
Returns: is.

25.7.6 Class steady_­clock [time.clock.steady]

namespace std::chrono {
  class steady_clock {
  public:
    using rep        = unspecified;
    using period     = ratio<unspecified, unspecified>;
    using duration   = chrono::duration<rep, period>;
    using time_point = chrono::time_point<unspecified, duration>;
    static constexpr bool is_steady = true;

    static time_point now() noexcept;
  };
}
Objects of class steady_­clock represent clocks for which values of time_­point never decrease as physical time advances and for which values of time_­point advance at a steady rate relative to real time.
That is, the clock may not be adjusted.

25.7.7 Class high_­resolution_­clock [time.clock.hires]

namespace std::chrono {
  class high_resolution_clock {
  public:
    using rep        = unspecified;
    using period     = ratio<unspecified, unspecified>;
    using duration   = chrono::duration<rep, period>;
    using time_point = chrono::time_point<unspecified, duration>;
    static constexpr bool is_steady = unspecified;

    static time_point now() noexcept;
  };
}
Objects of class high_­resolution_­clock represent clocks with the shortest tick period.
high_­resolution_­clock may be a synonym for system_­clock or steady_­clock.

25.7.8 Local time [time.clock.local]

The family of time points denoted by local_­time<Duration> are based on the pseudo clock local_­t.
local_­t has no member now() and thus does not meet the clock requirements.
Nevertheless local_­time<Duration> serves the vital role of representing local time with respect to a not-yet-specified time zone.
Aside from being able to get the current time, the complete time_­point algebra is available for local_­time<Duration> (just as for sys_­time<Duration>).
template<class charT, class traits, class Duration> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const local_time<Duration>& lt);
Effects:
os << sys_time<Duration>{lt.time_since_epoch()};
Returns: os.
template<class charT, class traits, class Duration> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const local_time<Duration>& tp, const string* abbrev = nullptr, const seconds* offset_sec = nullptr);
Effects: Streams tp into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
If %Z is used, it will be replaced with *abbrev if abbrev is not equal to nullptr.
If abbrev is equal to nullptr (and %Z is used), os.setstate(ios_­base::failbit) shall be called.
If %z is used (or a modified variant of %z), it will be formatted with the value of *offset_­sec if offset_­sec is not equal to nullptr.
If %z (or a modified variant of %z) is used, and offset_­sec is equal to nullptr, then os.setstate(ios_­base::failbit) shall be called.
Returns: os.
template<class charT, class traits, class Duration, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, local_time<Duration>& tp, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the local_­time tp using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid date, is.setstate(ios_­base::failbit) shall be called and tp shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Returns: is.

25.7.9 time_­point conversions [time.clock.cast]

25.7.9.1 Class template clock_­time_­conversion [time.clock.conv]

namespace std::chrono {
  template<class DestClock, class SourceClock>
  struct clock_time_conversion {};
}
clock_­time_­conversion serves as a trait which can be used to specify how to convert a source time_­point of type time_­point<SourceClock, Duration> to a destination time_­point of type time_­point<DestClock, Duration> via a specialization: clock_­time_­conversion<DestClock, SourceClock>.
A specialization of clock_­time_­conversion<DestClock, SourceClock> shall provide a const-qualified operator() that takes a parameter of type time_­point<SourceClock, Duration> and returns a time_­point<DestClock, OtherDuration> representing an equivalent point in time.
OtherDuration is a chrono::duration whose specialization is computed from the input Duration in a manner which can vary for each clock_­time_­conversion specialization.
A program may specialize clock_­time_­conversion if at least one of the template parameters is a user-defined clock type.
Several specializations are provided by the implementation, as described in [time.clock.cast.id], [time.clock.cast.sys.utc], [time.clock.cast.sys], and [time.clock.cast.utc].

25.7.9.2 Identity conversions [time.clock.cast.id]

template<typename Clock>
struct clock_time_conversion<Clock, Clock> {
  template<class Duration>
    time_point<Clock, Duration>
      operator()(const time_point<Clock, Duration>& t) const;
};
template<class Duration> time_point<Clock, Duration> operator()(const time_point<Clock, Duration>& t) const;
Returns: t.
template<>
struct clock_time_conversion<system_clock, system_clock> {
  template<class Duration>
    sys_time<Duration>
      operator()(const sys_time<Duration>& t) const;
};
template<class Duration> sys_time<Duration> operator()(const sys_time<Duration>& t) const;
Returns: t.
template<>
struct clock_time_conversion<utc_clock, utc_clock> {
  template<class Duration>
    utc_time<Duration>
      operator()(const utc_time<Duration>& t) const;
};
template<class Duration> utc_time<Duration> operator()(const utc_time<Duration>& t) const;
Returns: t.

25.7.9.3 Conversions between system_­clock and utc_­clock [time.clock.cast.sys.utc]

template<>
struct clock_time_conversion<utc_clock, system_clock> {
  template<class Duration>
    utc_time<common_type_t<Duration, seconds>>
      operator()(const sys_time<Duration>& t) const;
};
template<class Duration> utc_time<common_type_t<Duration, seconds>> operator()(const sys_time<Duration>& t) const;
Returns: utc_­clock::from_­sys(t).
template<>
struct clock_time_conversion<system_clock, utc_clock> {
  template<class Duration>
    sys_time<common_type_t<Duration, seconds>>
      operator()(const utc_time<Duration>& t) const;
};
template<class Duration> sys_time<common_type_t<Duration, seconds>> operator()(const utc_time<Duration>& t) const;
Returns: utc_­clock::to_­sys(t).

25.7.9.4 Conversions between system_­clock and other clocks [time.clock.cast.sys]

template<class SourceClock>
struct clock_time_conversion<system_clock, SourceClock> {
  template<class Duration>
    auto operator()(const time_point<SourceClock, Duration>& t) const
      -> decltype(SourceClock::to_sys(t));
};
template<class Duration> auto operator()(const time_point<SourceClock, Duration>& t) const -> decltype(SourceClock::to_sys(t));
Remarks: This function does not participate in overload resolution unless SourceClock::to_­sys(t) is well-formed.
If SourceClock::to_­sys(t) does not return sys_­time<Duration>, where Duration is a valid chrono::duration specialization, the program is ill-formed.
Returns: SourceClock::to_­sys(t).
template<class DestClock>
struct clock_time_conversion<DestClock, system_clock> {
  template<class Duration>
    auto operator()(const sys_time<Duration>& t) const
      -> decltype(DestClock::from_sys(t));
};
template<class Duration> auto operator()(const sys_time<Duration>& t) const -> decltype(DestClock::from_sys(t));
Remarks: This function does not participate in overload resolution unless DestClock::from_­sys(t) is well-formed.
If DestClock::from_­sys(t) does not return time_­point<DestClock, Duration>, where Duration is a valid chrono::duration specialization, the program is ill-formed.
Returns: DestClock::from_­sys(t).

25.7.9.5 Conversions between utc_­clock and other clocks [time.clock.cast.utc]

template<class SourceClock>
struct clock_time_conversion<utc_clock, SourceClock> {
  template<class Duration>
    auto operator()(const time_point<SourceClock, Duration>& t) const
      -> decltype(SourceClock::to_utc(t));
};
template<class Duration> auto operator()(const time_point<SourceClock, Duration>& t) const -> decltype(SourceClock::to_utc(t));
Remarks: This function does not participate in overload resolution unless SourceClock::to_­utc(t) is well-formed.
If SourceClock::to_­utc(t) does not return utc_­time<Duration>, where Duration is a valid chrono::duration specialization, the program is ill-formed.
Returns: SourceClock::to_­utc(t).
template<class DestClock>
struct clock_time_conversion<DestClock, utc_clock> {
  template<class Duration>
    auto operator()(const utc_time<Duration>& t) const
      -> decltype(DestClock::from_utc(t));
};
template<class Duration> auto operator()(const utc_time<Duration>& t) const -> decltype(DestClock::from_utc(t));
Remarks: This function does not participate in overload resolution unless DestClock::from_­utc(t) is well-formed.
If DestClock::from_­utc(t) does not return time_­point<DestClock, Duration>, where Duration is a valid chrono::duration specialization, the program is ill-formed.
Returns: DestClock::from_­utc(t).

25.7.9.6 Function template clock_­cast [time.clock.cast.fn]

template<class DestClock, class SourceClock, class Duration> auto clock_cast(const time_point<SourceClock, Duration>& t);
Remarks: This function does not participate in overload resolution unless at least one of the following clock time conversion expressions is well-formed:
  • clock_time_conversion<DestClock, SourceClock>{}(t)
    
  • clock_time_conversion<DestClock, system_clock>{}(
        clock_time_conversion<system_clock, SourceClock>{}(t))
    
  • clock_time_conversion<DestClock, utc_clock>{}(
        clock_time_conversion<utc_clock, SourceClock>{}(t))
    
  • clock_time_conversion<DestClock, utc_clock>{}(
        clock_time_conversion<utc_clock, system_clock>{}(
            clock_time_conversion<system_clock, SourceClock>{}(t)))
    
  • clock_time_conversion<DestClock, system_clock>{}(
        clock_time_conversion<system_clock, utc_clock>{}(
            clock_time_conversion<utc_clock, SourceClock>{}(t)))
    
A clock time conversion expression is considered better than another clock time conversion expression if it involves fewer operator() calls on clock_­time_­conversion specializations.
If, among the well-formed clock time conversion expressions from the above list, there is not a unique best expression, the clock_­cast is ambiguous and the program is ill-formed.
Returns: The best well-formed clock time conversion expression in the above list.

25.8 The civil calendar [time.cal]

25.8.1 In general [time.cal.general]

The types in [time.cal] describe the civil (Gregorian) calendar and its relationship to sys_­days and local_­days.

25.8.2 Class last_­spec [time.cal.last]

namespace std::chrono {
  struct last_spec {
    explicit last_spec() = default;
  };
}
The type last_­spec is used in conjunction with other calendar types to specify the last in a sequence.
For example, depending on context, it can represent the last day of a month, or the last day of the week of a month.

25.8.3 Class day [time.cal.day]

25.8.3.1 Overview [time.cal.day.overview]

namespace std::chrono {
  class day {
    unsigned char d_;           // exposition only
  public:
    day() = default;
    explicit constexpr day(unsigned d) noexcept;

    constexpr day& operator++()    noexcept;
    constexpr day  operator++(int) noexcept;
    constexpr day& operator--()    noexcept;
    constexpr day  operator--(int) noexcept;

    constexpr day& operator+=(const days& d) noexcept;
    constexpr day& operator-=(const days& d) noexcept;

    explicit constexpr operator unsigned() const noexcept;
    constexpr bool ok() const noexcept;
  };
}
day represents a day of a month.
It normally holds values in the range 1 to 31, but may hold non-negative values outside this range.
It can be constructed with any unsigned value, which will be subsequently truncated to fit into day's unspecified internal storage.
day is Cpp17EqualityComparable (Table 22) and Cpp17LessThanComparable (Table 23), and participates in basic arithmetic with days objects, which represent a difference between two day objects.
day is a trivially copyable and standard-layout class type.

25.8.3.2 Member functions [time.cal.day.members]

explicit constexpr day(unsigned d) noexcept;
Effects: Constructs an object of type day by initializing d_­ with d.
The value held is unspecified if d is not in the range [0, 255].
constexpr day& operator++() noexcept;
Effects: ++d_­.
Returns: *this.
constexpr day operator++(int) noexcept;
Effects: ++(*this).
Returns: A copy of *this as it existed on entry to this member function.
constexpr day& operator--() noexcept;
Effects: --d_­.
Returns: *this.
constexpr day operator--(int) noexcept;
Effects: --(*this).
Returns: A copy of *this as it existed on entry to this member function.
constexpr day& operator+=(const days& d) noexcept;
Effects: *this = *this + d.
Returns: *this.
constexpr day& operator-=(const days& d) noexcept;
Effects: *this = *this - d.
Returns: *this.
explicit constexpr operator unsigned() const noexcept;
Returns: d_­.
constexpr bool ok() const noexcept;
Returns: 1 <= d_­ && d_­ <= 31.

25.8.3.3 Non-member functions [time.cal.day.nonmembers]

constexpr bool operator==(const day& x, const day& y) noexcept;
Returns: unsigned{x} == unsigned{y}.
constexpr bool operator<(const day& x, const day& y) noexcept;
Returns: unsigned{x} < unsigned{y}.
constexpr day operator+(const day& x, const days& y) noexcept;
Returns: day(unsigned{x} + y.count()).
constexpr day operator+(const days& x, const day& y) noexcept;
Returns: y + x.
constexpr day operator-(const day& x, const days& y) noexcept;
Returns: x + -y.
constexpr days operator-(const day& x, const day& y) noexcept;
Returns: days{int(unsigned{x}) - int(unsigned{y}).
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const day& d);
Effects: Inserts format(fmt, d) where fmt is "%d" widened to charT.
If !d.ok(), appends with " is not a valid day".
Returns: os.
template<class charT, class traits> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const day& d);
Effects: Streams d into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
Returns: os.
template<class charT, class traits, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, day& d, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the day d using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid day, is.setstate(ios_­base::failbit) shall be called and d shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Returns: is.
constexpr day operator""d(unsigned long long d) noexcept;
Returns: day{static_­cast<unsigned>(d)}.

25.8.4 Class month [time.cal.month]

25.8.4.1 Overview [time.cal.month.overview]

namespace std::chrono {
  class month {
    unsigned char m_;           // exposition only
  public:
    month() = default;
    explicit constexpr month(unsigned m) noexcept;

    constexpr month& operator++()    noexcept;
    constexpr month  operator++(int) noexcept;
    constexpr month& operator--()    noexcept;
    constexpr month  operator--(int) noexcept;

    constexpr month& operator+=(const months& m) noexcept;
    constexpr month& operator-=(const months& m) noexcept;

    explicit constexpr operator unsigned() const noexcept;
    constexpr bool ok() const noexcept;
  };
}
month represents a month of a year.
It normally holds values in the range 1 to 12, but may hold non-negative values outside this range.
It can be constructed with any unsigned value, which will be subsequently truncated to fit into month's unspecified internal storage.
month is Cpp17EqualityComparable (Table 22) and Cpp17LessThanComparable (Table 23), and participates in basic arithmetic with months objects, which represent a difference between two month objects.
month is a trivially copyable and standard-layout class type.

25.8.4.2 Member functions [time.cal.month.members]

explicit constexpr month(unsigned m) noexcept;
Effects: Constructs an object of type month by initializing m_­ with m.
The value held is unspecified if m is not in the range [0, 255].
constexpr month& month::operator++() noexcept;
Effects: *this += months{1}.
Returns: *this.
constexpr month operator++(int) noexcept;
Effects: ++(*this).
Returns: A copy of *this as it existed on entry to this member function.
constexpr month& operator--() noexcept;
Effects: *this -= months{1}.
Returns: *this.
constexpr month operator--(int) noexcept;
Effects: --(*this).
Returns: A copy of *this as it existed on entry to this member function.
constexpr month& operator+=(const months& m) noexcept;
Effects: *this = *this + m.
Returns: *this.
constexpr month& operator-=(const months& m) noexcept;
Effects: *this = *this - m.
Returns: *this.
explicit constexpr month::operator unsigned() const noexcept;
Returns: m_­.
constexpr bool month::ok() const noexcept;
Returns: 1 <= m_­ && m_­ <= 12.

25.8.4.3 Non-member functions [time.cal.month.nonmembers]

constexpr bool operator==(const month& x, const month& y) noexcept;
Returns: unsigned{x} == unsigned{y}.
constexpr bool operator<(const month& x, const month& y) noexcept;
Returns: unsigned{x} < unsigned{y}.
constexpr month operator+(const month& x, const months& y) noexcept;
Returns:
month{modulo(static_cast<long long>(unsigned{x}) + (y.count() - 1), 12) + 1}
where modulo(n, 12) computes the remainder of n divided by 12 using Euclidean division.
[Note
:
Given a divisor of 12, Euclidean division truncates towards negative infinity and always produces a remainder in the range of [0, 11].
Assuming no overflow in the signed summation, this operation results in a month holding a value in the range [1, 12] even if !x.ok().
end note
]
[Example
:
February + months{11} == January.
end example
]
constexpr month operator+(const months& x, const month& y) noexcept;
Returns: y + x.
constexpr month operator-(const month& x, const months& y) noexcept;
Returns: x + -y.
constexpr months operator-(const month& x, const month& y) noexcept;
Returns: If x.ok() == true and y.ok() == true, returns a value m in the range [months{0}, months{11}] satisfying y + m == x.
Otherwise the value returned is unspecified.
[Example
:
January - February == months{11}.
end example
]
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const month& m);
Effects: If m.ok() == true inserts format(os.getloc(), fmt, m) where fmt is "%b" widened to charT.
Otherwise inserts unsigned{m} << " is not a valid month".
Returns: os.
template<class charT, class traits> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const month& m);
Effects: Streams m into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
Returns: os.
template<class charT, class traits, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, month& m, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the month m using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid month, is.setstate(ios_­base::failbit) shall be called and m shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Returns: is.

25.8.5 Class year [time.cal.year]

25.8.5.1 Overview [time.cal.year.overview]

namespace std::chrono {
  class year {
    short y_;                   // exposition only
  public:
    year() = default;
    explicit constexpr year(int y) noexcept;

    constexpr year& operator++()    noexcept;
    constexpr year  operator++(int) noexcept;
    constexpr year& operator--()    noexcept;
    constexpr year  operator--(int) noexcept;

    constexpr year& operator+=(const years& y) noexcept;
    constexpr year& operator-=(const years& y) noexcept;

    constexpr year operator+() const noexcept;
    constexpr year operator-() const noexcept;

    constexpr bool is_leap() const noexcept;

    explicit constexpr operator int() const noexcept;
    constexpr bool ok() const noexcept;

    static constexpr year min() noexcept;
    static constexpr year max() noexcept;
  };
}
year represents a year in the civil calendar.
It can represent values in the range [min(), max()].
It can be constructed with any int value, which will be subsequently truncated to fit into year's unspecified internal storage.
year is Cpp17EqualityComparable (Table 22) and Cpp17LessThanComparable (Table 23), and participates in basic arithmetic with years objects, which represent a difference between two year objects.
year is a trivially copyable and standard-layout class type.

25.8.5.2 Member functions [time.cal.year.members]

explicit constexpr year(int y) noexcept;
Effects: Constructs an object of type year by initializing y_­ with y.
The value held is unspecified if y is not in the range [-32767, 32767].
constexpr year& operator++() noexcept;
Effects: ++y_­.
Returns: *this.
constexpr year operator++(int) noexcept;
Effects: ++(*this).
Returns: A copy of *this as it existed on entry to this member function.
constexpr year& operator--() noexcept;
Effects: --y_­.
Returns: *this.
constexpr year operator--(int) noexcept;
Effects: --(*this).
Returns: A copy of *this as it existed on entry to this member function.
constexpr year& operator+=(const years& y) noexcept;
Effects: *this = *this + y.
Returns: *this.
constexpr year& operator-=(const years& y) noexcept;
Effects: *this = *this - y.
Returns: *this.
constexpr year operator+() const noexcept;
Returns: *this.
constexpr year year::operator-() const noexcept;
Returns: year{-y_­}.
constexpr bool is_leap() const noexcept;
Returns: y_­ % 4 == 0 && (y_­ % 100 != 0 || y_­ % 400 == 0).
explicit constexpr operator int() const noexcept;
Returns: y_­.
constexpr bool ok() const noexcept;
Returns: min() <= y_­ && y_­ <= max().
static constexpr year min() noexcept;
Returns: year{-32767}.
static constexpr year max() noexcept;
Returns: year{32767}.

25.8.5.3 Non-member functions [time.cal.year.nonmembers]

constexpr bool operator==(const year& x, const year& y) noexcept;
Returns: int{x} == int{y}.
constexpr bool operator<(const year& x, const year& y) noexcept;
Returns: int{x} < int{y}.
constexpr year operator+(const year& x, const years& y) noexcept;
Returns: year{int{x} + y.count()}.
constexpr year operator+(const years& x, const year& y) noexcept;
Returns: y + x.
constexpr year operator-(const year& x, const years& y) noexcept;
Returns: x + -y.
constexpr years operator-(const year& x, const year& y) noexcept;
Returns: years{int{x} - int{y}}.
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const year& y);
Effects: Inserts format(fmt, y) where fmt is "%Y" widened to charT.
If !y.ok(), appends with " is not a valid year".
Returns: os.
template<class charT, class traits> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const year& y);
Effects: Streams y into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
Returns: os.
template<class charT, class traits, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, year& y, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the year y using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid year, is.setstate(ios_­base::failbit) shall be called and y shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Returns: is.
constexpr year operator""y(unsigned long long y) noexcept;
Returns: year{static_­cast<int>(y)}.

25.8.6 Class weekday [time.cal.wd]

25.8.6.1 Overview [time.cal.wd.overview]

namespace std::chrono {
  class weekday {
    unsigned char wd_;          // exposition only
  public:
    weekday() = default;
    explicit constexpr weekday(unsigned wd) noexcept;
    constexpr weekday(const sys_days& dp) noexcept;
    explicit constexpr weekday(const local_days& dp) noexcept;

    constexpr weekday& operator++()    noexcept;
    constexpr weekday  operator++(int) noexcept;
    constexpr weekday& operator--()    noexcept;
    constexpr weekday  operator--(int) noexcept;

    constexpr weekday& operator+=(const days& d) noexcept;
    constexpr weekday& operator-=(const days& d) noexcept;

    explicit constexpr operator unsigned() const noexcept;
    constexpr bool ok() const noexcept;

    constexpr weekday_indexed operator[](unsigned index) const noexcept;
    constexpr weekday_last    operator[](last_spec) const noexcept;
  };
}
weekday represents a day of the week in the civil calendar.
It normally holds values in the range 0 to 6, corresponding to Sunday through Saturday, but it may hold non-negative values outside this range.
It can be constructed with any unsigned value, which will be subsequently truncated to fit into weekday's unspecified internal storage.
weekday is Cpp17EqualityComparable (Table 22).
[Note
:
weekday is not Cpp17LessThanComparable because there is no universal consensus on which day is the first day of the week.
weekday's arithmetic operations treat the days of the week as a circular range, with no beginning and no end.
end note
]
weekday is a trivially copyable and standard-layout class type.

25.8.6.2 Member functions [time.cal.wd.members]

explicit constexpr weekday(unsigned wd) noexcept;
Effects: Constructs an object of type weekday by initializing wd_­ with wd.
The value held is unspecified if wd is not in the range [0, 255].
constexpr weekday(const sys_days& dp) noexcept;
Effects: Constructs an object of type weekday by computing what day of the week corresponds to the sys_­days dp, and representing that day of the week in wd_­.
[Example
:
If dp represents 1970-01-01, the constructed weekday represents Thursday by storing 4 in wd_­.
end example
]
explicit constexpr weekday(const local_days& dp) noexcept;
Effects: Constructs an object of type weekday by computing what day of the week corresponds to the local_­days dp, and representing that day of the week in wd_­.
Remarks: The value after construction is identical to that constructed from sys_­days{dp.time_­since_­epoch()}.
constexpr weekday& operator++() noexcept;
Effects: *this += days{1}.
Returns: *this.
constexpr weekday operator++(int) noexcept;
Effects: ++(*this).
Returns: A copy of *this as it existed on entry to this member function.
constexpr weekday& operator--() noexcept;
Effects: *this -= days{1}.
Returns: *this.
constexpr weekday operator--(int) noexcept;
Effects: --(*this).
Returns: A copy of *this as it existed on entry to this member function.
constexpr weekday& operator+=(const days& d) noexcept;
Effects: *this = *this + d.
Returns: *this.
constexpr weekday& operator-=(const days& d) noexcept;
Effects: *this = *this - d.
Returns: *this.
explicit constexpr operator unsigned() const noexcept;
Returns: wd_­.
constexpr bool ok() const noexcept;
Returns: wd_­ <= 6.
constexpr weekday_indexed operator[](unsigned index) const noexcept;
Returns: {*this, index}.
constexpr weekday_last operator[](last_spec) const noexcept;
Returns: weekday_­last{*this}.

25.8.6.3 Non-member functions [time.cal.wd.nonmembers]

constexpr bool operator==(const weekday& x, const weekday& y) noexcept;
Returns: unsigned{x} == unsigned{y}.
constexpr weekday operator+(const weekday& x, const days& y) noexcept;
Returns:
weekday{modulo(static_cast<long long>(unsigned{x}) + y.count(), 7)}
where modulo(n, 7) computes the remainder of n divided by 7 using Euclidean division.
[Note
:
Given a divisor of 7, Euclidean division truncates towards negative infinity and always produces a remainder in the range of [0, 6].
Assuming no overflow in the signed summation, this operation results in a weekday holding a value in the range [0, 6] even if !x.ok().
end note
]
[Example
:
Monday + days{6} == Sunday.
end example
]
constexpr weekday operator+(const days& x, const weekday& y) noexcept;
Returns: y + x.
constexpr weekday operator-(const weekday& x, const days& y) noexcept;
Returns: x + -y.
constexpr days operator-(const weekday& x, const weekday& y) noexcept;
Returns: If x.ok() == true and y.ok() == true, returns a value d in the range [days{0}, days{6}] satisfying y + d == x.
Otherwise the value returned is unspecified.
[Example
:
Sunday - Monday == days{6}.
end example
]
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const weekday& wd);
Effects: If wd.ok() == true inserts format(os.getloc(), fmt, wd) where fmt is "%a" widened to charT.
Otherwise inserts unsigned{wd} << " is not a valid weekday".
Returns: os.
template<class charT, class traits> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const weekday& wd);
Effects: Streams wd into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
Returns: os.
template<class charT, class traits, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, weekday& wd, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the weekday wd using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid weekday, is.setstate(ios_­base::failbit) shall be called and wd shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Returns: is.

25.8.7 Class weekday_­indexed [time.cal.wdidx]

25.8.7.1 Overview [time.cal.wdidx.overview]

namespace std::chrono {
  class weekday_indexed {
    chrono::weekday  wd_;       // exposition only
    unsigned char    index_;    // exposition only

  public:
    weekday_indexed() = default;
    constexpr weekday_indexed(const chrono::weekday& wd, unsigned index) noexcept;

    constexpr chrono::weekday weekday() const noexcept;
    constexpr unsigned        index()   const noexcept;
    constexpr bool ok() const noexcept;
  };
}
weekday_­indexed represents a weekday and a small index in the range 1 to 5.
This class is used to represent the first, second, third, fourth, or fifth weekday of a month.
[Note
:
A weekday_­indexed object can be constructed by indexing a weekday with an unsigned.
end note
]
[Example
:
constexpr auto wdi = Sunday[2]; // wdi is the second Sunday of an as yet unspecified month
static_assert(wdi.weekday() == Sunday);
static_assert(wdi.index() == 2);
end example
]
weekday_­indexed is a trivially copyable and standard-layout class type.

25.8.7.2 Member functions [time.cal.wdidx.members]

constexpr weekday_indexed(const chrono::weekday& wd, unsigned index) noexcept;
Effects: Constructs an object of type weekday_­indexed by initializing wd_­ with wd and index_­ with index.
The values held are unspecified if !wd.ok() or index is not in the range [1, 5].
constexpr chrono::weekday weekday() const noexcept;
Returns: wd_­.
constexpr unsigned index() const noexcept;
Returns: index_­.
constexpr bool ok() const noexcept;
Returns: wd_­.ok() && 1 <= index_­ && index_­ <= 5.

25.8.7.3 Non-member functions [time.cal.wdidx.nonmembers]

constexpr bool operator==(const weekday_indexed& x, const weekday_indexed& y) noexcept;
Returns: x.weekday() == y.weekday() && x.index() == y.index().
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const weekday_indexed& wdi);
Effects: os << wdi.weekday() << '[' << wdi.index().
If wdi.index() is in the range [1, 5], appends with ']', otherwise appends with " is not a valid index]".
Returns: os.

25.8.8 Class weekday_­last [time.cal.wdlast]

25.8.8.1 Overview [time.cal.wdlast.overview]

namespace std::chrono {
  class weekday_last {
    chrono::weekday wd_;                // exposition only

    public:
    explicit constexpr weekday_last(const chrono::weekday& wd) noexcept;

    constexpr chrono::weekday weekday() const noexcept;
    constexpr bool ok() const noexcept;
  };
}
weekday_­last represents the last weekday of a month.
[Note
:
A weekday_­last object can be constructed by indexing a weekday with last.
end note
]
[Example
:
constexpr auto wdl = Sunday[last];      // wdl is the last Sunday of an as yet unspecified month
static_assert(wdl.weekday() == Sunday);
end example
]
weekday_­last is a trivially copyable and standard-layout class type.

25.8.8.2 Member functions [time.cal.wdlast.members]

explicit constexpr weekday_last(const chrono::weekday& wd) noexcept;
Effects: Constructs an object of type weekday_­last by initializing wd_­ with wd.
constexpr chrono::weekday weekday() const noexcept;
Returns: wd_­.
constexpr bool ok() const noexcept;
Returns: wd_­.ok().

25.8.8.3 Non-member functions [time.cal.wdlast.nonmembers]

constexpr bool operator==(const weekday_last& x, const weekday_last& y) noexcept;
Returns: x.weekday() == y.weekday().
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const weekday_last& wdl);
Returns: os << wdl.weekday() << "[last]".

25.8.9 Class month_­day [time.cal.md]

25.8.9.1 Overview [time.cal.md.overview]

namespace std::chrono {
  class month_day {
    chrono::month m_;           // exposition only
    chrono::day   d_;           // exposition only

  public:
    month_day() = default;
    constexpr month_day(const chrono::month& m, const chrono::day& d) noexcept;

    constexpr chrono::month month() const noexcept;
    constexpr chrono::day   day()   const noexcept;
    constexpr bool ok() const noexcept;
  };
}
month_­day represents a specific day of a specific month, but with an unspecified year.
month_­day is Cpp17EqualityComparable (Table 22) and Cpp17LessThanComparable (Table 23).
month_­day is a trivially copyable and standard-layout class type.

25.8.9.2 Member functions [time.cal.md.members]

constexpr month_day(const chrono::month& m, const chrono::day& d) noexcept;
Effects: Constructs an object of type month_­day by initializing m_­ with m, and d_­ with d.
constexpr chrono::month month() const noexcept;
Returns: m_­.
constexpr chrono::day day() const noexcept;
Returns: d_­.
constexpr bool ok() const noexcept;
Returns: true if m_­.ok() is true, 1d <= d_­, and d_­ is less than or equal to the number of days in month m_­; otherwise returns false.
When m_­ == February, the number of days is considered to be 29.

25.8.9.3 Non-member functions [time.cal.md.nonmembers]

constexpr bool operator==(const month_day& x, const month_day& y) noexcept;
Returns: x.month() == y.month() && x.day() == y.day().
constexpr bool operator<(const month_day& x, const month_day& y) noexcept;
Returns: If x.month() < y.month() returns true.
Otherwise, if x.month() > y.month() returns false.
Otherwise, returns x.day() < y.day().
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const month_day& md);
Returns: os << md.month() << '/' << md.day().
template<class charT, class traits> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const month_day& md);
Effects: Streams md into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
Returns: os.
template<class charT, class traits, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, month_day& md, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the month_­day md using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid month_­day, is.setstate(ios_­base::failbit) shall be called and md shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Returns: is.

25.8.10 Class month_­day_­last [time.cal.mdlast]

namespace std::chrono {
  class month_day_last {
    chrono::month m_;                   // exposition only

  public:
    explicit constexpr month_day_last(const chrono::month& m) noexcept;

    constexpr chrono::month month() const noexcept;
    constexpr bool ok() const noexcept;
  };
}
month_­day_­last represents the last day of a month.
[Note
:
A month_­day_­last object can be constructed using the expression m/last or last/m, where m is an expression of type month.
end note
]
[Example
:
constexpr auto mdl = February/last;     // mdl is the last day of February of an as yet unspecified year
static_assert(mdl.month() == February);
end example
]
month_­day_­last is a trivially copyable and standard-layout class type.
explicit constexpr month_day_last(const chrono::month& m) noexcept;
Effects: Constructs an object of type month_­day_­last by initializing m_­ with m.
constexpr month month() const noexcept;
Returns: m_­.
constexpr bool ok() const noexcept;
Returns: m_­.ok().
constexpr bool operator==(const month_day_last& x, const month_day_last& y) noexcept;
Returns: x.month() == y.month().
constexpr bool operator<(const month_day_last& x, const month_day_last& y) noexcept;
Returns: x.month() < y.month().
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const month_day_last& mdl);
Returns: os << mdl.month() << "/last".

25.8.11 Class month_­weekday [time.cal.mwd]

25.8.11.1 Overview [time.cal.mwd.overview]

namespace std::chrono {
  class month_weekday {
    chrono::month           m_;         // exposition only
    chrono::weekday_indexed wdi_;       // exposition only
  public:
    constexpr month_weekday(const chrono::month& m, const chrono::weekday_indexed& wdi) noexcept;

    constexpr chrono::month           month()           const noexcept;
    constexpr chrono::weekday_indexed weekday_indexed() const noexcept;
    constexpr bool ok() const noexcept;
  };
}
month_­weekday represents the weekday of a month, of an as yet unspecified year.
To do this the month_­weekday stores a month and a weekday_­indexed.
[Example
:
constexpr auto mwd
    = February/Tuesday[3];               // mwd is the third Tuesday of February of an as yet unspecified year
static_assert(mwd.month() == February);
static_assert(mwd.weekday_indexed() == Tuesday[3]);
end example
]
month_­weekday is a trivially copyable and standard-layout class type.

25.8.11.2 Member functions [time.cal.mwd.members]

constexpr month_weekday(const chrono::month& m, const chrono::weekday_indexed& wdi) noexcept;
Effects: Constructs an object of type month_­weekday by initializing m_­ with m, and wdi_­ with wdi.
constexpr chrono::month month() const noexcept;
Returns: m_­.
constexpr chrono::weekday_indexed weekday_indexed() const noexcept;
Returns: wdi_­.
constexpr bool ok() const noexcept;
Returns: m_­.ok() && wdi_­.ok().

25.8.11.3 Non-member functions [time.cal.mwd.nonmembers]

constexpr bool operator==(const month_weekday& x, const month_weekday& y) noexcept;
Returns: x.month() == y.month() && x.weekday_­indexed() == y.weekday_­indexed().
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const month_weekday& mwd);
Returns: os << mwd.month() << '/' << mwd.weekday_­indexed().

25.8.12 Class month_­weekday_­last [time.cal.mwdlast]

25.8.12.1 Overview [time.cal.mwdlast.overview]

namespace std::chrono {
  class month_weekday_last {
    chrono::month        m_;    // exposition only
    chrono::weekday_last wdl_;  // exposition only
  public:
    constexpr month_weekday_last(const chrono::month& m,
                                 const chrono::weekday_last& wdl) noexcept;

    constexpr chrono::month        month()        const noexcept;
    constexpr chrono::weekday_last weekday_last() const noexcept;
    constexpr bool ok() const noexcept;
  };
}
month_­weekday_­last represents the last weekday of a month, of an as yet unspecified year.
To do this the month_­weekday_­last stores a month and a weekday_­last.
[Example
:
constexpr auto mwd
    = February/Tuesday[last];    // mwd is the last Tuesday of February of an as yet unspecified year
static_assert(mwd.month() == February);
static_assert(mwd.weekday_last() == Tuesday[last]);
end example
]
month_­weekday_­last is a trivially copyable and standard-layout class type.

25.8.12.2 Member functions [time.cal.mwdlast.members]

constexpr month_weekday_last(const chrono::month& m, const chrono::weekday_last& wdl) noexcept;
Effects: Constructs an object of type month_­weekday_­last by initializing m_­ with m, and wdl_­ with wdl.
constexpr chrono::month month() const noexcept;
Returns: m_­.
constexpr chrono::weekday_last weekday_last() const noexcept;
Returns: wdl_­.
constexpr bool ok() const noexcept;
Returns: m_­.ok() && wdl_­.ok().

25.8.12.3 Non-member functions [time.cal.mwdlast.nonmembers]

constexpr bool operator==(const month_weekday_last& x, const month_weekday_last& y) noexcept;
Returns: x.month() == y.month() && x.weekday_­last() == y.weekday_­last().
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const month_weekday_last& mwdl);
Returns: os << mwdl.month() << '/' << mwdl.weekday_­last().

25.8.13 Class year_­month [time.cal.ym]

25.8.13.1 Overview [time.cal.ym.overview]

namespace std::chrono {
  class year_month {
    chrono::year  y_;           // exposition only
    chrono::month m_;           // exposition only

  public:
    year_month() = default;
    constexpr year_month(const chrono::year& y, const chrono::month& m) noexcept;

    constexpr chrono::year  year()  const noexcept;
    constexpr chrono::month month() const noexcept;

    constexpr year_month& operator+=(const months& dm) noexcept;
    constexpr year_month& operator-=(const months& dm) noexcept;
    constexpr year_month& operator+=(const years& dy)  noexcept;
    constexpr year_month& operator-=(const years& dy)  noexcept;

    constexpr bool ok() const noexcept;
  };
}
year_­month represents a specific month of a specific year, but with an unspecified day.
year_­month is a field-based time point with a resolution of months.
year_­month is Cpp17EqualityComparable (Table 22) and Cpp17LessThanComparable (Table 23).
year_­month is a trivially copyable and standard-layout class type.

25.8.13.2 Member functions [time.cal.ym.members]

constexpr year_month(const chrono::year& y, const chrono::month& m) noexcept;
Effects: Constructs an object of type year_­month by initializing y_­ with y, and m_­ with m.
constexpr chrono::year year() const noexcept;
Returns: y_­.
constexpr chrono::month month() const noexcept;
Returns: m_­.
constexpr year_month& operator+=(const months& dm) noexcept;
Effects: *this = *this + dm.
Returns: *this.
constexpr year_month& operator-=(const months& dm) noexcept;
Effects: *this = *this - dm.
Returns: *this.
constexpr year_month& operator+=(const years& dy) noexcept;
Effects: *this = *this + dy.
Returns: *this.
constexpr year_month& operator-=(const years& dy) noexcept;
Effects: *this = *this - dy.
Returns: *this.
constexpr bool ok() const noexcept;
Returns: y_­.ok() && m_­.ok().

25.8.13.3 Non-member functions [time.cal.ym.nonmembers]

constexpr bool operator==(const year_month& x, const year_month& y) noexcept;
Returns: x.year() == y.year() && x.month() == y.month().
constexpr bool operator<(const year_month& x, const year_month& y) noexcept;
Returns: If x.year() < y.year() returns true.
Otherwise, if x.year() > y.year() returns false.
Otherwise, returns x.month() < y.month().
constexpr year_month operator+(const year_month& ym, const months& dm) noexcept;
Returns: A year_­month value z such that z - ym == dm.
Complexity: with respect to the value of dm.
constexpr year_month operator+(const months& dm, const year_month& ym) noexcept;
Returns: ym + dm.
constexpr year_month operator-(const year_month& ym, const months& dm) noexcept;
Returns: ym + -dm.
constexpr months operator-(const year_month& x, const year_month& y) noexcept;
Returns:
x.year() - y.year() + months{static_cast<int>(unsigned{x.month()}) -
                             static_cast<int>(unsigned{y.month()})}
constexpr year_month operator+(const year_month& ym, const years& dy) noexcept;
Returns: (ym.year() + dy) / ym.month().
constexpr year_month operator+(const years& dy, const year_month& ym) noexcept;
Returns: ym + dy.
constexpr year_month operator-(const year_month& ym, const years& dy) noexcept;
Returns: ym + -dy.
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const year_month& ym);
Returns: os << ym.year() << '/' << ym.month().
template<class charT, class traits> basic_ostream<charT, traits>& to_stream(basic_ostream<charT, traits>& os, const charT* fmt, const year_month& ym);
Effects: Streams ym into os using the format specified by the NTCTS fmt.
fmt encoding follows the rules specified in [time.format].
Returns: os.
template<class charT, class traits, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, year_month& ym, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);
Effects: Attempts to parse the input stream is into the year_­month ym using the format flags given in the NTCTS fmt as specified in [time.parse].
If the parse fails to decode a valid year_­month, is.setstate(ios_­base::failbit) shall be called and ym shall not be modified.
If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.
If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.
Returns: is.

25.8.14 Class year_­month_­day [time.cal.ymd]

25.8.14.1 Overview [time.cal.ymd.overview]

namespace std::chrono {
  class year_month_day {
    chrono::year  y_;           // exposition only
    chrono::month m_;           // exposition only
    chrono::day   d_;           // exposition only

  public:
    year_month_day() = default;
    constexpr year_month_day(const chrono::year& y, const chrono::month& m,
                             const chrono::day& d) noexcept;
    constexpr year_month_day(const year_month_day_last& ymdl) noexcept;
    constexpr year_month_day(const sys_days& dp) noexcept;
    explicit constexpr year_month_day(const local_days& dp) noexcept;

    constexpr year_month_day& operator+=(const months& m) noexcept;
    constexpr year_month_day& operator-=(const months& m) noexcept;
    constexpr year_month_day& operator+=(const years& y)  noexcept;
    constexpr year_month_day& operator-=(const years& y)  noexcept;

    constexpr chrono::year  year()  const noexcept;
    constexpr chrono::month month() const noexcept;
    constexpr chrono::day   day()   const noexcept;

    constexpr          operator sys_days()   const noexcept;
    explicit constexpr operator local_days() const noexcept;
    constexpr bool ok() const noexcept;
  };
}
year_­month_­day represents a specific year, month, and day.
year_­month_­day is a field-based time point with a resolution of days.
[Note
:
year_­month_­day supports years- and months-oriented arithmetic, but not days-oriented arithmetic.
For the latter, there is a conversion to sys_­days, which efficiently supports days-oriented arithmetic.
end note
]
year_­month_­day is Cpp17EqualityComparable (Table 22) and Cpp17LessThanComparable (Table 23),
year_­month_­day is a trivially copyable and standard-layout class type.

25.8.14.2 Member functions [time.cal.ymd.members]

constexpr year_month_day(const chrono::year& y, const chrono::month& m, const chrono::day& d) noexcept;
Effects: Constructs an object of type year_­month_­day by initializing y_­ with y, m_­ with m, and d_­ with d.
constexpr year_month_day(const year_month_day_last& ymdl) noexcept;
Effects: Constructs an object of type year_­month_­day by initializing y_­ with ymdl.year(), m_­ with ymdl.month(), and d_­ with ymdl.day().
[Note
:
This conversion from year_­month_­day_­last to year_­month_­day may be more efficient than converting a year_­month_­day_­last to a sys_­days, and then converting that sys_­days to a year_­month_­day.
end note
]
constexpr year_month_day(const sys_days& dp) noexcept;
Effects: Constructs an object of type year_­month_­day that corresponds to the date represented by dp.
Remarks: For any value ymd of type year_­month_­day for which ymd.ok() is true, ymd == year_­month_­day{sys_­days{ymd}} is true.
explicit constexpr year_month_day(const local_days& dp) noexcept;
Effects: Constructs an object of type year_­month_­day that corresponds to the date represented by dp.
Remarks: Equivalent to constructing with sys_­days{dp.time_­since_­epoch()}.
constexpr year_month_day& operator+=(const months& m) noexcept;
Effects: *this = *this + m.
Returns: *this.
constexpr year_month_day& operator-=(const months& m) noexcept;
Effects: *this = *this - m.
Returns: *this.
constexpr year_month_day& year_month_day::operator+=(const years& y) noexcept;
Effects: *this = *this + y.
Returns: *this.
constexpr year_month_day& year_month_day::operator-=(const years& y) noexcept;
Effects: *this = *this - y.
Returns: *this.
constexpr chrono::year year() const noexcept;
Returns: y_­.
constexpr chrono::month month() const noexcept;
Returns: m_­.
constexpr chrono::day day() const noexcept;
Returns: d_­.
constexpr operator sys_days() const noexcept;
Returns: If ok(), returns a sys_­days holding a count of days from the sys_­days epoch to *this (a negative value if *this represents a date prior to the sys_­days epoch).
Otherwise, if y_­.ok() && m_­.ok() is true, returns a sys_­days which is offset from sys_­days{y_­/m_­/last} by the number of days d_­ is offset from sys_­days{y_­/m_­/last}.day().
Otherwise the value returned is unspecified.
Remarks: A sys_­days in the range [days{-12687428}, days{11248737}] which is converted to a year_­month_­day shall have the same value when converted back to a sys_­days.