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