xref: /PHP-5.5/ext/date/lib/dow.c (revision cdd2b8a7)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2015 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #include "timelib.h"
22 
23 static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
24 static int m_table_leap[13] =   { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
25 
positive_mod(timelib_sll x,timelib_sll y)26 static timelib_sll positive_mod(timelib_sll x, timelib_sll y)
27 {
28 	timelib_sll tmp;
29 
30 	tmp = x % y;
31 	if (tmp < 0) {
32 		tmp += y;
33 	}
34 
35 	return tmp;
36 }
37 
century_value(timelib_sll j)38 static timelib_sll century_value(timelib_sll j)
39 {
40 	return 6 - positive_mod(j, 4) * 2;
41 }
42 
timelib_day_of_week_ex(timelib_sll y,timelib_sll m,timelib_sll d,int iso)43 static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
44 {
45 	timelib_sll c1, y1, m1, dow;
46 
47 	/* Only valid for Gregorian calendar, commented out as we don't handle
48 	 * Julian calendar. We just return the 'wrong' day of week to be
49 	 * consistent. */
50 	c1 = century_value(y / 100);
51 	y1 = positive_mod(y, 100);
52 	m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
53 	dow = positive_mod((c1 + y1 + m1 + (y1 / 4) + d), 7);
54 	if (iso) {
55 		if (dow == 0) {
56 			dow = 7;
57 		}
58 	}
59 	return dow;
60 }
61 
timelib_day_of_week(timelib_sll y,timelib_sll m,timelib_sll d)62 timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
63 {
64 	return timelib_day_of_week_ex(y, m, d, 0);
65 }
66 
timelib_iso_day_of_week(timelib_sll y,timelib_sll m,timelib_sll d)67 timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
68 {
69 	return timelib_day_of_week_ex(y, m, d, 1);
70 }
71 
72                                 /*     jan  feb  mar  apr  may  jun  jul  aug  sep  oct  nov  dec */
73 static int d_table_common[13]  = {  0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
74 static int d_table_leap[13]    = {  0,   0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335 };
75 static int ml_table_common[13] = {  0,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
76 static int ml_table_leap[13]   = {  0,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
77 
timelib_day_of_year(timelib_sll y,timelib_sll m,timelib_sll d)78 timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
79 {
80 	return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
81 }
82 
timelib_days_in_month(timelib_sll y,timelib_sll m)83 timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
84 {
85 	return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
86 }
87 
timelib_isoweek_from_date(timelib_sll y,timelib_sll m,timelib_sll d,timelib_sll * iw,timelib_sll * iy)88 void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy)
89 {
90 	int y_leap, prev_y_leap, doy, jan1weekday, weekday;
91 
92 	y_leap = timelib_is_leap(y);
93 	prev_y_leap = timelib_is_leap(y-1);
94 	doy = timelib_day_of_year(y, m, d) + 1;
95 	if (y_leap && m > 2) {
96 		doy++;
97 	}
98 	jan1weekday = timelib_day_of_week(y, 1, 1);
99 	weekday = timelib_day_of_week(y, m, d);
100 	if (weekday == 0) weekday = 7;
101 	if (jan1weekday == 0) jan1weekday = 7;
102 	/* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
103 	if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
104 		*iy = y - 1;
105 		if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
106 			*iw = 53;
107 		} else {
108 			*iw = 52;
109 		}
110 	} else {
111 		*iy = y;
112 	}
113 	/* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
114 	if (*iy == y) {
115 		int i;
116 
117 		i = y_leap ? 366 : 365;
118 		if ((i - (doy - y_leap)) < (4 - weekday)) {
119 			*iy = y + 1;
120 			*iw = 1;
121 			return;
122 		}
123 	}
124 	/* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
125 	if (*iy == y) {
126 		int j;
127 
128 		j = doy + (7 - weekday) + (jan1weekday - 1);
129 		*iw = j / 7;
130 		if (jan1weekday > 4) {
131 			*iw -= 1;
132 		}
133 	}
134 }
135 
timelib_daynr_from_weeknr(timelib_sll y,timelib_sll w,timelib_sll d)136 timelib_sll timelib_daynr_from_weeknr(timelib_sll y, timelib_sll w, timelib_sll d)
137 {
138 	timelib_sll dow, day;
139 
140 	/* Figure out the dayofweek for y-1-1 */
141 	dow = timelib_day_of_week(y, 1, 1);
142 	/* then use that to figure out the offset for day 1 of week 1 */
143 	day = 0 - (dow > 4 ? dow - 7 : dow);
144 
145 	/* Add weeks and days */
146 	return day + ((w - 1) * 7) + d;
147 }
148 
timelib_valid_time(timelib_sll h,timelib_sll i,timelib_sll s)149 int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
150 {
151 	if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
152 		return 0;
153 	}
154 	return 1;
155 }
156 
timelib_valid_date(timelib_sll y,timelib_sll m,timelib_sll d)157 int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
158 {
159 	if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
160 		return 0;
161 	}
162 	return 1;
163 }
164 #if 0
165 int main(void)
166 {
167 	printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
168 	printf("dow = %d\n", timelib_day_of_week(2005,  2, 19)); /* 6 */
169 }
170 #endif
171