summaryrefslogtreecommitdiff
path: root/ext/date/lib/dow.c
blob: 4066d61b4cec2c0a0f0f7dbcec066e505a490647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
   +----------------------------------------------------------------------+
   | 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 <dr@ez.no>                                   |
   +----------------------------------------------------------------------+
 */

/* $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 timelib_sll century_value(timelib_sll j)
{
	timelib_sll i = j - 17;
	timelib_sll c = (4 - i * 2 + (i + 1) / 4) % 7;

	return c < 0 ? c + 7 : c;
}

timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
{
	timelib_sll 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;
}

timelib_sll timelib_daynr_from_weeknr(timelib_sll y, timelib_sll w, timelib_sll d)
{
	timelib_sll 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