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