☰
【STL】头文件:<chrono>
2026/10/28 1:59:03 网站建设 项目流程

包含标准头文件 <chrono>,用于定义类和函数,用来表示、处理时间时长与时间点。


目录

  • 1 说明
  • 2 日历类型
    • 2.1 时钟(Clocks)
    • 2.2 时区(Time zones)
    • 2.3 异常(Exceptions)
  • 3 函数
    • 3.1 时区相关函数(Time zone related)
  • 4 运算符(Operators)
  • 5 类型别名(Typedefs)
    • 5.1 便捷时长类型
    • 5.2 便捷时间点类型
  • 6 类型特征(Type traits)
  • 7 字面量(Literals, C++11 起)

1 说明

自Visual Studio 2015起,steady_clock 的实现发生变更,以满足 C++ 标准对时钟稳定性、单调性的要求:

  • steady_clock 现在基于 QueryPerformanceCounter() 实现;
  • high_resolution_clock 现在是 steady_clock 的别名,在微软 C++ 实现中,steady_clock::time_point 是 chrono::time_point<steady_clock> 的别名,但其他标准库实现不一定如此。

要求:

  • 头文件:<chrono>
  • 命名空间:std

地方立法机构会不时修改时区规则,国际标准组织会规定何时计入新闰秒。Windows 10新增了存储这些变更的数据库,具体版本要求:

  • 在旧版 Windows 上使用时区相关功能会触发运行时错误。
功能客户端版本服务器版本
闰秒数据库更新Windows 10 1809 及以上Windows Server 2019 及以上
时区支持Windows 10 1903/19H1 及以上Windows Server 2022 及以上

2 日历类型

名称类型说明
common_typestruct针对 duration 和 time_point 实例化的 common_type 类模版特化
dayclass月份中的某日,例如当月 25 号
durationclass一段时间间隔
duration_valuesstruct为 duration 的模板参数 Rep 提供特定数值
hh_mm_ssclass将一个 duration 拆分为 时:分:秒
last_spec用于标识某月的最后一项,例如:2020年2月最后一个周二、2019年4月最后一天
leap_secondclass一条闰秒记录:日期 + 插入的闰秒值
leap_second_infostructstd::chrono::get_leap_second_info 的返回数据结构
monthclass一年中的月份,例如七月
month_dayclass某月的特定日期,例如 7 月 30 日
month_day_lastclass某个月的最后一天
month_weekdayclass某月的第 N 个星期几
month_weekday_lastclass某月的最后一个星期几
time_pointclass一个时间点
weekdayclass星期几
weekday_lastclass某月的最后一个星期几
weekday_indexedclass星期几+序号,代表当月第 N 个该星期几
yearclass公历(前瞻格里高利历)年份
year_monthclass年+月,不指定日期
year_month_dayclass年、月、日
year_month_day_lastclass指定年月的最后一天
year_month_weekdayclass指定年月、当月第 N 个星期几
year_month_weekday_lastclass指定年月、当月最后一个星期几

2.1 时钟(Clocks)

名称类型说明
file_clockclassstd::filesystem::file_time_type 使用的时钟别名,用于表达文件时间戳
gps_clockclassGPS 时钟,计时起点:UTC 1980 年 1 月第一个周日 00:00:00
high_resolution_clockstruct纳秒级刻度的高精度时钟(MSVC 下等价 steady_clock)
local_tstruct伪时钟,作为 time_point 模版参数,标记该时间点为本地时间
steady_clockstruct稳定单调时钟,推荐用来测量时间间隔
system_clockstruct基于系统实时时钟的时钟,对应系统墙上时间
tai_clockclass国际原子时(TAI),起点:1958-01-01 00:00:00,不计闰秒
utc_clockclassUTC 时间,起点:UTC 1970-01-01 00:00:00,计入闰秒,全球通用时间标准

2.2 时区(Time zones)

名称类型说明
choose枚举,指定本地时间转系统时间时,存在歧义时间该如何解析
local_info底层接口,保存本地时间转系统时间的时区转换结果信息
sys_info底层接口,保存系统时间转本地时间的时区转换结果信息
time_zoneclass单个地理区域对应的全部时区变更规则
time_zone_linkclass时区的别名
tzdbstruct时区数据库副本
tzdb_listclass时区数据库列表(单例)
zoned_timeclass时区 + 带精度的 time_point 组合
zoned_traitsstruct用来给 zoned_time 绑定自定义默认时区,也可映射自定义时区名称

2.3 异常(Exceptions)

名称说明
ambiguous_local_time本地时间转系统时间时,时间存在歧义(夏令时回拨)抛出
nonexistent_local_time本地时间转系统时间时,该时间不存在(夏令时向前跳)抛出

