Duration

表示一个实践段,由时刻(tick)和时刻数组成

	std::chrono::duration<int,std::ratio<3600>> hours(1);
	std::chrono::duration<int,std::ratio<60>> minutes(14);
	std::cout<<std::chrono::duration_cast<std::chrono::seconds>(hours+minutes).count()<<'\n';
4440
//1h14min=4440s

提供了一些通俗易懂的成员函数

operator=
 
assigns the contents
(public member function)
count
 
returns the count of ticks
(public member function)
zero
  
[static]
 
returns the special duration value zero
(public static member function)
min
  
[static]
 
returns the special duration value min
(public static member function)
max
  
[static]
 
returns the special duration value max
(public static member function)
operator+
operator-
 
implements unary + and unary -
(public member function)
operator++
operator++(int)
operator--
operator--(int)
 
increments or decrements the tick count
(public member function)
operator+=
operator-=
operator*=
operator/=
operator%=
 
implements compound assignment between two durations
(public member function)

提供了比较


operator+
operator-
operator*
operator/
operator%
  
(C++11)
 
implements arithmetic operations with durations as arguments
(function template)
operator==
operator!=
operator<
operator<=
operator>
operator>=
operator<=>
  
(C++11)
(C++11)(removed in C++20)
(C++11)
(C++11)
(C++11)
(C++11)
(C++20)
 
compares two durations
(function template)

提供了简单的数学运算

duration_cast
  
(C++11)
 
converts a duration to another, with a different tick interval
(function template)
floor(std::chrono::duration)
  
(C++17)
 
converts a duration to another, rounding down
(function template)
ceil(std::chrono::duration)
  
(C++17)
 
converts a duration to another, rounding up
(function template)
round(std::chrono::duration)
  
(C++17)
 
converts a duration to another, rounding to nearest, ties to even
(function template)
abs(std::chrono::duration)
  
(C++17)
 
obtains the absolute value of the duration
(function template)

提供了流转换

operator<<
  
(C++20)
 
performs stream output on a duration
(function template)
from_stream
  
(C++20)
 
parses a duration from a stream according to the provided format
(function template)

这些目前编译器支持性还不是很好

Clocks

clocks由一个时间点(epoch)和一个tick组成

    auto now=std::chrono::system_clock::now();
    std::time_t t_c=std::chrono::system_clock::to_time_t(now);
    std::cout<<std::ctime(&t_c)<<'\n';
    std::this_thread::sleep_for(std::chrono::seconds(2l));

    now=std::chrono::system_clock::now();
    t_c=std::chrono::system_clock::to_time_t(now);
    std::cout<<std::ctime(&t_c)<<'\n';
Thu Dec 05 13:39:39 2024

Thu Dec 05 13:39:41 2024

主要提供了以下:

system_clock

(C++11)

wall clock time from the system-wide realtime clock
(class)

steady_clock

(C++11)

monotonic clock that will never be adjusted
(class)

high_resolution_clock

(C++11)

the clock with the shortest tick period available
(class)

is_clockis_clock_v

(C++20)

determines if a type is a Clock
(class template) (variable template)

utc_clock

(C++20)

Clock for Coordinated Universal Time (UTC)
(class)

tai_clock

(C++20)

Clock for International Atomic Time (TAI)
(class)

gps_clock

(C++20)

Clock for GPS time
(class)

file_clock

(C++20)

Clock used for file time
(typedef)

local_t

(C++20)

pseudo-clock representing local time
(class)

Time Point

代表一个时间点,是通过记录一个距离时钟开始时间的Duration实现的

auto base=std::chrono::steady_clock::now();
    std::this_thread::sleep_for(std::chrono::milliseconds(1145l));
    auto now=std::chrono::steady_clock::now();
    std::chrono::duration<double> diff=now-base;
    std::cout<<diff.count()<<'\n';
    std::cout<<base.time_since_epoch().count()<<'\n';
    std::cout<<now.time_since_epoch().count()<<'\n';
    std::cout<<now.time_since_epoch().count()-base.time_since_epoch().count()<<'\n';
1.1506
909922889200
911073490400
1150601200

提供了time_since_epoch()方法返回记录的Duration

提供了合乎常理的运算符重载和数学运算

operator+=operator-=

modifies the time point by the given duration
(public member function)

operator++operator++(int)operator--operator--(int)

(C++20)

increments or decrements the duration
(public member function)

min

[static]

returns the time point corresponding to the smallest duration
(public static member function)

max

[static]

returns the time point corresponding to the largest duration
(public static member function)

operator+operator-

(C++11)

performs add and subtract operations involving a time point
(function template)

operator==operator!=operator<operator<=operator>operator>=operator<=>

(C++11)(C++11)(removed in C++20)(C++11)(C++11)(C++11)(C++11)(C++20)

compares two time points
(function template)

time_point_cast

(C++11)

converts a time point to another time point on the same clock, with a different duration
(function template)

floor(std::chrono::time_point)

(C++17)

converts a time_point to another, rounding down
(function template)

ceil(std::chrono::time_point)

(C++17)

converts a time_point to another, rounding up
(function template)

round(std::chrono::time_point)

(C++17)

converts a time_point to another, rounding to nearest, ties to even
(function template)

关于TimePoint STL20还提供了时间点转换和clock转换

clock_time_conversion

(C++20)

traits class defining how to convert time points of one clock to another
(class template)

clock_cast

(C++20)

convert time points of one clock to another
(function template)