xref: /PHP-8.1/ext/date/lib/unixtime2tm.c (revision 091c0920)
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 			timelib_time_offset *gmt_offset;
101 
102 			gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
103 			timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
104 			timelib_time_offset_dtor(gmt_offset);
105 
106 			goto cleanup;
107 		}
108 
109 		default:
110 			timelib_unixtime2gmt(tm, tm->sse);
111 			goto cleanup;
112 	}
113 cleanup:
114 	tm->sse = sse;
115 	tm->is_localtime = 1;
116 	tm->have_zone = 1;
117 	tm->z = z;
118 	tm->dst = dst;
119 }
120 
timelib_unixtime2local(timelib_time * tm,timelib_sll ts)121 void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
122 {
123 	timelib_time_offset *gmt_offset;
124 	timelib_tzinfo      *tz = tm->tz_info;
125 
126 	switch (tm->zone_type) {
127 		case TIMELIB_ZONETYPE_ABBR:
128 		case TIMELIB_ZONETYPE_OFFSET: {
129 			int z = tm->z;
130 			signed int dst = tm->dst;
131 
132 			timelib_unixtime2gmt(tm, ts + tm->z + (tm->dst * 3600));
133 
134 			tm->sse = ts;
135 			tm->z = z;
136 			tm->dst = dst;
137 			break;
138 		}
139 
140 		case TIMELIB_ZONETYPE_ID:
141 			gmt_offset = timelib_get_time_zone_info(ts, tz);
142 			timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
143 
144 			/* we need to reset the sse here as unixtime2gmt modifies it */
145 			tm->sse = ts;
146 			tm->dst = gmt_offset->is_dst;
147 			tm->z = gmt_offset->offset;
148 			tm->tz_info = tz;
149 
150 			timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
151 			timelib_time_offset_dtor(gmt_offset);
152 			break;
153 
154 		default:
155 			tm->is_localtime = 0;
156 			tm->have_zone = 0;
157 			return;
158 	}
159 
160 	tm->is_localtime = 1;
161 	tm->have_zone = 1;
162 }
163 
timelib_set_timezone_from_offset(timelib_time * t,timelib_sll utc_offset)164 void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset)
165 {
166 	if (t->tz_abbr) {
167 		timelib_free(t->tz_abbr);
168 	}
169 	t->tz_abbr = NULL;
170 
171 	t->z = utc_offset;
172 	t->have_zone = 1;
173 	t->zone_type = TIMELIB_ZONETYPE_OFFSET;
174 	t->dst = 0;
175 	t->tz_info = NULL;
176 }
177 
timelib_set_timezone_from_abbr(timelib_time * t,timelib_abbr_info abbr_info)178 void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info)
179 {
180 	if (t->tz_abbr) {
181 		timelib_free(t->tz_abbr);
182 	}
183 	t->tz_abbr = timelib_strdup(abbr_info.abbr);
184 
185 	t->z = abbr_info.utc_offset;
186 	t->have_zone = 1;
187 	t->zone_type = TIMELIB_ZONETYPE_ABBR;
188 	t->dst = abbr_info.dst;
189 	t->tz_info = NULL;
190 }
191 
timelib_set_timezone(timelib_time * t,timelib_tzinfo * tz)192 void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
193 {
194 	timelib_time_offset *gmt_offset;
195 
196 	gmt_offset = timelib_get_time_zone_info(t->sse, tz);
197 	t->z = gmt_offset->offset;
198 /*
199 	if (t->dst != gmt_offset->is_dst) {
200 		printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
201 		exit(1);
202 	}
203 */
204 	t->dst = gmt_offset->is_dst;
205 	t->tz_info = tz;
206 	if (t->tz_abbr) {
207 		timelib_free(t->tz_abbr);
208 	}
209 	t->tz_abbr = timelib_strdup(gmt_offset->abbr);
210 	timelib_time_offset_dtor(gmt_offset);
211 
212 	t->have_zone = 1;
213 	t->zone_type = TIMELIB_ZONETYPE_ID;
214 }
215 
216 /* Converts the time stored in the struct to localtime if localtime = true,
217  * otherwise it converts it to gmttime. This is only done when necessary
218  * of course. */
timelib_apply_localtime(timelib_time * t,unsigned int localtime)219 int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
220 {
221 	if (localtime) {
222 		/* Converting from GMT time to local time */
223 		TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
224 
225 		/* Check if TZ is set */
226 		if (!t->tz_info) {
227 			TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
228 			return -1;
229 		}
230 
231 		timelib_unixtime2local(t, t->sse);
232 	} else {
233 		/* Converting from local time to GMT time */
234 		TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
235 
236 		timelib_unixtime2gmt(t, t->sse);
237 	}
238 	return 0;
239 }
240