2012-07-19 33 views
0

這個小程序重現了我項目中的錯誤。 time_t變量轉換爲struct_tm,然後轉換爲字符串,並序列化爲文件。稍後,該字符串將從該文件加載,並應該轉換回time_t。結果time_t與原始時間相差一小時(可能是由於夏令時)。我只能更改反序列化部分,因爲文件格式應該保持不變。序列化和反序列化time_t變量後的結果不正確

 
#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <time.h> 

using namespace std; 

string FormatDateTime(struct tm* time); 
void Test(); 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Test(); 
    return 0; 
} 


string FormatDateTime(struct tm* time) 
{ 
    static const char* months[] = 
    { 
     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 
    }; 

    char s[30]; 
    const char* month; 

    if (time->tm_mon >= 0 && time->tm_mon < 12) 
    { 
     month = months[time->tm_mon]; 
    } 
    else 
    { 
     month = "??"; 
    } 

    sprintf(s, "%d-%s-%02d %02d:%02d:%02d", 
     time->tm_year + 1900, 
     month, 
     time->tm_mday, 
     time->tm_hour, 
     time->tm_min, 
     time->tm_sec); 

    return s; 
} 

void Test() 
{ 
    // time_t variable is initialized with current time 
    time_t t = time(NULL); 

    // time_t variable is converted to struct tm and then to string 
    struct tm* ptm = localtime(&t); 

    char buffer[100]; 

    sprintf(buffer, "%d %d %d %d %d %d", 
     ptm->tm_mday, ptm->tm_mon + 1, ptm->tm_year + 1900, ptm->tm_hour, ptm->tm_min, ptm->tm_sec); 

    cout << buffer << endl;   // string is OK 


    // string is saved to the file 

    // ********************************************************************************************* 

    // later this string is restored from the file 

    // **** I can change something only after this line **** // 

    struct tm stm; 
    memset(&stm, 0, sizeof(stm)); 

    sscanf(buffer, "%d %d %d %d %d %d", 
     &stm.tm_mday, &stm.tm_mon, &stm.tm_year, &stm.tm_hour, &stm.tm_min, &stm.tm_sec); 

    stm.tm_mon -= 1; 
    stm.tm_year -= 1900; 

    string s = FormatDateTime(ptm); 

    cout << s << endl;    // OK 

    // ********************************************************************************************* 
    // Now I need to convert struct tm to time_t and keep it in the variable 

    time_t t1 = mktime(&stm);  // not the same time - 1 hour difference 

    if (t1 == t) 
    { 
     cout << "t1 == t" << endl; 
    } 
    else 
    { 
     cout << "t1 != t !!!!!" << endl; 
    } 

    // time_t is printed - result is incorrect 

    // Utilities::FormatDateTime 
    struct tm* ptm1 = localtime(&t1); 

    s = FormatDateTime(ptm1); 

    cout << s << endl; 
} 

當地時間是11.33。結果:

 
19 7 2012 11 33 17 
2012-Jul-19 11:33:17 
t1 != t !!!!! 
2012-Jul-19 12:33:17 

如何更改此程序的最後部分以獲得正確結果?

+0

這是什麼:'如果(時間 - > tm_mon> = 0 &&時間> tm_mon tm_mon]; }' – SingerOfTheFall 2012-07-19 08:35:30

+0

@SingerOfTheFall - 問題被編輯 – 2012-07-19 08:42:32

+0

可以嗎?。 't繁殖,它完全適用於我:'19 7 2012 12 45 48 2012-Jul-19 12:45:48 t1 == t 2012-Jul-19 12:45:48' – SingerOfTheFall 2012-07-19 08:46:01

回答

3

我的猜測是,tm_isdst是在你的兩個TM結構不同的兼容性問題。在一種情況下其0和在其他1.

0

這不會解決probleme但可能會晚一點時間0應該代表1970年1月1日,而不是1900