std::localtime
來自 cppreference.com
定義於標頭檔案 <ctime> |
||
std::tm* localtime( const std::time_t* time ); |
||
將給定自 epoch 以來的時間值 std::time_t 轉換為日曆時間,以本地時間表示。
目錄 |
[編輯] 引數
time | - | 指向要轉換的 std::time_t 物件的指標 |
[編輯] 返回值
成功時返回指向靜態內部 std::tm 物件的指標,否則返回空指標。該結構可能在 std::gmtime、std::localtime 和 std::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
[編輯] 參閱
將自紀元以來的時間轉換為以世界協調時間表示的日曆時間 (函式) | |
(C23)(C11) |
將自紀元以來的時間轉換為以本地時間表示的日曆時間 (函式) |
有關 localtime 的 C 文件
|