名稱空間
變體
操作

time

來自 cppreference.com
< c‎ | 時間
定義於標頭檔案 <time.h>
time_t time( time_t* arg );

返回當前日曆時間,編碼為 time_t 物件,並將其儲存在由 arg 指向的 time_t 物件中(除非 arg 是空指標)。

目錄

[編輯] 引數

arg - 指向 time_t 物件的指標,時間將儲存在此處,或為空指標。

[編輯] 返回值

成功時,返回編碼為 time_t 物件的當前日曆時間;出錯時,返回 (time_t)(-1)。如果 arg 不是空指標,則返回值也會儲存在由 arg 指向的物件中。

[編輯] 注意

time_t 中日曆時間的編碼未指定,但大多數系統都符合 POSIX 規範,並返回一個整數型別的值,表示自 紀元以來的秒數。其中 time_t 是 32 位有符號整數的實現(許多歷史實現)將在 2038 年失敗。

[編輯] 示例

#include <stdint.h>
#include <stdio.h>
#include <time.h>
 
int main(void)
{
    time_t result = time(NULL);
    if (result != (time_t)(-1))
        printf("The current time is %s(%jd seconds since the Epoch)\n",
               asctime(gmtime(&result)), (intmax_t)result);
}

可能的輸出

The current time is Fri Apr 24 15:05:25 2015
(1429887925 seconds since the Epoch)

[編輯] 參考

  • C23 標準 (ISO/IEC 9899:2024)
  • 7.27.2.4 The time function (p: TBD)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.27.2.4 The time function (p: 286)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.27.2.4 The time function (p: 391)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.23.2.4 The time function (p: 341)
  • C89/C90 標準 (ISO/IEC 9899:1990)
  • 4.12.2.4 The time function

[編輯] 另請參閱

將自紀元以來的時間轉換為以本地時間表示的日曆時間
(函式) [編輯]
將自紀元以來的時間轉換為協調世界時 (UTC) 表示的日曆時間
(函式) [編輯]
根據給定時間基準返回以秒和納秒錶示的日曆時間
(函式) [編輯]