Lv 5
asked in 電腦與網際網路程式設計 · 1 decade ago

c語言time()與mktime()怎麼會差了30天?

以今天,12/13/07為例

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

exam.tm_mday = 13;

exam.tm_mon = 12;

exam.tm_year = 107; //2007,12,13

now=time(NULL);

tk = mktime(&today);

if (t1 == -1 || t2 == -1)

printf("輸入錯誤\n");

else

{

diff = difftime(tk, now);

printf(" %d 天", int(diff)/86400);

}

然後輸出是30天,怎麼會這樣?

Update:

@@, 真的耶, 意思是和陣列一樣嚕, 那我再問一下,這個時間結構是已經考慮潤年了嗎?

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    http://www.cplusplus.com/reference/clibrary/ctime/...

    這是Time structure的網頁

    注意看

    tm_mon

    months since January

    0-11

    1月開始是0,所以你如果要改成12月,應該要輸入11而非12

    2007-12-13 21:59:00 補充:

    從結構中應該沒有說明

    考慮潤年是指什麼?

    設定時間的回傳值看看有沒有回傳什麼。可能會回傳成功或失敗!!

    Source(s): 自己+C/C++函式庫
Still have questions? Get your answers by asking now.