xref: /PHP-7.3/ext/date/lib/dow.c (revision 3725a446)
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015-2019 Derick Rethans
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "timelib.h"
26 #include "timelib_private.h"
27 
28 static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
29 static int m_table_leap[13] =   { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
30 
positive_mod(timelib_sll x,timelib_sll y)31 static timelib_sll positive_mod(timelib_sll x, timelib_sll y)
32 {
33 	timelib_sll tmp;
34 
35 	tmp = x % y;
36 	if (tmp < 0) {
37 		tmp += y;
38 	}
39 
40 	return tmp;
41 }
42 
century_value(timelib_sll j)43 static timelib_sll century_value(timelib_sll j)
44 {
45 	return 6 - positive_mod(j, 4) * 2;
46 }
47 
timelib_day_of_week_ex(timelib_sll y,timelib_sll m,timelib_sll d,int iso)48 static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
49 {
50 	timelib_sll c1, y1, m1, dow;
51 
52 	/* Only valid for Gregorian calendar, commented out as we don't handle
53 	 * Julian calendar. We just return the 'wrong' day of week to be
54 	 * consistent. */
55 	c1 = century_value(y / 100);
56 	y1 = positive_mod(y, 100);
57 	m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
58 	dow = positive_mod((c1 + y1 + m1 + (y1 / 4) + d), 7);
59 	if (iso) {
60 		if (dow == 0) {
61 			dow = 7;
62 		}
63 	}
64 	return dow;
65 }
66 
timelib_day_of_week(timelib_sll y,timelib_sll m,timelib_sll d)67 timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
68 {
69 	return timelib_day_of_week_ex(y, m, d, 0);
70 }
71 
timelib_iso_day_of_week(timelib_sll y,timelib_sll m,timelib_sll d)72 timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
73 {
74 	return timelib_day_of_week_ex(y, m, d, 1);
75 }
76 
77                                 /*     jan  feb  mar  apr  may  jun  jul  aug  sep  oct  nov  dec */
78 static int d_table_common[13]  = {  0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
79 static int d_table_leap[13]    = {  0,   0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335 };
80 static int ml_table_common[13] = {  0,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
81 static int ml_table_leap[13]   = {  0,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
82 
timelib_day_of_year(timelib_sll y,timelib_sll m,timelib_sll d)83 timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
84 {
85 	return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
86 }
87 
timelib_days_in_month(timelib_sll y,timelib_sll m)88 timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
89 {
90 	return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
91 }
92 
timelib_isoweek_from_date(timelib_sll y,timelib_sll m,timelib_sll d,timelib_sll * iw,timelib_sll * iy)93 void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy)
94 {
95 	int y_leap, prev_y_leap, doy, jan1weekday, weekday;
96 
97 	y_leap = timelib_is_leap(y);
98 	prev_y_leap = timelib_is_leap(y-1);
99 	doy = timelib_day_of_year(y, m, d) + 1;
100 	if (y_leap && m > 2) {
101 		doy++;
102 	}
103 	jan1weekday = timelib_day_of_week(y, 1, 1);
104 	weekday = timelib_day_of_week(y, m, d);
105 	if (weekday == 0) weekday = 7;
106 	if (jan1weekday == 0) jan1weekday = 7;
107 	/* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
108 	if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
109 		*iy = y - 1;
110 		if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
111 			*iw = 53;
112 		} else {
113 			*iw = 52;
114 		}
115 	} else {
116 		*iy = y;
117 	}
118 	/* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
119 	if (*iy == y) {
120 		int i;
121 
122 		i = y_leap ? 366 : 365;
123 		if ((i - (doy - y_leap)) < (4 - weekday)) {
124 			*iy = y + 1;
125 			*iw = 1;
126 			return;
127 		}
128 	}
129 	/* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
130 	if (*iy == y) {
131 		int j;
132 
133 		j = doy + (7 - weekday) + (jan1weekday - 1);
134 		*iw = j / 7;
135 		if (jan1weekday > 4) {
136 			*iw -= 1;
137 		}
138 	}
139 }
140 
timelib_isodate_from_date(timelib_sll y,timelib_sll m,timelib_sll d,timelib_sll * iy,timelib_sll * iw,timelib_sll * id)141 void timelib_isodate_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iy, timelib_sll *iw, timelib_sll *id)
142 {
143 	timelib_isoweek_from_date(y, m, d, iw, iy);
144 	*id = timelib_day_of_week_ex(y, m, d, 1);
145 }
146 
timelib_daynr_from_weeknr(timelib_sll iy,timelib_sll iw,timelib_sll id)147 timelib_sll timelib_daynr_from_weeknr(timelib_sll iy, timelib_sll iw, timelib_sll id)
148 {
149 	timelib_sll dow, day;
150 
151 	/* Figure out the dayofweek for y-1-1 */
152 	dow = timelib_day_of_week(iy, 1, 1);
153 	/* then use that to figure out the offset for day 1 of week 1 */
154 	day = 0 - (dow > 4 ? dow - 7 : dow);
155 
156 	/* Add weeks and days */
157 	return day + ((iw - 1) * 7) + id;
158 }
159 
timelib_date_from_isodate(timelib_sll iy,timelib_sll iw,timelib_sll id,timelib_sll * y,timelib_sll * m,timelib_sll * d)160 void timelib_date_from_isodate(timelib_sll iy, timelib_sll iw, timelib_sll id, timelib_sll *y, timelib_sll *m, timelib_sll *d)
161 {
162 	timelib_sll daynr = timelib_daynr_from_weeknr(iy, iw, id) + 1;
163 	int *table;
164 	bool is_leap_year;
165 
166 	// Invariant: is_leap_year == timelib_is_leap(*y)
167 	*y = iy;
168 	is_leap_year = timelib_is_leap(*y);
169 
170 	// Establish invariant that daynr >= 0
171 	while (daynr <= 0) {
172 		*y -= 1;
173 		daynr += (is_leap_year = timelib_is_leap(*y)) ? 366 : 365;
174 	}
175 
176 	// Establish invariant that daynr <= number of days in *yr
177 	while (daynr > (is_leap_year ? 366 : 365)) {
178 		daynr -= is_leap_year ? 366 : 365;
179 		*y += 1;
180 		is_leap_year = timelib_is_leap(*y);
181 	}
182 
183 	table = is_leap_year ? ml_table_leap : ml_table_common;
184 
185 	// Establish invariant that daynr <= number of days in *m
186 	*m = 1;
187 	while (daynr > table[*m]) {
188 		daynr -= table[*m];
189 		*m += 1;
190 	}
191 
192 	*d = daynr;
193 }
194 
timelib_valid_time(timelib_sll h,timelib_sll i,timelib_sll s)195 int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
196 {
197 	if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
198 		return 0;
199 	}
200 	return 1;
201 }
202 
timelib_valid_date(timelib_sll y,timelib_sll m,timelib_sll d)203 int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
204 {
205 	if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
206 		return 0;
207 	}
208 	return 1;
209 }
210 #if 0
211 int main(void)
212 {
213 	printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
214 	printf("dow = %d\n", timelib_day_of_week(2005,  2, 19)); /* 6 */
215 }
216 #endif
217