命名空间
变体
操作

std::localtime

来自 cppreference.cn
< cpp‎ | chrono‎ | c
 
 
 
 
定义于头文件 <ctime>
std::tm* localtime( const std::time_t* time );

将给定自 epoch 以来的时间值 std::time_t 转换为日历时间,以本地时间表示。

目录

[编辑] 参数

time - 指向要转换的 std::time_t 对象的指针

[编辑] 返回值

成功时返回指向静态内部 std::tm 对象的指针,否则返回空指针。该结构可能在 std::gmtimestd::localtimestd::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

[编辑] 参阅

将自纪元以来的时间转换为以世界协调时间表示的日历时间
(函数) [编辑]
将自纪元以来的时间转换为以本地时间表示的日历时间
(函数) [编辑]
有关 localtimeC 文档
English 日本語 中文(简体) 中文(繁體)