2014-02-05 89 views
1

我使用VS2010 此代碼被編譯爲ANSI C項目mktime不更新結構TM

int C90Clockadapter_GetCW_ISO8601(const C90Clockadapter * clockadapter, int * calendarweek) 
{ 
    int e = 0; 
    struct tm tm; 
    char timebuf[64]; 

    memset(&tm, 0, sizeof(tm)); 
    tm.tm_mday = clockadapter->mDay; 
    tm.tm_mon = clockadapter->mMonth; 
    tm.tm_year = clockadapter->mYear; 

    mktime(&tm); 

    if (0 != strftime(timebuf,sizeof(timebuf),"%W", &tm)) //i know %W is not ISO8601 
    { 
     *calendarweek = atoi(timebuf); 
    } 
    else 
    { 
     e |= 1; 
    } 

    return e; 
} 

不知何故mktime(&tm)不會改變的tm狀態。這種行爲完全不符合我的期望。

我寫了一些單元測試有一個自我解釋輸出:

4.1.1971 - expected CW 1 but was CW 0 
31.12.1992 - expected CW 53 but was CW 0 

什麼是錯我的代碼 - 或者是在有窗戶或VS2010和ANSI-C,我很想念一些特產?

回答

0

timeformat無效。 mktime返回-1,這是未處理的。

此代碼段顯示如何正確填寫struct tm

tm.tm_mday = clockadapter->mDay; 
tm.tm_mon = clockadapter->mMonth - 1; 
tm.tm_year = clockadapter->mYear - 1900;