2012-08-01 155 views
32

我想獲取當前的時間戳並使用fprintf將其打印出來。如何獲得C中的unix時間戳作爲int?

+2

有什麼特別的原因它需要特別是一個int? – 2012-08-01 18:29:05

+0

@ DennisMeng如果我不得不猜測,他可能想要得到時間戳作爲返回碼,並刪除輸出處理。 – 2017-01-07 11:09:41

+0

對不起,necrobump這個,但*技術上*所有這些答案都是不正確的,因爲自從Unix紀元以來''時間'**不保證**;根據維基百科,它可以是供應商選擇的任何時代。它也表示1900有時被使用。不過,我認爲這是自所有即使遠程智能系統上的Unix時代開始。 – Markasoftware 2017-05-25 01:10:37

回答

42

對於32位系統:

fprintf(stdout, "%u\n", (unsigned)time(NULL)); 

對於64位系統:

fprintf(stdout, "%lu\n", (unsigned long)time(NULL)); 
+0

如果我需要它幾微秒? – Tim 2012-08-01 21:26:54

+2

如果您想要微秒,您應該使用以下函數之一:帶CLOCK_MONOTONIC時鐘ID或gettimeofday的clock_gettime。但要小心gettimeofday可以返​​回遞減的時間值。 – 2012-08-01 21:34:07

+0

所以你的意思是,gettimeofday可能在時間T返回N,但在時間T + 1返回N-1? – Tim 2012-08-01 23:47:55

10

具有第二精度,您可以打印tv_sec字段timeval結構,您從gettimeofday()函數獲得的結構。例如:

#include <sys/time.h> 
#include <stdio.h> 

int main() 
{ 
    struct timeval tv; 
    gettimeofday(&tv, NULL); 
    printf("Seconds since Jan. 1, 1970: %ld\n", tv.tv_sec); 
    return 0; 
} 

編譯和運行的實施例:

$ gcc -Wall -o test ./test.c 
$ ./test 
Seconds since Jan. 1, 1970: 1343845834 

但請注意,它是自紀元一段時間,所以long int用於擬合的秒數這些天。

也有打印人類可讀時間的功能。詳情請參閱this manual page。在這裏不用使用ctime()一個例子:

#include <time.h> 
#include <stdio.h> 

int main() 
{ 
    time_t clk = time(NULL); 
    printf("%s", ctime(&clk)); 
    return 0; 
} 

實施例運行&輸出:

$ gcc -Wall -o test ./test.c 
$ ./test 
Wed Aug 1 14:43:23 2012 
$ 
+0

'gettimeofday'是實際的Linux系統調用。 – 2017-10-07 02:55:40

20

只是把由time()

#include <stdio.h> 
#include <time.h> 

int main(void) { 
    printf("Timestamp: %d\n",(int)time(NULL)); 
    return 0; 
} 

你想要什麼返回值?

$ gcc -Wall -Wextra -pedantic -std=c99 tstamp.c && ./a.out 
Timestamp: 1343846167 

要得到微秒從新紀元,從C11上,便攜的方法是使用

int timespec_get(struct timespec *ts, int base) 

不幸的是,C11尚未提供無處不在,所以截至目前,最接近便攜是使用POSIX函數clock_gettimegettimeofday(在POSIX.1-2008中標記爲過時,其中推薦clock_gettime)中的一個。

兩個函數的代碼幾乎是相同的:

#include <stdio.h> 
#include <time.h> 
#include <stdint.h> 
#include <inttypes.h> 

int main(void) { 

    struct timespec tms; 

    /* The C11 way */ 
    /* if (! timespec_get(&tms, TIME_UTC)) { */ 

    /* POSIX.1-2008 way */ 
    if (clock_gettime(CLOCK_REALTIME,&tms)) { 
     return -1; 
    } 
    /* seconds, multiplied with 1 million */ 
    int64_t micros = tms.tv_sec * 1000000; 
    /* Add full microseconds */ 
    micros += tms.tv_nsec/1000; 
    /* round up if necessary */ 
    if (tms.tv_nsec % 1000 >= 500) { 
     ++micros; 
    } 
    printf("Microseconds: %"PRId64"\n",micros); 
    return 0; 
} 
+1

如果我需要微秒,該怎麼辦? – Tim 2012-08-01 21:26:44

+0

這就意味着'int'在幾乎所有常見系統上都不是合適的類型。使用典型的'INT_MAX = 2147483647',如果你想要微秒,你可以將不足36分鐘的時間放入'int'中。當然,你通常不能使用'time()'。可移植的是,你必須使用'timespec_get()'。你想要什麼? Unix時間戳或微秒(自epoch以來)? – 2012-08-01 21:39:10

+0

微秒以來時代將是最好的。 – Tim 2012-08-01 23:47:30

0
#include <stdio.h> 
#include <time.h> 

int main() 
{ 
    time_t seconds; 

    seconds = time(NULL); 
    printf("Seconds since January 1, 1970 = %ld\n", seconds); 

    return(0); 
} 

而且會得到類似的結果:
秒自1970年1月1日= 1476107865

相關問題