2012-01-05 106 views
0

這裏是我的代碼應該是通過時間戳之後顯示兩個完整的日期:轉換秒,在C++ UNIX時間戳

#include <iostream> 
#include <fstream> 
#include <time.h> 
#include <string> 

using namespace std; 

int main (int argc, char* argv[]) 
{ 

    time_t startTime_t = (time_t) argv[1]; 
    time_t endTime_t = (time_t) argv[2]; 

    cout << argv[1] << endl << argv[2] << endl; 
    cout << startTime_t << endl << endTime_t << endl; 

    string startTime = asctime(localtime(&startTime_t)); 
    string endTime = asctime(localtime(&endTime_t)); 

    cout << startTime << endl << endTime << endl; 

    return 0; 
} 

我做給time_t的一些錯誤的轉換,從秒,你可以看到這個輸出:

$ ./a 1325783860 1325791065 
1325783860 
1325791065 
1629762852 
1629763900 
Mon Aug 23 19:54:12 2021 

Mon Aug 23 20:11:40 2021 

參考:

$ date -d @1325783860 
Thu Jan 5 12:17:40 EST 2012 
+0

如果這是作業,它應該被標記爲 – antlersoft 2012-01-05 19:37:06

+0

這不是作業。它出現在一個個人項目中。 – gsingh2011 2012-01-05 19:53:38

回答

3

你不能只是投了ST環指針指向time_t。使用atol()從字符串中獲取long,然後將其轉換爲time_t。

您將返回的值是指針算術值解釋爲一個時間,或基本上是隨機的。