std::gmtime
来自cppreference.com
| 在标头 <ctime> 定义
|
||
| |
||
将给定的作为 std::time_t 值的从纪元起时间转换为以协调世界时(UTC)表达的日历时间。
参数
| time | - | 指向要转换的 time_t 对象的指针 |
返回值
成功时为指向静态内部 std::tm 对象的指针,否则为 NULL。该结构体可能在 std::gmtime、std::localtime 和 std::ctime 之间共享,并可能在每次调用时被覆写。
注意
此函数可能不是线程安全的。
POSIX 要求若此函数因实参过大而失败,则设置 errno 为 EOVERFLOW。
示例
运行此代码
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
int main()
{
setenv("TZ", "/usr/share/zoneinfo/Europe/London", 1); // POSIX-specific
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
参阅
| 转换纪元起时间为以本地时间表示的日历时间 (函数) | |
gmtime 的 C 文档
| |