xref: /PHP-5.6/ext/date/lib/interval.c (revision 822ca452)
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 	if (one_backup.dst == 0 && two_backup.dst == 1 && two->sse >= one->sse + 86400 - dst_corr) {
69 		rt->h += dst_h_corr;
70 		rt->i += dst_m_corr;
71 	}
72 
73 	rt->days = fabs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
74 
75 	timelib_do_rel_normalize(rt->invert ? one : two, rt);
76 
77 	/* We need to do this after normalisation otherwise we can't get "24H" */
78 	if (one_backup.dst == 1 && two_backup.dst == 0 && two->sse >= one->sse + 86400) {
79 		if (two->sse < one->sse + 86400 - dst_corr) {
80 			rt->d--;
81 			rt->h = 24;
82 		} else {
83 			rt->h += dst_h_corr;
84 			rt->i += dst_m_corr;
85 		}
86 	}
87 
88 	/* Restore old TZ info */
89 	memcpy(one, &one_backup, sizeof(one_backup));
90 	memcpy(two, &two_backup, sizeof(two_backup));
91 
92 	return rt;
93 }
94 
timelib_add(timelib_time * old_time,timelib_rel_time * interval)95 timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
96 {
97 	int bias = 1;
98 	timelib_time *t = timelib_time_clone(old_time);
99 
100 	if (interval->have_weekday_relative || interval->have_special_relative) {
101 		memcpy(&t->relative, interval, sizeof(struct timelib_rel_time));
102 	} else {
103 		if (interval->invert) {
104 			bias = -1;
105 		}
106 		memset(&t->relative, 0, sizeof(struct timelib_rel_time));
107 		t->relative.y = interval->y * bias;
108 		t->relative.m = interval->m * bias;
109 		t->relative.d = interval->d * bias;
110 		t->relative.h = interval->h * bias;
111 		t->relative.i = interval->i * bias;
112 		t->relative.s = interval->s * bias;
113 	}
114 	t->have_relative = 1;
115 	t->sse_uptodate = 0;
116 
117 	timelib_update_ts(t, NULL);
118 
119 //	printf("%lld %lld %d\n", old_time->dst, t->dst, (t->sse - old_time->sse));
120 	/* Adjust for backwards DST changeover */
121 	if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
122 		t->sse -= old_time->z;
123 		t->sse += t->z;
124 	}
125 
126 	timelib_update_from_sse(t);
127 	t->have_relative = 0;
128 
129 	return t;
130 }
131 
timelib_sub(timelib_time * old_time,timelib_rel_time * interval)132 timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
133 {
134 	int bias = 1;
135 	timelib_time *t = timelib_time_clone(old_time);
136 
137 	if (interval->invert) {
138 		bias = -1;
139 	}
140 
141 	memset(&t->relative, 0, sizeof(struct timelib_rel_time));
142 	t->relative.y = 0 - (interval->y * bias);
143 	t->relative.m = 0 - (interval->m * bias);
144 	t->relative.d = 0 - (interval->d * bias);
145 	t->relative.h = 0 - (interval->h * bias);
146 	t->relative.i = 0 - (interval->i * bias);
147 	t->relative.s = 0 - (interval->s * bias);
148 	t->have_relative = 1;
149 	t->sse_uptodate = 0;
150 
151 	timelib_update_ts(t, NULL);
152 
153 	/* Adjust for backwards DST changeover */
154 	if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
155 		t->sse -= old_time->z;
156 		t->sse += t->z;
157 	}
158 	/* Adjust for forwards DST changeover */
159 	if (old_time->dst == 0 && t->dst == 1 && !interval->y && !interval->m && !interval->d ) {
160 		t->sse -= old_time->z;
161 		t->sse += t->z;
162 	}
163 
164 	timelib_update_from_sse(t);
165 
166 	t->have_relative = 0;
167 
168 	return t;
169 }
170