xref: /PHP-7.0/ext/date/lib/dow.c (revision 60a6feed)
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015 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 
27 static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
28 static int m_table_leap[13] =   { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
29 
positive_mod(timelib_sll x,timelib_sll y)30 static timelib_sll positive_mod(timelib_sll x, timelib_sll y)
31 {
32 	timelib_sll tmp;
33 
34 	tmp = x % y;
35 	if (tmp < 0) {
36 		tmp += y;
37 	}
38 
39 	return tmp;
40 }
41 
century_value(timelib_sll j)42 static timelib_sll century_value(timelib_sll j)
43 {
44 	return 6 - positive_mod(j, 4) * 2;
45 }
46 
timelib_day_of_week_ex(timelib_sll y,timelib_sll m,timelib_sll d,int iso)47 static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
48 {
49 	timelib_sll c1, y1, m1, dow;
50 
51 	/* Only valid for Gregorian calendar, commented out as we don't handle
52 	 * Julian calendar. We just return the 'wrong' day of week to be
53 	 * consistent. */
54 	c1 = century_value(y / 100);
55 	y1 = positive_mod(y, 100);
56 	m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
57 	dow = positive_mod((c1 + y1 + m1 + (y1 / 4) + d), 7);
58 	if (iso) {
59 		if (dow == 0) {
60 			dow = 7;
61 		}
62 	}
63 	return dow;
64 }
65 
timelib_day_of_week(timelib_sll y,timelib_sll m,timelib_sll d)66 timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
67 {
68 	return timelib_day_of_week_ex(y, m, d, 0);
69 }
70 
timelib_iso_day_of_week(timelib_sll y,timelib_sll m,timelib_sll d)71 timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
72 {
73 	return timelib_day_of_week_ex(y, m, d, 1);
74 }
75 
76                                 /*     jan  feb  mar  apr  may  jun  jul  aug  sep  oct  nov  dec */
77 static int d_table_common[13]  = {  0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
78 static int d_table_leap[13]    = {  0,   0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335 };
79 static int ml_table_common[13] = {  0,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
80 static int ml_table_leap[13]   = {  0,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
81 
timelib_day_of_year(timelib_sll y,timelib_sll m,timelib_sll d)82 timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
83 {
84 	return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
85 }
86 
timelib_days_in_month(timelib_sll y,timelib_sll m)87 timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
88 {
89 	return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
90 }
91 
timelib_isoweek_from_date(timelib_sll y,timelib_sll m,timelib_sll d,timelib_sll * iw,timelib_sll * iy)92 void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy)
93 {
94 	int y_leap, prev_y_leap, doy, jan1weekday, weekday;
95 
96 	y_leap = timelib_is_leap(y);
97 	prev_y_leap = timelib_is_leap(y-1);
98 	doy = timelib_day_of_year(y, m, d) + 1;
99 	if (y_leap && m > 2) {
100 		doy++;
101 	}
102 	jan1weekday = timelib_day_of_week(y, 1, 1);
103 	weekday = timelib_day_of_week(y, m, d);
104 	if (weekday == 0) weekday = 7;
105 	if (jan1weekday == 0) jan1weekday = 7;
106 	/* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
107 	if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
108 		*iy = y - 1;
109 		if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
110 			*iw = 53;
111 		} else {
112 			*iw = 52;
113 		}
114 	} else {
115 		*iy = y;
116 	}
117 	/* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
118 	if (*iy == y) {
119 		int i;
120 
121 		i = y_leap ? 366 : 365;
122 		if ((i - (doy - y_leap)) < (4 - weekday)) {
123 			*iy = y + 1;
124 			*iw = 1;
125 			return;
126 		}
127 	}
128 	/* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
129 	if (*iy == y) {
130 		int j;
131 
132 		j = doy + (7 - weekday) + (jan1weekday - 1);
133 		*iw = j / 7;
134 		if (jan1weekday > 4) {
135 			*iw -= 1;
136 		}
137 	}
138 }
139 
timelib_daynr_from_weeknr(timelib_sll y,timelib_sll w,timelib_sll d)140 timelib_sll timelib_daynr_from_weeknr(timelib_sll y, timelib_sll w, timelib_sll d)
141 {
142 	timelib_sll dow, day;
143 
144 	/* Figure out the dayofweek for y-1-1 */
145 	dow = timelib_day_of_week(y, 1, 1);
146 	/* then use that to figure out the offset for day 1 of week 1 */
147 	day = 0 - (dow > 4 ? dow - 7 : dow);
148 
149 	/* Add weeks and days */
150 	return day + ((w - 1) * 7) + d;
151 }
152 
timelib_valid_time(timelib_sll h,timelib_sll i,timelib_sll s)153 int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
154 {
155 	if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
156 		return 0;
157 	}
158 	return 1;
159 }
160 
timelib_valid_date(timelib_sll y,timelib_sll m,timelib_sll d)161 int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
162 {
163 	if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
164 		return 0;
165 	}
166 	return 1;
167 }
168 #if 0
169 int main(void)
170 {
171 	printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
172 	printf("dow = %d\n", timelib_day_of_week(2005,  2, 19)); /* 6 */
173 }
174 #endif
175