Files
2025-10-25 03:02:53 +03:00

221 lines
7.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[time.clock.utc]
# 30 Time library [[time]](./#time)
## 30.7 Clocks [[time.clock]](time.clock#utc)
### 30.7.3 Class utc_clock [time.clock.utc]
#### [30.7.3.1](#overview) Overview [[time.clock.utc.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); };}
[1](#overview-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2834)
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.
[*Note [1](#overview-note-1)*:
The UTC time standard began on 1972-01-01 00:00:10 TAI.
To measure time since this epoch instead, one can add/subtract the constantsys_days{1972y/1/1} - sys_days{1970y/1/1} (63'072'000s)
from the utc_time[.](#overview-1.sentence-1)
— *end note*]
[*Example [1](#overview-example-1)*:
clock_cast<utc_clock>(sys_seconds{sys_days{1970y/January/1}}).time_since_epoch() is 0s[.](#overview-1.sentence-2)
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[.](#overview-1.sentence-4)
— *end example*]
[2](#overview-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2852)
utc_clock is not a [*Cpp17TrivialClock*](time.clock.req#:Cpp17TrivialClock "30.3Cpp17Clock requirements[time.clock.req]") unless the implementation can guarantee that utc_clock::now() does not propagate an exception[.](#overview-2.sentence-1)
[*Note [2](#overview-note-2)*:
noexcept(from_sys(system_clock::now())) is false[.](#overview-2.sentence-2)
— *end note*]
#### [30.7.3.2](#members) Member functions [[time.clock.utc.members]](time.clock.utc.members)
[🔗](#lib:now,utc_clock)
`static time_point now();
`
[1](#members-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2868)
*Returns*: from_sys(system_clock::now()), or a more accurate value of utc_time[.](#members-1.sentence-1)
[🔗](#lib:to_sys,utc_clock)
`template<class Duration>
static sys_time<common_type_t<Duration, seconds>>
to_sys(const utc_time<Duration>& u);
`
[2](#members-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2881)
*Returns*: A sys_time t,
such that from_sys(t) == u if such a mapping exists[.](#members-2.sentence-1)
Otherwise u represents a time_point during a positive leap second insertion,
the conversion counts that leap second as not inserted,
and the last representable value of sys_time prior to the insertion of the leap second is returned[.](#members-2.sentence-2)
[🔗](#lib:from_sys,utc_clock)
`template<class Duration>
static utc_time<common_type_t<Duration, seconds>>
from_sys(const sys_time<Duration>& t);
`
[3](#members-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2900)
*Returns*: A utc_time u, such thatu.time_since_epoch() - t.time_since_epoch() is equal to the sum of leap seconds that were inserted
between t and 1970-01-01[.](#members-3.sentence-1)
If t is exactly the date of leap second insertion,
then the conversion counts that leap second as inserted[.](#members-3.sentence-2)
[*Example [1](#members-example-1)*: 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*]
#### [30.7.3.3](#nonmembers) Non-member functions [[time.clock.utc.nonmembers]](time.clock.utc.nonmembers)
[🔗](#lib:operator%3c%3c,utc_time)
`template<class charT, class traits, class Duration>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os, const utc_time<Duration>& t);
`
[1](#nonmembers-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2937)
*Effects*: Equivalent to:return os << format(os.getloc(), *STATICALLY-WIDEN*<charT>("{:L%F %T}"), t);
[2](#nonmembers-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2944)
[*Example [1](#nonmembers-example-1)*: 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*]
[🔗](#lib:from_stream,utc_time)
`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);
`
[3](#nonmembers-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2977)
*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]](time.parse "30.13Parsing")[.](#nonmembers-3.sentence-1)
If the parse fails to decode a valid date,is.setstate(ios_base::failbit) is called andtp is not modified[.](#nonmembers-3.sentence-2)
If %Z is used and successfully parsed,
that value will be assigned to *abbrev if abbrev is non-null[.](#nonmembers-3.sentence-3)
If %z (or a modified variant) is used and successfully parsed,
that value will be assigned to *offset if offset is non-null[.](#nonmembers-3.sentence-4)
Additionally, the parsed offset will be subtracted from
the successfully parsed timestamp
prior to assigning that difference to tp[.](#nonmembers-3.sentence-5)
[4](#nonmembers-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2994)
*Returns*: is[.](#nonmembers-4.sentence-1)
[🔗](#lib:leap_second_info)
`struct leap_second_info {
bool is_leap_second;
seconds elapsed;
};
`
[5](#nonmembers-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3008)
The type leap_second_info has data members and special members specified above[.](#nonmembers-5.sentence-1)
It has no base classes or members other than those specified[.](#nonmembers-5.sentence-2)
[🔗](#lib:get_leap_second_info)
`template<class Duration>
leap_second_info get_leap_second_info(const utc_time<Duration>& ut);
`
[6](#nonmembers-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3021)
*Returns*: A leap_second_info lsi,
where lsi.is_leap_second is true if ut is during a positive leap second insertion, and
otherwise false[.](#nonmembers-6.sentence-1)
lsi.elapsed is the sum of leap seconds between 1970-01-01 and ut[.](#nonmembers-6.sentence-2)
If lsi.is_leap_second is true,
the leap second referred to by ut is included in the sum[.](#nonmembers-6.sentence-3)