/* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Derick Rethans | +----------------------------------------------------------------------+ */ /* $Id$ */ #include "timelib_structs.h" static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */ static int m_table_leap[13] = { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */ static int century_value(int j) { int i = j - 17; int c = (4 - i * 2 + (i + 1) / 4) % 7; return c < 0 ? c + 7 : c; } int timelib_day_of_week(int y, int m, int d) { int c1, y1, m1; /* Only valid for Gregorian calendar */ if (y < 1753) { return -1; } c1 = century_value(y / 100); y1 = (y % 100); m1 = is_leap(y) ? m_table_leap[m] : m_table_common[m]; return (c1 + y1 + m1 + (y1 / 4) + d) % 7; } int timelib_daynr_from_weeknr(int y, int w, int d) { int dow, day; /* Figure out the dayofweek for y-1-1 */ dow = timelib_day_of_week(y, 1, 1); /* then use that to figure out the offset for day 1 of week 1 */ day = 0 - (dow > 4 ? dow - 7 : dow); /* Add weeks and days */ return day + ((w - 1) * 7) + d; } #if 0 int main(void) { printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */ printf("dow = %d\n", timelib_day_of_week(2005, 2, 19)); /* 6 */ } #endif