25 Time library [time]

25.7 Clocks [time.clock]

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.