1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2015-2019 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 /* dec jan feb mrt apr may jun jul aug sep oct nov dec */
29 static int days_in_month_leap[13] = { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
30 static int days_in_month[13] = { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
31
do_range_limit(timelib_sll start,timelib_sll end,timelib_sll adj,timelib_sll * a,timelib_sll * b)32 static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
33 {
34 if (*a < start) {
35 /* We calculate 'a + 1' first as 'start - *a - 1' causes an int64_t overflows if *a is
36 * LONG_MIN. 'start' is 0 in this context, and '0 - LONG_MIN > LONG_MAX'. */
37 timelib_sll a_plus_1 = *a + 1;
38
39 *b -= (start - a_plus_1) / adj + 1;
40
41 /* This code add the extra 'adj' separately, as otherwise this can overflow int64_t in
42 * situations where *b is near LONG_MIN. */
43 *a += adj * ((start - a_plus_1) / adj);
44 *a += adj;
45 }
46 if (*a >= end) {
47 *b += *a / adj;
48 *a -= adj * (*a / adj);
49 }
50 }
51
inc_month(timelib_sll * y,timelib_sll * m)52 static void inc_month(timelib_sll *y, timelib_sll *m)
53 {
54 (*m)++;
55 if (*m > 12) {
56 *m -= 12;
57 (*y)++;
58 }
59 }
60
dec_month(timelib_sll * y,timelib_sll * m)61 static void dec_month(timelib_sll *y, timelib_sll *m)
62 {
63 (*m)--;
64 if (*m < 1) {
65 *m += 12;
66 (*y)--;
67 }
68 }
69
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)70 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)
71 {
72 timelib_sll leapyear;
73 timelib_sll month, year;
74 timelib_sll days;
75
76 do_range_limit(1, 13, 12, base_m, base_y);
77
78 year = *base_y;
79 month = *base_m;
80
81 /*
82 printf( "S: Y%d M%d %d %d %d %d\n", year, month, *y, *m, *d, days);
83 */
84 if (!invert) {
85 while (*d < 0) {
86 dec_month(&year, &month);
87 leapyear = timelib_is_leap(year);
88 days = leapyear ? days_in_month_leap[month] : days_in_month[month];
89
90 /* printf( "I Y%d M%d %d %d %d %d\n", year, month, *y, *m, *d, days); */
91
92 *d += days;
93 (*m)--;
94 }
95 } else {
96 while (*d < 0) {
97 leapyear = timelib_is_leap(year);
98 days = leapyear ? days_in_month_leap[month] : days_in_month[month];
99
100 /* printf( "I Y%d M%d %d %d %d %d\n", year, month, *y, *m, *d, days); */
101
102 *d += days;
103 (*m)--;
104 inc_month(&year, &month);
105 }
106 }
107 /*
108 printf( "E: Y%d M%d %d %d %d %d\n", year, month, *y, *m, *d, days);
109 */
110 }
111
do_range_limit_days(timelib_sll * y,timelib_sll * m,timelib_sll * d)112 static int do_range_limit_days(timelib_sll *y, timelib_sll *m, timelib_sll *d)
113 {
114 timelib_sll leapyear;
115 timelib_sll previous_month, previous_year;
116 timelib_sll days_in_previous_month;
117 int retval = 0;
118 int *days_per_month_current_year;
119
120 /* can jump an entire leap year period quickly */
121 if (*d >= DAYS_PER_ERA || *d <= -DAYS_PER_ERA) {
122 *y += YEARS_PER_ERA * (*d / DAYS_PER_ERA);
123 *d -= DAYS_PER_ERA * (*d / DAYS_PER_ERA);
124 }
125
126 do_range_limit(1, 13, 12, m, y);
127
128 leapyear = timelib_is_leap(*y);
129 days_per_month_current_year = leapyear ? days_in_month_leap : days_in_month;
130
131 while (*d <= 0 && *m > 0) {
132 previous_month = (*m) - 1;
133 if (previous_month < 1) {
134 previous_month += 12;
135 previous_year = (*y) - 1;
136 } else {
137 previous_year = (*y);
138 }
139 leapyear = timelib_is_leap(previous_year);
140 days_in_previous_month = leapyear ? days_in_month_leap[previous_month] : days_in_month[previous_month];
141
142 *d += days_in_previous_month;
143 (*m)--;
144 retval = 1;
145 }
146 while (*d > 0 && *m <= 12 && *d > days_per_month_current_year[*m]) {
147 *d -= days_per_month_current_year[*m];
148 (*m)++;
149 retval = 1;
150 }
151 return retval;
152 }
153
do_adjust_for_weekday(timelib_time * time)154 static void do_adjust_for_weekday(timelib_time* time)
155 {
156 timelib_sll current_dow, difference;
157
158 current_dow = timelib_day_of_week(time->y, time->m, time->d);
159 if (time->relative.weekday_behavior == 2)
160 {
161 /* To make "this week" work, where the current DOW is a "sunday" */
162 if (current_dow == 0 && time->relative.weekday != 0) {
163 time->relative.weekday -= 7;
164 }
165
166 /* To make "sunday this week" work, where the current DOW is not a
167 * "sunday" */
168 if (time->relative.weekday == 0 && current_dow != 0) {
169 time->relative.weekday = 7;
170 }
171
172 time->d -= current_dow;
173 time->d += time->relative.weekday;
174 return;
175 }
176 difference = time->relative.weekday - current_dow;
177 if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
178 difference += 7;
179 }
180 if (time->relative.weekday >= 0) {
181 time->d += difference;
182 } else {
183 time->d -= (7 - (abs(time->relative.weekday) - current_dow));
184 }
185 time->relative.have_weekday_relative = 0;
186 }
187
timelib_do_rel_normalize(timelib_time * base,timelib_rel_time * rt)188 void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
189 {
190 do_range_limit(0, 1000000, 1000000, &rt->us, &rt->s);
191 do_range_limit(0, 60, 60, &rt->s, &rt->i);
192 do_range_limit(0, 60, 60, &rt->i, &rt->h);
193 do_range_limit(0, 24, 24, &rt->h, &rt->d);
194 do_range_limit(0, 12, 12, &rt->m, &rt->y);
195
196 do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
197 do_range_limit(0, 12, 12, &rt->m, &rt->y);
198 }
199
magic_date_calc(timelib_time * time)200 static void magic_date_calc(timelib_time *time)
201 {
202 timelib_sll y, ddd, mi, mm, dd, g;
203
204 /* The algorithm doesn't work before the year 1 */
205 if (time->d < -719498) {
206 return;
207 }
208
209 g = time->d + HINNANT_EPOCH_SHIFT - 1;
210
211 y = (10000 * g + 14780) / 3652425;
212 ddd = g - ((365*y) + (y/4) - (y/100) + (y/400));
213 if (ddd < 0) {
214 y--;
215 ddd = g - ((365*y) + (y/4) - (y/100) + (y/400));
216 }
217 mi = (100 * ddd + 52) / 3060;
218 mm = ((mi + 2) % 12) + 1;
219 y = y + (mi + 2) / 12;
220 dd = ddd - ((mi * 306 + 5) / 10) + 1;
221 time->y = y;
222 time->m = mm;
223 time->d = dd;
224 }
225
timelib_do_normalize(timelib_time * time)226 void timelib_do_normalize(timelib_time* time)
227 {
228 if (time->us != TIMELIB_UNSET) do_range_limit(0, 1000000, 1000000, &time->us, &time->s);
229 if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->s, &time->i);
230 if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->i, &time->h);
231 if (time->s != TIMELIB_UNSET) do_range_limit(0, 24, 24, &time->h, &time->d);
232 do_range_limit(1, 13, 12, &time->m, &time->y);
233
234 /* Short cut if we're doing things against the Epoch */
235 if (time->y == 1970 && time->m == 1 && time->d != 1) {
236 magic_date_calc(time);
237 }
238
239 do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
240 do_range_limit(1, 13, 12, &time->m, &time->y);
241 }
242
do_adjust_relative(timelib_time * time)243 static void do_adjust_relative(timelib_time* time)
244 {
245 if (time->relative.have_weekday_relative) {
246 do_adjust_for_weekday(time);
247 }
248 timelib_do_normalize(time);
249
250 if (time->have_relative) {
251 time->us += time->relative.us;
252
253 time->s += time->relative.s;
254 time->i += time->relative.i;
255 time->h += time->relative.h;
256
257 time->d += time->relative.d;
258 time->m += time->relative.m;
259 time->y += time->relative.y;
260 }
261
262 switch (time->relative.first_last_day_of) {
263 case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
264 time->d = 1;
265 break;
266 case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
267 time->d = 0;
268 time->m++;
269 break;
270 }
271
272 timelib_do_normalize(time);
273 }
274
do_adjust_special_weekday(timelib_time * time)275 static void do_adjust_special_weekday(timelib_time* time)
276 {
277 timelib_sll count, dow, rem;
278
279 count = time->relative.special.amount;
280 dow = timelib_day_of_week(time->y, time->m, time->d);
281
282 /* Add increments of 5 weekdays as a week, leaving the DOW unchanged. */
283 time->d += (count / 5) * 7;
284
285 /* Deal with the remainder. */
286 rem = (count % 5);
287
288 if (count > 0) {
289 if (rem == 0) {
290 /* Head back to Friday if we stop on the weekend. */
291 if (dow == 0) {
292 time->d -= 2;
293 } else if (dow == 6) {
294 time->d -= 1;
295 }
296 } else if (dow == 6) {
297 /* We ended up on Saturday, but there's still work to do, so move
298 * to Sunday and continue from there. */
299 time->d += 1;
300 } else if (dow + rem > 5) {
301 /* We're on a weekday, but we're going past Friday, so skip right
302 * over the weekend. */
303 time->d += 2;
304 }
305 } else {
306 /* Completely mirror the forward direction. This also covers the 0
307 * case, since if we start on the weekend, we want to move forward as
308 * if we stopped there while going backwards. */
309 if (rem == 0) {
310 if (dow == 6) {
311 time->d += 2;
312 } else if (dow == 0) {
313 time->d += 1;
314 }
315 } else if (dow == 0) {
316 time->d -= 1;
317 } else if (dow + rem < 1) {
318 time->d -= 2;
319 }
320 }
321
322 time->d += rem;
323 }
324
do_adjust_special(timelib_time * time)325 static void do_adjust_special(timelib_time* time)
326 {
327 if (time->relative.have_special_relative) {
328 switch (time->relative.special.type) {
329 case TIMELIB_SPECIAL_WEEKDAY:
330 do_adjust_special_weekday(time);
331 break;
332 }
333 }
334 timelib_do_normalize(time);
335 memset(&(time->relative.special), 0, sizeof(time->relative.special));
336 }
337
do_adjust_special_early(timelib_time * time)338 static void do_adjust_special_early(timelib_time* time)
339 {
340 if (time->relative.have_special_relative) {
341 switch (time->relative.special.type) {
342 case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
343 time->d = 1;
344 time->m += time->relative.m;
345 time->relative.m = 0;
346 break;
347 case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
348 time->d = 1;
349 time->m += time->relative.m + 1;
350 time->relative.m = 0;
351 break;
352 }
353 }
354 switch (time->relative.first_last_day_of) {
355 case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
356 time->d = 1;
357 break;
358 case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
359 time->d = 0;
360 time->m++;
361 break;
362 }
363 timelib_do_normalize(time);
364 }
365
do_adjust_timezone(timelib_time * tz,timelib_tzinfo * tzi)366 static void do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
367 {
368 switch (tz->zone_type) {
369 case TIMELIB_ZONETYPE_OFFSET:
370
371 tz->is_localtime = 1;
372 tz->sse += -tz->z;
373 return;
374
375 case TIMELIB_ZONETYPE_ABBR: {
376
377 tz->is_localtime = 1;
378 tz->sse += (-tz->z - tz->dst * SECS_PER_HOUR);
379 return;
380 }
381
382 case TIMELIB_ZONETYPE_ID:
383 tzi = tz->tz_info;
384 TIMELIB_BREAK_INTENTIONALLY_MISSING
385
386 default: {
387 /* No timezone in struct, fallback to reference if possible */
388 int32_t current_offset = 0;
389 timelib_sll current_transition_time = 0;
390 unsigned int current_is_dst = 0;
391 int32_t after_offset = 0;
392 timelib_sll after_transition_time = 0;
393 timelib_sll adjustment;
394 int in_transition;
395 int32_t actual_offset;
396 timelib_sll actual_transition_time;
397
398 if (!tzi) {
399 return;
400 }
401
402 timelib_get_time_zone_offset_info(tz->sse, tzi, ¤t_offset, ¤t_transition_time, ¤t_is_dst);
403 timelib_get_time_zone_offset_info(tz->sse - current_offset, tzi, &after_offset, &after_transition_time, NULL);
404 actual_offset = after_offset;
405 actual_transition_time = after_transition_time;
406 if (current_offset == after_offset && tz->have_zone) {
407 /* Make sure we're not missing a DST change because we don't know the actual offset yet */
408 if (current_offset >= 0 && tz->dst && !current_is_dst) {
409 /* Timezone or its DST at or east of UTC, so the local time, interpreted as UTC, leaves DST (as defined in the actual timezone) before the actual local time */
410 int32_t earlier_offset;
411 timelib_sll earlier_transition_time;
412 timelib_get_time_zone_offset_info(tz->sse - current_offset - 7200, tzi, &earlier_offset, &earlier_transition_time, NULL);
413 if ((earlier_offset != after_offset) && (tz->sse - earlier_offset < after_transition_time)) {
414 /* Looking behind a bit clarified the actual offset to use */
415 actual_offset = earlier_offset;
416 actual_transition_time = earlier_transition_time;
417 }
418 } else if (current_offset <= 0 && current_is_dst && !tz->dst) {
419 /* Timezone west of UTC, so the local time, interpreted as UTC, leaves DST (as defined in the actual timezone) after the actual local time */
420 int32_t later_offset;
421 timelib_sll later_transition_time;
422 timelib_get_time_zone_offset_info(tz->sse - current_offset + 7200, tzi, &later_offset, &later_transition_time, NULL);
423 if ((later_offset != after_offset) && (tz->sse - later_offset >= later_transition_time)) {
424 /* Looking ahead a bit clarified the actual offset to use */
425 actual_offset = later_offset;
426 actual_transition_time = later_transition_time;
427 }
428 }
429 }
430
431 tz->is_localtime = 1;
432
433 in_transition = (
434 actual_transition_time != INT64_MIN &&
435 ((tz->sse - actual_offset) >= (actual_transition_time + (current_offset - actual_offset))) &&
436 ((tz->sse - actual_offset) < actual_transition_time)
437 );
438
439 if ((current_offset != actual_offset) && !in_transition) {
440 adjustment = -actual_offset;
441 } else {
442 adjustment = -current_offset;
443 }
444
445 tz->sse += adjustment;
446 timelib_set_timezone(tz, tzi);
447 return;
448 }
449 }
450 return;
451 }
452
timelib_epoch_days_from_time(timelib_time * time)453 timelib_sll timelib_epoch_days_from_time(timelib_time *time)
454 {
455 timelib_sll y = time->y; // Make copy, as we don't want to change the original one
456 timelib_sll era, year_of_era, day_of_year, day_of_era;
457
458 y -= time->m <= 2;
459 era = (y >= 0 ? y : y - 399) / YEARS_PER_ERA;
460 year_of_era = y - era * YEARS_PER_ERA; // [0, 399]
461 day_of_year = (153 * (time->m + (time->m > 2 ? -3 : 9)) + 2)/5 + time->d - 1; // [0, 365]
462 day_of_era = year_of_era * DAYS_PER_YEAR + year_of_era / 4 - year_of_era / 100 + day_of_year; // [0, 146096]
463
464 return era * DAYS_PER_ERA + day_of_era - HINNANT_EPOCH_SHIFT;
465 }
466
timelib_update_ts(timelib_time * time,timelib_tzinfo * tzi)467 void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
468 {
469 do_adjust_special_early(time);
470 do_adjust_relative(time);
471 do_adjust_special(time);
472
473 /* You might be wondering, why this code does this in two steps. This is because
474 * timelib_epoch_days_from_time(time) * SECS_PER_DAY with the lowest limit of
475 * timelib_epoch_days_from_time() is less than the range of an int64_t. This then overflows. In
476 * order to be able to still allow for any time in that day that only halfly fits in the int64_t
477 * range, we add the time element first, which is always positive, and then twice half the value
478 * of the earliest day as expressed as unix timestamp. */
479 time->sse = timelib_hms_to_seconds(time->h, time->i, time->s);
480 time->sse += timelib_epoch_days_from_time(time) * (SECS_PER_DAY / 2);
481 time->sse += timelib_epoch_days_from_time(time) * (SECS_PER_DAY / 2);
482
483 // This modifies time->sse, if needed
484 do_adjust_timezone(time, tzi);
485
486 time->sse_uptodate = 1;
487 time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = time->relative.first_last_day_of = 0;
488 }
489
490 #if 0
491 int main(void)
492 {
493 timelib_sll res;
494 timelib_time time;
495
496 time = timelib_strtotime("10 Feb 2005 06:07:03 PM CET"); /* 1108055223 */
497 printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
498 time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
499 if (time.have_relative) {
500 printf ("%3dY %3dM %3dD / %3dH %3dM %3dS",
501 time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
502 }
503 if (time.have_weekday_relative) {
504 printf (" / %d", time.relative.weekday);
505 }
506 res = time2unixtime(&time);
507 printf("%Ld\n", res);
508
509 return 0;
510 }
511 #endif
512