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

275 lines
11 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.cast]
# 30 Time library [[time]](./#time)
## 30.7 Clocks [[time.clock]](time.clock#cast)
### 30.7.10 time_point conversions [time.clock.cast]
#### [30.7.10.1](#time.clock.conv) Class template clock_time_conversion [[time.clock.conv]](time.clock.conv)
namespace std::chrono {template<class DestClock, class SourceClock>struct clock_time_conversion {};}
[1](#time.clock.conv-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3583)
clock_time_conversion serves as a trait
which can be used to specify how to convert
a source time_point of typetime_point<SourceClock, Duration> to a destination time_point of typetime_point<DestClock, Duration> via a specialization:clock_time_conversion<DestClock, SourceClock>[.](#time.clock.conv-1.sentence-1)
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[.](#time.clock.conv-1.sentence-2)
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[.](#time.clock.conv-1.sentence-3)
A program may specialize clock_time_conversion if at least one of the template parameters is a user-defined clock type[.](#time.clock.conv-1.sentence-4)
[2](#time.clock.conv-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3603)
Several specializations are provided by the implementation,
as described in[[time.clock.cast.id]](#id "30.7.10.2Identity conversions"),[[time.clock.cast.sys.utc]](#sys.utc "30.7.10.3Conversions between system_­clock and utc_­clock"),[[time.clock.cast.sys]](#sys "30.7.10.4Conversions between system_­clock and other clocks"), and[[time.clock.cast.utc]](#utc "30.7.10.5Conversions between utc_­clock and other clocks")[.](#time.clock.conv-2.sentence-1)
#### [30.7.10.2](#id) Identity conversions [[time.clock.cast.id]](time.clock.cast.id)
template<class Clock>struct clock_time_conversion<Clock, Clock> {template<class Duration> time_point<Clock, Duration>operator()(const time_point<Clock, Duration>& t) const;};
[🔗](#lib:operator(),clock_time_conversion)
`template<class Duration>
time_point<Clock, Duration>
operator()(const time_point<Clock, Duration>& t) const;
`
[1](#id-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3630)
*Returns*: t[.](#id-1.sentence-1)
template<>struct clock_time_conversion<system_clock, system_clock> {template<class Duration> sys_time<Duration>operator()(const sys_time<Duration>& t) const;};
[🔗](#lib:operator(),clock_time_conversion_)
`template<class Duration>
sys_time<Duration>
operator()(const sys_time<Duration>& t) const;
`
[2](#id-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3652)
*Returns*: t[.](#id-2.sentence-1)
template<>struct clock_time_conversion<utc_clock, utc_clock> {template<class Duration> utc_time<Duration>operator()(const utc_time<Duration>& t) const;};
[🔗](#lib:operator(),clock_time_conversion__)
`template<class Duration>
utc_time<Duration>
operator()(const utc_time<Duration>& t) const;
`
[3](#id-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3674)
*Returns*: t[.](#id-3.sentence-1)
#### [30.7.10.3](#sys.utc) Conversions between system_clock and utc_clock [[time.clock.cast.sys.utc]](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;};
[🔗](#lib:operator(),clock_time_conversion___)
`template<class Duration>
utc_time<common_type_t<Duration, seconds>>
operator()(const sys_time<Duration>& t) const;
`
[1](#sys.utc-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3698)
*Returns*: utc_clock::from_sys(t)[.](#sys.utc-1.sentence-1)
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;};
[🔗](#lib:operator(),clock_time_conversion____)
`template<class Duration>
sys_time<common_type_t<Duration, seconds>>
operator()(const utc_time<Duration>& t) const;
`
[2](#sys.utc-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3720)
*Returns*: utc_clock::to_sys(t)[.](#sys.utc-2.sentence-1)
#### [30.7.10.4](#sys) Conversions between system_clock and other clocks [[time.clock.cast.sys]](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));};
[🔗](#lib:operator(),clock_time_conversion_____)
`template<class Duration>
auto operator()(const time_point<SourceClock, Duration>& t) const
-> decltype(SourceClock::to_sys(t));
`
[1](#sys-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3744)
*Constraints*: SourceClock::to_sys(t) is well-formed[.](#sys-1.sentence-1)
[2](#sys-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3748)
*Mandates*: SourceClock::to_sys(t) returns a sys_time<Duration2> for some type Duration2 ([[time.point.general]](time.point.general "30.6.1General"))[.](#sys-2.sentence-1)
[3](#sys-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3753)
*Returns*: SourceClock::to_sys(t)[.](#sys-3.sentence-1)
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));};
[🔗](#lib:operator(),clock_time_conversion______)
`template<class Duration>
auto operator()(const sys_time<Duration>& t) const
-> decltype(DestClock::from_sys(t));
`
[4](#sys-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3775)
*Constraints*: DestClock::from_sys(t) is well-formed[.](#sys-4.sentence-1)
[5](#sys-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3779)
*Mandates*: DestClock::from_sys(t) returns a time_point<DestClock, Duration2> for some type Duration2 ([[time.point.general]](time.point.general "30.6.1General"))[.](#sys-5.sentence-1)
[6](#sys-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3784)
*Returns*: DestClock::from_sys(t)[.](#sys-6.sentence-1)
#### [30.7.10.5](#utc) Conversions between utc_clock and other clocks [[time.clock.cast.utc]](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));};
[🔗](#lib:operator(),clock_time_conversion_______)
`template<class Duration>
auto operator()(const time_point<SourceClock, Duration>& t) const
-> decltype(SourceClock::to_utc(t));
`
[1](#utc-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3808)
*Constraints*: SourceClock::to_utc(t) is well-formed[.](#utc-1.sentence-1)
[2](#utc-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3812)
*Mandates*: SourceClock::to_utc(t) returns a utc_time<Duration2> for some type Duration2 ([[time.point.general]](time.point.general "30.6.1General"))[.](#utc-2.sentence-1)
[3](#utc-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3817)
*Returns*: SourceClock::to_utc(t)[.](#utc-3.sentence-1)
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));};
[🔗](#lib:operator(),clock_time_conversion________)
`template<class Duration>
auto operator()(const utc_time<Duration>& t) const
-> decltype(DestClock::from_utc(t));
`
[4](#utc-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3839)
*Constraints*: DestClock::from_utc(t) is well-formed[.](#utc-4.sentence-1)
[5](#utc-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3843)
*Mandates*: DestClock::from_utc(t) returns a time_point<DestClock, Duration2> for some type Duration2 ([[time.point.general]](time.point.general "30.6.1General"))[.](#utc-5.sentence-1)
[6](#utc-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3848)
*Returns*: DestClock::from_utc(t)[.](#utc-6.sentence-1)
#### [30.7.10.6](#fn) Function template clock_cast [[time.clock.cast.fn]](time.clock.cast.fn)
[🔗](#lib:clock_cast)
`template<class DestClock, class SourceClock, class Duration>
auto clock_cast(const time_point<SourceClock, Duration>& t);
`
[1](#fn-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3862)
*Constraints*: At least one of the following clock time conversion expressions
is well-formed:
- [(1.1)](#fn-1.1)
clock_time_conversion<DestClock, SourceClock>{}(t)
- [(1.2)](#fn-1.2)
clock_time_conversion<DestClock, system_clock>{}( clock_time_conversion<system_clock, SourceClock>{}(t))
- [(1.3)](#fn-1.3)
clock_time_conversion<DestClock, utc_clock>{}( clock_time_conversion<utc_clock, SourceClock>{}(t))
- [(1.4)](#fn-1.4)
clock_time_conversion<DestClock, utc_clock>{}( clock_time_conversion<utc_clock, system_clock>{}( clock_time_conversion<system_clock, SourceClock>{}(t)))
- [(1.5)](#fn-1.5)
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 feweroperator() calls on clock_time_conversion specializations[.](#fn-1.sentence-2)
[2](#fn-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3904)
*Mandates*: Among the well-formed clock time conversion expressions
from the above list, there is a unique best expression[.](#fn-2.sentence-1)
[3](#fn-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L3909)
*Returns*: The best well-formed clock time conversion expression in the above list[.](#fn-3.sentence-1)