Su Mo Tu We Th Fr Sa
Su 0 -1 -2 -3 -4 -5 -6
Mo -6 0 -1 -2 -3 -4 -5
Tu -5 -6 0 -1 -2 -3 -4
We -4 -5 -6 0 -1 -2 -3
Th -3 -4 -5 -6 0 -1 -2
Fr -2 -3 -4 -5 -6 0 -1
Sa -1 -2 -3 -4 -5 -6 0
Az első hét az első, legalább 4 nap hosszú hét:
Su Mo Tu We Th Fr Sa
Su 0 -1 -2 -3 3 2 1
Tu 1 0 -1 -2 -3 3 2
We 2 1 0 -1 -2 -3 3
Th 3 2 1 0 -1 -2 -3
Fr -3 3 2 1 0 -1 -2
Sa -1 -2 -3 3 2 1 0
Az első hét az első teljes, 7 nap hosszú hét:
Su Mo Tu We Th Fr Sa
Su 0 6 5 4 3 2 1
Mo 1 0 6 5 4 3 2
Tu 2 1 0 6 5 4 3
We 3 2 1 0 6 5 4
Th 4 3 2 1 0 6 5
Fr 5 4 3 2 1 0 6
Sa 6 5 4 3 2 1 0
A három táblától megzakkanva nézzük meg az algoritmust:
/* a datetime struktúra így néz ki: typedef struct s_datetime { int year; int month; int day; int dow; int doy; int woy; int hour; int minute; int second; } datetime; */ int getDoy(int y, int m, int d) { struct tm t; // get dow from date t.tm_year = y - 1900; t.tm_mon = m - 1; t.tm_mday = d; t.tm_hour = 0; t.tm_min = 0; t.tm_sec = 0; mktime(&t); return(t.tm_yday); } // getDoy() void calcDateTime(datetime* pillanat, int week1Rule, int weekStartsOn) { int week1offset, jan1dow, dayCount; // get dow from date pillanat->doy = getDoy(pillanat->year, pillanat->month, pillanat->day); pillanat->dow = getDow(pillanat->year, pillanat->month, pillanat->day); // get dow for jan1 jan1dow = getDow(pillanat->year, 1, 1); // get offset for first day of first week comparing to 1st of january switch (week1Rule) { case 1: // first week includes jan1 week1offset = (weekStartsOn - jan1dow) <= 0 ? weekStartsOn - jan1dow : weekStartsOn - jan1dow - 7; break; case 4: // first week is min. 4 days long week1offset = (weekStartsOn - jan1dow) < 4 ? weekStartsOn - jan1dow : weekStartsOn - jan1dow - 7; break; case 7: // first week is the first full (7 day long) week week1offset = (weekStartsOn - jan1dow) >= 0 ? weekStartsOn - jan1dow : weekStartsOn - jan1dow + 7; break; } // calculate number of days between dates dayCount = pillanat->doy - week1offset; pillanat->woy = ((6 + dayCount) / 7); } //calcDateTime()