2017-02-28 102 views
0

我需要將time_t作爲字符串存儲在char數組中。 還有一些關於將time_t轉換爲字符串的問題,但它們對我沒有幫助。我想將time_t的值存儲在一個字符串中,而不是將其轉換爲可讀格式。任何答案前請先看看this questiontime_t的C字符串表示形式

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

int main() 
{ 
    struct tm epoch_date; 
    time_t Time_Epoch; 
    strptime("2017 Jan 1 23:59:59", "%Y %b %d %T", &epoch_date); 

    Time_Epoch = timegm(&epoch_date); // Time_Epoch: 1488268396 
    return 0; 
} 

這段代碼將timestamp返回爲Time_Epoch。

我應該如何轉換是時間戳字符串給所需的輸出如下:

所需的輸出:Current date and time are: 1488268396

+2

'printf(「當前日期和時間是:%d \ n」,(int)Time_Epoch);' – mch

回答

1

如果目的是存儲的time_t在字符數組的值,可以使用sprintf as:

char strTime[50]; 
sprintf(strTime,"%d",Time_Epoch); 
相關問題