xref: /PHP-7.4/ext/date/lib/interval.c (revision aae5907c)
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 
timelib_diff(timelib_time * one,timelib_time * two)29 timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
30 {
31 	timelib_rel_time *rt;
32 	timelib_time *swp;
33 	timelib_sll dst_corr = 0 ,dst_h_corr = 0, dst_m_corr = 0;
34 	timelib_time one_backup, two_backup;
35 
36 	rt = timelib_rel_time_ctor();
37 	rt->invert = 0;
38 	if (
39 		(one->sse > two->sse) ||
40 		(one->sse == two->sse && one->us > two->us)
41 	) {
42 		swp = two;
43 		two = one;
44 		one = swp;
45 		rt->invert = 1;
46 	}
47 
48 	/* Calculate correction for DST change over, but only if the TZ type is ID
49 	 * and it's the same */
50 	if (one->zone_type == 3 && two->zone_type == 3
51 		&& (strcmp(one->tz_info->name, two->tz_info->name) == 0)
52 		&& (one->z != two->z))
53 	{
54 		dst_corr = two->z - one->z;
55 		dst_h_corr = dst_corr / 3600;
56 		dst_m_corr = (dst_corr % 3600) / 60;
57 	}
58 
59 	/* Save old TZ info */
60 	memcpy(&one_backup, one, sizeof(one_backup));
61 	memcpy(&two_backup, two, sizeof(two_backup));
62 
63     timelib_apply_localtime(one, 0);
64     timelib_apply_localtime(two, 0);
65 
66 	rt->y = two->y - one->y;
67 	rt->m = two->m - one->m;
68 	rt->d = two->d - one->d;
69 	rt->h = two->h - one->h;
70 	rt->i = two->i - one->i;
71 	rt->s = two->s - one->s;
72 	rt->us = two->us - one->us;
73 	if (one_backup.dst == 0 && two_backup.dst == 1 && two->sse >= one->sse + 86400 - dst_corr) {
74 		rt->h += dst_h_corr;
75 		rt->i += dst_m_corr;
76 	}
77 
78 	rt->days = fabs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
79 
80 	timelib_do_rel_normalize(rt->invert ? one : two, rt);
81 
82 	/* We need to do this after normalisation otherwise we can't get "24H" */
83 	if (one_backup.dst == 1 && two_backup.dst == 0 && two->sse >= one->sse + 86400) {
84 		if (two->sse < one->sse + 86400 - dst_corr) {
85 			rt->d--;
86 			rt->h = 24;
87 		} else {
88 			rt->h += dst_h_corr;
89 			rt->i += dst_m_corr;
90 		}
91 	}
92 
93 	/* Restore old TZ info */
94 	memcpy(one, &one_backup, sizeof(one_backup));
95 	memcpy(two, &two_backup, sizeof(two_backup));
96 
97 	return rt;
98 }
99 
timelib_add(timelib_time * old_time,timelib_rel_time * interval)100 timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
101 {
102 	int bias = 1;
103 	timelib_time *t = timelib_time_clone(old_time);
104 
105 	if (interval->have_weekday_relative || interval->have_special_relative) {
106 		memcpy(&t->relative, interval, sizeof(timelib_rel_time));
107 	} else {
108 		if (interval->invert) {
109 			bias = -1;
110 		}
111 		memset(&t->relative, 0, sizeof(timelib_rel_time));
112 		t->relative.y = interval->y * bias;
113 		t->relative.m = interval->m * bias;
114 		t->relative.d = interval->d * bias;
115 		t->relative.h = interval->h * bias;
116 		t->relative.i = interval->i * bias;
117 		t->relative.s = interval->s * bias;
118 		t->relative.us = interval->us * bias;
119 	}
120 	t->have_relative = 1;
121 	t->sse_uptodate = 0;
122 
123 	timelib_update_ts(t, NULL);
124 
125 //	printf("%lld %lld %d\n", old_time->dst, t->dst, (t->sse - old_time->sse));
126 	/* Adjust for backwards DST changeover */
127 	if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
128 		t->sse -= old_time->z;
129 		t->sse += t->z;
130 	}
131 
132 	timelib_update_from_sse(t);
133 	t->have_relative = 0;
134 
135 	return t;
136 }
137 
timelib_sub(timelib_time * old_time,timelib_rel_time * interval)138 timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
139 {
140 	int bias = 1;
141 	timelib_time *t = timelib_time_clone(old_time);
142 
143 	if (interval->invert) {
144 		bias = -1;
145 	}
146 
147 	memset(&t->relative, 0, sizeof(timelib_rel_time));
148 	t->relative.y = 0 - (interval->y * bias);
149 	t->relative.m = 0 - (interval->m * bias);
150 	t->relative.d = 0 - (interval->d * bias);
151 	t->relative.h = 0 - (interval->h * bias);
152 	t->relative.i = 0 - (interval->i * bias);
153 	t->relative.s = 0 - (interval->s * bias);
154 	t->relative.us = 0 - (interval->us * bias);
155 	t->have_relative = 1;
156 	t->sse_uptodate = 0;
157 
158 	timelib_update_ts(t, NULL);
159 
160 	/* Adjust for backwards DST changeover */
161 	if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
162 		t->sse -= old_time->z;
163 		t->sse += t->z;
164 	}
165 	/* Adjust for forwards DST changeover */
166 	if (old_time->dst == 0 && t->dst == 1 && !interval->y && !interval->m && !interval->d ) {
167 		t->sse -= old_time->z;
168 		t->sse += t->z;
169 	}
170 
171 	timelib_update_from_sse(t);
172 
173 	t->have_relative = 0;
174 
175 	return t;
176 }
177