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