xref: /PHP-5.5/ext/date/lib/tm2unixtime.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 /*                                    jan  feb  mrt  apr  may  jun  jul  aug  sep  oct  nov  dec */
24 static int month_tab_leap[12]     = {  -1,  30,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
25 static int month_tab[12]          = {   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
26 
27 /*                                    dec  jan  feb  mrt  apr  may  jun  jul  aug  sep  oct  nov  dec */
28 static int days_in_month_leap[13] = {  31,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
29 static int days_in_month[13]      = {  31,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
30 
do_range_limit(timelib_sll start,timelib_sll end,timelib_sll adj,timelib_sll * a,timelib_sll * b)31 static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
32 {
33 	if (*a < start) {
34 		*b -= (start - *a - 1) / adj + 1;
35 		*a += adj * ((start - *a - 1) / adj + 1);
36 	}
37 	if (*a >= end) {
38 		*b += *a / adj;
39 		*a -= adj * (*a / adj);
40 	}
41 }
42 
inc_month(timelib_sll * y,timelib_sll * m)43 static void inc_month(timelib_sll *y, timelib_sll *m)
44 {
45 	(*m)++;
46 	if (*m > 12) {
47 		*m -= 12;
48 		(*y)++;
49 	}
50 }
51 
dec_month(timelib_sll * y,timelib_sll * m)52 static void dec_month(timelib_sll *y, timelib_sll *m)
53 {
54 	(*m)--;
55 	if (*m < 1) {
56 		*m += 12;
57 		(*y)--;
58 	}
59 }
60 
do_range_limit_days_relative(timelib_sll * base_y,timelib_sll * base_m,timelib_sll * y,timelib_sll * m,timelib_sll * d,timelib_sll invert)61 static void do_range_limit_days_relative(timelib_sll *base_y, timelib_sll *base_m, timelib_sll *y, timelib_sll *m, timelib_sll *d, timelib_sll invert)
62 {
63 	timelib_sll leapyear;
64 	timelib_sll month, year;
65 	timelib_sll days;
66 
67 	do_range_limit(1, 13, 12, base_m, base_y);
68 
69 	year = *base_y;
70 	month = *base_m;
71 
72 /*
73 	printf( "S: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
74 */
75 	if (!invert) {
76 		while (*d < 0) {
77 			dec_month(&year, &month);
78 			leapyear = timelib_is_leap(year);
79 			days = leapyear ? days_in_month_leap[month] : days_in_month[month];
80 
81 			/* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
82 
83 			*d += days;
84 			(*m)--;
85 		}
86 	} else {
87 		while (*d < 0) {
88 			leapyear = timelib_is_leap(year);
89 			days = leapyear ? days_in_month_leap[month] : days_in_month[month];
90 
91 			/* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
92 
93 			*d += days;
94 			(*m)--;
95 			inc_month(&year, &month);
96 		}
97 	}
98 	/*
99 	printf( "E: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
100 	*/
101 }
102 
do_range_limit_days(timelib_sll * y,timelib_sll * m,timelib_sll * d)103 static int do_range_limit_days(timelib_sll *y, timelib_sll *m, timelib_sll *d)
104 {
105 	timelib_sll leapyear;
106 	timelib_sll days_this_month;
107 	timelib_sll last_month, last_year;
108 	timelib_sll days_last_month;
109 
110 	/* can jump an entire leap year period quickly */
111 	if (*d >= DAYS_PER_LYEAR_PERIOD || *d <= -DAYS_PER_LYEAR_PERIOD) {
112 		*y += YEARS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
113 		*d -= DAYS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
114 	}
115 
116 	do_range_limit(1, 13, 12, m, y);
117 
118 	leapyear = timelib_is_leap(*y);
119 	days_this_month = leapyear ? days_in_month_leap[*m] : days_in_month[*m];
120 	last_month = (*m) - 1;
121 
122 	if (last_month < 1) {
123 		last_month += 12;
124 		last_year = (*y) - 1;
125 	} else {
126 		last_year = (*y);
127 	}
128 	leapyear = timelib_is_leap(last_year);
129 	days_last_month = leapyear ? days_in_month_leap[last_month] : days_in_month[last_month];
130 
131 	if (*d <= 0) {
132 		*d += days_last_month;
133 		(*m)--;
134 		return 1;
135 	}
136 	if (*d > days_this_month) {
137 		*d -= days_this_month;
138 		(*m)++;
139 		return 1;
140 	}
141 	return 0;
142 }
143 
do_adjust_for_weekday(timelib_time * time)144 static void do_adjust_for_weekday(timelib_time* time)
145 {
146 	timelib_sll current_dow, difference;
147 
148 	current_dow = timelib_day_of_week(time->y, time->m, time->d);
149 	if (time->relative.weekday_behavior == 2)
150 	{
151 		if (time->relative.weekday == 0) {
152 			time->relative.weekday = 7;
153 		}
154 		time->d -= current_dow;
155 		time->d += time->relative.weekday;
156 		return;
157 	}
158 	difference = time->relative.weekday - current_dow;
159 	if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
160 		difference += 7;
161 	}
162 	if (time->relative.weekday >= 0) {
163 		time->d += difference;
164 	} else {
165 		time->d -= (7 - (abs(time->relative.weekday) - current_dow));
166 	}
167 	time->relative.have_weekday_relative = 0;
168 }
169 
timelib_do_rel_normalize(timelib_time * base,timelib_rel_time * rt)170 void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
171 {
172 	do_range_limit(0, 60, 60, &rt->s, &rt->i);
173 	do_range_limit(0, 60, 60, &rt->i, &rt->h);
174 	do_range_limit(0, 24, 24, &rt->h, &rt->d);
175 	do_range_limit(0, 12, 12, &rt->m, &rt->y);
176 
177 	do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
178 	do_range_limit(0, 12, 12, &rt->m, &rt->y);
179 }
180 
timelib_do_normalize(timelib_time * time)181 void timelib_do_normalize(timelib_time* time)
182 {
183 	if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->s, &time->i);
184 	if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->i, &time->h);
185 	if (time->s != TIMELIB_UNSET) do_range_limit(0, 24, 24, &time->h, &time->d);
186 	do_range_limit(1, 13, 12, &time->m, &time->y);
187 
188 	do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
189 	do_range_limit(1, 13, 12, &time->m, &time->y);
190 }
191 
do_adjust_relative(timelib_time * time)192 static void do_adjust_relative(timelib_time* time)
193 {
194 	if (time->relative.have_weekday_relative) {
195 		do_adjust_for_weekday(time);
196 	}
197 	timelib_do_normalize(time);
198 
199 	if (time->have_relative) {
200 		time->s += time->relative.s;
201 		time->i += time->relative.i;
202 		time->h += time->relative.h;
203 
204 		time->d += time->relative.d;
205 		time->m += time->relative.m;
206 		time->y += time->relative.y;
207 	}
208 
209 	switch (time->relative.first_last_day_of) {
210 		case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
211 			time->d = 1;
212 			break;
213 		case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
214 			time->d = 0;
215 			time->m++;
216 			break;
217 	}
218 
219 	timelib_do_normalize(time);
220 }
221 
do_adjust_special_weekday(timelib_time * time)222 static void do_adjust_special_weekday(timelib_time* time)
223 {
224 	timelib_sll count, dow, rem;
225 
226 	count = time->relative.special.amount;
227 	dow = timelib_day_of_week(time->y, time->m, time->d);
228 
229 	/* Add increments of 5 weekdays as a week, leaving the DOW unchanged. */
230 	time->d += (count / 5) * 7;
231 
232 	/* Deal with the remainder. */
233 	rem = (count % 5);
234 
235 	if (count > 0) {
236 		if (rem == 0) {
237 			/* Head back to Friday if we stop on the weekend. */
238 			if (dow == 0) {
239 				time->d -= 2;
240 			} else if (dow == 6) {
241 				time->d -= 1;
242 			}
243 		} else if (dow == 6) {
244 			/* We ended up on Saturday, but there's still work to do, so move
245 			 * to Sunday and continue from there. */
246 			time->d += 1;
247 		} else if (dow + rem > 5) {
248 			/* We're on a weekday, but we're going past Friday, so skip right
249 			 * over the weekend. */
250 			time->d += 2;
251 		}
252 	} else {
253 		/* Completely mirror the forward direction. This also covers the 0
254 		 * case, since if we start on the weekend, we want to move forward as
255 		 * if we stopped there while going backwards. */
256 		if (rem == 0) {
257 			if (dow == 6) {
258 				time->d += 2;
259 			} else if (dow == 0) {
260 				time->d += 1;
261 			}
262 		} else if (dow == 0) {
263 			time->d -= 1;
264 		} else if (dow + rem < 1) {
265 			time->d -= 2;
266 		}
267 	}
268 
269 	time->d += rem;
270 }
271 
do_adjust_special(timelib_time * time)272 static void do_adjust_special(timelib_time* time)
273 {
274 	if (time->relative.have_special_relative) {
275 		switch (time->relative.special.type) {
276 			case TIMELIB_SPECIAL_WEEKDAY:
277 				do_adjust_special_weekday(time);
278 				break;
279 		}
280 	}
281 	timelib_do_normalize(time);
282 	memset(&(time->relative.special), 0, sizeof(time->relative.special));
283 }
284 
do_adjust_special_early(timelib_time * time)285 static void do_adjust_special_early(timelib_time* time)
286 {
287 	if (time->relative.have_special_relative) {
288 		switch (time->relative.special.type) {
289 			case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
290 				time->d = 1;
291 				time->m += time->relative.m;
292 				time->relative.m = 0;
293 				break;
294 			case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
295 				time->d = 1;
296 				time->m += time->relative.m + 1;
297 				time->relative.m = 0;
298 				break;
299 		}
300 	}
301 	switch (time->relative.first_last_day_of) {
302 		case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
303 			time->d = 1;
304 			break;
305 		case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
306 			time->d = 0;
307 			time->m++;
308 			break;
309 	}
310 	timelib_do_normalize(time);
311 }
312 
do_years(timelib_sll year)313 static timelib_sll do_years(timelib_sll year)
314 {
315 	timelib_sll i;
316 	timelib_sll res = 0;
317 	timelib_sll eras;
318 
319 	eras = (year - 1970) / 40000;
320 	if (eras != 0) {
321 		year = year - (eras * 40000);
322 		res += (SECS_PER_ERA * eras * 100);
323 	}
324 
325 	if (year >= 1970) {
326 		for (i = year - 1; i >= 1970; i--) {
327 			if (timelib_is_leap(i)) {
328 				res += (DAYS_PER_LYEAR * SECS_PER_DAY);
329 			} else {
330 				res += (DAYS_PER_YEAR * SECS_PER_DAY);
331 			}
332 		}
333 	} else {
334 		for (i = 1969; i >= year; i--) {
335 			if (timelib_is_leap(i)) {
336 				res -= (DAYS_PER_LYEAR * SECS_PER_DAY);
337 			} else {
338 				res -= (DAYS_PER_YEAR * SECS_PER_DAY);
339 			}
340 		}
341 	}
342 	return res;
343 }
344 
do_months(timelib_ull month,timelib_ull year)345 static timelib_sll do_months(timelib_ull month, timelib_ull year)
346 {
347 	if (timelib_is_leap(year)) {
348 		return ((month_tab_leap[month - 1] + 1) * SECS_PER_DAY);
349 	} else {
350 		return ((month_tab[month - 1]) * SECS_PER_DAY);
351 	}
352 }
353 
do_days(timelib_ull day)354 static timelib_sll do_days(timelib_ull day)
355 {
356 	return ((day - 1) * SECS_PER_DAY);
357 }
358 
do_time(timelib_ull hour,timelib_ull minute,timelib_ull second)359 static timelib_sll do_time(timelib_ull hour, timelib_ull minute, timelib_ull second)
360 {
361 	timelib_sll res = 0;
362 
363 	res += hour * 3600;
364 	res += minute * 60;
365 	res += second;
366 	return res;
367 }
368 
do_adjust_timezone(timelib_time * tz,timelib_tzinfo * tzi)369 static timelib_sll do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
370 {
371 	switch (tz->zone_type) {
372 		case TIMELIB_ZONETYPE_OFFSET:
373 
374 			tz->is_localtime = 1;
375 			return tz->z * 60;
376 			break;
377 
378 		case TIMELIB_ZONETYPE_ABBR: {
379 			timelib_sll tmp;
380 
381 			tz->is_localtime = 1;
382 			tmp = tz->z;
383 			tmp -= tz->dst * 60;
384 			tmp *= 60;
385 			return tmp;
386 			}
387 			break;
388 
389 		case TIMELIB_ZONETYPE_ID:
390 			tzi = tz->tz_info;
391 			/* Break intentionally missing */
392 
393 		default:
394 			/* No timezone in struct, fallback to reference if possible */
395 			if (tzi) {
396 				timelib_time_offset *before, *after;
397 				timelib_sll          tmp;
398 				int                  in_transistion;
399 
400 				tz->is_localtime = 1;
401 				before = timelib_get_time_zone_info(tz->sse, tzi);
402 				after = timelib_get_time_zone_info(tz->sse - before->offset, tzi);
403 				timelib_set_timezone(tz, tzi);
404 
405 				in_transistion = (
406 					((tz->sse - after->offset) >= (after->transistion_time + (before->offset - after->offset))) &&
407 					((tz->sse - after->offset) < after->transistion_time)
408 				);
409 
410 				if ((before->offset != after->offset) && !in_transistion) {
411 					tmp = -after->offset;
412 				} else {
413 					tmp = -tz->z;
414 				}
415 				timelib_time_offset_dtor(before);
416 				timelib_time_offset_dtor(after);
417 
418 				{
419 					timelib_time_offset *gmt_offset;
420 
421 					gmt_offset = timelib_get_time_zone_info(tz->sse + tmp, tzi);
422 					tz->z = gmt_offset->offset;
423 
424 					tz->dst = gmt_offset->is_dst;
425 					if (tz->tz_abbr) {
426 						free(tz->tz_abbr);
427 					}
428 					tz->tz_abbr = strdup(gmt_offset->abbr);
429 					timelib_time_offset_dtor(gmt_offset);
430 				}
431 				return tmp;
432 			}
433 	}
434 	return 0;
435 }
436 
timelib_update_ts(timelib_time * time,timelib_tzinfo * tzi)437 void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
438 {
439 	timelib_sll res = 0;
440 
441 	do_adjust_special_early(time);
442 	do_adjust_relative(time);
443 	do_adjust_special(time);
444 	res += do_years(time->y);
445 	res += do_months(time->m, time->y);
446 	res += do_days(time->d);
447 	res += do_time(time->h, time->i, time->s);
448 	time->sse = res;
449 
450 	res += do_adjust_timezone(time, tzi);
451 	time->sse = res;
452 
453 	time->sse_uptodate = 1;
454 	time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = 0;
455 }
456 
457 #if 0
458 int main(void)
459 {
460 	timelib_sll res;
461 	timelib_time time;
462 
463 	time = timelib_strtotime("10 Feb 2005 06:07:03 PM CET"); /* 1108055223 */
464 	printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
465 		time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
466 	if (time.have_relative) {
467 		printf ("%3dY %3dM %3dD / %3dH %3dM %3dS",
468 			time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
469 	}
470 	if (time.have_weekday_relative) {
471 		printf (" / %d", time.relative.weekday);
472 	}
473 	res = time2unixtime(&time);
474 	printf("%Ld\n", res);
475 
476 	return 0;
477 }
478 #endif
479