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)
- 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> 的别名,但其他标准库实现不一定如此。
要求:
地方立法机构会不时修改时区规则,国际标准组织会规定何时计入新闰秒。Windows 10新增了存储这些变更的数据库,具体版本要求:
- 在旧版 Windows 上使用时区相关功能会触发运行时错误。
| 功能 | 客户端版本 | 服务器版本 |
|---|
| 闰秒数据库更新 | Windows 10 1809 及以上 | Windows Server 2019 及以上 |
| 时区支持 | Windows 10 1903/19H1 及以上 | Windows Server 2022 及以上 |
2 日历类型
| 名称 | 类型 | 说明 |
|---|
| common_type | struct | 针对 duration 和 time_point 实例化的 common_type 类模版特化 |
| day | class | 月份中的某日,例如当月 25 号 |
| duration | class | 一段时间间隔 |
| duration_values | struct | 为 duration 的模板参数 Rep 提供特定数值 |
| hh_mm_ss | class | 将一个 duration 拆分为 时:分:秒 |
| last_spec | 用于标识某月的最后一项,例如:2020年2月最后一个周二、2019年4月最后一天 |
| leap_second | class | 一条闰秒记录:日期 + 插入的闰秒值 |
| leap_second_info | struct | std::chrono::get_leap_second_info 的返回数据结构 |
| month | class | 一年中的月份,例如七月 |
| month_day | class | 某月的特定日期,例如 7 月 30 日 |
| month_day_last | class | 某个月的最后一天 |
| month_weekday | class | 某月的第 N 个星期几 |
| month_weekday_last | class | 某月的最后一个星期几 |
| time_point | class | 一个时间点 |
| weekday | class | 星期几 |
| weekday_last | class | 某月的最后一个星期几 |
| weekday_indexed | class | 星期几+序号,代表当月第 N 个该星期几 |
| year | class | 公历(前瞻格里高利历)年份 |
| year_month | class | 年+月,不指定日期 |
| year_month_day | class | 年、月、日 |
| year_month_day_last | class | 指定年月的最后一天 |
| year_month_weekday | class | 指定年月、当月第 N 个星期几 |
| year_month_weekday_last | class | 指定年月、当月最后一个星期几 |
2.1 时钟(Clocks)
| 名称 | 类型 | 说明 |
|---|
| file_clock | class | std::filesystem::file_time_type 使用的时钟别名,用于表达文件时间戳 |
| gps_clock | class | GPS 时钟,计时起点:UTC 1980 年 1 月第一个周日 00:00:00 |
| high_resolution_clock | struct | 纳秒级刻度的高精度时钟(MSVC 下等价 steady_clock) |
| local_t | struct | 伪时钟,作为 time_point 模版参数,标记该时间点为本地时间 |
| steady_clock | struct | 稳定单调时钟,推荐用来测量时间间隔 |
| system_clock | struct | 基于系统实时时钟的时钟,对应系统墙上时间 |
| tai_clock | class | 国际原子时(TAI),起点:1958-01-01 00:00:00,不计闰秒 |
| utc_clock | class | UTC 时间,起点:UTC 1970-01-01 00:00:00,计入闰秒,全球通用时间标准 |
2.2 时区(Time zones)
| 名称 | 类型 | 说明 |
|---|
| choose | 枚举,指定本地时间转系统时间时,存在歧义时间该如何解析 |
| local_info | 底层接口,保存本地时间转系统时间的时区转换结果信息 |
| sys_info | 底层接口,保存系统时间转本地时间的时区转换结果信息 |
| time_zone | class | 单个地理区域对应的全部时区变更规则 |
| time_zone_link | class | 时区的别名 |
| tzdb | struct | 时区数据库副本 |
| tzdb_list | class | 时区数据库列表(单例) |
| zoned_time | class | 时区 + 带精度的 time_point 组合 |
| zoned_traits | struct | 用来给 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 modulo | duration 取模 |
| 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_time | time_point<file_clock, Duration>,文件时间戳 |
| gps_seconds | gps_time<seconds> |
| gps_time | time_point<gps_clock, Duration>,GPS 时间点 |
| local_days | local_time<days>,本地日期计数(无时区绑定) |
| local_seconds | local_time<seconds> |
| local_time | time_point<local_t, Duration>,未绑定时区的本地时间;不等于电脑当前本地时间,只有和时区绑定后,才能转为 UTC / 其他时区时间 |
| sys_days | sys_time<days>,自 system_clock 纪元起的天数 |
| sys_seconds | sys_time<seconds> |
| sys_time | time_point<system_clock, Duration>,Unix 时间,近似 UTC,system_clock::now() 返回类型 |
| tai_seconds | tai_time<seconds> |
| tai_time | time_point<tai_clock, Duration>,原子时时间点 |
| utc_seconds | utc_time<seconds> |
| utc_time | time_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;