mktime
來自 cppreference.com
定義於標頭檔案 <time.h> |
||
將表示為 struct tm 物件的本地日曆時間規範化,並將其轉換為自紀元以來的時間,表示為 time_t 物件。arg->tm_wday 和 arg->tm_yday 被忽略。arg 中的值不會檢查是否超出範圍。
如果 arg->tm_isdst 的值為負,則 mktime
會嘗試確定在指定時間是否啟用了夏令時。
如果成功轉換為 time_t
,則會修改 arg 物件。arg 的所有欄位都會更新以適應其正確的範圍。arg->tm_wday 和 arg->tm_yday 會根據其他可用欄位的資訊重新計算。
目錄 |
[編輯] 引數
arg | - | 指向 tm 物件的指標,指定要轉換的本地日曆時間 |
[編輯] 返回值
成功時返回自紀元以來的時間,表示為 time_t 物件,如果 arg 無法表示為 time_t 物件,則返回 -1(POSIX 也要求在這種情況下將 EOVERFLOW
儲存在 errno 中)。
[編輯] 注意
如果 struct tm 物件是從 POSIX strptime
或等效函式獲取的,則 tm_isdst
的值是不確定的,需要在呼叫 mktime
之前顯式設定。
[編輯] 示例
執行此程式碼
#define _POSIX_C_SOURCE 200112L // for setenv on gcc #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { setenv("TZ", "/usr/share/zoneinfo/America/New_York", 1); // POSIX-specific struct tm tm = *localtime(&(time_t){time(NULL)}); printf("Today is %s", asctime(&tm)); printf("(DST is %s)\n", tm.tm_isdst ? "in effect" : "not in effect"); tm.tm_mon -= 100; // tm_mon is now outside its normal range mktime(&tm); // tm_isdst is not set to -1; today's DST status is used printf("100 months ago was %s", asctime(&tm)); printf("(DST was %s)\n", tm.tm_isdst ? "in effect" : "not in effect"); }
可能的輸出
Today is Fri Apr 22 11:53:36 2016 (DST is in effect) 100 months ago was Sat Dec 22 10:53:36 2007 (DST was not in effect)
[編輯] 參考資料
- C23 標準 (ISO/IEC 9899:2024)
- 7.27.2.3 mktime 函式 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.27.2.3 mktime 函式 (p: 285-286)
- C11 標準 (ISO/IEC 9899:2011)
- 7.27.2.3 mktime 函式 (p: 390-391)
- C99 標準 (ISO/IEC 9899:1999)
- 7.23.2.3 mktime 函式 (p: 340-341)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 4.12.2.3 mktime 函式
[編輯] 另請參閱
(C23)(C11) |
將自紀元以來的時間轉換為以本地時間表示的日曆時間 (函式) |
C++ 文件,關於 mktime
|