std::localtime
来自 cppreference.cn
定义于头文件 <ctime> |
||
std::tm* localtime( const std::time_t* time ); |
||
将给定的自 epoch 起经过的时间(作为 std::time_t 值)转换为日历时间,以本地时间表示。
目录 |
[编辑] 参数
time | - | 要转换的 std::time_t 对象的指针 |
[编辑] 返回值
成功时,指向静态内部 std::tm 对象的指针;否则为空指针。该结构可能在 std::gmtime、std::localtime 和 std::ctime 之间共享,并且可能在每次调用时被覆盖。
[编辑] 注解
此函数可能不是线程安全的。
POSIX 要求如果此函数因参数过大而失败,则将 errno 设置为 EOVERFLOW。
POSIX 规定 此函数确定时区信息的方式如同调用 tzset
一样,后者读取环境变量 TZ。
[编辑] 示例
运行此代码
#include <ctime> #include <iomanip> #include <iostream> #include <sstream> int main() { setenv("TZ", "/usr/share/zoneinfo/America/Los_Angeles", 1); // POSIX-specific std::tm tm{}; // Zero initialise tm.tm_year = 2020 - 1900; // 2020 tm.tm_mon = 2 - 1; // February tm.tm_mday = 15; // 15th tm.tm_hour = 10; tm.tm_min = 15; tm.tm_isdst = 0; // Not daylight saving std::time_t t = std::mktime(&tm); std::cout << "UTC: " << std::put_time(std::gmtime(&t), "%c %Z") << '\n'; std::cout << "local: " << std::put_time(std::localtime(&t), "%c %Z") << '\n'; }
可能的输出
UTC: Sat Feb 15 18:15:00 2020 GMT local: Sat Feb 15 10:15:00 2020 PST
[编辑] 参见
将自 epoch 起的时间转换为以世界协调时间表示的日历时间 (函数) | |
(C23)(C11) |
将自 epoch 起的时间转换为以本地时间表示的日历时间 (函数) |
C 文档 关于 localtime
|