std:: localtime
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<ctime>
|
||
|
std::
tm
*
localtime
(
const
std::
time_t
*
time
)
;
|
||
将给定的自纪元起的时间作为 std::time_t 值转换为日历时间,以本地时间表示。
目录 |
参数
| time | - | 指向要转换的 std::time_t 对象的指针 |
返回值
成功时返回指向静态内部 std::tm 对象的指针,否则返回空指针。该结构可能由 std::gmtime 、 std::localtime 和 std::ctime 共享使用,且可能在每次调用时被覆盖。
注释
此函数可能不具备线程安全性。 Microsoft CRT 的实现是线程安全的。
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专用 std::tm tm{}; // 零初始化 tm.tm_year = 2020 - 1900; // 2020年 tm.tm_mon = 2 - 1; // 二月 tm.tm_mday = 15; // 15日 tm.tm_hour = 10; tm.tm_min = 15; tm.tm_isdst = 0; // 非夏令时 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
参见
|
将自纪元起的时间转换为协调世界时表示的日历时间
(函数) |
|
|
(C23)
(C11)
|
将自纪元起的时间转换为本地时间表示的日历时间
(函数) |
|
C 文档
关于
localtime
|
|