xref: /php-src/ext/date/lib/unixtime2tm.c (revision 06d4c70e)
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015-2021 Derick Rethans
5  * Copyright (c) 2021 MongoDB
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #include "timelib.h"
27 #include "timelib_private.h"
28 
timelib_unixtime2date(timelib_sll ts,timelib_sll * y,timelib_sll * m,timelib_sll * d)29 void timelib_unixtime2date(timelib_sll ts, timelib_sll *y, timelib_sll *m, timelib_sll *d)
30 {
31 	timelib_sll days, era, t;
32 	timelib_ull day_of_era, year_of_era, day_of_year, month_portion;
33 
34 	/* Calculate days since algorithm's epoch (0000-03-01) */
35 	days = ts / SECS_PER_DAY + HINNANT_EPOCH_SHIFT;
36 
37 	/* Adjustment for a negative time portion */
38 	t = ts % SECS_PER_DAY;
39 	days += (t < 0) ? -1 : 0;
40 
41 	/* Calculate year, month, and day. Algorithm from:
42 	 * http://howardhinnant.github.io/date_algorithms.html#civil_from_days */
43 	era = (days >= 0 ? days : days - DAYS_PER_ERA + 1) / DAYS_PER_ERA;
44 	day_of_era = days - era * DAYS_PER_ERA;
45 	year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146096) / DAYS_PER_YEAR;
46 	*y = year_of_era + era * YEARS_PER_ERA;
47 	day_of_year = day_of_era - (DAYS_PER_YEAR * year_of_era + year_of_era / 4 - year_of_era / 100);
48 	month_portion = (5 * day_of_year + 2) / 153;
49 	*d = day_of_year - (153 * month_portion + 2) / 5 + 1;
50 	*m = month_portion + (month_portion < 10 ? 3 : -9);
51 	*y += (*m <= 2);
52 
53 	TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, *y, *m, *d););
54 }
55 
56 /* Converts a Unix timestamp value into broken down time, in GMT */
timelib_unixtime2gmt(timelib_time * tm,timelib_sll ts)57 void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
58 {
59 	timelib_sll remainder;
60 	timelib_sll hours, minutes, seconds;
61 
62 	timelib_unixtime2date(ts, &tm->y, &tm->m, &tm->d);
63 	remainder = ts % SECS_PER_DAY;
64 	remainder += (remainder < 0) * SECS_PER_DAY;
65 
66 	/* That was the date, now we do the time */
67 	hours = remainder / 3600;
68 	minutes = (remainder - hours * 3600) / 60;
69 	seconds = remainder % 60;
70 	TIMELIB_DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
71 
72 	tm->h = hours;
73 	tm->i = minutes;
74 	tm->s = seconds;
75 	tm->z = 0;
76 	tm->dst = 0;
77 	tm->sse = ts;
78 	tm->sse_uptodate = 1;
79 	tm->tim_uptodate = 1;
80 	tm->is_localtime = 0;
81 }
82 
timelib_update_from_sse(timelib_time * tm)83 void timelib_update_from_sse(timelib_time *tm)
84 {
85 	timelib_sll sse;
86 	int z = tm->z;
87 	signed int dst = tm->dst;
88 
89 	sse = tm->sse;
90 
91 	switch (tm->zone_type) {
92 		case TIMELIB_ZONETYPE_ABBR:
93 		case TIMELIB_ZONETYPE_OFFSET: {
94 			timelib_unixtime2gmt(tm, tm->sse + tm->z + (tm->dst * 3600));
95 
96 			goto cleanup;
97 		}
98 
99 		case TIMELIB_ZONETYPE_ID: {
100 			int32_t  offset = 0;
101 
102 			timelib_get_time_zone_offset_info(tm->sse, tm->tz_info, &offset, NULL, NULL);
103 			timelib_unixtime2gmt(tm, tm->sse + offset);
104 
105 			goto cleanup;
106 		}
107 
108 		default:
109 			timelib_unixtime2gmt(tm, tm->sse);
110 			goto cleanup;
111 	}
112 cleanup:
113 	tm->sse = sse;
114 	tm->is_localtime = 1;
115 	tm->have_zone = 1;
116 	tm->z = z;
117 	tm->dst = dst;
118 }
119 
timelib_unixtime2local(timelib_time * tm,timelib_sll ts)120 void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
121 {
122 	timelib_time_offset *gmt_offset;
123 	timelib_tzinfo      *tz = tm->tz_info;
124 
125 	switch (tm->zone_type) {
126 		case TIMELIB_ZONETYPE_ABBR:
127 		case TIMELIB_ZONETYPE_OFFSET: {
128 			int z = tm->z;
129 			signed int dst = tm->dst;
130 
131 			timelib_unixtime2gmt(tm, ts + tm->z + (tm->dst * 3600));
132 
133 			tm->sse = ts;
134 			tm->z = z;
135 			tm->dst = dst;
136 			break;
137 		}
138 
139 		case TIMELIB_ZONETYPE_ID:
140 			gmt_offset = timelib_get_time_zone_info(ts, tz);
141 			timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
142 
143 			/* we need to reset the sse here as unixtime2gmt modifies it */
144 			tm->sse = ts;
145 			tm->dst = gmt_offset->is_dst;
146 			tm->z = gmt_offset->offset;
147 			tm->tz_info = tz;
148 
149 			timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
150 			timelib_time_offset_dtor(gmt_offset);
151 			break;
152 
153 		default:
154 			tm->is_localtime = 0;
155 			tm->have_zone = 0;
156 			return;
157 	}
158 
159 	tm->is_localtime = 1;
160 	tm->have_zone = 1;
161 }
162 
timelib_set_timezone_from_offset(timelib_time * t,timelib_sll utc_offset)163 void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset)
164 {
165 	if (t->tz_abbr) {
166 		timelib_free(t->tz_abbr);
167 	}
168 	t->tz_abbr = NULL;
169 
170 	t->z = utc_offset;
171 	t->have_zone = 1;
172 	t->zone_type = TIMELIB_ZONETYPE_OFFSET;
173 	t->dst = 0;
174 	t->tz_info = NULL;
175 }
176 
timelib_set_timezone_from_abbr(timelib_time * t,timelib_abbr_info abbr_info)177 void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info)
178 {
179 	if (t->tz_abbr) {
180 		timelib_free(t->tz_abbr);
181 	}
182 	t->tz_abbr = timelib_strdup(abbr_info.abbr);
183 
184 	t->z = abbr_info.utc_offset;
185 	t->have_zone = 1;
186 	t->zone_type = TIMELIB_ZONETYPE_ABBR;
187 	t->dst = abbr_info.dst;
188 	t->tz_info = NULL;
189 }
190 
timelib_set_timezone(timelib_time * t,timelib_tzinfo * tz)191 void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
192 {
193 	timelib_time_offset *gmt_offset;
194 
195 	gmt_offset = timelib_get_time_zone_info(t->sse, tz);
196 	t->z = gmt_offset->offset;
197 /*
198 	if (t->dst != gmt_offset->is_dst) {
199 		printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
200 		exit(1);
201 	}
202 */
203 	t->dst = gmt_offset->is_dst;
204 	t->tz_info = tz;
205 	if (t->tz_abbr) {
206 		timelib_free(t->tz_abbr);
207 	}
208 	t->tz_abbr = timelib_strdup(gmt_offset->abbr);
209 	timelib_time_offset_dtor(gmt_offset);
210 
211 	t->have_zone = 1;
212 	t->zone_type = TIMELIB_ZONETYPE_ID;
213 }
214 
215 /* Converts the time stored in the struct to localtime if localtime = true,
216  * otherwise it converts it to gmttime. This is only done when necessary
217  * of course. */
timelib_apply_localtime(timelib_time * t,unsigned int localtime)218 int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
219 {
220 	if (localtime) {
221 		/* Converting from GMT time to local time */
222 		TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
223 
224 		/* Check if TZ is set */
225 		if (!t->tz_info) {
226 			TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
227 			return -1;
228 		}
229 
230 		timelib_unixtime2local(t, t->sse);
231 	} else {
232 		/* Converting from local time to GMT time */
233 		TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
234 
235 		timelib_unixtime2gmt(t, t->sse);
236 	}
237 	return 0;
238 }
239