C語言time頭文件(time.h)中time函數的用法及代碼示例。
用法:
time_t time (time_t* timer);
獲取當前時間
該函數返回該值,如果參數不是空指針,它還會將此值設置為所指向的對象timer。
返回的值通常代表自UTC 1970年1月1日00:00以來的秒數(即當前Unix時間戳)。盡管庫可能使用不同的時間表示形式:可移植程序不應直接使用此函數返回的值,而應始終依靠對標準庫其他元素的調用將它們轉換為可移植類型(例如localtime,gmtime或者difftime)。
參數
返回值
當前日曆時間為time_t目的。如果參數不是空指針,返回值與參數所指向的位置中存儲的值相同timer。
如果該函數無法檢索日曆時間,則返回值
-1
。time_t是基本麵的別名算術類型能夠代表時間。
示例
/* time example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, difftime, time, mktime */
int main ()
{
time_t timer;
struct tm y2k = {0};
double seconds;
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
time(&timer); /* get current time; same as: timer = time(NULL) */
seconds = difftime(timer,mktime(&y2k));
printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);
return 0;
}
可能的輸出:
414086872 seconds since January 1, 2000 in the current timezone |
相關用法
- C語言 clock用法及代碼示例
- C語言 difftime用法及代碼示例
- C語言 mktime用法及代碼示例
- C語言 asctime用法及代碼示例
- C語言 ctime用法及代碼示例
- C語言 gmtime用法及代碼示例
- C語言 localtime用法及代碼示例
- C語言 strftime用法及代碼示例
注:本文由純淨天空篩選整理自C標準庫大神的英文原創作品 C time function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。