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