time ()- 判定現行時間

格式

#include <time.h>
time_t time(time_t *timeptr);

語言層次

ANSI

安全執行緒

說明

time() 函數決定現行行事曆時間 (以秒為單位)。
附註: 行事曆時間是自 EPOCH (即世界座標時間 (UTC) 1970 年 1 月 1 日 00:00:00) 以來所經歷的秒數。

回覆值

time() 函數會傳回現行行事曆時間。 回覆值也會儲存在 timeptr提供的位置中。 如果 timeptrNULL,則不會儲存回覆值。 如果行事曆時間無法使用,則會傳回值 (time_t) (-1)

範例

此範例會取得時間,並將它指派給 ltime。 然後, ctime() 函數會將秒數轉換為現行日期和時間。 然後,此範例會列印提供現行時間的訊息。
#include <time.h>
#include <stdio.h>
 
int main(void)
{
   time_t ltime;
   if (time(&ltime) == -1)
   {
      printf("Calendar time not available.\n");
      exit(1);
   }
   printf("The time is %s\n", ctime(&ltime));
}
 
/******************  Output should be similar to:  ****************
 
The time is Mon Mar 22 19:01:41 2004
*/

相關資訊