std:: gmtime
From cppreference.net
C++
Date and time library
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<ctime>
|
||
|
std::
tm
*
gmtime
(
const
std::
time_t
*
time
)
;
|
||
将给定的自纪元起时间作为 std::time_t 值转换为日历时间,以协调世界时(UTC)表示。
目录 |
参数
| time | - | 指向要转换的 time_t 对象的指针 |
返回值
成功时返回指向静态内部 std::tm 对象的指针,否则返回空指针。该结构体可能在 std::gmtime 、 std::localtime 和 std::ctime 之间共享,且每次调用时可能被覆盖。
注释
此函数可能不是线程安全的。
POSIX 要求
gmtime
在因参数过大而失败时将
errno
设置为
EOVERFLOW
。
示例
运行此代码
#include <ctime> #include <iomanip> #include <iostream> #include <sstream> int main() { setenv("TZ", "/usr/share/zoneinfo/Europe/London", 1); // POSIX 特定 std::tm tm{}; // get_time 不会设置所有字段,因此使用 {} tm.tm_year = 2020 - 1900; // 2020 年 tm.tm_mon = 7 - 1; // 七月 tm.tm_mday = 15; // 15 日 tm.tm_hour = 10; tm.tm_min = 15; tm.tm_isdst = 1; // 伦敦夏令时 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: Wed Jul 15 09:15:00 2020 GMT local: Wed Jul 15 10:15:00 2020 BST
参见
|
将纪元起计时转换为本地时间表示的日历时间
(函数) |
|
|
C 文档
中的
gmtime
|
|