xref: /PHP-5.4/ext/date/lib/unixtime2tm.c (revision 22dba2f5)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2014 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #include "timelib.h"
22 
23 #include <stdio.h>
24 
25 #ifdef HAVE_STDLIB_H
26 #include <stdlib.h>
27 #endif
28 
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #else
32 #include <strings.h>
33 #endif
34 
35 static int month_tab_leap[12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
36 static int month_tab[12] =      { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
37 
38 
39 /* Converts a Unix timestamp value into broken down time, in GMT */
timelib_unixtime2gmt(timelib_time * tm,timelib_sll ts)40 void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
41 {
42 	timelib_sll days, remainder, tmp_days;
43 	timelib_sll cur_year = 1970;
44 	timelib_sll i;
45 	timelib_sll hours, minutes, seconds;
46 	int *months;
47 
48 	days = ts / SECS_PER_DAY;
49 	remainder = ts - (days * SECS_PER_DAY);
50 	if (ts < 0 && remainder == 0) {
51 		days++;
52 		remainder -= SECS_PER_DAY;
53 	}
54 	TIMELIB_DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););
55 
56 	if (ts >= 0) {
57 		tmp_days = days + 1;
58 
59 		if (tmp_days >= DAYS_PER_LYEAR_PERIOD || tmp_days <= -DAYS_PER_LYEAR_PERIOD) {
60 			cur_year += YEARS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
61 			tmp_days -= DAYS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
62 		}
63 
64 		while (tmp_days >= DAYS_PER_LYEAR) {
65 			cur_year++;
66 			if (timelib_is_leap(cur_year)) {
67 				tmp_days -= DAYS_PER_LYEAR;
68 			} else {
69 				tmp_days -= DAYS_PER_YEAR;
70 			}
71 		}
72 	} else {
73 		tmp_days = days;
74 
75 		/* Guess why this might be for, it has to do with a pope ;-). It's also
76 		 * only valid for Great Brittain and it's colonies. It needs fixing for
77 		 * other locales. *sigh*, why is this crap so complex! */
78 		/*
79 		if (ts <= TIMELIB_LL_CONST(-6857352000)) {
80 			tmp_days -= 11;
81 		}
82 		*/
83 
84 		while (tmp_days <= 0) {
85 			if (tmp_days < -1460970) {
86 				cur_year -= 4000;
87 				TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
88 				tmp_days += 1460970;
89 			} else {
90 				cur_year--;
91 				TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
92 				if (timelib_is_leap(cur_year)) {
93 					tmp_days += DAYS_PER_LYEAR;
94 				} else {
95 					tmp_days += DAYS_PER_YEAR;
96 				}
97 			}
98 		}
99 		remainder += SECS_PER_DAY;
100 	}
101 	TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
102 
103 	months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;
104 	if (timelib_is_leap(cur_year) && cur_year < 1970) {
105 		tmp_days--;
106 	}
107 	i = 11;
108 	while (i > 0) {
109 		TIMELIB_DEBUG(printf("month=%lld (%d)\n", i, months[i]););
110 		if (tmp_days > months[i]) {
111 			break;
112 		}
113 		i--;
114 	}
115 	TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, tmp_days - months[i]););
116 
117 	/* That was the date, now we do the tiiiime */
118 	hours = remainder / 3600;
119 	minutes = (remainder - hours * 3600) / 60;
120 	seconds = remainder % 60;
121 	TIMELIB_DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
122 
123 	tm->y = cur_year;
124 	tm->m = i + 1;
125 	tm->d = tmp_days - months[i];
126 	tm->h = hours;
127 	tm->i = minutes;
128 	tm->s = seconds;
129 	tm->z = 0;
130 	tm->dst = 0;
131 	tm->sse = ts;
132 	tm->sse_uptodate = 1;
133 	tm->tim_uptodate = 1;
134 	tm->is_localtime = 0;
135 }
136 
timelib_update_from_sse(timelib_time * tm)137 void timelib_update_from_sse(timelib_time *tm)
138 {
139 	timelib_sll sse;
140 	int z = tm->z;
141 	signed int dst = tm->dst;
142 
143 	sse = tm->sse;
144 
145 	switch (tm->zone_type) {
146 		case TIMELIB_ZONETYPE_ABBR:
147 		case TIMELIB_ZONETYPE_OFFSET: {
148 			timelib_unixtime2gmt(tm, tm->sse - (tm->z * 60) + (tm->dst * 3600));
149 
150 			goto cleanup;
151 		}
152 
153 		case TIMELIB_ZONETYPE_ID: {
154 			timelib_time_offset *gmt_offset;
155 
156 			gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
157 			timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
158 			timelib_time_offset_dtor(gmt_offset);
159 
160 			goto cleanup;
161 		}
162 
163 		default:
164 			timelib_unixtime2gmt(tm, tm->sse);
165 			goto cleanup;
166 	}
167 cleanup:
168 	tm->sse = sse;
169 	tm->is_localtime = 1;
170 	tm->have_zone = 1;
171 	tm->z = z;
172 	tm->dst = dst;
173 }
174 
timelib_unixtime2local(timelib_time * tm,timelib_sll ts)175 void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
176 {
177 	timelib_time_offset *gmt_offset;
178 	timelib_tzinfo      *tz = tm->tz_info;
179 
180 	switch (tm->zone_type) {
181 		case TIMELIB_ZONETYPE_ABBR:
182 		case TIMELIB_ZONETYPE_OFFSET: {
183 			int z = tm->z;
184 			signed int dst = tm->dst;
185 
186 			timelib_unixtime2gmt(tm, ts - (tm->z * 60) + (tm->dst * 3600));
187 
188 			tm->z = z;
189 			tm->dst = dst;
190 			break;
191 		}
192 
193 		case TIMELIB_ZONETYPE_ID:
194 			gmt_offset = timelib_get_time_zone_info(ts, tz);
195 			timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
196 
197 			/* we need to reset the sse here as unixtime2gmt modifies it */
198 			tm->sse = ts;
199 			tm->dst = gmt_offset->is_dst;
200 			tm->z = gmt_offset->offset;
201 			tm->tz_info = tz;
202 
203 			timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
204 			timelib_time_offset_dtor(gmt_offset);
205 			break;
206 
207 		default:
208 			tm->is_localtime = 0;
209 			tm->have_zone = 0;
210 			return;
211 	}
212 
213 	tm->is_localtime = 1;
214 	tm->have_zone = 1;
215 }
216 
timelib_set_timezone_from_offset(timelib_time * t,timelib_sll utc_offset)217 void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset)
218 {
219 	if (t->tz_abbr) {
220 		free(t->tz_abbr);
221 	}
222 	t->tz_abbr = NULL;
223 
224 	t->z = utc_offset;
225 	t->have_zone = 1;
226 	t->zone_type = TIMELIB_ZONETYPE_OFFSET;
227 	t->dst = 0;
228 	t->tz_info = NULL;
229 }
230 
timelib_set_timezone_from_abbr(timelib_time * t,timelib_abbr_info abbr_info)231 void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info)
232 {
233 	if (t->tz_abbr) {
234 		free(t->tz_abbr);
235 	}
236 	t->tz_abbr = strdup(abbr_info.abbr);
237 
238 	t->z = abbr_info.utc_offset;
239 	t->have_zone = 1;
240 	t->zone_type = TIMELIB_ZONETYPE_ABBR;
241 	t->dst = abbr_info.dst;
242 	t->tz_info = NULL;
243 }
244 
timelib_set_timezone(timelib_time * t,timelib_tzinfo * tz)245 void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
246 {
247 	timelib_time_offset *gmt_offset;
248 
249 	gmt_offset = timelib_get_time_zone_info(t->sse, tz);
250 	t->z = gmt_offset->offset;
251 /*
252 	if (t->dst != gmt_offset->is_dst) {
253 		printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
254 		exit(1);
255 	}
256 */
257 	t->dst = gmt_offset->is_dst;
258 	t->tz_info = tz;
259 	if (t->tz_abbr) {
260 		free(t->tz_abbr);
261 	}
262 	t->tz_abbr = strdup(gmt_offset->abbr);
263 	timelib_time_offset_dtor(gmt_offset);
264 
265 	t->have_zone = 1;
266 	t->zone_type = TIMELIB_ZONETYPE_ID;
267 }
268 
269 /* Converts the time stored in the struct to localtime if localtime = true,
270  * otherwise it converts it to gmttime. This is only done when necessary
271  * ofcourse. */
timelib_apply_localtime(timelib_time * t,unsigned int localtime)272 int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
273 {
274 	if (localtime) {
275 		/* Converting from GMT time to local time */
276 		TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
277 
278 		/* Check if TZ is set */
279 		if (!t->tz_info) {
280 			TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
281 			return -1;
282 		}
283 
284 		timelib_unixtime2local(t, t->sse);
285 	} else {
286 		/* Converting from local time to GMT time */
287 		TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
288 
289 		timelib_unixtime2gmt(t, t->sse);
290 	}
291 	return 0;
292 }
293