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 #include <math.h>
28
swap_times(timelib_time ** one,timelib_time ** two,timelib_rel_time * rt)29 static void swap_times(timelib_time **one, timelib_time **two, timelib_rel_time *rt)
30 {
31 timelib_time *swp;
32
33 swp = *two;
34 *two = *one;
35 *one = swp;
36 rt->invert = 1;
37 }
38
swap_if_negative(timelib_rel_time * rt)39 static void swap_if_negative(timelib_rel_time *rt)
40 {
41 if (rt->y == 0 && rt->m == 0 && rt->d == 0 && rt->h == 0 && rt->i == 0 && rt->s == 0 && rt->us == 0) {
42 return;
43 }
44 if (rt->y >= 0 && rt->m >= 0 && rt->d >= 0 && rt->h >= 0 && rt->i >= 0 && rt->s >= 0 && rt->us >= 0) {
45 return;
46 }
47
48 rt->invert = 1 - rt->invert;
49 rt->y = 0 - rt->y;
50 rt->m = 0 - rt->m;
51 rt->d = 0 - rt->d;
52 rt->h = 0 - rt->h;
53 rt->i = 0 - rt->i;
54 rt->s = 0 - rt->s;
55 rt->us = 0 - rt->us;
56 }
57
sort_old_to_new(timelib_time ** one,timelib_time ** two,timelib_rel_time * rt)58 static void sort_old_to_new(timelib_time **one, timelib_time **two, timelib_rel_time *rt)
59 {
60 /* Check whether date/times need to be inverted. If both times are
61 * TIMELIB_ZONETYPE_ID times with the same TZID, we use the y-s + us fields. */
62 if (
63 (*one)->zone_type == TIMELIB_ZONETYPE_ID &&
64 (*two)->zone_type == TIMELIB_ZONETYPE_ID &&
65 (strcmp((*one)->tz_info->name, (*two)->tz_info->name) == 0)
66 ) {
67 if (
68 ((*one)->y > (*two)->y) ||
69 ((*one)->y == (*two)->y && (*one)->m > (*two)->m) ||
70 ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d > (*two)->d) ||
71 ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h > (*two)->h) ||
72 ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h == (*two)->h && (*one)->i > (*two)->i) ||
73 ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h == (*two)->h && (*one)->i == (*two)->i && (*one)->s > (*two)->s) ||
74 ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h == (*two)->h && (*one)->i == (*two)->i && (*one)->s == (*two)->s && (*one)->us > (*two)->us)
75 ) {
76 swap_times(one, two, rt);
77 }
78 return;
79 }
80
81 /* Fall back to using the SSE instead to rearrange */
82 if (
83 ((*one)->sse > (*two)->sse) ||
84 ((*one)->sse == (*two)->sse && (*one)->us > (*two)->us)
85 ) {
86 swap_times(one, two, rt);
87 }
88 }
89
timelib_diff_with_tzid(timelib_time * one,timelib_time * two)90 static timelib_rel_time *timelib_diff_with_tzid(timelib_time *one, timelib_time *two)
91 {
92 timelib_rel_time *rt;
93 timelib_sll dst_corr = 0, dst_h_corr = 0, dst_m_corr = 0;
94 timelib_time_offset *trans = NULL;
95
96 rt = timelib_rel_time_ctor();
97 rt->invert = 0;
98
99 sort_old_to_new(&one, &two, rt);
100
101 /* Calculate correction for UTC offset changes between first and second SSE */
102 dst_corr = two->z - one->z;
103 dst_h_corr = dst_corr / 3600;
104 dst_m_corr = (dst_corr % 3600) / 60;
105
106 rt->y = two->y - one->y;
107 rt->m = two->m - one->m;
108 rt->d = two->d - one->d;
109 rt->h = two->h - one->h;
110 rt->i = two->i - one->i;
111 rt->s = two->s - one->s;
112 rt->us = two->us - one->us;
113
114 rt->days = timelib_diff_days(one, two);
115
116 /* Fall Back: Cater for transition period, where rt->invert is 0, but there are negative numbers */
117 if (one->dst == 1 && two->dst == 0) {
118 /* First for two "Type 3" times */
119 if (one->zone_type == TIMELIB_ZONETYPE_ID && two->zone_type == TIMELIB_ZONETYPE_ID) {
120 trans = timelib_get_time_zone_info(two->sse, two->tz_info);
121 if (trans) {
122 if (one->sse < trans->transition_time && one->sse >= trans->transition_time + dst_corr) {
123 timelib_sll flipped = SECS_PER_HOUR + (rt->i * 60) + (rt->s);
124 rt->h = flipped / SECS_PER_HOUR;
125 rt->i = (flipped - rt->h * SECS_PER_HOUR) / 60;
126 rt->s = flipped % 60;
127 }
128 timelib_time_offset_dtor(trans);
129 trans = NULL;
130 }
131 } else if (rt->h == 0 && (rt->i < 0 || rt->s < 0)) {
132 /* Then for all the others */
133 timelib_sll flipped = SECS_PER_HOUR + (rt->i * 60) + (rt->s);
134 rt->h = flipped / SECS_PER_HOUR;
135 rt->i = (flipped - rt->h * SECS_PER_HOUR) / 60;
136 rt->s = flipped % 60;
137 dst_corr += SECS_PER_HOUR;
138 dst_h_corr++;
139 }
140 }
141
142 timelib_do_rel_normalize(rt->invert ? one : two, rt);
143
144 /* Do corrections for "Type 3" times with the same TZID */
145 if (one->zone_type == TIMELIB_ZONETYPE_ID && two->zone_type == TIMELIB_ZONETYPE_ID && strcmp(one->tz_info->name, two->tz_info->name) == 0) {
146 if (one->dst == 1 && two->dst == 0) { /* Fall Back */
147 if (two->tz_info) {
148 trans = timelib_get_time_zone_info(two->sse, two->tz_info);
149
150 if (
151 trans &&
152 two->sse >= trans->transition_time &&
153 ((two->sse - one->sse + dst_corr) % SECS_PER_DAY) > (two->sse - trans->transition_time)
154 ) {
155 rt->h -= dst_h_corr;
156 rt->i -= dst_m_corr;
157 }
158 }
159 } else if (one->dst == 0 && two->dst == 1) { /* Spring Forward */
160 if (two->tz_info) {
161 trans = timelib_get_time_zone_info(two->sse, two->tz_info);
162
163 if (
164 trans &&
165 !((one->sse + SECS_PER_DAY > trans->transition_time) && (one->sse + SECS_PER_DAY <= (trans->transition_time + dst_corr))) &&
166 two->sse >= trans->transition_time &&
167 ((two->sse - one->sse + dst_corr) % SECS_PER_DAY) > (two->sse - trans->transition_time)
168 ) {
169 rt->h -= dst_h_corr;
170 rt->i -= dst_m_corr;
171 }
172 }
173 } else if (two->sse - one->sse >= SECS_PER_DAY) {
174 /* Check whether we're in the period to the next transition time */
175 trans = timelib_get_time_zone_info(two->sse - two->z, two->tz_info);
176 dst_corr = one->z - trans->offset;
177
178 if (two->sse >= trans->transition_time - dst_corr && two->sse < trans->transition_time) {
179 rt->d--;
180 rt->h = 24;
181 }
182 }
183 } else {
184 rt->h -= dst_h_corr;
185 rt->i -= dst_m_corr;
186
187 swap_if_negative(rt);
188
189 timelib_do_rel_normalize(rt->invert ? one : two, rt);
190 }
191
192 if (trans) {
193 timelib_time_offset_dtor(trans);
194 }
195
196 return rt;
197 }
198
timelib_diff(timelib_time * one,timelib_time * two)199 timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
200 {
201 timelib_rel_time *rt;
202
203 if (one->zone_type == TIMELIB_ZONETYPE_ID && two->zone_type == TIMELIB_ZONETYPE_ID && strcmp(one->tz_info->name, two->tz_info->name) == 0) {
204 return timelib_diff_with_tzid(one, two);
205 }
206
207 rt = timelib_rel_time_ctor();
208 rt->invert = 0;
209
210 sort_old_to_new(&one, &two, rt);
211
212 rt->y = two->y - one->y;
213 rt->m = two->m - one->m;
214 rt->d = two->d - one->d;
215 rt->h = two->h - one->h;
216 if (one->zone_type != TIMELIB_ZONETYPE_ID) {
217 rt->h = rt->h + one->dst;
218 }
219 if (two->zone_type != TIMELIB_ZONETYPE_ID) {
220 rt->h = rt->h - two->dst;
221 }
222 rt->i = two->i - one->i;
223 rt->s = two->s - one->s - two->z + one->z;
224 rt->us = two->us - one->us;
225
226 rt->days = timelib_diff_days(one, two);
227
228 timelib_do_rel_normalize(rt->invert ? one : two, rt);
229
230 return rt;
231 }
232
233
timelib_diff_days(timelib_time * one,timelib_time * two)234 int timelib_diff_days(timelib_time *one, timelib_time *two)
235 {
236 int days = 0;
237
238 if (timelib_same_timezone(one, two)) {
239 timelib_time *earliest, *latest;
240 double earliest_time, latest_time;
241
242 if (timelib_time_compare(one, two) < 0) {
243 earliest = one;
244 latest = two;
245 } else {
246 earliest = two;
247 latest = one;
248 }
249 timelib_hmsf_to_decimal_hour(earliest->h, earliest->i, earliest->s, earliest->us, &earliest_time);
250 timelib_hmsf_to_decimal_hour(latest->h, latest->i, latest->s, latest->us, &latest_time);
251
252 days = llabs(timelib_epoch_days_from_time(one) - timelib_epoch_days_from_time(two));
253 if (latest_time < earliest_time && days > 0) {
254 days--;
255 }
256 } else {
257 days = fabs(floor(one->sse - two->sse) / 86400);
258 }
259
260 return days;
261 }
262
263
timelib_add(timelib_time * old_time,timelib_rel_time * interval)264 timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
265 {
266 int bias = 1;
267 timelib_time *t = timelib_time_clone(old_time);
268
269 if (interval->have_weekday_relative || interval->have_special_relative) {
270 memcpy(&t->relative, interval, sizeof(timelib_rel_time));
271 } else {
272 if (interval->invert) {
273 bias = -1;
274 }
275 memset(&t->relative, 0, sizeof(timelib_rel_time));
276 t->relative.y = interval->y * bias;
277 t->relative.m = interval->m * bias;
278 t->relative.d = interval->d * bias;
279 t->relative.h = interval->h * bias;
280 t->relative.i = interval->i * bias;
281 t->relative.s = interval->s * bias;
282 t->relative.us = interval->us * bias;
283 }
284 t->have_relative = 1;
285 t->sse_uptodate = 0;
286
287 timelib_update_ts(t, NULL);
288
289 timelib_update_from_sse(t);
290 t->have_relative = 0;
291
292 return t;
293 }
294
timelib_sub(timelib_time * old_time,timelib_rel_time * interval)295 timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
296 {
297 int bias = 1;
298 timelib_time *t = timelib_time_clone(old_time);
299
300 if (interval->invert) {
301 bias = -1;
302 }
303
304 memset(&t->relative, 0, sizeof(timelib_rel_time));
305 t->relative.y = 0 - (interval->y * bias);
306 t->relative.m = 0 - (interval->m * bias);
307 t->relative.d = 0 - (interval->d * bias);
308 t->relative.h = 0 - (interval->h * bias);
309 t->relative.i = 0 - (interval->i * bias);
310 t->relative.s = 0 - (interval->s * bias);
311 t->relative.us = 0 - (interval->us * bias);
312 t->have_relative = 1;
313 t->sse_uptodate = 0;
314
315 timelib_update_ts(t, NULL);
316
317 timelib_update_from_sse(t);
318
319 t->have_relative = 0;
320
321 return t;
322 }
323
do_range_limit(timelib_sll start,timelib_sll end,timelib_sll adj,timelib_sll * a,timelib_sll * b)324 static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
325 {
326 if (*a < start) {
327 *b -= (start - *a - 1) / adj + 1;
328 *a += adj * ((start - *a - 1) / adj + 1);
329 }
330 if (*a >= end) {
331 *b += *a / adj;
332 *a -= adj * (*a / adj);
333 }
334 }
335
336
timelib_add_wall(timelib_time * old_time,timelib_rel_time * interval)337 timelib_time *timelib_add_wall(timelib_time *old_time, timelib_rel_time *interval)
338 {
339 int bias = 1;
340 timelib_time *t = timelib_time_clone(old_time);
341
342 t->have_relative = 1;
343 t->sse_uptodate = 0;
344
345 if (interval->have_weekday_relative || interval->have_special_relative) {
346 memcpy(&t->relative, interval, sizeof(timelib_rel_time));
347
348 timelib_update_ts(t, NULL);
349
350 timelib_update_from_sse(t);
351 } else {
352 if (interval->invert) {
353 bias = -1;
354 }
355 memset(&t->relative, 0, sizeof(timelib_rel_time));
356 t->relative.y = interval->y * bias;
357 t->relative.m = interval->m * bias;
358 t->relative.d = interval->d * bias;
359
360 if (t->relative.y || t->relative.m || t->relative.d) {
361 timelib_update_ts(t, NULL);
362 }
363
364 if (interval->us == 0) {
365 t->sse += bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
366 timelib_update_from_sse(t);
367 } else {
368 timelib_rel_time *temp_interval = timelib_rel_time_clone(interval);
369
370 do_range_limit(0, 1000000, 1000000, &temp_interval->us, &temp_interval->s);
371 t->sse += bias * timelib_hms_to_seconds(temp_interval->h, temp_interval->i, temp_interval->s);
372 timelib_update_from_sse(t);
373 t->us += temp_interval->us * bias;
374
375 timelib_do_normalize(t);
376 timelib_update_ts(t, NULL);
377
378 timelib_rel_time_dtor(temp_interval);
379 }
380 timelib_do_normalize(t);
381 }
382
383 if (t->zone_type == TIMELIB_ZONETYPE_ID) {
384 timelib_set_timezone(t, t->tz_info);
385 }
386 t->have_relative = 0;
387
388 return t;
389 }
390
timelib_sub_wall(timelib_time * old_time,timelib_rel_time * interval)391 timelib_time *timelib_sub_wall(timelib_time *old_time, timelib_rel_time *interval)
392 {
393 int bias = 1;
394 timelib_time *t = timelib_time_clone(old_time);
395
396 t->have_relative = 1;
397 t->sse_uptodate = 0;
398
399 if (interval->have_weekday_relative || interval->have_special_relative) {
400 memcpy(&t->relative, interval, sizeof(timelib_rel_time));
401
402 timelib_update_ts(t, NULL);
403 timelib_update_from_sse(t);
404 } else {
405 if (interval->invert) {
406 bias = -1;
407 }
408 memset(&t->relative, 0, sizeof(timelib_rel_time));
409 t->relative.y = 0 - (interval->y * bias);
410 t->relative.m = 0 - (interval->m * bias);
411 t->relative.d = 0 - (interval->d * bias);
412
413 if (t->relative.y || t->relative.m || t->relative.d) {
414 timelib_update_ts(t, NULL);
415 }
416
417 if (interval->us == 0) {
418 t->sse -= bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
419 timelib_update_from_sse(t);
420 } else {
421 timelib_rel_time *temp_interval = timelib_rel_time_clone(interval);
422
423 do_range_limit(0, 1000000, 1000000, &temp_interval->us, &temp_interval->s);
424 t->sse -= bias * timelib_hms_to_seconds(temp_interval->h, temp_interval->i, temp_interval->s);
425 timelib_update_from_sse(t);
426 t->us -= temp_interval->us * bias;
427
428 timelib_do_normalize(t);
429 timelib_update_ts(t, NULL);
430
431 timelib_rel_time_dtor(temp_interval);
432 }
433 timelib_do_normalize(t);
434 }
435
436 if (t->zone_type == TIMELIB_ZONETYPE_ID) {
437 timelib_set_timezone(t, t->tz_info);
438 }
439 t->have_relative = 0;
440
441 return t;
442 }
443