3 函数

名称说明
ceil(duration)将 duration 向上取整,转为指定类型
ceil(time_point)将 time_point 向上取整,转为指定类型
clock_cast把一个时钟的 time_point 转换为另一个时钟等价时间点
duration_cast强制转换 duration 到指定类型
floor(duration)将 duration 向下取整
floor(time_point)将 time_point 向下取整
from_stream()按指定格式,从输入流解析 day/month/weekday/year 等 chrono 时间类型
get_leap_second_info获取闰秒信息结构体
is_am判断小时是否为上午
is_pm判断小时是否为下午
locate_zone根据名称查找指定时区
make12转为 12 小时制小时
make24转为 24 小时制小时
round(duration)四舍五入 duration 到指定类型
round(time_point)四舍五入 time_point 到指定类型
time_point_cast强制转换 time_point 到指定类型

3.1 时区相关函数(Time zone related)

名称说明
current_zone获取当前系统时区
get_tzdb获取第一个时区数据库条目
get_tzdb_list获取时区数据库列表
reload_tzdb重新加载更新后的时区数据库
remote_version检查是否有新版本时区数据库

4 运算符(Operators)

名称说明
operator-对 chrono 对象做减法、取负
operator!=不等比较
operator moduloduration 取模
operator*duration 乘法
operator/duration 除法;也支持日历日期构造语法
operator+chrono 对象加法
operator<小于比较
operator<=小于等于比较
operator==相等比较
operator>大于比较
operator>=大于等于比较

5 类型别名(Typedefs)

注⚠️:下文用到的 ratio 类型参考 <ratio>。

5.1 便捷时长类型

名称说明
typedef duration<long long, nano> nanoseconds;刻度:1 纳秒(1/109秒)
typedef duration<long long, micro> microseconds;刻度:1 微秒(1/106秒)
typedef duration<long long, milli> milliseconds;刻度:1 毫秒(1/103秒)
typedef duration<long long> seconds;刻度:1 秒
typedef duration<int, ratio<60>> minutes;刻度:1 分钟
typedef duration<int, ratio<3600>> hours;刻度:1 小时

5.2 便捷时间点类型

名称说明
file_timetime_point<file_clock, Duration>,文件时间戳
gps_secondsgps_time<seconds>
gps_timetime_point<gps_clock, Duration>,GPS 时间点
local_dayslocal_time<days>,本地日期计数(无时区绑定)
local_secondslocal_time<seconds>
local_timetime_point<local_t, Duration>,未绑定时区的本地时间;不等于电脑当前本地时间,只有和时区绑定后,才能转为 UTC / 其他时区时间
sys_dayssys_time<days>,自 system_clock 纪元起的天数
sys_secondssys_time<seconds>
sys_timetime_point<system_clock, Duration>,Unix 时间,近似 UTC,system_clock::now() 返回类型
tai_secondstai_time<seconds>
tai_timetime_point<tai_clock, Duration>,原子时时间点
utc_secondsutc_time<seconds>
utc_timetime_point<utc_clock, Duration>,UTC 时间点

6 类型特征(Type traits)

名称说明
clock_time_conversion特征类,定义不同时钟之间 time_point 的转换规则
is_clock判断类型是否为时钟
treat_as_floating_point判断一个 duration 是否允许转换为另一个不同刻度的 duration

7 字面量(Literals, C++11 起)

<chrono> 定义了用户自定义字面量,提升代码类型安全与可读性;定义在内联命名空间literals::chrono_literals,std::chrono 可见时自动生效。

声明说明
hours operator ""h(unsigned long long Val)小时字面量(整数)
duration<double, ratio<3600>> operator ""h(long double Val)小时字面量(浮点数)
minutes (operator “” min)(unsigned long long Val)分钟字面量(整数)
duration<double, ratio<60>> (operator “” min)(long double Val)分钟字面量(浮点数)
seconds operator “” s(unsigned long long Val)秒字面量(整数)
duration<double> operator “” s(long double Val)秒字面量(浮点数)
milliseconds operator “” ms(unsigned long long Val)毫秒字面量(整数)
duration<double, milli> operator “” ms(long double Val)毫秒字面量(浮点数)
microseconds operator “” us(unsigned long long Val)微秒字面量(整数)
duration<double, micro> operator “” us(long double Val)微秒字面量(浮点数)
nanoseconds operator “” ns(unsigned long long Val)纳秒字面量(整数)
duration<double, nano> operator “” ns(long double Val)纳秒字面量(浮点数)

示例:

constexprautoday=24h;constexprautoweek=24h*7;constexprautomy_duration_unit=108ms;

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询