3

Not sure if this is a timezone problem: currently on SGT.

Following code reports Fri, 17 April 2009 as day of week 6 (??), but Fri, Jan 2 as day of the week 5.

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

#define ONE_DAY 86400

int main() {
    time_t StartDate = (time_t) 1239984000;
    struct tm StartDateStruct = *localtime(&StartDate);
    fprintf(stdout, "Date is %s", asctime(gmtime(&StartDate)));
    fprintf(stdout, "Day of the week is = %i\n\n", StartDateStruct.tm_wday);

    StartDate = (time_t) 0;
    for (int i=0; i<7; i++) {
        StartDate = ONE_DAY * i;
        StartDateStruct = *localtime(&StartDate);
        fprintf(stdout, "Date is %s", asctime(gmtime(&StartDate)));
        fprintf(stdout, "Day of the week is = %i\n", StartDateStruct.tm_wday);
    }
}

f19 3.11.4-201.fc19.x86_64, gcc 4.8.1 / clang 3.3

Thanking you in advance.

Michael

myk
  • 708
  • 2
  • 8
  • 20
  • Friday should always be number five in `tm_wday`, no matter the timezone. so the first result is wrong. – Some programmer dude Oct 23 '13 at 13:07
  • Works fine here, getting friday as day of week 5 for both output. The time zone on the test machine is set to CEST ("Central European Day Light Saving Time"). – alk Oct 23 '13 at 13:08
  • 1
    Interesting enough if I set `TZ='Asia/Singapore'; export TZ` the OP's source gives the same result as observed by the OP. So this is definitly time zone dependend. – alk Oct 23 '13 at 13:17
  • Uh....localtime adjusts to local timezone, while gmtime does not. Replacing localtime with gmtime when populating the tm struct resolves the day to the week to 5! – myk Oct 24 '13 at 11:39

0 Answers0