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