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