2014-09-24 32 views
1

我得到的絕對時間值,如下例所示如何將無符號值轉換爲struct tm?

1413399540000 
1411047780000 
1411574340000 

我怎樣才能將它們轉換爲「結構TM」在C++?

您好勒夫,

這是從GUI用於accesslist中時間範圍的輸入和我嘗試以下,

的typedef無符號長長uint64_t中;

uint64_t mytime; 
struct tm *tm; 
time_t realtime; 

mytime = 1447862580005ULL; 
realtime = (time_t) (mytime/1000000); 
printf ("The current local time is: %s", ctime(&realtime)); 
tm = localtime(&realtime); 
printf ("The current local time is: %s", asctime(tm)); 

tm->tm_year +=1900; 
tm->tm_mon +=1; 

printf("%04d-%02d-%02d %02d:%02d:%02d\n", 
     tm->tm_year,tm->tm_mon, tm->tm_mday, 
     tm->tm_hour, tm->tm_min, tm->tm_sec); 

O/P:

 
The current local time is: Sat Jan 17 10:11:02 1970 
The current local time is: Sat Jan 17 10:11:02 1970 
1970-01-17 10:11:02 

外貌迷惑到me.Not知道如何驗證轉換得到corect值與否。

+1

這些時間戳的單位是什麼?你有什麼嘗試? – 2014-09-24 12:57:49

+1

http://www.cplusplus.com/reference/ctime/gmtime/ – dasblinkenlight 2014-09-24 12:58:17

+0

嗨Joachim,這是一個來自GUI的輸入訪問列表時間範圍,我嘗試了以下, – user2652753 2014-09-24 13:04:32

回答

4

1413399540000看起來像一個數量自Unix紀元,即15.10.2014 18:59:00毫秒。

auto millisec = 1413399540000; 
time_t time = millisec/1000; 

tm local; 
localtime_r(&time, &local); // TODO: Check the errors. 

tm utc; 
gmtime_r(&time, &utc); // TODO: Check the errors. 
+0

我會提及localtime_r()爲清晰 – Slava 2014-09-24 13:53:24

+0

@Slava很有道理。更新。 – 2014-09-24 13:56:11

+0

@MaximEgorushkin頭文件是否定義了'gmtime_r'? – 2017-06-12 14:30:05