获取固定大小整数的时间

Get time as fixed size integer

本文关键字:整数 时间 获取      更新时间:2023-10-16

当你有时间的时候通过此结构:http://www.cplusplus.com/reference/ctime/tm/可以看到这些值(例如,tm_sec, tm_min)都是int的。问题是我想将这些int存储在某个地方。但是我想使用int32_t类型而不是int,以便大小是固定的。对此我能做些什么吗?

C++从 C 获取struct tm类型。根据C标准,这些struct tm元素具有一定的范围:

C99 §7.23.1 时间的组成部分

tm结构应至少包含以下成员,顺序不限。成员的语义及其正常范围在注释中表示。

int tm_sec;   //seconds after the minute — [0, 60]
int tm_min;   //minutes after the hour — [0, 59]
int tm_hour;  //hours since midnight — [0, 23]
int tm_mday;  //day of the month — [1, 31]
int tm_mon;   //months since January — [0, 11]
int tm_year;  //years since 1900
int tm_wday;  //days since Sunday — [0, 6]
int tm_yday;  //days since January 1 — [0, 365]
int tm_isdst; // Daylight Saving Time flag

这些值可以安全地转换为int32_t