Namespaces
Variants

std::chrono:: clock_time_conversion

From cppreference.net
定义于头文件 <chrono>
template < class Dest, class Source >
struct clock_time_conversion { } ;
(C++20 起)

std::chrono::clock_time_conversion 是一个特性类,用于指定如何将 std::chrono::time_point Source 时钟转换到 Dest 时钟。它通过提供一个常量可调用的 operator() 来实现,该操作符接受类型为 std:: chrono :: time_point < Source, Duration > 的参数,并返回一个表示相同时间点的 std:: chrono :: time_point < Dest, OtherDuration > 。返回时间点的时长是根据源时长计算得出的,具体计算方式因特化实现而异。 clock_time_conversion 通常仅通过 std::chrono::clock_cast 间接使用。

当至少一个模板参数为用户定义的时钟类型时,程序可以特化 clock_time_conversion

主模板是一个空结构体。标准定义了以下特化版本:

template < class Clock >
struct clock_time_conversion < Clock, Clock > ;
(1) (自 C++20 起)
template <>
struct clock_time_conversion < std:: chrono :: system_clock , std:: chrono :: system_clock > ;
(2) (自 C++20 起)
template <>
struct clock_time_conversion < std:: chrono :: utc_clock , std:: chrono :: utc_clock > ;
(3) (自 C++20 起)
template <>
struct clock_time_conversion < std:: chrono :: system_clock , std:: chrono :: utc_clock > ;
(4) (自 C++20 起)
template <>
struct clock_time_conversion < std:: chrono :: utc_clock , std:: chrono :: system_clock > ;
(5) (自 C++20 起)
template < class Clock >
struct clock_time_conversion < Clock, std:: chrono :: system_clock > ;
(6) (自 C++20 起)
template < class Clock >
struct clock_time_conversion < std:: chrono :: system_clock , Clock > ;
(7) (自 C++20 起)
template < class Clock >
struct clock_time_conversion < Clock, std:: chrono :: utc_clock > ;
(8) (自 C++20 起)
template < class Clock >
struct clock_time_conversion < std:: chrono :: utc_clock , Clock > ;
(9) (自 C++20 起)
1-3) 恒等转换: operator() 返回参数的副本。
4,5) std::chrono::sys_time std::chrono::utc_time 之间的转换: operator() 分别调用 std::chrono::utc_clock::to_sys std::chrono::utc_clock::from_sys
6,7) std::chrono::sys_time 的相互转换(当 Clock 支持 from_sys to_sys 时): operator() 分别调用 Clock :: to_sys Clock :: from_sys
8,9) std::chrono::utc_time 的相互转换(当 Clock 支持 from_utc to_utc 时): operator() 分别调用 Clock :: to_utc Clock :: from_utc

目录

成员函数

每个特化都有一个隐式声明的默认构造函数、复制构造函数、移动构造函数、复制赋值运算符、移动赋值运算符和析构函数。

std::chrono::clock_time_conversion:: operator()

template < class Duration >

std:: chrono :: time_point < Clock, Duration >

operator ( ) ( const std:: chrono :: time_point < Clock, Duration > & t ) const ;
(1) (特化的成员函数 (1) )
template < class Duration >

std:: chrono :: sys_time < Duration >

operator ( ) ( const std:: chrono :: sys_time < Duration > & t ) const ;
(2) (特化成员 (2)
template < class Duration >

std:: chrono :: utc_time < Duration >

operator ( ) ( const std:: chrono :: utc_time < Duration > & t ) const ;
(3) (特化成员 (3)
template < class Duration >

std:: chrono :: sys_time < Duration >

operator ( ) ( const std:: chrono :: utc_time < Duration > & t ) const ;
(4) (特化成员 (4)
template < class Duration >

std:: chrono :: utc_time < Duration >

operator ( ) ( const std:: chrono :: sys_time < Duration > & t ) const ;
(5) (特化成员 (5)
template < class Duration >

auto operator ( ) ( const std:: chrono :: sys_time < Duration > & t ) const

- > decltype ( Clock :: from_sys ( t ) ) ;
(6) (特化成员 (6)
template < class Duration >

auto operator ( ) ( const std:: chrono :: time_point < SourceClock, Duration > & t ) const

- > decltype ( Clock :: to_sys ( t ) ) ;
(7) (特化成员 (7)
template < class Duration >

auto operator ( ) ( const std:: chrono :: utc_time < Duration > & t ) const

- > decltype ( Clock :: from_utc ( t ) ) ;
(8) (特化成员 (8)
template < class Duration >

auto operator ( ) ( const std:: chrono :: time_point < Clock, Duration > & t ) const

- > decltype ( Clock :: to_utc ( t ) ) ;
(9) (特化成员 (9) )

将参数 std::chrono::time_point 转换为目标时钟。

1-3) 恒等转换。返回未更改的 t
6) 返回 Clock :: from_sys ( t ) 。此重载仅当表达式 Clock :: from_sys ( t ) 格式正确时才参与重载决议。若 Clock :: from_sys ( t ) 未返回 std:: chrono :: time_point < Clock, Duration > (其中 Duration std::chrono::duration 的某个有效特化),则程序非良构。
7) 返回 Clock :: to_sys ( t ) 。此重载仅当表达式 Clock :: to_sys ( t ) 格式正确时才参与重载决议。若 Clock :: to_sys ( t ) 未返回 std:: chrono :: sys_time < Duration > (其中 Duration std::chrono::duration 的某个有效特化),则程序非良构。
8) 返回 Clock :: from_utc ( t ) 。此重载仅当表达式 Clock :: from_utc ( t ) 格式正确时才参与重载决议。若 Clock :: from_utc ( t ) 未返回 std:: chrono :: time_point < Clock, Duration > (其中 Duration std::chrono::duration 的某个有效特化),则程序非良构。
9) 返回 Clock :: to_utc ( t ) 。此重载仅当表达式 Clock :: to_utc ( t ) 格式正确时才参与重载决议。若 Clock :: to_utc ( t ) 未返回 std:: chrono :: utc_time < Duration > (其中 Duration std::chrono::duration 的某个有效特化),则程序非良构。

参数

t - 要转换的时间点

返回值

如上所述的转换结果:

1-3) t .
6) Clock :: from_sys ( t ) .
7) Clock :: to_sys ( t ) .
8) Clock :: from_utc ( t ) .
9) Clock :: to_utc ( t ) .

另请参阅

(C++20)
将一个时钟的时间点转换为另一个时钟的时间点
(函数模板)