std::gmtime
來自 cppreference.com
定義於標頭檔案 <ctime> |
||
std::tm* gmtime( const std::time_t* time ); |
||
將給定自 epoch 以來的時間(std::time_t 值)轉換為日曆時間,以協調世界時(UTC)表示。
目錄 |
[編輯] 引數
time | - | 指向要轉換的 time_t 物件的指標 |
[編輯] 返回值
成功時返回指向靜態內部 std::tm 物件的指標,否則返回空指標。該結構可能在 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 does not set all fields hence {} tm.tm_year = 2020 - 1900; // 2020 tm.tm_mon = 7 - 1; // July tm.tm_mday = 15; // 15th tm.tm_hour = 10; tm.tm_min = 15; tm.tm_isdst = 1; // Daylight saving in London 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
[編輯] 參閱
將自紀元以來的時間轉換為以本地時間表示的日曆時間 (function) | |
C 文件 關於 gmtime
|