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