Line data Source code
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 :
32 27336 : static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
33 : {
34 27336 : 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 0 : timelib_sll a_plus_1 = *a + 1;
38 :
39 0 : *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 0 : *a += adj * ((start - a_plus_1) / adj);
44 0 : *a += adj;
45 : }
46 27336 : if (*a >= end) {
47 0 : *b += *a / adj;
48 0 : *a -= adj * (*a / adj);
49 : }
50 27336 : }
51 :
52 0 : static void inc_month(timelib_sll *y, timelib_sll *m)
53 : {
54 0 : (*m)++;
55 0 : if (*m > 12) {
56 0 : *m -= 12;
57 0 : (*y)++;
58 : }
59 0 : }
60 :
61 0 : static void dec_month(timelib_sll *y, timelib_sll *m)
62 : {
63 0 : (*m)--;
64 0 : if (*m < 1) {
65 0 : *m += 12;
66 0 : (*y)--;
67 : }
68 0 : }
69 :
70 0 : 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 0 : do_range_limit(1, 13, 12, base_m, base_y);
77 :
78 0 : year = *base_y;
79 0 : 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 0 : if (!invert) {
85 0 : while (*d < 0) {
86 0 : dec_month(&year, &month);
87 0 : leapyear = timelib_is_leap(year);
88 0 : 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 0 : *d += days;
93 0 : (*m)--;
94 : }
95 : } else {
96 0 : while (*d < 0) {
97 0 : leapyear = timelib_is_leap(year);
98 0 : 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 0 : *d += days;
103 0 : (*m)--;
104 0 : 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 0 : }
111 :
112 3912 : 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 3912 : int retval = 0;
118 : int *days_per_month_current_year;
119 :
120 : /* can jump an entire leap year period quickly */
121 3912 : if (*d >= DAYS_PER_ERA || *d <= -DAYS_PER_ERA) {
122 0 : *y += YEARS_PER_ERA * (*d / DAYS_PER_ERA);
123 0 : *d -= DAYS_PER_ERA * (*d / DAYS_PER_ERA);
124 : }
125 :
126 3912 : do_range_limit(1, 13, 12, m, y);
127 :
128 3912 : leapyear = timelib_is_leap(*y);
129 3912 : days_per_month_current_year = leapyear ? days_in_month_leap : days_in_month;
130 :
131 3920 : while (*d <= 0 && *m > 0) {
132 8 : previous_month = (*m) - 1;
133 8 : if (previous_month < 1) {
134 0 : previous_month += 12;
135 0 : previous_year = (*y) - 1;
136 : } else {
137 8 : previous_year = (*y);
138 : }
139 8 : leapyear = timelib_is_leap(previous_year);
140 8 : days_in_previous_month = leapyear ? days_in_month_leap[previous_month] : days_in_month[previous_month];
141 :
142 8 : *d += days_in_previous_month;
143 8 : (*m)--;
144 8 : retval = 1;
145 : }
146 3912 : while (*d > 0 && *m <= 12 && *d > days_per_month_current_year[*m]) {
147 0 : *d -= days_per_month_current_year[*m];
148 0 : (*m)++;
149 0 : retval = 1;
150 : }
151 3912 : return retval;
152 : }
153 :
154 0 : static void do_adjust_for_weekday(timelib_time* time)
155 : {
156 : timelib_sll current_dow, difference;
157 :
158 0 : current_dow = timelib_day_of_week(time->y, time->m, time->d);
159 0 : if (time->relative.weekday_behavior == 2)
160 : {
161 : /* To make "this week" work, where the current DOW is a "sunday" */
162 0 : if (current_dow == 0 && time->relative.weekday != 0) {
163 0 : time->relative.weekday -= 7;
164 : }
165 :
166 : /* To make "sunday this week" work, where the current DOW is not a
167 : * "sunday" */
168 0 : if (time->relative.weekday == 0 && current_dow != 0) {
169 0 : time->relative.weekday = 7;
170 : }
171 :
172 0 : time->d -= current_dow;
173 0 : time->d += time->relative.weekday;
174 0 : return;
175 : }
176 0 : difference = time->relative.weekday - current_dow;
177 0 : if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
178 0 : difference += 7;
179 : }
180 0 : if (time->relative.weekday >= 0) {
181 0 : time->d += difference;
182 : } else {
183 0 : time->d -= (7 - (abs(time->relative.weekday) - current_dow));
184 : }
185 0 : time->relative.have_weekday_relative = 0;
186 : }
187 :
188 0 : void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
189 : {
190 0 : do_range_limit(0, 1000000, 1000000, &rt->us, &rt->s);
191 0 : do_range_limit(0, 60, 60, &rt->s, &rt->i);
192 0 : do_range_limit(0, 60, 60, &rt->i, &rt->h);
193 0 : do_range_limit(0, 24, 24, &rt->h, &rt->d);
194 0 : do_range_limit(0, 12, 12, &rt->m, &rt->y);
195 :
196 0 : do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
197 0 : do_range_limit(0, 12, 12, &rt->m, &rt->y);
198 0 : }
199 :
200 0 : 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 0 : if (time->d < -719498) {
206 0 : return;
207 : }
208 :
209 0 : g = time->d + HINNANT_EPOCH_SHIFT - 1;
210 :
211 0 : y = (10000 * g + 14780) / 3652425;
212 0 : ddd = g - ((365*y) + (y/4) - (y/100) + (y/400));
213 0 : if (ddd < 0) {
214 0 : y--;
215 0 : ddd = g - ((365*y) + (y/4) - (y/100) + (y/400));
216 : }
217 0 : mi = (100 * ddd + 52) / 3060;
218 0 : mm = ((mi + 2) % 12) + 1;
219 0 : y = y + (mi + 2) / 12;
220 0 : dd = ddd - ((mi * 306 + 5) / 10) + 1;
221 0 : time->y = y;
222 0 : time->m = mm;
223 0 : time->d = dd;
224 : }
225 :
226 3904 : void timelib_do_normalize(timelib_time* time)
227 : {
228 3904 : if (time->us != TIMELIB_UNSET) do_range_limit(0, 1000000, 1000000, &time->us, &time->s);
229 3904 : if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->s, &time->i);
230 3904 : if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->i, &time->h);
231 3904 : if (time->s != TIMELIB_UNSET) do_range_limit(0, 24, 24, &time->h, &time->d);
232 3904 : do_range_limit(1, 13, 12, &time->m, &time->y);
233 :
234 : /* Short cut if we're doing things against the Epoch */
235 3904 : if (time->y == 1970 && time->m == 1 && time->d != 1) {
236 0 : magic_date_calc(time);
237 : }
238 :
239 3912 : do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
240 3904 : do_range_limit(1, 13, 12, &time->m, &time->y);
241 3904 : }
242 :
243 976 : static void do_adjust_relative(timelib_time* time)
244 : {
245 976 : if (time->relative.have_weekday_relative) {
246 0 : do_adjust_for_weekday(time);
247 : }
248 976 : timelib_do_normalize(time);
249 :
250 976 : if (time->have_relative) {
251 368 : time->us += time->relative.us;
252 :
253 368 : time->s += time->relative.s;
254 368 : time->i += time->relative.i;
255 368 : time->h += time->relative.h;
256 :
257 368 : time->d += time->relative.d;
258 368 : time->m += time->relative.m;
259 368 : time->y += time->relative.y;
260 : }
261 :
262 976 : switch (time->relative.first_last_day_of) {
263 0 : case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
264 0 : time->d = 1;
265 0 : break;
266 0 : case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
267 0 : time->d = 0;
268 0 : time->m++;
269 0 : break;
270 : }
271 :
272 976 : timelib_do_normalize(time);
273 976 : }
274 :
275 0 : static void do_adjust_special_weekday(timelib_time* time)
276 : {
277 : timelib_sll count, dow, rem;
278 :
279 0 : count = time->relative.special.amount;
280 0 : 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 0 : time->d += (count / 5) * 7;
284 :
285 : /* Deal with the remainder. */
286 0 : rem = (count % 5);
287 :
288 0 : if (count > 0) {
289 0 : if (rem == 0) {
290 : /* Head back to Friday if we stop on the weekend. */
291 0 : if (dow == 0) {
292 0 : time->d -= 2;
293 0 : } else if (dow == 6) {
294 0 : time->d -= 1;
295 : }
296 0 : } 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 0 : time->d += 1;
300 0 : } 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 0 : 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 0 : if (rem == 0) {
310 0 : if (dow == 6) {
311 0 : time->d += 2;
312 0 : } else if (dow == 0) {
313 0 : time->d += 1;
314 : }
315 0 : } else if (dow == 0) {
316 0 : time->d -= 1;
317 0 : } else if (dow + rem < 1) {
318 0 : time->d -= 2;
319 : }
320 : }
321 :
322 0 : time->d += rem;
323 0 : }
324 :
325 976 : static void do_adjust_special(timelib_time* time)
326 : {
327 976 : if (time->relative.have_special_relative) {
328 0 : switch (time->relative.special.type) {
329 0 : case TIMELIB_SPECIAL_WEEKDAY:
330 0 : do_adjust_special_weekday(time);
331 0 : break;
332 : }
333 : }
334 976 : timelib_do_normalize(time);
335 976 : memset(&(time->relative.special), 0, sizeof(time->relative.special));
336 976 : }
337 :
338 976 : static void do_adjust_special_early(timelib_time* time)
339 : {
340 976 : if (time->relative.have_special_relative) {
341 0 : switch (time->relative.special.type) {
342 0 : case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
343 0 : time->d = 1;
344 0 : time->m += time->relative.m;
345 0 : time->relative.m = 0;
346 0 : break;
347 0 : case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
348 0 : time->d = 1;
349 0 : time->m += time->relative.m + 1;
350 0 : time->relative.m = 0;
351 0 : break;
352 : }
353 : }
354 976 : switch (time->relative.first_last_day_of) {
355 0 : case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
356 0 : time->d = 1;
357 0 : break;
358 0 : case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
359 0 : time->d = 0;
360 0 : time->m++;
361 0 : break;
362 : }
363 976 : timelib_do_normalize(time);
364 976 : }
365 :
366 976 : static void do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
367 : {
368 976 : switch (tz->zone_type) {
369 0 : case TIMELIB_ZONETYPE_OFFSET:
370 :
371 0 : tz->is_localtime = 1;
372 0 : tz->sse += -tz->z;
373 0 : return;
374 :
375 0 : case TIMELIB_ZONETYPE_ABBR: {
376 :
377 0 : tz->is_localtime = 1;
378 0 : tz->sse += (-tz->z - tz->dst * SECS_PER_HOUR);
379 0 : return;
380 : }
381 :
382 340 : case TIMELIB_ZONETYPE_ID:
383 340 : tzi = tz->tz_info;
384 : TIMELIB_BREAK_INTENTIONALLY_MISSING
385 :
386 976 : default: {
387 : /* No timezone in struct, fallback to reference if possible */
388 976 : int32_t current_offset = 0;
389 976 : timelib_sll current_transition_time = 0;
390 976 : unsigned int current_is_dst = 0;
391 976 : int32_t after_offset = 0;
392 976 : 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 976 : if (!tzi) {
399 636 : return;
400 : }
401 :
402 340 : timelib_get_time_zone_offset_info(tz->sse, tzi, ¤t_offset, ¤t_transition_time, ¤t_is_dst);
403 340 : timelib_get_time_zone_offset_info(tz->sse - current_offset, tzi, &after_offset, &after_transition_time, NULL);
404 340 : actual_offset = after_offset;
405 340 : actual_transition_time = after_transition_time;
406 340 : 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 340 : 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 0 : timelib_get_time_zone_offset_info(tz->sse - current_offset - 7200, tzi, &earlier_offset, &earlier_transition_time, NULL);
413 0 : if ((earlier_offset != after_offset) && (tz->sse - earlier_offset < after_transition_time)) {
414 : /* Looking behind a bit clarified the actual offset to use */
415 0 : actual_offset = earlier_offset;
416 0 : actual_transition_time = earlier_transition_time;
417 : }
418 340 : } 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 0 : timelib_get_time_zone_offset_info(tz->sse - current_offset + 7200, tzi, &later_offset, &later_transition_time, NULL);
423 0 : if ((later_offset != after_offset) && (tz->sse - later_offset >= later_transition_time)) {
424 : /* Looking ahead a bit clarified the actual offset to use */
425 0 : actual_offset = later_offset;
426 0 : actual_transition_time = later_transition_time;
427 : }
428 : }
429 : }
430 :
431 340 : tz->is_localtime = 1;
432 :
433 340 : in_transition = (
434 36 : actual_transition_time != INT64_MIN &&
435 376 : ((tz->sse - actual_offset) >= (actual_transition_time + (current_offset - actual_offset))) &&
436 36 : ((tz->sse - actual_offset) < actual_transition_time)
437 : );
438 :
439 340 : if ((current_offset != actual_offset) && !in_transition) {
440 0 : adjustment = -actual_offset;
441 : } else {
442 340 : adjustment = -current_offset;
443 : }
444 :
445 340 : tz->sse += adjustment;
446 340 : timelib_set_timezone(tz, tzi);
447 340 : return;
448 : }
449 : }
450 : return;
451 : }
452 :
453 1952 : timelib_sll timelib_epoch_days_from_time(timelib_time *time)
454 : {
455 1952 : 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 1952 : y -= time->m <= 2;
459 1952 : era = (y >= 0 ? y : y - 399) / YEARS_PER_ERA;
460 1952 : year_of_era = y - era * YEARS_PER_ERA; // [0, 399]
461 1952 : day_of_year = (153 * (time->m + (time->m > 2 ? -3 : 9)) + 2)/5 + time->d - 1; // [0, 365]
462 1952 : day_of_era = year_of_era * DAYS_PER_YEAR + year_of_era / 4 - year_of_era / 100 + day_of_year; // [0, 146096]
463 :
464 1952 : return era * DAYS_PER_ERA + day_of_era - HINNANT_EPOCH_SHIFT;
465 : }
466 :
467 976 : void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
468 : {
469 976 : do_adjust_special_early(time);
470 976 : do_adjust_relative(time);
471 976 : 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 976 : time->sse = timelib_hms_to_seconds(time->h, time->i, time->s);
480 976 : time->sse += timelib_epoch_days_from_time(time) * (SECS_PER_DAY / 2);
481 976 : time->sse += timelib_epoch_days_from_time(time) * (SECS_PER_DAY / 2);
482 :
483 : // This modifies time->sse, if needed
484 976 : do_adjust_timezone(time, tzi);
485 :
486 976 : time->sse_uptodate = 1;
487 976 : time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = time->relative.first_last_day_of = 0;
488 976 : }
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
|