2012-10-09 74 views
1
 char buffer[800]; 
     struct tm str_time; 

     str_time.tm_mday = Cur_Day; 
     str_time.tm_mon = Cur_Month - 1; 
     str_time.tm_year = entries[i].Year_Start - 1900; 
     int len = strftime(buffer, 100, "%A, %d %B %Y", &str_time); 
     printf("\n%s\n", buffer); 

無論Cur_Day和Cur_Month的值如何,上面的結果都是星期幾總是星期日?strftime問題一週中的某天

輸出示例:

Sunday, 23 November 2012 
------------------------ 
stuff 

Sunday, 25 November 2012 
------------------------ 
stuff 

Sunday, 26 November 2012 
------------------------ 
stuff 
+1

沒有足夠的代碼甚至可以在此猜測。例如,Cur_Day設置爲什麼?和Cur_Month等 –

+0

不工作的整個部分高於...其他所有工作。 Cur_Day是條目所在的月份的日期(1到31之間的數字),Cur_Month是月份(1到12之間的數字)。 –

+1

@ j.w.r如果您知道要查找的內容,則從顯示的代碼中可以明顯看出該錯誤。 – zwol

回答

3

您的str_time結構(如果,因爲它似乎是,它是一個局部變量)在其字段中具有不確定的值,除非您明確地設置它們。所有strftime確實使用它的值,它不會首先調整值以符合其他字段。

由於您沒有設置tm_wday,它會保留原來的狀態(0表示它的外觀,因爲它總是星期天)。

如果你想根據其他領域調整字段,你應該看看mktime()

從標準(ISO C99):

mktime函數轉換破碎下降時間,表示爲本地時間,在 結構指向timeptr成日曆時間值與相同的編碼爲 由時間函數返回的值。

該結構的tm_wday和tm_yday組件的原始值將被忽略,並且其他組件的原始值不限於上述範圍。

成功完成後,結構中tm_wday和tm_yday組件的值被適當設置,其他組件被設置爲表示指定的日曆時間,但其值被強制設置爲上述範圍;在確定tm_mon和tm_year之前tm_mday的最終值不會被設置。

最好的辦法是使用time()localtime()來填充tm結構,然後改變你想打電話mktime()之前更改的字段。這樣,你保證所有字段將有明智的價值。

以下程序顯示了這樣做的一種方法:

#include <stdio.h> 
#include <time.h> 

int main (void) { 
    char buffer[100]; 
    time_t now; 
    struct tm *ts; 

    // Get today in local time and output it. 

    now = time (NULL); 
    struct tm *ts = localtime (&now); 
    strftime (buffer, 100, "%A, %d %B %Y", ts); 
    printf ("Now  = %s\n", buffer); 

    // Advance day-of-month and make new date. 
    // Probably need to intelligently handle month rollover. 

    ts->tm_mday++; 
    mktime (ts); 
    strftime (buffer, 100, "%A, %d %B %Y", ts); 
    printf ("Tomorrow = %s\n", buffer); 

    return 0; 
} 

該程序的輸出是:

Now  = Tuesday, 09 October 2012 
Tomorrow = Wednesday, 10 October 2012 

對於它的價值,這是一個使用的是一個完整的程序方法爲給定日期的星期幾(默認爲今天)。

您可以更改年份,月份和月的一天,可選-y-m-d參數在任何你想要的順序,只要你想了很多次,但只有最後一次爲每種類型的計數。

#include <stdio.h> 
#include <time.h> 

static int makeError (char *argVal, char *errStr) { 
    printf ("Error with argument '%s': %s\n", argVal, errStr); 
    printf ("Usage: dow [-y<year>] [-m<month>] [-d<day>]\n"); 
    return 1; 
} 

int main (int argc, char *argv[]) { 
    int idx, intVal; 
    char chVal; 
    char buff[100]; 
    time_t now = time (NULL); 
    struct tm *nowStr = localtime (&now); 

    for (idx = 1; idx < argc; idx++) { 
     chVal = (*argv[idx] != '-') ? '\0' : *(argv[idx] + 1); 
     if ((chVal != 'y') && (chVal != 'm') && (chVal != 'd')) 
      return makeError (argv[idx], "does not start with '-y/m/d'"); 

     intVal = atoi (argv[idx] + 2); 
     if (intVal < 0) 
      return makeError (argv[idx], "suffix is negative"); 
     sprintf (buff, "%d", intVal); 
     if (strcmp (buff, argv[idx] + 2) != 0) 
      return makeError (argv[idx], "suffix is not numeric"); 

     switch (chVal) { 
      case 'y': nowStr->tm_year = intVal - 1900; break; 
      case 'm': nowStr->tm_mon = intVal - 1; break; 
      case 'd': nowStr->tm_mday = intVal; break; 
     } 
    } 

    mktime (nowStr); 
    strftime (buff, sizeof (buff), "%A, %d %B %Y", nowStr); 
    printf ("%s\n", buff); 

    return 0; 
} 

樣本成績單:

pax> ./dow 
Tuesday, 09 October 2012 

pax> ./dow -y2011 
Sunday, 09 October 2011 

pax> ./dow -y2000 -m1 -d1 
Saturday, 01 January 2000 
+0

+1,因爲我不知道'mktime' /'timegm'具有規範'tm'結構的副作用。 – zwol

1

最可能的解釋是,你需要strftimetm_wday有一個有意義的價值,如果你要問它打印星期幾。

這是爲了避免自己計算它的最簡單的方法可用:

struct tm tm; 

memset(&tm, 0, sizeof(struct tm)); 
tm.tm_mday = Cur_Day; 
tm.tm_mon = Cur_Month - 1; 
tm.tm_year = entries[i].Year_Start - 1900; 
tm.tm_hour = 12; 

(void) timegm(&tm); /* fills in the rest of `tm` as a side effect */ 

/* now call strftime */ 

如果你沒有timegm您可以用mktime,而不是(的問題,這樣計算脫身如果你只想打印日期),那麼在'本地'時間是非常不相關的。請勿使用Linux聯機幫助頁中描述的timegm的「便攜版本」,它的幾乎每一行都有可移植性問題!

相關問題