1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 "php.h"
22 #include "php_streams.h"
23 #include "php_main.h"
24 #include "php_globals.h"
25 #include "php_ini.h"
26 #include "ext/standard/info.h"
27 #include "ext/standard/php_versioning.h"
28 #include "ext/standard/php_math.h"
29 #include "php_date.h"
30 #include "zend_interfaces.h"
31 #include "lib/timelib.h"
32 #include "lib/timelib_private.h"
33 #ifndef PHP_WIN32
34 #include <time.h>
35 #else
36 #include "win32/time.h"
37 #endif
38 
39 #ifdef PHP_WIN32
php_date_llabs(__int64 i)40 static __inline __int64 php_date_llabs( __int64 i ) { return i >= 0? i: -i; }
41 #elif defined(__GNUC__) && __GNUC__ < 3
php_date_llabs(__int64_t i)42 static __inline __int64_t php_date_llabs( __int64_t i ) { return i >= 0 ? i : -i; }
43 #else
php_date_llabs(long long i)44 static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i; }
45 #endif
46 
47 #ifdef PHP_WIN32
48 #define DATE_I64_BUF_LEN 65
49 # define DATE_I64A(i, s, len) _i64toa_s(i, s, len, 10)
50 # define DATE_A64I(i, s) i = _atoi64(s)
51 #else
52 #define DATE_I64_BUF_LEN 65
53 # define DATE_I64A(i, s, len) \
54 	do { \
55 		int st = snprintf(s, len, "%lld", i); \
56 		s[st] = '\0'; \
57 	} while (0);
58 #ifdef HAVE_ATOLL
59 # define DATE_A64I(i, s) i = atoll(s)
60 #else
61 # define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
62 #endif
63 #endif
64 
65 /* {{{ arginfo */
66 ZEND_BEGIN_ARG_INFO_EX(arginfo_date, 0, 0, 1)
67 	ZEND_ARG_INFO(0, format)
68 	ZEND_ARG_INFO(0, timestamp)
69 ZEND_END_ARG_INFO()
70 
71 ZEND_BEGIN_ARG_INFO_EX(arginfo_gmdate, 0, 0, 1)
72 	ZEND_ARG_INFO(0, format)
73 	ZEND_ARG_INFO(0, timestamp)
74 ZEND_END_ARG_INFO()
75 
76 ZEND_BEGIN_ARG_INFO_EX(arginfo_idate, 0, 0, 1)
77 	ZEND_ARG_INFO(0, format)
78 	ZEND_ARG_INFO(0, timestamp)
79 ZEND_END_ARG_INFO()
80 
81 ZEND_BEGIN_ARG_INFO_EX(arginfo_strtotime, 0, 0, 1)
82 	ZEND_ARG_INFO(0, time)
83 	ZEND_ARG_INFO(0, now)
84 ZEND_END_ARG_INFO()
85 
86 ZEND_BEGIN_ARG_INFO_EX(arginfo_mktime, 0, 0, 0)
87 	ZEND_ARG_INFO(0, hour)
88 	ZEND_ARG_INFO(0, min)
89 	ZEND_ARG_INFO(0, sec)
90 	ZEND_ARG_INFO(0, mon)
91 	ZEND_ARG_INFO(0, day)
92 	ZEND_ARG_INFO(0, year)
93 ZEND_END_ARG_INFO()
94 
95 ZEND_BEGIN_ARG_INFO_EX(arginfo_gmmktime, 0, 0, 0)
96 	ZEND_ARG_INFO(0, hour)
97 	ZEND_ARG_INFO(0, min)
98 	ZEND_ARG_INFO(0, sec)
99 	ZEND_ARG_INFO(0, mon)
100 	ZEND_ARG_INFO(0, day)
101 	ZEND_ARG_INFO(0, year)
102 ZEND_END_ARG_INFO()
103 
104 ZEND_BEGIN_ARG_INFO(arginfo_checkdate, 0)
105 	ZEND_ARG_INFO(0, month)
106 	ZEND_ARG_INFO(0, day)
107 	ZEND_ARG_INFO(0, year)
108 ZEND_END_ARG_INFO()
109 
110 ZEND_BEGIN_ARG_INFO_EX(arginfo_strftime, 0, 0, 1)
111 	ZEND_ARG_INFO(0, format)
112 	ZEND_ARG_INFO(0, timestamp)
113 ZEND_END_ARG_INFO()
114 
115 ZEND_BEGIN_ARG_INFO_EX(arginfo_gmstrftime, 0, 0, 1)
116 	ZEND_ARG_INFO(0, format)
117 	ZEND_ARG_INFO(0, timestamp)
118 ZEND_END_ARG_INFO()
119 
120 ZEND_BEGIN_ARG_INFO(arginfo_time, 0)
121 ZEND_END_ARG_INFO()
122 
123 ZEND_BEGIN_ARG_INFO_EX(arginfo_localtime, 0, 0, 0)
124 	ZEND_ARG_INFO(0, timestamp)
125 	ZEND_ARG_INFO(0, associative_array)
126 ZEND_END_ARG_INFO()
127 
128 ZEND_BEGIN_ARG_INFO_EX(arginfo_getdate, 0, 0, 0)
129 	ZEND_ARG_INFO(0, timestamp)
130 ZEND_END_ARG_INFO()
131 
132 ZEND_BEGIN_ARG_INFO(arginfo_date_default_timezone_set, 0)
133 	ZEND_ARG_INFO(0, timezone_identifier)
134 ZEND_END_ARG_INFO()
135 
136 ZEND_BEGIN_ARG_INFO(arginfo_date_default_timezone_get, 0)
137 ZEND_END_ARG_INFO()
138 
139 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_sunrise, 0, 0, 1)
140 	ZEND_ARG_INFO(0, time)
141 	ZEND_ARG_INFO(0, format)
142 	ZEND_ARG_INFO(0, latitude)
143 	ZEND_ARG_INFO(0, longitude)
144 	ZEND_ARG_INFO(0, zenith)
145 	ZEND_ARG_INFO(0, gmt_offset)
146 ZEND_END_ARG_INFO()
147 
148 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_sunset, 0, 0, 1)
149 	ZEND_ARG_INFO(0, time)
150 	ZEND_ARG_INFO(0, format)
151 	ZEND_ARG_INFO(0, latitude)
152 	ZEND_ARG_INFO(0, longitude)
153 	ZEND_ARG_INFO(0, zenith)
154 	ZEND_ARG_INFO(0, gmt_offset)
155 ZEND_END_ARG_INFO()
156 
157 ZEND_BEGIN_ARG_INFO(arginfo_date_sun_info, 0)
158 	ZEND_ARG_INFO(0, time)
159 	ZEND_ARG_INFO(0, latitude)
160 	ZEND_ARG_INFO(0, longitude)
161 ZEND_END_ARG_INFO()
162 
163 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_create, 0, 0, 0)
164 	ZEND_ARG_INFO(0, time)
165 	ZEND_ARG_INFO(0, timezone)
166 ZEND_END_ARG_INFO()
167 
168 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_create_from_format, 0, 0, 2)
169 	ZEND_ARG_INFO(0, format)
170 	ZEND_ARG_INFO(0, time)
171 	ZEND_ARG_OBJ_INFO(0, object, DateTimeZone, 1)
172 ZEND_END_ARG_INFO()
173 
174 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_parse, 0, 0, 1)
175 	ZEND_ARG_INFO(0, date)
176 ZEND_END_ARG_INFO()
177 
178 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_parse_from_format, 0, 0, 2)
179 	ZEND_ARG_INFO(0, format)
180 	ZEND_ARG_INFO(0, date)
181 ZEND_END_ARG_INFO()
182 
183 ZEND_BEGIN_ARG_INFO(arginfo_date_get_last_errors, 0)
184 ZEND_END_ARG_INFO()
185 
186 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_format, 0, 0, 2)
187 	ZEND_ARG_INFO(0, object)
188 	ZEND_ARG_INFO(0, format)
189 ZEND_END_ARG_INFO()
190 
191 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_format, 0, 0, 1)
192 	ZEND_ARG_INFO(0, format)
193 ZEND_END_ARG_INFO()
194 
195 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_modify, 0, 0, 2)
196 	ZEND_ARG_INFO(0, object)
197 	ZEND_ARG_INFO(0, modify)
198 ZEND_END_ARG_INFO()
199 
200 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_modify, 0, 0, 1)
201 	ZEND_ARG_INFO(0, modify)
202 ZEND_END_ARG_INFO()
203 
204 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_add, 0, 0, 2)
205 	ZEND_ARG_INFO(0, object)
206 	ZEND_ARG_INFO(0, interval)
207 ZEND_END_ARG_INFO()
208 
209 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_add, 0, 0, 1)
210 	ZEND_ARG_INFO(0, interval)
211 ZEND_END_ARG_INFO()
212 
213 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_sub, 0, 0, 2)
214 	ZEND_ARG_INFO(0, object)
215 	ZEND_ARG_INFO(0, interval)
216 ZEND_END_ARG_INFO()
217 
218 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_sub, 0, 0, 1)
219 	ZEND_ARG_INFO(0, interval)
220 ZEND_END_ARG_INFO()
221 
222 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_timezone_get, 0, 0, 1)
223 	ZEND_ARG_INFO(0, object)
224 ZEND_END_ARG_INFO()
225 
226 ZEND_BEGIN_ARG_INFO(arginfo_date_method_timezone_get, 0)
227 ZEND_END_ARG_INFO()
228 
229 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_timezone_set, 0, 0, 2)
230 	ZEND_ARG_INFO(0, object)
231 	ZEND_ARG_INFO(0, timezone)
232 ZEND_END_ARG_INFO()
233 
234 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_timezone_set, 0, 0, 1)
235 	ZEND_ARG_INFO(0, timezone)
236 ZEND_END_ARG_INFO()
237 
238 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_offset_get, 0, 0, 1)
239 	ZEND_ARG_INFO(0, object)
240 ZEND_END_ARG_INFO()
241 
242 ZEND_BEGIN_ARG_INFO(arginfo_date_method_offset_get, 0)
243 ZEND_END_ARG_INFO()
244 
245 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_diff, 0, 0, 2)
246 	ZEND_ARG_INFO(0, object)
247 	ZEND_ARG_INFO(0, object2)
248 	ZEND_ARG_INFO(0, absolute)
249 ZEND_END_ARG_INFO()
250 
251 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_diff, 0, 0, 1)
252 	ZEND_ARG_INFO(0, object)
253 	ZEND_ARG_INFO(0, absolute)
254 ZEND_END_ARG_INFO()
255 
256 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_time_set, 0, 0, 3)
257 	ZEND_ARG_INFO(0, object)
258 	ZEND_ARG_INFO(0, hour)
259 	ZEND_ARG_INFO(0, minute)
260 	ZEND_ARG_INFO(0, second)
261 	ZEND_ARG_INFO(0, microseconds)
262 ZEND_END_ARG_INFO()
263 
264 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_time_set, 0, 0, 2)
265 	ZEND_ARG_INFO(0, hour)
266 	ZEND_ARG_INFO(0, minute)
267 	ZEND_ARG_INFO(0, second)
268 	ZEND_ARG_INFO(0, microseconds)
269 ZEND_END_ARG_INFO()
270 
271 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_date_set, 0, 0, 4)
272 	ZEND_ARG_INFO(0, object)
273 	ZEND_ARG_INFO(0, year)
274 	ZEND_ARG_INFO(0, month)
275 	ZEND_ARG_INFO(0, day)
276 ZEND_END_ARG_INFO()
277 
278 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_date_set, 0, 0, 3)
279 	ZEND_ARG_INFO(0, year)
280 	ZEND_ARG_INFO(0, month)
281 	ZEND_ARG_INFO(0, day)
282 ZEND_END_ARG_INFO()
283 
284 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_isodate_set, 0, 0, 3)
285 	ZEND_ARG_INFO(0, object)
286 	ZEND_ARG_INFO(0, year)
287 	ZEND_ARG_INFO(0, week)
288 	ZEND_ARG_INFO(0, day)
289 ZEND_END_ARG_INFO()
290 
291 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_isodate_set, 0, 0, 2)
292 	ZEND_ARG_INFO(0, year)
293 	ZEND_ARG_INFO(0, week)
294 	ZEND_ARG_INFO(0, day)
295 ZEND_END_ARG_INFO()
296 
297 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_timestamp_set, 0, 0, 2)
298 	ZEND_ARG_INFO(0, object)
299 	ZEND_ARG_INFO(0, unixtimestamp)
300 ZEND_END_ARG_INFO()
301 
302 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_timestamp_set, 0, 0, 1)
303 	ZEND_ARG_INFO(0, unixtimestamp)
304 ZEND_END_ARG_INFO()
305 
306 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_timestamp_get, 0, 0, 1)
307 	ZEND_ARG_INFO(0, object)
308 ZEND_END_ARG_INFO()
309 
310 ZEND_BEGIN_ARG_INFO(arginfo_date_method_timestamp_get, 0)
311 ZEND_END_ARG_INFO()
312 
313 
314 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_immutable, 0, 0, 1)
315 	ZEND_ARG_INFO(0, DateTimeImmutable)
316 ZEND_END_ARG_INFO()
317 
318 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_mutable, 0, 0, 1)
319 	ZEND_ARG_INFO(0, DateTime)
320 ZEND_END_ARG_INFO()
321 
322 ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_open, 0, 0, 1)
323 	ZEND_ARG_INFO(0, timezone)
324 ZEND_END_ARG_INFO()
325 
326 ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_name_get, 0, 0, 1)
327 	ZEND_ARG_INFO(0, object)
328 ZEND_END_ARG_INFO()
329 
330 ZEND_BEGIN_ARG_INFO(arginfo_timezone_method_name_get, 0)
331 ZEND_END_ARG_INFO()
332 
333 ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_name_from_abbr, 0, 0, 1)
334 	ZEND_ARG_INFO(0, abbr)
335 	ZEND_ARG_INFO(0, gmtoffset)
336 	ZEND_ARG_INFO(0, isdst)
337 ZEND_END_ARG_INFO()
338 
339 ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_offset_get, 0, 0, 2)
340 	ZEND_ARG_OBJ_INFO(0, object, DateTimeZone, 0)
341 	ZEND_ARG_OBJ_INFO(0, datetime, DateTimeInterface, 0)
342 ZEND_END_ARG_INFO()
343 
344 ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_method_offset_get, 0, 0, 1)
345 	ZEND_ARG_INFO(0, object)
346 ZEND_END_ARG_INFO()
347 
348 ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_transitions_get, 0, 0, 1)
349 	ZEND_ARG_INFO(0, object)
350 	ZEND_ARG_INFO(0, timestamp_begin)
351 	ZEND_ARG_INFO(0, timestamp_end)
352 ZEND_END_ARG_INFO()
353 
354 ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_method_transitions_get, 0, 0, 0)
355 	ZEND_ARG_INFO(0, timestamp_begin)
356 	ZEND_ARG_INFO(0, timestamp_end)
357 ZEND_END_ARG_INFO()
358 
359 ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_location_get, 0, 0, 1)
360 	ZEND_ARG_INFO(0, object)
361 ZEND_END_ARG_INFO()
362 
363 ZEND_BEGIN_ARG_INFO(arginfo_timezone_method_location_get, 0)
364 ZEND_END_ARG_INFO()
365 
366 ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_identifiers_list, 0, 0, 0)
367 	ZEND_ARG_INFO(0, what)
368 	ZEND_ARG_INFO(0, country)
369 ZEND_END_ARG_INFO()
370 
371 ZEND_BEGIN_ARG_INFO(arginfo_timezone_abbreviations_list, 0)
372 ZEND_END_ARG_INFO()
373 
374 ZEND_BEGIN_ARG_INFO(arginfo_timezone_version_get, 0)
375 ZEND_END_ARG_INFO()
376 
377 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_interval_create_from_date_string, 0, 0, 1)
378 	ZEND_ARG_INFO(0, time)
379 ZEND_END_ARG_INFO()
380 
381 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_interval_format, 0, 0, 2)
382 	ZEND_ARG_INFO(0, object)
383 	ZEND_ARG_INFO(0, format)
384 ZEND_END_ARG_INFO()
385 
386 ZEND_BEGIN_ARG_INFO(arginfo_date_method_interval_format, 0)
387 	ZEND_ARG_INFO(0, format)
388 ZEND_END_ARG_INFO()
389 
390 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_period_construct, 0, 0, 3)
391 	ZEND_ARG_INFO(0, start)
392 	ZEND_ARG_INFO(0, interval)
393 	ZEND_ARG_INFO(0, end)
394 ZEND_END_ARG_INFO()
395 
396 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_set_state, 0, 0, 1)
397 	ZEND_ARG_ARRAY_INFO(0, array, 0)
398 ZEND_END_ARG_INFO()
399 
400 ZEND_BEGIN_ARG_INFO_EX(arginfo_date_interval_construct, 0, 0, 1)
401 	ZEND_ARG_INFO(0, interval_spec)
402 ZEND_END_ARG_INFO()
403 /* }}} */
404 
405 /* {{{ Function table */
406 static const zend_function_entry date_functions[] = {
407 	PHP_FE(strtotime, arginfo_strtotime)
408 	PHP_FE(date, arginfo_date)
409 	PHP_FE(idate, arginfo_idate)
410 	PHP_FE(gmdate, arginfo_gmdate)
411 	PHP_FE(mktime, arginfo_mktime)
412 	PHP_FE(gmmktime, arginfo_gmmktime)
413 	PHP_FE(checkdate, arginfo_checkdate)
414 
415 #ifdef HAVE_STRFTIME
416 	PHP_FE(strftime, arginfo_strftime)
417 	PHP_FE(gmstrftime, arginfo_gmstrftime)
418 #endif
419 
420 	PHP_FE(time, arginfo_time)
421 	PHP_FE(localtime, arginfo_localtime)
422 	PHP_FE(getdate, arginfo_getdate)
423 
424 	/* Advanced Interface */
425 	PHP_FE(date_create, arginfo_date_create)
426 	PHP_FE(date_create_immutable, arginfo_date_create)
427 	PHP_FE(date_create_from_format, arginfo_date_create_from_format)
428 	PHP_FE(date_create_immutable_from_format, arginfo_date_create_from_format)
429 	PHP_FE(date_parse, arginfo_date_parse)
430 	PHP_FE(date_parse_from_format, arginfo_date_parse_from_format)
431 	PHP_FE(date_get_last_errors, arginfo_date_get_last_errors)
432 	PHP_FE(date_format, arginfo_date_format)
433 	PHP_FE(date_modify, arginfo_date_modify)
434 	PHP_FE(date_add, arginfo_date_add)
435 	PHP_FE(date_sub, arginfo_date_sub)
436 	PHP_FE(date_timezone_get, arginfo_date_timezone_get)
437 	PHP_FE(date_timezone_set, arginfo_date_timezone_set)
438 	PHP_FE(date_offset_get, arginfo_date_offset_get)
439 	PHP_FE(date_diff, arginfo_date_diff)
440 
441 	PHP_FE(date_time_set, arginfo_date_time_set)
442 	PHP_FE(date_date_set, arginfo_date_date_set)
443 	PHP_FE(date_isodate_set, arginfo_date_isodate_set)
444 	PHP_FE(date_timestamp_set, arginfo_date_timestamp_set)
445 	PHP_FE(date_timestamp_get, arginfo_date_timestamp_get)
446 
447 	PHP_FE(timezone_open, arginfo_timezone_open)
448 	PHP_FE(timezone_name_get, arginfo_timezone_name_get)
449 	PHP_FE(timezone_name_from_abbr, arginfo_timezone_name_from_abbr)
450 	PHP_FE(timezone_offset_get, arginfo_timezone_offset_get)
451 	PHP_FE(timezone_transitions_get, arginfo_timezone_transitions_get)
452 	PHP_FE(timezone_location_get, arginfo_timezone_location_get)
453 	PHP_FE(timezone_identifiers_list, arginfo_timezone_identifiers_list)
454 	PHP_FE(timezone_abbreviations_list, arginfo_timezone_abbreviations_list)
455 	PHP_FE(timezone_version_get, arginfo_timezone_version_get)
456 
457 	PHP_FE(date_interval_create_from_date_string, arginfo_date_interval_create_from_date_string)
458 	PHP_FE(date_interval_format, arginfo_date_interval_format)
459 
460 	/* Options and Configuration */
461 	PHP_FE(date_default_timezone_set, arginfo_date_default_timezone_set)
462 	PHP_FE(date_default_timezone_get, arginfo_date_default_timezone_get)
463 
464 	/* Astronomical functions */
465 	PHP_FE(date_sunrise, arginfo_date_sunrise)
466 	PHP_FE(date_sunset, arginfo_date_sunset)
467 	PHP_FE(date_sun_info, arginfo_date_sun_info)
468 	PHP_FE_END
469 };
470 
471 static const zend_function_entry date_funcs_interface[] = {
472 	PHP_ABSTRACT_ME(DateTimeInterface, format, arginfo_date_method_format)
473 	PHP_ABSTRACT_ME(DateTimeInterface, getTimezone, arginfo_date_method_timezone_get)
474 	PHP_ABSTRACT_ME(DateTimeInterface, getOffset, arginfo_date_method_offset_get)
475 	PHP_ABSTRACT_ME(DateTimeInterface, getTimestamp, arginfo_date_method_timestamp_get)
476 	PHP_ABSTRACT_ME(DateTimeInterface, diff, arginfo_date_method_diff)
477 	PHP_ABSTRACT_ME(DateTimeInterface, __wakeup, NULL)
478 	PHP_FE_END
479 };
480 
481 static const zend_function_entry date_funcs_date[] = {
482 	PHP_ME(DateTime,			__construct,		arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
483 	PHP_ME(DateTime,			__wakeup,			NULL, ZEND_ACC_PUBLIC)
484 	PHP_ME(DateTime,			__set_state,		arginfo_date_set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
485 	PHP_ME(DateTime,			createFromImmutable,	arginfo_date_method_create_from_immutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
486 	PHP_ME_MAPPING(createFromFormat, date_create_from_format,	arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
487 	PHP_ME_MAPPING(getLastErrors, date_get_last_errors,	arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
488 	PHP_ME_MAPPING(format,		date_format,		arginfo_date_method_format, 0)
489 	PHP_ME_MAPPING(modify,		date_modify,		arginfo_date_method_modify, 0)
490 	PHP_ME_MAPPING(add,			date_add,			arginfo_date_method_add, 0)
491 	PHP_ME_MAPPING(sub,			date_sub,			arginfo_date_method_sub, 0)
492 	PHP_ME_MAPPING(getTimezone, date_timezone_get,	arginfo_date_method_timezone_get, 0)
493 	PHP_ME_MAPPING(setTimezone, date_timezone_set,	arginfo_date_method_timezone_set, 0)
494 	PHP_ME_MAPPING(getOffset,	date_offset_get,	arginfo_date_method_offset_get, 0)
495 	PHP_ME_MAPPING(setTime,		date_time_set,		arginfo_date_method_time_set, 0)
496 	PHP_ME_MAPPING(setDate,		date_date_set,		arginfo_date_method_date_set, 0)
497 	PHP_ME_MAPPING(setISODate,	date_isodate_set,	arginfo_date_method_isodate_set, 0)
498 	PHP_ME_MAPPING(setTimestamp,	date_timestamp_set, arginfo_date_method_timestamp_set, 0)
499 	PHP_ME_MAPPING(getTimestamp,	date_timestamp_get, arginfo_date_method_timestamp_get, 0)
500 	PHP_ME_MAPPING(diff,			date_diff, arginfo_date_method_diff, 0)
501 	PHP_FE_END
502 };
503 
504 static const zend_function_entry date_funcs_immutable[] = {
505 	PHP_ME(DateTimeImmutable, __construct,   arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
506 	PHP_ME(DateTime, __wakeup,       NULL, ZEND_ACC_PUBLIC)
507 	PHP_ME(DateTimeImmutable, __set_state,   arginfo_date_set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
508 	PHP_ME_MAPPING(createFromFormat, date_create_immutable_from_format, arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
509 	PHP_ME_MAPPING(getLastErrors,    date_get_last_errors,    arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
510 	PHP_ME_MAPPING(format,           date_format,             arginfo_date_method_format, 0)
511 	PHP_ME_MAPPING(getTimezone, date_timezone_get,	arginfo_date_method_timezone_get, 0)
512 	PHP_ME_MAPPING(getOffset,	date_offset_get,	arginfo_date_method_offset_get, 0)
513 	PHP_ME_MAPPING(getTimestamp,	date_timestamp_get, arginfo_date_method_timestamp_get, 0)
514 	PHP_ME_MAPPING(diff,			date_diff, arginfo_date_method_diff, 0)
515 	PHP_ME(DateTimeImmutable, modify,        arginfo_date_method_modify, 0)
516 	PHP_ME(DateTimeImmutable, add,           arginfo_date_method_add, 0)
517 	PHP_ME(DateTimeImmutable, sub,           arginfo_date_method_sub, 0)
518 	PHP_ME(DateTimeImmutable, setTimezone,   arginfo_date_method_timezone_set, 0)
519 	PHP_ME(DateTimeImmutable, setTime,       arginfo_date_method_time_set, 0)
520 	PHP_ME(DateTimeImmutable, setDate,       arginfo_date_method_date_set, 0)
521 	PHP_ME(DateTimeImmutable, setISODate,    arginfo_date_method_isodate_set, 0)
522 	PHP_ME(DateTimeImmutable, setTimestamp,  arginfo_date_method_timestamp_set, 0)
523 	PHP_ME(DateTimeImmutable, createFromMutable, arginfo_date_method_create_from_mutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
524 	PHP_FE_END
525 };
526 
527 static const zend_function_entry date_funcs_timezone[] = {
528 	PHP_ME(DateTimeZone,              __construct,                 arginfo_timezone_open, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
529 	PHP_ME(DateTimeZone,              __wakeup,                    NULL, ZEND_ACC_PUBLIC)
530 	PHP_ME(DateTimeZone,              __set_state,                 arginfo_date_set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
531 	PHP_ME_MAPPING(getName,           timezone_name_get,           arginfo_timezone_method_name_get, 0)
532 	PHP_ME_MAPPING(getOffset,         timezone_offset_get,         arginfo_timezone_method_offset_get, 0)
533 	PHP_ME_MAPPING(getTransitions,    timezone_transitions_get,    arginfo_timezone_method_transitions_get, 0)
534 	PHP_ME_MAPPING(getLocation,       timezone_location_get,       arginfo_timezone_method_location_get, 0)
535 	PHP_ME_MAPPING(listAbbreviations, timezone_abbreviations_list, arginfo_timezone_abbreviations_list, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
536 	PHP_ME_MAPPING(listIdentifiers,   timezone_identifiers_list,   arginfo_timezone_identifiers_list, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
537 	PHP_FE_END
538 };
539 
540 static const zend_function_entry date_funcs_interval[] = {
541 	PHP_ME(DateInterval,              __construct,                 arginfo_date_interval_construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
542 	PHP_ME(DateInterval,              __wakeup,                    NULL, ZEND_ACC_PUBLIC)
543 	PHP_ME(DateInterval,              __set_state,                 arginfo_date_set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
544 	PHP_ME_MAPPING(format,            date_interval_format,        arginfo_date_method_interval_format, 0)
545 	PHP_ME_MAPPING(createFromDateString, date_interval_create_from_date_string,	arginfo_date_interval_create_from_date_string, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
546 	PHP_FE_END
547 };
548 
549 static const zend_function_entry date_funcs_period[] = {
550 	PHP_ME(DatePeriod,                __construct,                 arginfo_date_period_construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
551 	PHP_ME(DatePeriod,                __wakeup,                    NULL, ZEND_ACC_PUBLIC)
552 	PHP_ME(DatePeriod,                __set_state,                 arginfo_date_set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
553 	PHP_ME(DatePeriod,                getStartDate,                NULL, ZEND_ACC_PUBLIC)
554 	PHP_ME(DatePeriod,                getEndDate,                  NULL, ZEND_ACC_PUBLIC)
555 	PHP_ME(DatePeriod,                getDateInterval,             NULL, ZEND_ACC_PUBLIC)
556 	PHP_FE_END
557 };
558 
559 static char* guess_timezone(const timelib_tzdb *tzdb);
560 static void date_register_classes(void);
561 /* }}} */
562 
563 ZEND_DECLARE_MODULE_GLOBALS(date)
564 static PHP_GINIT_FUNCTION(date);
565 
566 /* True global */
567 timelib_tzdb *php_date_global_timezone_db;
568 int php_date_global_timezone_db_enabled;
569 
570 #define DATE_DEFAULT_LATITUDE "31.7667"
571 #define DATE_DEFAULT_LONGITUDE "35.2333"
572 
573 /* on 90'35; common sunset declaration (start of sun body appear) */
574 #define DATE_SUNSET_ZENITH "90.583333"
575 
576 /* on 90'35; common sunrise declaration (sun body disappeared) */
577 #define DATE_SUNRISE_ZENITH "90.583333"
578 
579 static PHP_INI_MH(OnUpdate_date_timezone);
580 
581 /* {{{ INI Settings */
582 PHP_INI_BEGIN()
583 	STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
584 	PHP_INI_ENTRY("date.default_latitude",           DATE_DEFAULT_LATITUDE,        PHP_INI_ALL, NULL)
585 	PHP_INI_ENTRY("date.default_longitude",          DATE_DEFAULT_LONGITUDE,       PHP_INI_ALL, NULL)
586 	PHP_INI_ENTRY("date.sunset_zenith",              DATE_SUNSET_ZENITH,           PHP_INI_ALL, NULL)
587 	PHP_INI_ENTRY("date.sunrise_zenith",             DATE_SUNRISE_ZENITH,          PHP_INI_ALL, NULL)
588 PHP_INI_END()
589 /* }}} */
590 
591 zend_class_entry *date_ce_date, *date_ce_timezone, *date_ce_interval, *date_ce_period;
592 zend_class_entry *date_ce_immutable, *date_ce_interface;
593 
594 
php_date_get_date_ce(void)595 PHPAPI zend_class_entry *php_date_get_date_ce(void)
596 {
597 	return date_ce_date;
598 }
599 
php_date_get_immutable_ce(void)600 PHPAPI zend_class_entry *php_date_get_immutable_ce(void)
601 {
602 	return date_ce_immutable;
603 }
604 
php_date_get_interface_ce(void)605 PHPAPI zend_class_entry *php_date_get_interface_ce(void)
606 {
607 	return date_ce_interface;
608 }
609 
php_date_get_timezone_ce(void)610 PHPAPI zend_class_entry *php_date_get_timezone_ce(void)
611 {
612 	return date_ce_timezone;
613 }
614 
615 static zend_object_handlers date_object_handlers_date;
616 static zend_object_handlers date_object_handlers_immutable;
617 static zend_object_handlers date_object_handlers_timezone;
618 static zend_object_handlers date_object_handlers_interval;
619 static zend_object_handlers date_object_handlers_period;
620 
621 #define DATE_SET_CONTEXT \
622 	zval *object; \
623 	object = getThis(); \
624 
625 #define DATE_FETCH_OBJECT	\
626 	php_date_obj *obj;	\
627 	DATE_SET_CONTEXT; \
628 	if (object) {	\
629 		if (zend_parse_parameters_none() == FAILURE) {	\
630 			return;	\
631 		}	\
632 	} else {	\
633 		if (zend_parse_method_parameters(ZEND_NUM_ARGS(), NULL, "O", &object, date_ce_date) == FAILURE) {	\
634 			RETURN_FALSE;	\
635 		}	\
636 	}	\
637 	obj = Z_PHPDATE_P(object);	\
638 
639 #define DATE_CHECK_INITIALIZED(member, class_name) \
640 	if (!(member)) { \
641 		php_error_docref(NULL, E_WARNING, "The " #class_name " object has not been correctly initialized by its constructor"); \
642 		RETURN_FALSE; \
643 	}
644 
645 static void date_object_free_storage_date(zend_object *object);
646 static void date_object_free_storage_timezone(zend_object *object);
647 static void date_object_free_storage_interval(zend_object *object);
648 static void date_object_free_storage_period(zend_object *object);
649 
650 static zend_object *date_object_new_date(zend_class_entry *class_type);
651 static zend_object *date_object_new_timezone(zend_class_entry *class_type);
652 static zend_object *date_object_new_interval(zend_class_entry *class_type);
653 static zend_object *date_object_new_period(zend_class_entry *class_type);
654 
655 static zend_object *date_object_clone_date(zval *this_ptr);
656 static zend_object *date_object_clone_timezone(zval *this_ptr);
657 static zend_object *date_object_clone_interval(zval *this_ptr);
658 static zend_object *date_object_clone_period(zval *this_ptr);
659 
660 static int date_object_compare_date(zval *d1, zval *d2);
661 static HashTable *date_object_get_gc(zval *object, zval **table, int *n);
662 static HashTable *date_object_get_properties(zval *object);
663 static HashTable *date_object_get_gc_interval(zval *object, zval **table, int *n);
664 static HashTable *date_object_get_properties_interval(zval *object);
665 static HashTable *date_object_get_gc_period(zval *object, zval **table, int *n);
666 static HashTable *date_object_get_properties_period(zval *object);
667 static HashTable *date_object_get_properties_timezone(zval *object);
668 static HashTable *date_object_get_gc_timezone(zval *object, zval **table, int *n);
669 static HashTable *date_object_get_debug_info_timezone(zval *object, int *is_temp);
670 static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv);
671 
672 zval *date_interval_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv);
673 void date_interval_write_property(zval *object, zval *member, zval *value, void **cache_slot);
674 static zval *date_interval_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot);
675 static zval *date_period_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv);
676 static void date_period_write_property(zval *object, zval *member, zval *value, void **cache_slot);
677 
678 /* {{{ Module struct */
679 zend_module_entry date_module_entry = {
680 	STANDARD_MODULE_HEADER_EX,
681 	NULL,
682 	NULL,
683 	"date",                     /* extension name */
684 	date_functions,             /* function list */
685 	PHP_MINIT(date),            /* process startup */
686 	PHP_MSHUTDOWN(date),        /* process shutdown */
687 	PHP_RINIT(date),            /* request startup */
688 	PHP_RSHUTDOWN(date),        /* request shutdown */
689 	PHP_MINFO(date),            /* extension info */
690 	PHP_DATE_VERSION,                /* extension version */
691 	PHP_MODULE_GLOBALS(date),   /* globals descriptor */
692 	PHP_GINIT(date),            /* globals ctor */
693 	NULL,                       /* globals dtor */
694 	NULL,                       /* post deactivate */
695 	STANDARD_MODULE_PROPERTIES_EX
696 };
697 /* }}} */
698 
699 
700 /* {{{ PHP_GINIT_FUNCTION */
PHP_GINIT_FUNCTION(date)701 static PHP_GINIT_FUNCTION(date)
702 {
703 	date_globals->default_timezone = NULL;
704 	date_globals->timezone = NULL;
705 	date_globals->tzcache = NULL;
706 	date_globals->timezone_valid = 0;
707 }
708 /* }}} */
709 
710 
_php_date_tzinfo_dtor(zval * zv)711 static void _php_date_tzinfo_dtor(zval *zv) /* {{{ */
712 {
713 	timelib_tzinfo *tzi = (timelib_tzinfo*)Z_PTR_P(zv);
714 
715 	timelib_tzinfo_dtor(tzi);
716 } /* }}} */
717 
718 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(date)719 PHP_RINIT_FUNCTION(date)
720 {
721 	if (DATEG(timezone)) {
722 		efree(DATEG(timezone));
723 	}
724 	DATEG(timezone) = NULL;
725 	DATEG(tzcache) = NULL;
726 	DATEG(last_errors) = NULL;
727 
728 	return SUCCESS;
729 }
730 /* }}} */
731 
732 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(date)733 PHP_RSHUTDOWN_FUNCTION(date)
734 {
735 	if (DATEG(timezone)) {
736 		efree(DATEG(timezone));
737 	}
738 	DATEG(timezone) = NULL;
739 	if(DATEG(tzcache)) {
740 		zend_hash_destroy(DATEG(tzcache));
741 		FREE_HASHTABLE(DATEG(tzcache));
742 		DATEG(tzcache) = NULL;
743 	}
744 	if (DATEG(last_errors)) {
745 		timelib_error_container_dtor(DATEG(last_errors));
746 		DATEG(last_errors) = NULL;
747 	}
748 
749 	return SUCCESS;
750 }
751 /* }}} */
752 
753 #define DATE_TIMEZONEDB      php_date_global_timezone_db ? php_date_global_timezone_db : timelib_builtin_db()
754 
755 /*
756  * RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
757  *  date-time   =  [ day "," ] date time        ; dd mm yy hh:mm:ss zzz
758  *  day         =  "Mon"  / "Tue" /  "Wed"  / "Thu"  /  "Fri"  / "Sat" /  "Sun"
759  *  date        =  1*2DIGIT month 2DIGIT        ; day month year e.g. 20 Jun 82
760  *  month       =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"  /  "May"  /  "Jun" /  "Jul"  /  "Aug"  /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
761  *  time        =  hour zone                    ; ANSI and Military
762  *  hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
763  *  zone        =  "UT"  / "GMT"  /  "EST" / "EDT"  /  "CST" / "CDT"  /  "MST" / "MDT"  /  "PST" / "PDT"  /  1ALPHA  / ( ("+" / "-") 4DIGIT )
764  */
765 #define DATE_FORMAT_RFC822   "D, d M y H:i:s O"
766 
767 /*
768  * RFC850, Section 2.1.4: http://www.ietf.org/rfc/rfc850.txt
769  *  Format must be acceptable both to the ARPANET and to the getdate routine.
770  *  One format that is acceptable to both is Weekday, DD-Mon-YY HH:MM:SS TIMEZONE
771  *  TIMEZONE can be any timezone name (3 or more letters)
772  */
773 #define DATE_FORMAT_RFC850   "l, d-M-y H:i:s T"
774 
775 /*
776  * RFC1036, Section 2.1.2: http://www.ietf.org/rfc/rfc1036.txt
777  *  Its format must be acceptable both in RFC-822 and to the getdate(3)
778  *  Wdy, DD Mon YY HH:MM:SS TIMEZONE
779  *  There is no hope of having a complete list of timezones.  Universal
780  *  Time (GMT), the North American timezones (PST, PDT, MST, MDT, CST,
781  *  CDT, EST, EDT) and the +/-hhmm offset specifed in RFC-822 should be supported.
782  */
783 #define DATE_FORMAT_RFC1036  "D, d M y H:i:s O"
784 
785 /*
786  * RFC1123, Section 5.2.14: http://www.ietf.org/rfc/rfc1123.txt
787  *  RFC-822 Date and Time Specification: RFC-822 Section 5
788  *  The syntax for the date is hereby changed to: date = 1*2DIGIT month 2*4DIGIT
789  */
790 #define DATE_FORMAT_RFC1123  "D, d M Y H:i:s O"
791 
792 /*
793  * RFC7231, Section 7.1.1: http://tools.ietf.org/html/rfc7231
794  */
795 #define DATE_FORMAT_RFC7231  "D, d M Y H:i:s \\G\\M\\T"
796 
797 /*
798  * RFC2822, Section 3.3: http://www.ietf.org/rfc/rfc2822.txt
799  *  FWS             =       ([*WSP CRLF] 1*WSP) /   ; Folding white space
800  *  CFWS            =       *([FWS] comment) (([FWS] comment) / FWS)
801  *
802  *  date-time       =       [ day-of-week "," ] date FWS time [CFWS]
803  *  day-of-week     =       ([FWS] day-name)
804  *  day-name        =       "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
805  *  date            =       day month year
806  *  year            =       4*DIGIT
807  *  month           =       (FWS month-name FWS)
808  *  month-name      =       "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
809  *  day             =       ([FWS] 1*2DIGIT)
810  *  time            =       time-of-day FWS zone
811  *  time-of-day     =       hour ":" minute [ ":" second ]
812  *  hour            =       2DIGIT
813  *  minute          =       2DIGIT
814  *  second          =       2DIGIT
815  *  zone            =       (( "+" / "-" ) 4DIGIT)
816  */
817 #define DATE_FORMAT_RFC2822  "D, d M Y H:i:s O"
818 /*
819  * RFC3339, Section 5.6: http://www.ietf.org/rfc/rfc3339.txt
820  *  date-fullyear   = 4DIGIT
821  *  date-month      = 2DIGIT  ; 01-12
822  *  date-mday       = 2DIGIT  ; 01-28, 01-29, 01-30, 01-31 based on month/year
823  *
824  *  time-hour       = 2DIGIT  ; 00-23
825  *  time-minute     = 2DIGIT  ; 00-59
826  *  time-second     = 2DIGIT  ; 00-58, 00-59, 00-60 based on leap second rules
827  *
828  *  time-secfrac    = "." 1*DIGIT
829  *  time-numoffset  = ("+" / "-") time-hour ":" time-minute
830  *  time-offset     = "Z" / time-numoffset
831  *
832  *  partial-time    = time-hour ":" time-minute ":" time-second [time-secfrac]
833  *  full-date       = date-fullyear "-" date-month "-" date-mday
834  *  full-time       = partial-time time-offset
835  *
836  *  date-time       = full-date "T" full-time
837  */
838 #define DATE_FORMAT_RFC3339  "Y-m-d\\TH:i:sP"
839 
840 #define DATE_FORMAT_ISO8601  "Y-m-d\\TH:i:sO"
841 
842 /*
843  * RFC3339, Appendix A: http://www.ietf.org/rfc/rfc3339.txt
844  *  ISO 8601 also requires (in section 5.3.1.3) that a decimal fraction
845  *  be proceeded by a "0" if less than unity.  Annex B.2 of ISO 8601
846  *  gives examples where the decimal fractions are not preceded by a "0".
847  *  This grammar assumes section 5.3.1.3 is correct and that Annex B.2 is
848  *  in error.
849  */
850 #define DATE_FORMAT_RFC3339_EXTENDED  "Y-m-d\\TH:i:s.vP"
851 
852 /*
853  * This comes from various sources that like to contradict. I'm going with the
854  * format here because of:
855  * http://msdn.microsoft.com/en-us/library/windows/desktop/aa384321%28v=vs.85%29.aspx
856  * and http://curl.haxx.se/rfc/cookie_spec.html
857  */
858 #define DATE_FORMAT_COOKIE   "l, d-M-Y H:i:s T"
859 
860 #define SUNFUNCS_RET_TIMESTAMP 0
861 #define SUNFUNCS_RET_STRING    1
862 #define SUNFUNCS_RET_DOUBLE    2
863 
864 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(date)865 PHP_MINIT_FUNCTION(date)
866 {
867 	REGISTER_INI_ENTRIES();
868 	date_register_classes();
869 /*
870  * RFC4287, Section 3.3: http://www.ietf.org/rfc/rfc4287.txt
871  *   A Date construct is an element whose content MUST conform to the
872  *   "date-time" production in [RFC3339].  In addition, an uppercase "T"
873  *   character MUST be used to separate date and time, and an uppercase
874  *   "Z" character MUST be present in the absence of a numeric time zone offset.
875  */
876 	REGISTER_STRING_CONSTANT("DATE_ATOM",    DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
877 /*
878  * Preliminary specification: http://wp.netscape.com/newsref/std/cookie_spec.html
879  *   "This is based on RFC 822, RFC 850,  RFC 1036, and  RFC 1123,
880  *   with the variations that the only legal time zone is GMT
881  *   and the separators between the elements of the date must be dashes."
882  */
883 	REGISTER_STRING_CONSTANT("DATE_COOKIE",  DATE_FORMAT_COOKIE,  CONST_CS | CONST_PERSISTENT);
884 	REGISTER_STRING_CONSTANT("DATE_ISO8601", DATE_FORMAT_ISO8601, CONST_CS | CONST_PERSISTENT);
885 
886 	REGISTER_STRING_CONSTANT("DATE_RFC822",  DATE_FORMAT_RFC822,  CONST_CS | CONST_PERSISTENT);
887 	REGISTER_STRING_CONSTANT("DATE_RFC850",  DATE_FORMAT_RFC850,  CONST_CS | CONST_PERSISTENT);
888 	REGISTER_STRING_CONSTANT("DATE_RFC1036", DATE_FORMAT_RFC1036, CONST_CS | CONST_PERSISTENT);
889 	REGISTER_STRING_CONSTANT("DATE_RFC1123", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
890 	REGISTER_STRING_CONSTANT("DATE_RFC7231", DATE_FORMAT_RFC7231, CONST_CS | CONST_PERSISTENT);
891 	REGISTER_STRING_CONSTANT("DATE_RFC2822", DATE_FORMAT_RFC2822, CONST_CS | CONST_PERSISTENT);
892 	REGISTER_STRING_CONSTANT("DATE_RFC3339", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
893 	REGISTER_STRING_CONSTANT("DATE_RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED, CONST_CS | CONST_PERSISTENT);
894 
895 /*
896  * RSS 2.0 Specification: http://blogs.law.harvard.edu/tech/rss
897  *   "All date-times in RSS conform to the Date and Time Specification of RFC 822,
898  *   with the exception that the year may be expressed with two characters or four characters (four preferred)"
899  */
900 	REGISTER_STRING_CONSTANT("DATE_RSS",     DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
901 	REGISTER_STRING_CONSTANT("DATE_W3C",     DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
902 
903 	REGISTER_LONG_CONSTANT("SUNFUNCS_RET_TIMESTAMP", SUNFUNCS_RET_TIMESTAMP, CONST_CS | CONST_PERSISTENT);
904 	REGISTER_LONG_CONSTANT("SUNFUNCS_RET_STRING", SUNFUNCS_RET_STRING, CONST_CS | CONST_PERSISTENT);
905 	REGISTER_LONG_CONSTANT("SUNFUNCS_RET_DOUBLE", SUNFUNCS_RET_DOUBLE, CONST_CS | CONST_PERSISTENT);
906 
907 	php_date_global_timezone_db = NULL;
908 	php_date_global_timezone_db_enabled = 0;
909 	DATEG(last_errors) = NULL;
910 	return SUCCESS;
911 }
912 /* }}} */
913 
914 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(date)915 PHP_MSHUTDOWN_FUNCTION(date)
916 {
917 	UNREGISTER_INI_ENTRIES();
918 
919 	if (DATEG(last_errors)) {
920 		timelib_error_container_dtor(DATEG(last_errors));
921 	}
922 
923 #ifndef ZTS
924 	DATEG(default_timezone) = NULL;
925 #endif
926 
927 	return SUCCESS;
928 }
929 /* }}} */
930 
931 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(date)932 PHP_MINFO_FUNCTION(date)
933 {
934 	const timelib_tzdb *tzdb = DATE_TIMEZONEDB;
935 
936 	php_info_print_table_start();
937 	php_info_print_table_row(2, "date/time support", "enabled");
938 	php_info_print_table_row(2, "timelib version", TIMELIB_ASCII_VERSION);
939 	php_info_print_table_row(2, "\"Olson\" Timezone Database Version", tzdb->version);
940 	php_info_print_table_row(2, "Timezone Database", php_date_global_timezone_db_enabled ? "external" : "internal");
941 	php_info_print_table_row(2, "Default timezone", guess_timezone(tzdb));
942 	php_info_print_table_end();
943 
944 	DISPLAY_INI_ENTRIES();
945 }
946 /* }}} */
947 
948 /* {{{ Timezone Cache functions */
php_date_parse_tzfile(char * formal_tzname,const timelib_tzdb * tzdb)949 static timelib_tzinfo *php_date_parse_tzfile(char *formal_tzname, const timelib_tzdb *tzdb)
950 {
951 	timelib_tzinfo *tzi;
952 	int dummy_error_code;
953 
954 	if(!DATEG(tzcache)) {
955 		ALLOC_HASHTABLE(DATEG(tzcache));
956 		zend_hash_init(DATEG(tzcache), 4, NULL, _php_date_tzinfo_dtor, 0);
957 	}
958 
959 	if ((tzi = zend_hash_str_find_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname))) != NULL) {
960 		return tzi;
961 	}
962 
963 	tzi = timelib_parse_tzfile(formal_tzname, tzdb, &dummy_error_code);
964 	if (tzi) {
965 		zend_hash_str_add_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname), tzi);
966 	}
967 	return tzi;
968 }
969 
php_date_parse_tzfile_wrapper(char * formal_tzname,const timelib_tzdb * tzdb,int * dummy_error_code)970 timelib_tzinfo *php_date_parse_tzfile_wrapper(char *formal_tzname, const timelib_tzdb *tzdb, int *dummy_error_code)
971 {
972 	return php_date_parse_tzfile(formal_tzname, tzdb);
973 }
974 /* }}} */
975 
976 /* Callback to check the date.timezone only when changed increases performance */
977 /* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
PHP_INI_MH(OnUpdate_date_timezone)978 static PHP_INI_MH(OnUpdate_date_timezone)
979 {
980 	if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
981 		return FAILURE;
982 	}
983 
984 	DATEG(timezone_valid) = 0;
985 	if (stage == PHP_INI_STAGE_RUNTIME) {
986 		if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
987 			if (DATEG(default_timezone) && *DATEG(default_timezone)) {
988 				php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
989 			}
990 		} else {
991 			DATEG(timezone_valid) = 1;
992 		}
993 	}
994 
995 	return SUCCESS;
996 }
997 /* }}} */
998 
999 /* {{{ Helper functions */
guess_timezone(const timelib_tzdb * tzdb)1000 static char* guess_timezone(const timelib_tzdb *tzdb)
1001 {
1002 	/* Checking configure timezone */
1003 	if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
1004 		return DATEG(timezone);
1005 	}
1006 	/* Check config setting for default timezone */
1007 	if (!DATEG(default_timezone)) {
1008 		/* Special case: ext/date wasn't initialized yet */
1009 		zval *ztz;
1010 
1011 		if (NULL != (ztz = cfg_get_entry("date.timezone", sizeof("date.timezone")))
1012 			&& Z_TYPE_P(ztz) == IS_STRING && Z_STRLEN_P(ztz) > 0 && timelib_timezone_id_is_valid(Z_STRVAL_P(ztz), tzdb)) {
1013 			return Z_STRVAL_P(ztz);
1014 		}
1015 	} else if (*DATEG(default_timezone)) {
1016 		if (DATEG(timezone_valid) == 1) {
1017 			return DATEG(default_timezone);
1018 		}
1019 
1020 		if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
1021 			php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
1022 			return "UTC";
1023 		}
1024 
1025 		DATEG(timezone_valid) = 1;
1026 		return DATEG(default_timezone);
1027 	}
1028 	/* Fallback to UTC */
1029 	return "UTC";
1030 }
1031 
get_timezone_info(void)1032 PHPAPI timelib_tzinfo *get_timezone_info(void)
1033 {
1034 	char *tz;
1035 	timelib_tzinfo *tzi;
1036 
1037 	tz = guess_timezone(DATE_TIMEZONEDB);
1038 	tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
1039 	if (! tzi) {
1040 		php_error_docref(NULL, E_ERROR, "Timezone database is corrupt - this should *never* happen!");
1041 	}
1042 	return tzi;
1043 }
1044 /* }}} */
1045 
1046 
1047 /* {{{ date() and gmdate() data */
1048 #include "zend_smart_str.h"
1049 
1050 static const char * const mon_full_names[] = {
1051 	"January", "February", "March", "April",
1052 	"May", "June", "July", "August",
1053 	"September", "October", "November", "December"
1054 };
1055 
1056 static const char * const mon_short_names[] = {
1057 	"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1058 };
1059 
1060 static const char * const day_full_names[] = {
1061 	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
1062 };
1063 
1064 static const char * const day_short_names[] = {
1065 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1066 };
1067 
english_suffix(timelib_sll number)1068 static char *english_suffix(timelib_sll number)
1069 {
1070 	if (number >= 10 && number <= 19) {
1071 		return "th";
1072 	} else {
1073 		switch (number % 10) {
1074 			case 1: return "st";
1075 			case 2: return "nd";
1076 			case 3: return "rd";
1077 		}
1078 	}
1079 	return "th";
1080 }
1081 /* }}} */
1082 
1083 /* {{{ day of week helpers */
php_date_full_day_name(timelib_sll y,timelib_sll m,timelib_sll d)1084 static const char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
1085 {
1086 	timelib_sll day_of_week = timelib_day_of_week(y, m, d);
1087 	if (day_of_week < 0) {
1088 		return "Unknown";
1089 	}
1090 	return day_full_names[day_of_week];
1091 }
1092 
php_date_short_day_name(timelib_sll y,timelib_sll m,timelib_sll d)1093 static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
1094 {
1095 	timelib_sll day_of_week = timelib_day_of_week(y, m, d);
1096 	if (day_of_week < 0) {
1097 		return "Unknown";
1098 	}
1099 	return day_short_names[day_of_week];
1100 }
1101 /* }}} */
1102 
1103 /* {{{ date_format - (gm)date helper */
date_format(char * format,size_t format_len,timelib_time * t,int localtime)1104 static zend_string *date_format(char *format, size_t format_len, timelib_time *t, int localtime)
1105 {
1106 	smart_str            string = {0};
1107 	size_t               i;
1108 	int                  length = 0;
1109 	char                 buffer[97];
1110 	timelib_time_offset *offset = NULL;
1111 	timelib_sll          isoweek, isoyear;
1112 	int                  rfc_colon;
1113 	int                  weekYearSet = 0;
1114 
1115 	if (!format_len) {
1116 		return ZSTR_EMPTY_ALLOC();
1117 	}
1118 
1119 	if (localtime) {
1120 		if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
1121 			offset = timelib_time_offset_ctor();
1122 			offset->offset = (t->z + (t->dst * 3600));
1123 			offset->leap_secs = 0;
1124 			offset->is_dst = t->dst;
1125 			offset->abbr = timelib_strdup(t->tz_abbr);
1126 		} else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
1127 			offset = timelib_time_offset_ctor();
1128 			offset->offset = (t->z);
1129 			offset->leap_secs = 0;
1130 			offset->is_dst = 0;
1131 			offset->abbr = timelib_malloc(9); /* GMT��xxxx\0 */
1132 			snprintf(offset->abbr, 9, "GMT%c%02d%02d",
1133 			                          (offset->offset < 0) ? '-' : '+',
1134 			                          abs(offset->offset / 3600),
1135 			                          abs((offset->offset % 3600) / 60));
1136 		} else {
1137 			offset = timelib_get_time_zone_info(t->sse, t->tz_info);
1138 		}
1139 	}
1140 
1141 	for (i = 0; i < format_len; i++) {
1142 		rfc_colon = 0;
1143 		switch (format[i]) {
1144 			/* day */
1145 			case 'd': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
1146 			case 'D': length = slprintf(buffer, sizeof(buffer), "%s", php_date_short_day_name(t->y, t->m, t->d)); break;
1147 			case 'j': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
1148 			case 'l': length = slprintf(buffer, sizeof(buffer), "%s", php_date_full_day_name(t->y, t->m, t->d)); break;
1149 			case 'S': length = slprintf(buffer, sizeof(buffer), "%s", english_suffix(t->d)); break;
1150 			case 'w': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_week(t->y, t->m, t->d)); break;
1151 			case 'N': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_iso_day_of_week(t->y, t->m, t->d)); break;
1152 			case 'z': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_year(t->y, t->m, t->d)); break;
1153 
1154 			/* week */
1155 			case 'W':
1156 				if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
1157 				length = slprintf(buffer, sizeof(buffer), "%02d", (int) isoweek); break; /* iso weeknr */
1158 			case 'o':
1159 				if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
1160 				length = slprintf(buffer, sizeof(buffer), "%d", (int) isoyear); break; /* iso year */
1161 
1162 			/* month */
1163 			case 'F': length = slprintf(buffer, sizeof(buffer), "%s", mon_full_names[t->m - 1]); break;
1164 			case 'm': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
1165 			case 'M': length = slprintf(buffer, sizeof(buffer), "%s", mon_short_names[t->m - 1]); break;
1166 			case 'n': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
1167 			case 't': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_days_in_month(t->y, t->m)); break;
1168 
1169 			/* year */
1170 			case 'L': length = slprintf(buffer, sizeof(buffer), "%d", timelib_is_leap((int) t->y)); break;
1171 			case 'y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->y % 100); break;
1172 			case 'Y': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
1173 
1174 			/* time */
1175 			case 'a': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "pm" : "am"); break;
1176 			case 'A': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "PM" : "AM"); break;
1177 			case 'B': {
1178 				int retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
1179 				if (retval < 0) {
1180 					retval += 864000;
1181 				}
1182 				/* Make sure to do this on a positive int to avoid rounding errors */
1183 				retval = (retval / 864)  % 1000;
1184 				length = slprintf(buffer, sizeof(buffer), "%03d", retval);
1185 				break;
1186 			}
1187 			case 'g': length = slprintf(buffer, sizeof(buffer), "%d", (t->h % 12) ? (int) t->h % 12 : 12); break;
1188 			case 'G': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
1189 			case 'h': length = slprintf(buffer, sizeof(buffer), "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
1190 			case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
1191 			case 'i': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
1192 			case 's': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->s); break;
1193 			case 'u': length = slprintf(buffer, sizeof(buffer), "%06d", (int) floor(t->us)); break;
1194 			case 'v': length = slprintf(buffer, sizeof(buffer), "%03d", (int) floor(t->us / 1000)); break;
1195 
1196 			/* timezone */
1197 			case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
1198 			case 'P': rfc_colon = 1; /* break intentionally missing */
1199 			case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
1200 											localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
1201 											localtime ? abs(offset->offset / 3600) : 0,
1202 											rfc_colon ? ":" : "",
1203 											localtime ? abs((offset->offset % 3600) / 60) : 0
1204 							  );
1205 					  break;
1206 			case 'T': length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); break;
1207 			case 'e': if (!localtime) {
1208 					      length = slprintf(buffer, sizeof(buffer), "%s", "UTC");
1209 					  } else {
1210 						  switch (t->zone_type) {
1211 							  case TIMELIB_ZONETYPE_ID:
1212 								  length = slprintf(buffer, sizeof(buffer), "%s", t->tz_info->name);
1213 								  break;
1214 							  case TIMELIB_ZONETYPE_ABBR:
1215 								  length = slprintf(buffer, sizeof(buffer), "%s", offset->abbr);
1216 								  break;
1217 							  case TIMELIB_ZONETYPE_OFFSET:
1218 								  length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d",
1219 												((offset->offset < 0) ? '-' : '+'),
1220 												abs(offset->offset / 3600),
1221 												abs((offset->offset % 3600) / 60)
1222 										   );
1223 								  break;
1224 						  }
1225 					  }
1226 					  break;
1227 			case 'Z': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); break;
1228 
1229 			/* full date/time */
1230 			case 'c': length = slprintf(buffer, sizeof(buffer), "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
1231 							                (int) t->y, (int) t->m, (int) t->d,
1232 											(int) t->h, (int) t->i, (int) t->s,
1233 											localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
1234 											localtime ? abs(offset->offset / 3600) : 0,
1235 											localtime ? abs((offset->offset % 3600) / 60) : 0
1236 							  );
1237 					  break;
1238 			case 'r': length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04d %02d:%02d:%02d %c%02d%02d",
1239 							                php_date_short_day_name(t->y, t->m, t->d),
1240 											(int) t->d, mon_short_names[t->m - 1],
1241 											(int) t->y, (int) t->h, (int) t->i, (int) t->s,
1242 											localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
1243 											localtime ? abs(offset->offset / 3600) : 0,
1244 											localtime ? abs((offset->offset % 3600) / 60) : 0
1245 							  );
1246 					  break;
1247 			case 'U': length = slprintf(buffer, sizeof(buffer), "%lld", (timelib_sll) t->sse); break;
1248 
1249 			case '\\': if (i < format_len) i++; /* break intentionally missing */
1250 
1251 			default: buffer[0] = format[i]; buffer[1] = '\0'; length = 1; break;
1252 		}
1253 		smart_str_appendl(&string, buffer, length);
1254 	}
1255 
1256 	smart_str_0(&string);
1257 
1258 	if (localtime) {
1259 		timelib_time_offset_dtor(offset);
1260 	}
1261 
1262 	return string.s;
1263 }
1264 
php_date(INTERNAL_FUNCTION_PARAMETERS,int localtime)1265 static void php_date(INTERNAL_FUNCTION_PARAMETERS, int localtime)
1266 {
1267 	zend_string *format;
1268 	zend_long    ts;
1269 
1270 	ZEND_PARSE_PARAMETERS_START(1, 2)
1271 		Z_PARAM_STR(format)
1272 		Z_PARAM_OPTIONAL
1273 		Z_PARAM_LONG(ts)
1274 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1275 
1276 	if (ZEND_NUM_ARGS() == 1) {
1277 		ts = time(NULL);
1278 	}
1279 
1280 	RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
1281 }
1282 /* }}} */
1283 
php_format_date(char * format,size_t format_len,time_t ts,int localtime)1284 PHPAPI zend_string *php_format_date(char *format, size_t format_len, time_t ts, int localtime) /* {{{ */
1285 {
1286 	timelib_time   *t;
1287 	timelib_tzinfo *tzi;
1288 	zend_string *string;
1289 
1290 	t = timelib_time_ctor();
1291 
1292 	if (localtime) {
1293 		tzi = get_timezone_info();
1294 		t->tz_info = tzi;
1295 		t->zone_type = TIMELIB_ZONETYPE_ID;
1296 		timelib_unixtime2local(t, ts);
1297 	} else {
1298 		tzi = NULL;
1299 		timelib_unixtime2gmt(t, ts);
1300 	}
1301 
1302 	string = date_format(format, format_len, t, localtime);
1303 
1304 	timelib_time_dtor(t);
1305 	return string;
1306 }
1307 /* }}} */
1308 
1309 /* {{{ php_idate
1310  */
php_idate(char format,time_t ts,int localtime)1311 PHPAPI int php_idate(char format, time_t ts, int localtime)
1312 {
1313 	timelib_time   *t;
1314 	timelib_tzinfo *tzi;
1315 	int retval = -1;
1316 	timelib_time_offset *offset = NULL;
1317 	timelib_sll isoweek, isoyear;
1318 
1319 	t = timelib_time_ctor();
1320 
1321 	if (!localtime) {
1322 		tzi = get_timezone_info();
1323 		t->tz_info = tzi;
1324 		t->zone_type = TIMELIB_ZONETYPE_ID;
1325 		timelib_unixtime2local(t, ts);
1326 	} else {
1327 		tzi = NULL;
1328 		timelib_unixtime2gmt(t, ts);
1329 	}
1330 
1331 	if (!localtime) {
1332 		if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
1333 			offset = timelib_time_offset_ctor();
1334 			offset->offset = (t->z + (t->dst * 3600));
1335 			offset->leap_secs = 0;
1336 			offset->is_dst = t->dst;
1337 			offset->abbr = timelib_strdup(t->tz_abbr);
1338 		} else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
1339 			offset = timelib_time_offset_ctor();
1340 			offset->offset = (t->z + (t->dst * 3600));
1341 			offset->leap_secs = 0;
1342 			offset->is_dst = t->dst;
1343 			offset->abbr = timelib_malloc(9); /* GMT��xxxx\0 */
1344 			snprintf(offset->abbr, 9, "GMT%c%02d%02d",
1345 			                          (offset->offset < 0) ? '-' : '+',
1346 			                          abs(offset->offset / 3600),
1347 			                          abs((offset->offset % 3600) / 60));
1348 		} else {
1349 			offset = timelib_get_time_zone_info(t->sse, t->tz_info);
1350 		}
1351 	}
1352 
1353 	timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear);
1354 
1355 	switch (format) {
1356 		/* day */
1357 		case 'd': case 'j': retval = (int) t->d; break;
1358 
1359 		case 'w': retval = (int) timelib_day_of_week(t->y, t->m, t->d); break;
1360 		case 'z': retval = (int) timelib_day_of_year(t->y, t->m, t->d); break;
1361 
1362 		/* week */
1363 		case 'W': retval = (int) isoweek; break; /* iso weeknr */
1364 
1365 		/* month */
1366 		case 'm': case 'n': retval = (int) t->m; break;
1367 		case 't': retval = (int) timelib_days_in_month(t->y, t->m); break;
1368 
1369 		/* year */
1370 		case 'L': retval = (int) timelib_is_leap((int) t->y); break;
1371 		case 'y': retval = (int) (t->y % 100); break;
1372 		case 'Y': retval = (int) t->y; break;
1373 
1374 		/* Swatch Beat a.k.a. Internet Time */
1375 		case 'B':
1376 			retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
1377 			if (retval < 0) {
1378 				retval += 864000;
1379 			}
1380 			/* Make sure to do this on a positive int to avoid rounding errors */
1381 			retval = (retval / 864) % 1000;
1382 			break;
1383 
1384 		/* time */
1385 		case 'g': case 'h': retval = (int) ((t->h % 12) ? (int) t->h % 12 : 12); break;
1386 		case 'H': case 'G': retval = (int) t->h; break;
1387 		case 'i': retval = (int) t->i; break;
1388 		case 's': retval = (int) t->s; break;
1389 
1390 		/* timezone */
1391 		case 'I': retval = (int) (!localtime ? offset->is_dst : 0); break;
1392 		case 'Z': retval = (int) (!localtime ? offset->offset : 0); break;
1393 
1394 		case 'U': retval = (int) t->sse; break;
1395 	}
1396 
1397 	if (!localtime) {
1398 		timelib_time_offset_dtor(offset);
1399 	}
1400 	timelib_time_dtor(t);
1401 
1402 	return retval;
1403 }
1404 /* }}} */
1405 
1406 /* {{{ proto string date(string format [, int timestamp])
1407    Format a local date/time */
PHP_FUNCTION(date)1408 PHP_FUNCTION(date)
1409 {
1410 	php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1411 }
1412 /* }}} */
1413 
1414 /* {{{ proto string gmdate(string format [, int timestamp])
1415    Format a GMT date/time */
PHP_FUNCTION(gmdate)1416 PHP_FUNCTION(gmdate)
1417 {
1418 	php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1419 }
1420 /* }}} */
1421 
1422 /* {{{ proto int idate(string format [, int timestamp])
1423    Format a local time/date as integer */
PHP_FUNCTION(idate)1424 PHP_FUNCTION(idate)
1425 {
1426 	zend_string *format;
1427 	zend_long    ts = 0;
1428 	int ret;
1429 
1430 	ZEND_PARSE_PARAMETERS_START(1, 2)
1431 		Z_PARAM_STR(format)
1432 		Z_PARAM_OPTIONAL
1433 		Z_PARAM_LONG(ts)
1434 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1435 
1436 	if (ZSTR_LEN(format) != 1) {
1437 		php_error_docref(NULL, E_WARNING, "idate format is one char");
1438 		RETURN_FALSE;
1439 	}
1440 
1441 	if (ZEND_NUM_ARGS() == 1) {
1442 		ts = time(NULL);
1443 	}
1444 
1445 	ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
1446 	if (ret == -1) {
1447 		php_error_docref(NULL, E_WARNING, "Unrecognized date format token.");
1448 		RETURN_FALSE;
1449 	}
1450 	RETURN_LONG(ret);
1451 }
1452 /* }}} */
1453 
1454 /* {{{ php_date_set_tzdb - NOT THREADSAFE */
php_date_set_tzdb(timelib_tzdb * tzdb)1455 PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb)
1456 {
1457 	const timelib_tzdb *builtin = timelib_builtin_db();
1458 
1459 	if (php_version_compare(tzdb->version, builtin->version) > 0) {
1460 		php_date_global_timezone_db = tzdb;
1461 		php_date_global_timezone_db_enabled = 1;
1462 	}
1463 }
1464 /* }}} */
1465 
1466 /* {{{ php_parse_date: Backwards compatibility function */
php_parse_date(char * string,zend_long * now)1467 PHPAPI zend_long php_parse_date(char *string, zend_long *now)
1468 {
1469 	timelib_time *parsed_time;
1470 	timelib_error_container *error = NULL;
1471 	int           error2;
1472 	zend_long   retval;
1473 
1474 	parsed_time = timelib_strtotime(string, strlen(string), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
1475 	if (error->error_count) {
1476 		timelib_time_dtor(parsed_time);
1477 		timelib_error_container_dtor(error);
1478 		return -1;
1479 	}
1480 	timelib_error_container_dtor(error);
1481 	timelib_update_ts(parsed_time, NULL);
1482 	retval = timelib_date_to_int(parsed_time, &error2);
1483 	timelib_time_dtor(parsed_time);
1484 	if (error2) {
1485 		return -1;
1486 	}
1487 	return retval;
1488 }
1489 /* }}} */
1490 
1491 /* {{{ proto int strtotime(string time [, int now ])
1492    Convert string representation of date and time to a timestamp */
PHP_FUNCTION(strtotime)1493 PHP_FUNCTION(strtotime)
1494 {
1495 	zend_string *times;
1496 	int error1, error2;
1497 	timelib_error_container *error;
1498 	zend_long preset_ts = 0, ts;
1499 	timelib_time *t, *now;
1500 	timelib_tzinfo *tzi;
1501 
1502 	ZEND_PARSE_PARAMETERS_START(1, 2)
1503 		Z_PARAM_STR(times)
1504 		Z_PARAM_OPTIONAL
1505 		Z_PARAM_LONG(preset_ts)
1506 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1507 
1508 	tzi = get_timezone_info();
1509 
1510 	now = timelib_time_ctor();
1511 	now->tz_info = tzi;
1512 	now->zone_type = TIMELIB_ZONETYPE_ID;
1513 	timelib_unixtime2local(now,
1514 		(ZEND_NUM_ARGS() == 2) ? (timelib_sll) preset_ts : (timelib_sll) time(NULL));
1515 
1516 	t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
1517 		DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
1518 	error1 = error->error_count;
1519 	timelib_error_container_dtor(error);
1520 	timelib_fill_holes(t, now, TIMELIB_NO_CLONE);
1521 	timelib_update_ts(t, tzi);
1522 	ts = timelib_date_to_int(t, &error2);
1523 
1524 	timelib_time_dtor(now);
1525 	timelib_time_dtor(t);
1526 
1527 	if (error1 || error2) {
1528 		RETURN_FALSE;
1529 	} else {
1530 		RETURN_LONG(ts);
1531 	}
1532 }
1533 /* }}} */
1534 
1535 /* {{{ php_mktime - (gm)mktime helper */
php_mktime(INTERNAL_FUNCTION_PARAMETERS,int gmt)1536 PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
1537 {
1538 	zend_long hou = 0, min = 0, sec = 0, mon = 0, day = 0, yea = 0;
1539 	timelib_time *now;
1540 	timelib_tzinfo *tzi = NULL;
1541 	zend_long ts, adjust_seconds = 0;
1542 	int error;
1543 
1544 	ZEND_PARSE_PARAMETERS_START(0, 6)
1545 		Z_PARAM_OPTIONAL
1546 		Z_PARAM_LONG(hou)
1547 		Z_PARAM_LONG(min)
1548 		Z_PARAM_LONG(sec)
1549 		Z_PARAM_LONG(mon)
1550 		Z_PARAM_LONG(day)
1551 		Z_PARAM_LONG(yea)
1552 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1553 
1554 	/* Initialize structure with current time */
1555 	now = timelib_time_ctor();
1556 	if (gmt) {
1557 		timelib_unixtime2gmt(now, (timelib_sll) time(NULL));
1558 	} else {
1559 		tzi = get_timezone_info();
1560 		now->tz_info = tzi;
1561 		now->zone_type = TIMELIB_ZONETYPE_ID;
1562 		timelib_unixtime2local(now, (timelib_sll) time(NULL));
1563 	}
1564 	/* Fill in the new data */
1565 	switch (ZEND_NUM_ARGS()) {
1566 		case 7:
1567 			/* break intentionally missing */
1568 		case 6:
1569 			if (yea >= 0 && yea < 70) {
1570 				yea += 2000;
1571 			} else if (yea >= 70 && yea <= 100) {
1572 				yea += 1900;
1573 			}
1574 			now->y = yea;
1575 			/* break intentionally missing again */
1576 		case 5:
1577 			now->d = day;
1578 			/* break missing intentionally here too */
1579 		case 4:
1580 			now->m = mon;
1581 			/* and here */
1582 		case 3:
1583 			now->s = sec;
1584 			/* yup, this break isn't here on purpose too */
1585 		case 2:
1586 			now->i = min;
1587 			/* last intentionally missing break */
1588 		case 1:
1589 			now->h = hou;
1590 			break;
1591 		default:
1592 			php_error_docref(NULL, E_DEPRECATED, "You should be using the time() function instead");
1593 	}
1594 	/* Update the timestamp */
1595 	if (gmt) {
1596 		timelib_update_ts(now, NULL);
1597 	} else {
1598 		timelib_update_ts(now, tzi);
1599 	}
1600 
1601 	/* Clean up and return */
1602 	ts = timelib_date_to_int(now, &error);
1603 	ts += adjust_seconds;
1604 	timelib_time_dtor(now);
1605 
1606 	if (error) {
1607 		RETURN_FALSE;
1608 	} else {
1609 		RETURN_LONG(ts);
1610 	}
1611 }
1612 /* }}} */
1613 
1614 /* {{{ proto int mktime([int hour [, int min [, int sec [, int mon [, int day [, int year]]]]]])
1615    Get UNIX timestamp for a date */
PHP_FUNCTION(mktime)1616 PHP_FUNCTION(mktime)
1617 {
1618 	php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1619 }
1620 /* }}} */
1621 
1622 /* {{{ proto int gmmktime([int hour [, int min [, int sec [, int mon [, int day [, int year]]]]]])
1623    Get UNIX timestamp for a GMT date */
PHP_FUNCTION(gmmktime)1624 PHP_FUNCTION(gmmktime)
1625 {
1626 	php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1627 }
1628 /* }}} */
1629 
1630 /* {{{ proto bool checkdate(int month, int day, int year)
1631    Returns true(1) if it is a valid date in gregorian calendar */
PHP_FUNCTION(checkdate)1632 PHP_FUNCTION(checkdate)
1633 {
1634 	zend_long m, d, y;
1635 
1636 	ZEND_PARSE_PARAMETERS_START(3, 3)
1637 		Z_PARAM_LONG(m)
1638 		Z_PARAM_LONG(d)
1639 		Z_PARAM_LONG(y)
1640 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1641 
1642 	if (y < 1 || y > 32767 || !timelib_valid_date(y, m, d)) {
1643 		RETURN_FALSE;
1644 	}
1645 	RETURN_TRUE;	/* True : This month, day, year arguments are valid */
1646 }
1647 /* }}} */
1648 
1649 #ifdef HAVE_STRFTIME
1650 /* {{{ php_strftime - (gm)strftime helper */
php_strftime(INTERNAL_FUNCTION_PARAMETERS,int gmt)1651 PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
1652 {
1653 	zend_string         *format;
1654 	zend_long            timestamp = 0;
1655 	struct tm            ta;
1656 	int                  max_reallocs = 5;
1657 	size_t               buf_len = 256, real_len;
1658 	timelib_time        *ts;
1659 	timelib_tzinfo      *tzi;
1660 	timelib_time_offset *offset = NULL;
1661 	zend_string 		*buf;
1662 
1663 	timestamp = (zend_long) time(NULL);
1664 
1665 	ZEND_PARSE_PARAMETERS_START(1, 2)
1666 		Z_PARAM_STR(format)
1667 		Z_PARAM_OPTIONAL
1668 		Z_PARAM_LONG(timestamp)
1669 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1670 
1671 	if (ZSTR_LEN(format) == 0) {
1672 		RETURN_FALSE;
1673 	}
1674 
1675 	ts = timelib_time_ctor();
1676 	if (gmt) {
1677 		tzi = NULL;
1678 		timelib_unixtime2gmt(ts, (timelib_sll) timestamp);
1679 	} else {
1680 		tzi = get_timezone_info();
1681 		ts->tz_info = tzi;
1682 		ts->zone_type = TIMELIB_ZONETYPE_ID;
1683 		timelib_unixtime2local(ts, (timelib_sll) timestamp);
1684 	}
1685 	ta.tm_sec   = ts->s;
1686 	ta.tm_min   = ts->i;
1687 	ta.tm_hour  = ts->h;
1688 	ta.tm_mday  = ts->d;
1689 	ta.tm_mon   = ts->m - 1;
1690 	ta.tm_year  = ts->y - 1900;
1691 	ta.tm_wday  = timelib_day_of_week(ts->y, ts->m, ts->d);
1692 	ta.tm_yday  = timelib_day_of_year(ts->y, ts->m, ts->d);
1693 	if (gmt) {
1694 		ta.tm_isdst = 0;
1695 #if HAVE_TM_GMTOFF
1696 		ta.tm_gmtoff = 0;
1697 #endif
1698 #if HAVE_TM_ZONE
1699 		ta.tm_zone = "GMT";
1700 #endif
1701 	} else {
1702 		offset = timelib_get_time_zone_info(timestamp, tzi);
1703 
1704 		ta.tm_isdst = offset->is_dst;
1705 #if HAVE_TM_GMTOFF
1706 		ta.tm_gmtoff = offset->offset;
1707 #endif
1708 #if HAVE_TM_ZONE
1709 		ta.tm_zone = offset->abbr;
1710 #endif
1711 	}
1712 
1713 	/* VS2012 crt has a bug where strftime crash with %z and %Z format when the
1714 	   initial buffer is too small. See
1715 	   http://connect.microsoft.com/VisualStudio/feedback/details/759720/vs2012-strftime-crash-with-z-formatting-code */
1716 	buf = zend_string_alloc(buf_len, 0);
1717 	while ((real_len = strftime(ZSTR_VAL(buf), buf_len, ZSTR_VAL(format), &ta)) == buf_len || real_len == 0) {
1718 		buf_len *= 2;
1719 		buf = zend_string_extend(buf, buf_len, 0);
1720 		if (!--max_reallocs) {
1721 			break;
1722 		}
1723 	}
1724 #ifdef PHP_WIN32
1725 	/* VS2012 strftime() returns number of characters, not bytes.
1726 		See VC++11 bug id 766205. */
1727 	if (real_len > 0) {
1728 		real_len = strlen(buf->val);
1729 	}
1730 #endif
1731 
1732 	timelib_time_dtor(ts);
1733 	if (!gmt) {
1734 		timelib_time_offset_dtor(offset);
1735 	}
1736 
1737 	if (real_len && real_len != buf_len) {
1738 		buf = zend_string_truncate(buf, real_len, 0);
1739 		RETURN_NEW_STR(buf);
1740 	}
1741 	zend_string_free(buf);
1742 	RETURN_FALSE;
1743 }
1744 /* }}} */
1745 
1746 /* {{{ proto string strftime(string format [, int timestamp])
1747    Format a local time/date according to locale settings */
PHP_FUNCTION(strftime)1748 PHP_FUNCTION(strftime)
1749 {
1750 	php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1751 }
1752 /* }}} */
1753 
1754 /* {{{ proto string gmstrftime(string format [, int timestamp])
1755    Format a GMT/UCT time/date according to locale settings */
PHP_FUNCTION(gmstrftime)1756 PHP_FUNCTION(gmstrftime)
1757 {
1758 	php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1759 }
1760 /* }}} */
1761 #endif
1762 
1763 /* {{{ proto int time(void)
1764    Return current UNIX timestamp */
PHP_FUNCTION(time)1765 PHP_FUNCTION(time)
1766 {
1767 	if (zend_parse_parameters_none() == FAILURE) {
1768 		return;
1769 	}
1770 
1771 	RETURN_LONG((zend_long)time(NULL));
1772 }
1773 /* }}} */
1774 
1775 /* {{{ proto array localtime([int timestamp [, bool associative_array]])
1776    Returns the results of the C system call localtime as an associative array if the associative_array argument is set to 1 other wise it is a regular array */
PHP_FUNCTION(localtime)1777 PHP_FUNCTION(localtime)
1778 {
1779 	zend_long timestamp = (zend_long)time(NULL);
1780 	zend_bool associative = 0;
1781 	timelib_tzinfo *tzi;
1782 	timelib_time   *ts;
1783 
1784 	ZEND_PARSE_PARAMETERS_START(0, 2)
1785 		Z_PARAM_OPTIONAL
1786 		Z_PARAM_LONG(timestamp)
1787 		Z_PARAM_BOOL(associative)
1788 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1789 
1790 	tzi = get_timezone_info();
1791 	ts = timelib_time_ctor();
1792 	ts->tz_info = tzi;
1793 	ts->zone_type = TIMELIB_ZONETYPE_ID;
1794 	timelib_unixtime2local(ts, (timelib_sll) timestamp);
1795 
1796 	array_init(return_value);
1797 
1798 	if (associative) {
1799 		add_assoc_long(return_value, "tm_sec",   ts->s);
1800 		add_assoc_long(return_value, "tm_min",   ts->i);
1801 		add_assoc_long(return_value, "tm_hour",  ts->h);
1802 		add_assoc_long(return_value, "tm_mday",  ts->d);
1803 		add_assoc_long(return_value, "tm_mon",   ts->m - 1);
1804 		add_assoc_long(return_value, "tm_year",  ts->y - 1900);
1805 		add_assoc_long(return_value, "tm_wday",  timelib_day_of_week(ts->y, ts->m, ts->d));
1806 		add_assoc_long(return_value, "tm_yday",  timelib_day_of_year(ts->y, ts->m, ts->d));
1807 		add_assoc_long(return_value, "tm_isdst", ts->dst);
1808 	} else {
1809 		add_next_index_long(return_value, ts->s);
1810 		add_next_index_long(return_value, ts->i);
1811 		add_next_index_long(return_value, ts->h);
1812 		add_next_index_long(return_value, ts->d);
1813 		add_next_index_long(return_value, ts->m - 1);
1814 		add_next_index_long(return_value, ts->y- 1900);
1815 		add_next_index_long(return_value, timelib_day_of_week(ts->y, ts->m, ts->d));
1816 		add_next_index_long(return_value, timelib_day_of_year(ts->y, ts->m, ts->d));
1817 		add_next_index_long(return_value, ts->dst);
1818 	}
1819 
1820 	timelib_time_dtor(ts);
1821 }
1822 /* }}} */
1823 
1824 /* {{{ proto array getdate([int timestamp])
1825    Get date/time information */
PHP_FUNCTION(getdate)1826 PHP_FUNCTION(getdate)
1827 {
1828 	zend_long timestamp = (zend_long)time(NULL);
1829 	timelib_tzinfo *tzi;
1830 	timelib_time   *ts;
1831 
1832 	ZEND_PARSE_PARAMETERS_START(0, 1)
1833 		Z_PARAM_OPTIONAL
1834 		Z_PARAM_LONG(timestamp)
1835 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1836 
1837 	tzi = get_timezone_info();
1838 	ts = timelib_time_ctor();
1839 	ts->tz_info = tzi;
1840 	ts->zone_type = TIMELIB_ZONETYPE_ID;
1841 	timelib_unixtime2local(ts, (timelib_sll) timestamp);
1842 
1843 	array_init(return_value);
1844 
1845 	add_assoc_long(return_value, "seconds", ts->s);
1846 	add_assoc_long(return_value, "minutes", ts->i);
1847 	add_assoc_long(return_value, "hours", ts->h);
1848 	add_assoc_long(return_value, "mday", ts->d);
1849 	add_assoc_long(return_value, "wday", timelib_day_of_week(ts->y, ts->m, ts->d));
1850 	add_assoc_long(return_value, "mon", ts->m);
1851 	add_assoc_long(return_value, "year", ts->y);
1852 	add_assoc_long(return_value, "yday", timelib_day_of_year(ts->y, ts->m, ts->d));
1853 	add_assoc_string(return_value, "weekday", php_date_full_day_name(ts->y, ts->m, ts->d));
1854 	add_assoc_string(return_value, "month", mon_full_names[ts->m - 1]);
1855 	add_index_long(return_value, 0, timestamp);
1856 
1857 	timelib_time_dtor(ts);
1858 }
1859 /* }}} */
1860 
1861 #define PHP_DATE_TIMEZONE_GROUP_AFRICA     0x0001
1862 #define PHP_DATE_TIMEZONE_GROUP_AMERICA    0x0002
1863 #define PHP_DATE_TIMEZONE_GROUP_ANTARCTICA 0x0004
1864 #define PHP_DATE_TIMEZONE_GROUP_ARCTIC     0x0008
1865 #define PHP_DATE_TIMEZONE_GROUP_ASIA       0x0010
1866 #define PHP_DATE_TIMEZONE_GROUP_ATLANTIC   0x0020
1867 #define PHP_DATE_TIMEZONE_GROUP_AUSTRALIA  0x0040
1868 #define PHP_DATE_TIMEZONE_GROUP_EUROPE     0x0080
1869 #define PHP_DATE_TIMEZONE_GROUP_INDIAN     0x0100
1870 #define PHP_DATE_TIMEZONE_GROUP_PACIFIC    0x0200
1871 #define PHP_DATE_TIMEZONE_GROUP_UTC        0x0400
1872 #define PHP_DATE_TIMEZONE_GROUP_ALL        0x07FF
1873 #define PHP_DATE_TIMEZONE_GROUP_ALL_W_BC   0x0FFF
1874 #define PHP_DATE_TIMEZONE_PER_COUNTRY      0x1000
1875 
1876 #define PHP_DATE_PERIOD_EXCLUDE_START_DATE 0x0001
1877 
1878 
1879 /* define an overloaded iterator structure */
1880 typedef struct {
1881 	zend_object_iterator  intern;
1882 	zval                  current;
1883 	php_period_obj       *object;
1884 	int                   current_index;
1885 } date_period_it;
1886 
1887 /* {{{ date_period_it_invalidate_current */
date_period_it_invalidate_current(zend_object_iterator * iter)1888 static void date_period_it_invalidate_current(zend_object_iterator *iter)
1889 {
1890 	date_period_it *iterator = (date_period_it *)iter;
1891 
1892 	if (Z_TYPE(iterator->current) != IS_UNDEF) {
1893 		zval_ptr_dtor(&iterator->current);
1894 		ZVAL_UNDEF(&iterator->current);
1895 	}
1896 }
1897 /* }}} */
1898 
1899 /* {{{ date_period_it_dtor */
date_period_it_dtor(zend_object_iterator * iter)1900 static void date_period_it_dtor(zend_object_iterator *iter)
1901 {
1902 	date_period_it *iterator = (date_period_it *)iter;
1903 
1904 	date_period_it_invalidate_current(iter);
1905 
1906 	zval_ptr_dtor(&iterator->intern.data);
1907 }
1908 /* }}} */
1909 
1910 /* {{{ date_period_it_has_more */
date_period_it_has_more(zend_object_iterator * iter)1911 static int date_period_it_has_more(zend_object_iterator *iter)
1912 {
1913 	date_period_it *iterator = (date_period_it *)iter;
1914 	php_period_obj *object   = Z_PHPPERIOD_P(&iterator->intern.data);
1915 	timelib_time   *it_time = object->current;
1916 
1917 	/* apply modification if it's not the first iteration */
1918 	if (!object->include_start_date || iterator->current_index > 0) {
1919 		it_time->have_relative = 1;
1920 		it_time->relative = *object->interval;
1921 		it_time->sse_uptodate = 0;
1922 		timelib_update_ts(it_time, NULL);
1923 		timelib_update_from_sse(it_time);
1924 	}
1925 
1926 	if (object->end) {
1927 		return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
1928 	} else {
1929 		return (iterator->current_index < object->recurrences) ? SUCCESS : FAILURE;
1930 	}
1931 }
1932 /* }}} */
1933 
1934 /* {{{ date_period_it_current_data */
date_period_it_current_data(zend_object_iterator * iter)1935 static zval *date_period_it_current_data(zend_object_iterator *iter)
1936 {
1937 	date_period_it *iterator = (date_period_it *)iter;
1938 	php_period_obj *object   = Z_PHPPERIOD_P(&iterator->intern.data);
1939 	timelib_time   *it_time = object->current;
1940 	php_date_obj   *newdateobj;
1941 
1942 	/* Create new object */
1943 	php_date_instantiate(object->start_ce, &iterator->current);
1944 	newdateobj = Z_PHPDATE_P(&iterator->current);
1945 	newdateobj->time = timelib_time_ctor();
1946 	*newdateobj->time = *it_time;
1947 	if (it_time->tz_abbr) {
1948 		newdateobj->time->tz_abbr = timelib_strdup(it_time->tz_abbr);
1949 	}
1950 	if (it_time->tz_info) {
1951 		newdateobj->time->tz_info = it_time->tz_info;
1952 	}
1953 
1954 	return &iterator->current;
1955 }
1956 /* }}} */
1957 
1958 /* {{{ date_period_it_current_key */
date_period_it_current_key(zend_object_iterator * iter,zval * key)1959 static void date_period_it_current_key(zend_object_iterator *iter, zval *key)
1960 {
1961 	date_period_it *iterator = (date_period_it *)iter;
1962 	ZVAL_LONG(key, iterator->current_index);
1963 }
1964 /* }}} */
1965 
1966 /* {{{ date_period_it_move_forward */
date_period_it_move_forward(zend_object_iterator * iter)1967 static void date_period_it_move_forward(zend_object_iterator *iter)
1968 {
1969 	date_period_it   *iterator = (date_period_it *)iter;
1970 
1971 	iterator->current_index++;
1972 	date_period_it_invalidate_current(iter);
1973 }
1974 /* }}} */
1975 
1976 /* {{{ date_period_it_rewind */
date_period_it_rewind(zend_object_iterator * iter)1977 static void date_period_it_rewind(zend_object_iterator *iter)
1978 {
1979 	date_period_it *iterator = (date_period_it *)iter;
1980 
1981 	iterator->current_index = 0;
1982 	if (iterator->object->current) {
1983 		timelib_time_dtor(iterator->object->current);
1984 	}
1985 	if (!iterator->object->start) {
1986 		zend_throw_error(NULL, "DatePeriod has not been initialized correctly");
1987 		return;
1988 	}
1989 	iterator->object->current = timelib_time_clone(iterator->object->start);
1990 	date_period_it_invalidate_current(iter);
1991 }
1992 /* }}} */
1993 
1994 /* iterator handler table */
1995 static const zend_object_iterator_funcs date_period_it_funcs = {
1996 	date_period_it_dtor,
1997 	date_period_it_has_more,
1998 	date_period_it_current_data,
1999 	date_period_it_current_key,
2000 	date_period_it_move_forward,
2001 	date_period_it_rewind,
2002 	date_period_it_invalidate_current
2003 };
2004 
date_object_period_get_iterator(zend_class_entry * ce,zval * object,int by_ref)2005 zend_object_iterator *date_object_period_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
2006 {
2007 	date_period_it *iterator;
2008 
2009 	if (by_ref) {
2010 		zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
2011 		return NULL;
2012 	}
2013 
2014 	iterator = emalloc(sizeof(date_period_it));
2015 
2016 	zend_iterator_init((zend_object_iterator*)iterator);
2017 
2018 	ZVAL_COPY(&iterator->intern.data, object);
2019 	iterator->intern.funcs = &date_period_it_funcs;
2020 	iterator->object = Z_PHPPERIOD_P(object);
2021 	ZVAL_UNDEF(&iterator->current);
2022 
2023 	return (zend_object_iterator*)iterator;
2024 } /* }}} */
2025 
implement_date_interface_handler(zend_class_entry * interface,zend_class_entry * implementor)2026 static int implement_date_interface_handler(zend_class_entry *interface, zend_class_entry *implementor) /* {{{ */
2027 {
2028 	if (implementor->type == ZEND_USER_CLASS &&
2029 		!instanceof_function(implementor, date_ce_date) &&
2030 		!instanceof_function(implementor, date_ce_immutable)
2031 	) {
2032 		zend_error(E_ERROR, "DateTimeInterface can't be implemented by user classes");
2033 	}
2034 
2035 	return SUCCESS;
2036 } /* }}} */
2037 
date_interval_has_property(zval * object,zval * member,int type,void ** cache_slot)2038 static int date_interval_has_property(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
2039 {
2040 	php_interval_obj *obj;
2041 	zval tmp_member;
2042 	zval rv;
2043 	zval *prop;
2044 	int retval = 0;
2045 
2046 	if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
2047 		ZVAL_STR(&tmp_member, zval_get_string_func(member));
2048 		member = &tmp_member;
2049 		cache_slot = NULL;
2050 	}
2051 
2052 	obj = Z_PHPINTERVAL_P(object);
2053 
2054 	if (!obj->initialized) {
2055 		retval = (zend_get_std_object_handlers())->has_property(object, member, type, cache_slot);
2056 		if (member == &tmp_member) {
2057 			zval_dtor(member);
2058 		}
2059 		return retval;
2060 	}
2061 
2062 	prop = date_interval_read_property(object, member, BP_VAR_IS, cache_slot, &rv);
2063 
2064 	if (prop != &EG(uninitialized_zval)) {
2065 		if (type == 2) {
2066 			retval = 1;
2067 		} else if (type == 1) {
2068 			retval = zend_is_true(prop);
2069 		} else if (type == 0) {
2070 			retval = (Z_TYPE_P(prop) != IS_NULL);
2071 		}
2072 	} else {
2073 		retval = (zend_get_std_object_handlers())->has_property(object, member, type, cache_slot);
2074 	}
2075 
2076 	if (member == &tmp_member) {
2077 		zval_dtor(member);
2078 	}
2079 
2080 	return retval;
2081 
2082 }
2083 /* }}} */
2084 
date_register_classes(void)2085 static void date_register_classes(void) /* {{{ */
2086 {
2087 	zend_class_entry ce_date, ce_immutable, ce_timezone, ce_interval, ce_period, ce_interface;
2088 
2089 	INIT_CLASS_ENTRY(ce_interface, "DateTimeInterface", date_funcs_interface);
2090 	date_ce_interface = zend_register_internal_interface(&ce_interface);
2091 	date_ce_interface->interface_gets_implemented = implement_date_interface_handler;
2092 
2093 #define REGISTER_DATE_INTERFACE_CONST_STRING(const_name, value) \
2094 	zend_declare_class_constant_stringl(date_ce_interface, const_name, sizeof(const_name)-1, value, sizeof(value)-1);
2095 
2096 	REGISTER_DATE_INTERFACE_CONST_STRING("ATOM",             DATE_FORMAT_RFC3339);
2097 	REGISTER_DATE_INTERFACE_CONST_STRING("COOKIE",           DATE_FORMAT_COOKIE);
2098 	REGISTER_DATE_INTERFACE_CONST_STRING("ISO8601",          DATE_FORMAT_ISO8601);
2099 	REGISTER_DATE_INTERFACE_CONST_STRING("RFC822",           DATE_FORMAT_RFC822);
2100 	REGISTER_DATE_INTERFACE_CONST_STRING("RFC850",           DATE_FORMAT_RFC850);
2101 	REGISTER_DATE_INTERFACE_CONST_STRING("RFC1036",          DATE_FORMAT_RFC1036);
2102 	REGISTER_DATE_INTERFACE_CONST_STRING("RFC1123",          DATE_FORMAT_RFC1123);
2103 	REGISTER_DATE_INTERFACE_CONST_STRING("RFC7231",          DATE_FORMAT_RFC7231);
2104 	REGISTER_DATE_INTERFACE_CONST_STRING("RFC2822",          DATE_FORMAT_RFC2822);
2105 	REGISTER_DATE_INTERFACE_CONST_STRING("RFC3339",          DATE_FORMAT_RFC3339);
2106 	REGISTER_DATE_INTERFACE_CONST_STRING("RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED);
2107 	REGISTER_DATE_INTERFACE_CONST_STRING("RSS",              DATE_FORMAT_RFC1123);
2108 	REGISTER_DATE_INTERFACE_CONST_STRING("W3C",              DATE_FORMAT_RFC3339);
2109 
2110 	INIT_CLASS_ENTRY(ce_date, "DateTime", date_funcs_date);
2111 	ce_date.create_object = date_object_new_date;
2112 	date_ce_date = zend_register_internal_class_ex(&ce_date, NULL);
2113 	memcpy(&date_object_handlers_date, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2114 	date_object_handlers_date.offset = XtOffsetOf(php_date_obj, std);
2115 	date_object_handlers_date.free_obj = date_object_free_storage_date;
2116 	date_object_handlers_date.clone_obj = date_object_clone_date;
2117 	date_object_handlers_date.compare_objects = date_object_compare_date;
2118 	date_object_handlers_date.get_properties = date_object_get_properties;
2119 	date_object_handlers_date.get_gc = date_object_get_gc;
2120 	zend_class_implements(date_ce_date, 1, date_ce_interface);
2121 
2122 	INIT_CLASS_ENTRY(ce_immutable, "DateTimeImmutable", date_funcs_immutable);
2123 	ce_immutable.create_object = date_object_new_date;
2124 	date_ce_immutable = zend_register_internal_class_ex(&ce_immutable, NULL);
2125 	memcpy(&date_object_handlers_immutable, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2126 	date_object_handlers_immutable.clone_obj = date_object_clone_date;
2127 	date_object_handlers_immutable.compare_objects = date_object_compare_date;
2128 	date_object_handlers_immutable.get_properties = date_object_get_properties;
2129 	date_object_handlers_immutable.get_gc = date_object_get_gc;
2130 	zend_class_implements(date_ce_immutable, 1, date_ce_interface);
2131 
2132 	INIT_CLASS_ENTRY(ce_timezone, "DateTimeZone", date_funcs_timezone);
2133 	ce_timezone.create_object = date_object_new_timezone;
2134 	date_ce_timezone = zend_register_internal_class_ex(&ce_timezone, NULL);
2135 	memcpy(&date_object_handlers_timezone, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2136 	date_object_handlers_timezone.offset = XtOffsetOf(php_timezone_obj, std);
2137 	date_object_handlers_timezone.free_obj = date_object_free_storage_timezone;
2138 	date_object_handlers_timezone.clone_obj = date_object_clone_timezone;
2139 	date_object_handlers_timezone.get_properties = date_object_get_properties_timezone;
2140 	date_object_handlers_timezone.get_gc = date_object_get_gc_timezone;
2141 	date_object_handlers_timezone.get_debug_info = date_object_get_debug_info_timezone;
2142 
2143 #define REGISTER_TIMEZONE_CLASS_CONST_STRING(const_name, value) \
2144 	zend_declare_class_constant_long(date_ce_timezone, const_name, sizeof(const_name)-1, value);
2145 
2146 	REGISTER_TIMEZONE_CLASS_CONST_STRING("AFRICA",      PHP_DATE_TIMEZONE_GROUP_AFRICA);
2147 	REGISTER_TIMEZONE_CLASS_CONST_STRING("AMERICA",     PHP_DATE_TIMEZONE_GROUP_AMERICA);
2148 	REGISTER_TIMEZONE_CLASS_CONST_STRING("ANTARCTICA",  PHP_DATE_TIMEZONE_GROUP_ANTARCTICA);
2149 	REGISTER_TIMEZONE_CLASS_CONST_STRING("ARCTIC",      PHP_DATE_TIMEZONE_GROUP_ARCTIC);
2150 	REGISTER_TIMEZONE_CLASS_CONST_STRING("ASIA",        PHP_DATE_TIMEZONE_GROUP_ASIA);
2151 	REGISTER_TIMEZONE_CLASS_CONST_STRING("ATLANTIC",    PHP_DATE_TIMEZONE_GROUP_ATLANTIC);
2152 	REGISTER_TIMEZONE_CLASS_CONST_STRING("AUSTRALIA",   PHP_DATE_TIMEZONE_GROUP_AUSTRALIA);
2153 	REGISTER_TIMEZONE_CLASS_CONST_STRING("EUROPE",      PHP_DATE_TIMEZONE_GROUP_EUROPE);
2154 	REGISTER_TIMEZONE_CLASS_CONST_STRING("INDIAN",      PHP_DATE_TIMEZONE_GROUP_INDIAN);
2155 	REGISTER_TIMEZONE_CLASS_CONST_STRING("PACIFIC",     PHP_DATE_TIMEZONE_GROUP_PACIFIC);
2156 	REGISTER_TIMEZONE_CLASS_CONST_STRING("UTC",         PHP_DATE_TIMEZONE_GROUP_UTC);
2157 	REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL",         PHP_DATE_TIMEZONE_GROUP_ALL);
2158 	REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL_WITH_BC", PHP_DATE_TIMEZONE_GROUP_ALL_W_BC);
2159 	REGISTER_TIMEZONE_CLASS_CONST_STRING("PER_COUNTRY", PHP_DATE_TIMEZONE_PER_COUNTRY);
2160 
2161 	INIT_CLASS_ENTRY(ce_interval, "DateInterval", date_funcs_interval);
2162 	ce_interval.create_object = date_object_new_interval;
2163 	date_ce_interval = zend_register_internal_class_ex(&ce_interval, NULL);
2164 	memcpy(&date_object_handlers_interval, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2165 	date_object_handlers_interval.offset = XtOffsetOf(php_interval_obj, std);
2166 	date_object_handlers_interval.free_obj = date_object_free_storage_interval;
2167 	date_object_handlers_interval.clone_obj = date_object_clone_interval;
2168 	date_object_handlers_interval.has_property = date_interval_has_property;
2169 	date_object_handlers_interval.read_property = date_interval_read_property;
2170 	date_object_handlers_interval.write_property = date_interval_write_property;
2171 	date_object_handlers_interval.get_properties = date_object_get_properties_interval;
2172 	date_object_handlers_interval.get_property_ptr_ptr = date_interval_get_property_ptr_ptr;
2173 	date_object_handlers_interval.get_gc = date_object_get_gc_interval;
2174 
2175 	INIT_CLASS_ENTRY(ce_period, "DatePeriod", date_funcs_period);
2176 	ce_period.create_object = date_object_new_period;
2177 	date_ce_period = zend_register_internal_class_ex(&ce_period, NULL);
2178 	date_ce_period->get_iterator = date_object_period_get_iterator;
2179 	date_ce_period->iterator_funcs.funcs = &date_period_it_funcs;
2180 	zend_class_implements(date_ce_period, 1, zend_ce_traversable);
2181 	memcpy(&date_object_handlers_period, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2182 	date_object_handlers_period.offset = XtOffsetOf(php_period_obj, std);
2183 	date_object_handlers_period.free_obj = date_object_free_storage_period;
2184 	date_object_handlers_period.clone_obj = date_object_clone_period;
2185 	date_object_handlers_period.get_properties = date_object_get_properties_period;
2186 	date_object_handlers_period.get_property_ptr_ptr = NULL;
2187 	date_object_handlers_period.get_gc = date_object_get_gc_period;
2188 	date_object_handlers_period.read_property = date_period_read_property;
2189 	date_object_handlers_period.write_property = date_period_write_property;
2190 
2191 #define REGISTER_PERIOD_CLASS_CONST_STRING(const_name, value) \
2192 	zend_declare_class_constant_long(date_ce_period, const_name, sizeof(const_name)-1, value);
2193 
2194 	REGISTER_PERIOD_CLASS_CONST_STRING("EXCLUDE_START_DATE", PHP_DATE_PERIOD_EXCLUDE_START_DATE);
2195 } /* }}} */
2196 
date_object_new_date(zend_class_entry * class_type)2197 static zend_object *date_object_new_date(zend_class_entry *class_type) /* {{{ */
2198 {
2199 	php_date_obj *intern = zend_object_alloc(sizeof(php_date_obj), class_type);
2200 
2201 	zend_object_std_init(&intern->std, class_type);
2202 	object_properties_init(&intern->std, class_type);
2203 	intern->std.handlers = &date_object_handlers_date;
2204 
2205 	return &intern->std;
2206 } /* }}} */
2207 
date_object_clone_date(zval * this_ptr)2208 static zend_object *date_object_clone_date(zval *this_ptr) /* {{{ */
2209 {
2210 	php_date_obj *old_obj = Z_PHPDATE_P(this_ptr);
2211 	php_date_obj *new_obj = php_date_obj_from_obj(date_object_new_date(old_obj->std.ce));
2212 
2213 	zend_objects_clone_members(&new_obj->std, &old_obj->std);
2214 	if (!old_obj->time) {
2215 		return &new_obj->std;
2216 	}
2217 
2218 	/* this should probably moved to a new `timelib_time *timelime_time_clone(timelib_time *)` */
2219 	new_obj->time = timelib_time_ctor();
2220 	*new_obj->time = *old_obj->time;
2221 	if (old_obj->time->tz_abbr) {
2222 		new_obj->time->tz_abbr = timelib_strdup(old_obj->time->tz_abbr);
2223 	}
2224 	if (old_obj->time->tz_info) {
2225 		new_obj->time->tz_info = old_obj->time->tz_info;
2226 	}
2227 
2228 	return &new_obj->std;
2229 } /* }}} */
2230 
date_clone_immutable(zval * object,zval * new_object)2231 static void date_clone_immutable(zval *object, zval *new_object) /* {{{ */
2232 {
2233 	ZVAL_OBJ(new_object, date_object_clone_date(object));
2234 } /* }}} */
2235 
date_object_compare_date(zval * d1,zval * d2)2236 static int date_object_compare_date(zval *d1, zval *d2) /* {{{ */
2237 {
2238 	php_date_obj *o1 = Z_PHPDATE_P(d1);
2239 	php_date_obj *o2 = Z_PHPDATE_P(d2);
2240 
2241 	if (!o1->time || !o2->time) {
2242 		php_error_docref(NULL, E_WARNING, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
2243 		return 1;
2244 	}
2245 	if (!o1->time->sse_uptodate) {
2246 		timelib_update_ts(o1->time, o1->time->tz_info);
2247 	}
2248 	if (!o2->time->sse_uptodate) {
2249 		timelib_update_ts(o2->time, o2->time->tz_info);
2250 	}
2251 
2252 	return timelib_time_compare(o1->time, o2->time);
2253 } /* }}} */
2254 
date_object_get_gc(zval * object,zval ** table,int * n)2255 static HashTable *date_object_get_gc(zval *object, zval **table, int *n) /* {{{ */
2256 {
2257 	*table = NULL;
2258 	*n = 0;
2259 	return zend_std_get_properties(object);
2260 } /* }}} */
2261 
date_object_get_gc_timezone(zval * object,zval ** table,int * n)2262 static HashTable *date_object_get_gc_timezone(zval *object, zval **table, int *n) /* {{{ */
2263 {
2264        *table = NULL;
2265        *n = 0;
2266        return zend_std_get_properties(object);
2267 } /* }}} */
2268 
date_object_get_properties(zval * object)2269 static HashTable *date_object_get_properties(zval *object) /* {{{ */
2270 {
2271 	HashTable *props;
2272 	zval zv;
2273 	php_date_obj     *dateobj;
2274 
2275 
2276 	dateobj = Z_PHPDATE_P(object);
2277 
2278 	props = zend_std_get_properties(object);
2279 
2280 	if (!dateobj->time) {
2281 		return props;
2282 	}
2283 
2284 	/* first we add the date and time in ISO format */
2285 	ZVAL_STR(&zv, date_format("Y-m-d H:i:s.u", sizeof("Y-m-d H:i:s.u")-1, dateobj->time, 1));
2286 	zend_hash_str_update(props, "date", sizeof("date")-1, &zv);
2287 
2288 	/* then we add the timezone name (or similar) */
2289 	if (dateobj->time->is_localtime) {
2290 		ZVAL_LONG(&zv, dateobj->time->zone_type);
2291 		zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
2292 
2293 		switch (dateobj->time->zone_type) {
2294 			case TIMELIB_ZONETYPE_ID:
2295 				ZVAL_STRING(&zv, dateobj->time->tz_info->name);
2296 				break;
2297 			case TIMELIB_ZONETYPE_OFFSET: {
2298 				zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
2299 				int utc_offset = dateobj->time->z;
2300 
2301 				ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
2302 					utc_offset < 0 ? '-' : '+',
2303 					abs(utc_offset / 3600),
2304 					abs(((utc_offset % 3600) / 60)));
2305 
2306 				ZVAL_NEW_STR(&zv, tmpstr);
2307 				}
2308 				break;
2309 			case TIMELIB_ZONETYPE_ABBR:
2310 				ZVAL_STRING(&zv, dateobj->time->tz_abbr);
2311 				break;
2312 		}
2313 		zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
2314 	}
2315 
2316 	return props;
2317 } /* }}} */
2318 
date_object_new_timezone(zend_class_entry * class_type)2319 static zend_object *date_object_new_timezone(zend_class_entry *class_type) /* {{{ */
2320 {
2321 	php_timezone_obj *intern = zend_object_alloc(sizeof(php_timezone_obj), class_type);
2322 
2323 	zend_object_std_init(&intern->std, class_type);
2324 	object_properties_init(&intern->std, class_type);
2325 	intern->std.handlers = &date_object_handlers_timezone;
2326 
2327 	return &intern->std;
2328 } /* }}} */
2329 
date_object_clone_timezone(zval * this_ptr)2330 static zend_object *date_object_clone_timezone(zval *this_ptr) /* {{{ */
2331 {
2332 	php_timezone_obj *old_obj = Z_PHPTIMEZONE_P(this_ptr);
2333 	php_timezone_obj *new_obj = php_timezone_obj_from_obj(date_object_new_timezone(old_obj->std.ce));
2334 
2335 	zend_objects_clone_members(&new_obj->std, &old_obj->std);
2336 	if (!old_obj->initialized) {
2337 		return &new_obj->std;
2338 	}
2339 
2340 	new_obj->type = old_obj->type;
2341 	new_obj->initialized = 1;
2342 	switch (new_obj->type) {
2343 		case TIMELIB_ZONETYPE_ID:
2344 			new_obj->tzi.tz = old_obj->tzi.tz;
2345 			break;
2346 		case TIMELIB_ZONETYPE_OFFSET:
2347 			new_obj->tzi.utc_offset = old_obj->tzi.utc_offset;
2348 			break;
2349 		case TIMELIB_ZONETYPE_ABBR:
2350 			new_obj->tzi.z.utc_offset = old_obj->tzi.z.utc_offset;
2351 			new_obj->tzi.z.dst        = old_obj->tzi.z.dst;
2352 			new_obj->tzi.z.abbr       = timelib_strdup(old_obj->tzi.z.abbr);
2353 			break;
2354 	}
2355 
2356 	return &new_obj->std;
2357 } /* }}} */
2358 
date_object_get_properties_timezone(zval * object)2359 static HashTable *date_object_get_properties_timezone(zval *object) /* {{{ */
2360 {
2361 	HashTable *props;
2362 	zval zv;
2363 	php_timezone_obj     *tzobj;
2364 
2365 
2366 	tzobj = Z_PHPTIMEZONE_P(object);
2367 
2368 	props = zend_std_get_properties(object);
2369 
2370 	if (!tzobj->initialized) {
2371 		return props;
2372 	}
2373 
2374 	ZVAL_LONG(&zv, tzobj->type);
2375 	zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
2376 
2377 	switch (tzobj->type) {
2378 		case TIMELIB_ZONETYPE_ID:
2379 			ZVAL_STRING(&zv, tzobj->tzi.tz->name);
2380 			break;
2381 		case TIMELIB_ZONETYPE_OFFSET: {
2382 			zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
2383 
2384 			ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
2385 				tzobj->tzi.utc_offset < 0 ? '-' : '+',
2386 				abs((int)(tzobj->tzi.utc_offset / 3600)),
2387 				abs(((int)(tzobj->tzi.utc_offset % 3600) / 60)));
2388 
2389 			ZVAL_NEW_STR(&zv, tmpstr);
2390 			}
2391 			break;
2392 		case TIMELIB_ZONETYPE_ABBR:
2393 			ZVAL_STRING(&zv, tzobj->tzi.z.abbr);
2394 			break;
2395 	}
2396 	zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
2397 
2398 	return props;
2399 } /* }}} */
2400 
php_timezone_to_string(php_timezone_obj * tzobj,zval * zv)2401 static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
2402 {
2403 	switch (tzobj->type) {
2404 		case TIMELIB_ZONETYPE_ID:
2405 			ZVAL_STRING(zv, tzobj->tzi.tz->name);
2406 			break;
2407 		case TIMELIB_ZONETYPE_OFFSET: {
2408 			zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
2409 			timelib_sll utc_offset = tzobj->tzi.utc_offset;
2410 
2411 			ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
2412 				utc_offset < 0 ? '-' : '+',
2413 				abs(utc_offset / 3600),
2414 				abs(((utc_offset % 3600) / 60)));
2415 
2416 			ZVAL_NEW_STR(zv, tmpstr);
2417 			}
2418 			break;
2419 		case TIMELIB_ZONETYPE_ABBR:
2420 			ZVAL_STRING(zv, tzobj->tzi.z.abbr);
2421 			break;
2422 	}
2423 }
2424 
date_object_get_debug_info_timezone(zval * object,int * is_temp)2425 static HashTable *date_object_get_debug_info_timezone(zval *object, int *is_temp) /* {{{ */
2426 {
2427 	HashTable *ht, *props;
2428 	zval zv;
2429 	php_timezone_obj *tzobj;
2430 
2431 	tzobj = Z_PHPTIMEZONE_P(object);
2432 	props = zend_std_get_properties(object);
2433 
2434 	*is_temp = 1;
2435 	ht = zend_array_dup(props);
2436 
2437 	ZVAL_LONG(&zv, tzobj->type);
2438 	zend_hash_str_update(ht, "timezone_type", sizeof("timezone_type")-1, &zv);
2439 
2440 	php_timezone_to_string(tzobj, &zv);
2441 	zend_hash_str_update(ht, "timezone", sizeof("timezone")-1, &zv);
2442 
2443 	return ht;
2444 } /* }}} */
2445 
date_object_new_interval(zend_class_entry * class_type)2446 static zend_object *date_object_new_interval(zend_class_entry *class_type) /* {{{ */
2447 {
2448 	php_interval_obj *intern = zend_object_alloc(sizeof(php_interval_obj), class_type);
2449 
2450 	zend_object_std_init(&intern->std, class_type);
2451 	object_properties_init(&intern->std, class_type);
2452 	intern->std.handlers = &date_object_handlers_interval;
2453 
2454 	return &intern->std;
2455 } /* }}} */
2456 
date_object_clone_interval(zval * this_ptr)2457 static zend_object *date_object_clone_interval(zval *this_ptr) /* {{{ */
2458 {
2459 	php_interval_obj *old_obj = Z_PHPINTERVAL_P(this_ptr);
2460 	php_interval_obj *new_obj = php_interval_obj_from_obj(date_object_new_interval(old_obj->std.ce));
2461 
2462 	zend_objects_clone_members(&new_obj->std, &old_obj->std);
2463 	new_obj->initialized = old_obj->initialized;
2464 	if (old_obj->diff) {
2465 		new_obj->diff = timelib_rel_time_clone(old_obj->diff);
2466 	}
2467 
2468 	return &new_obj->std;
2469 } /* }}} */
2470 
date_object_get_gc_interval(zval * object,zval ** table,int * n)2471 static HashTable *date_object_get_gc_interval(zval *object, zval **table, int *n) /* {{{ */
2472 {
2473 
2474 	*table = NULL;
2475 	*n = 0;
2476 	return zend_std_get_properties(object);
2477 } /* }}} */
2478 
date_object_get_properties_interval(zval * object)2479 static HashTable *date_object_get_properties_interval(zval *object) /* {{{ */
2480 {
2481 	HashTable *props;
2482 	zval zv;
2483 	php_interval_obj     *intervalobj;
2484 
2485 	intervalobj = Z_PHPINTERVAL_P(object);
2486 
2487 	props = zend_std_get_properties(object);
2488 
2489 	if (!intervalobj->initialized) {
2490 		return props;
2491 	}
2492 
2493 #define PHP_DATE_INTERVAL_ADD_PROPERTY(n,f) \
2494 	ZVAL_LONG(&zv, (zend_long)intervalobj->diff->f); \
2495 	zend_hash_str_update(props, n, sizeof(n)-1, &zv);
2496 
2497 	PHP_DATE_INTERVAL_ADD_PROPERTY("y", y);
2498 	PHP_DATE_INTERVAL_ADD_PROPERTY("m", m);
2499 	PHP_DATE_INTERVAL_ADD_PROPERTY("d", d);
2500 	PHP_DATE_INTERVAL_ADD_PROPERTY("h", h);
2501 	PHP_DATE_INTERVAL_ADD_PROPERTY("i", i);
2502 	PHP_DATE_INTERVAL_ADD_PROPERTY("s", s);
2503 	ZVAL_DOUBLE(&zv, (double)intervalobj->diff->us / 1000000.0);
2504 	zend_hash_str_update(props, "f", sizeof("f") - 1, &zv);
2505 	PHP_DATE_INTERVAL_ADD_PROPERTY("weekday", weekday);
2506 	PHP_DATE_INTERVAL_ADD_PROPERTY("weekday_behavior", weekday_behavior);
2507 	PHP_DATE_INTERVAL_ADD_PROPERTY("first_last_day_of", first_last_day_of);
2508 	PHP_DATE_INTERVAL_ADD_PROPERTY("invert", invert);
2509 	if (intervalobj->diff->days != -99999) {
2510 		PHP_DATE_INTERVAL_ADD_PROPERTY("days", days);
2511 	} else {
2512 		ZVAL_FALSE(&zv);
2513 		zend_hash_str_update(props, "days", sizeof("days")-1, &zv);
2514 	}
2515 	PHP_DATE_INTERVAL_ADD_PROPERTY("special_type", special.type);
2516 	PHP_DATE_INTERVAL_ADD_PROPERTY("special_amount", special.amount);
2517 	PHP_DATE_INTERVAL_ADD_PROPERTY("have_weekday_relative", have_weekday_relative);
2518 	PHP_DATE_INTERVAL_ADD_PROPERTY("have_special_relative", have_special_relative);
2519 
2520 	return props;
2521 } /* }}} */
2522 
date_object_new_period(zend_class_entry * class_type)2523 static zend_object *date_object_new_period(zend_class_entry *class_type) /* {{{ */
2524 {
2525 	php_period_obj *intern = zend_object_alloc(sizeof(php_period_obj), class_type);
2526 
2527 	zend_object_std_init(&intern->std, class_type);
2528 	object_properties_init(&intern->std, class_type);
2529 
2530 	intern->std.handlers = &date_object_handlers_period;
2531 
2532 	return &intern->std;
2533 } /* }}} */
2534 
date_object_clone_period(zval * this_ptr)2535 static zend_object *date_object_clone_period(zval *this_ptr) /* {{{ */
2536 {
2537 	php_period_obj *old_obj = Z_PHPPERIOD_P(this_ptr);
2538 	php_period_obj *new_obj = php_period_obj_from_obj(date_object_new_period(old_obj->std.ce));
2539 
2540 	zend_objects_clone_members(&new_obj->std, &old_obj->std);
2541 	new_obj->initialized = old_obj->initialized;
2542 	new_obj->recurrences = old_obj->recurrences;
2543 	new_obj->include_start_date = old_obj->include_start_date;
2544 	new_obj->start_ce = old_obj->start_ce;
2545 
2546 	if (old_obj->start) {
2547 		new_obj->start = timelib_time_clone(old_obj->start);
2548 	}
2549 	if (old_obj->current) {
2550 		new_obj->current = timelib_time_clone(old_obj->current);
2551 	}
2552 	if (old_obj->end) {
2553         new_obj->end = timelib_time_clone(old_obj->end);
2554     }
2555     if (old_obj->interval) {
2556         new_obj->interval = timelib_rel_time_clone(old_obj->interval);
2557     }
2558 	return &new_obj->std;
2559 } /* }}} */
2560 
date_object_free_storage_date(zend_object * object)2561 static void date_object_free_storage_date(zend_object *object) /* {{{ */
2562 {
2563 	php_date_obj *intern = php_date_obj_from_obj(object);
2564 
2565 	if (intern->time) {
2566 		timelib_time_dtor(intern->time);
2567 	}
2568 
2569 	zend_object_std_dtor(&intern->std);
2570 } /* }}} */
2571 
date_object_free_storage_timezone(zend_object * object)2572 static void date_object_free_storage_timezone(zend_object *object) /* {{{ */
2573 {
2574 	php_timezone_obj *intern = php_timezone_obj_from_obj(object);
2575 
2576 	if (intern->type == TIMELIB_ZONETYPE_ABBR) {
2577 		timelib_free(intern->tzi.z.abbr);
2578 	}
2579 	zend_object_std_dtor(&intern->std);
2580 } /* }}} */
2581 
date_object_free_storage_interval(zend_object * object)2582 static void date_object_free_storage_interval(zend_object *object) /* {{{ */
2583 {
2584 	php_interval_obj *intern = php_interval_obj_from_obj(object);
2585 
2586 	timelib_rel_time_dtor(intern->diff);
2587 	zend_object_std_dtor(&intern->std);
2588 } /* }}} */
2589 
date_object_free_storage_period(zend_object * object)2590 static void date_object_free_storage_period(zend_object *object) /* {{{ */
2591 {
2592 	php_period_obj *intern = php_period_obj_from_obj(object);
2593 
2594 	if (intern->start) {
2595 		timelib_time_dtor(intern->start);
2596 	}
2597 
2598 	if (intern->current) {
2599 		timelib_time_dtor(intern->current);
2600 	}
2601 
2602 	if (intern->end) {
2603 		timelib_time_dtor(intern->end);
2604 	}
2605 
2606 	timelib_rel_time_dtor(intern->interval);
2607 	zend_object_std_dtor(&intern->std);
2608 } /* }}} */
2609 
2610 /* Advanced Interface */
php_date_instantiate(zend_class_entry * pce,zval * object)2611 PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object) /* {{{ */
2612 {
2613 	object_init_ex(object, pce);
2614 	return object;
2615 } /* }}} */
2616 
2617 /* Helper function used to store the latest found warnings and errors while
2618  * parsing, from either strtotime or parse_from_format. */
update_errors_warnings(timelib_error_container * last_errors)2619 static void update_errors_warnings(timelib_error_container *last_errors) /* {{{ */
2620 {
2621 	if (DATEG(last_errors)) {
2622 		timelib_error_container_dtor(DATEG(last_errors));
2623 		DATEG(last_errors) = NULL;
2624 	}
2625 	DATEG(last_errors) = last_errors;
2626 } /* }}} */
2627 
php_date_set_time_fraction(timelib_time * time,int microseconds)2628 static void php_date_set_time_fraction(timelib_time *time, int microseconds)
2629 {
2630 	time->us = microseconds;
2631 }
2632 
php_date_get_current_time_with_fraction(time_t * sec,suseconds_t * usec)2633 static void php_date_get_current_time_with_fraction(time_t *sec, suseconds_t *usec)
2634 {
2635 #if HAVE_GETTIMEOFDAY
2636 	struct timeval tp = {0}; /* For setting microseconds */
2637 
2638 	gettimeofday(&tp, NULL);
2639 	*sec = tp.tv_sec;
2640 	*usec = tp.tv_usec;
2641 #else
2642 	*sec = time(NULL);
2643 	*usec = 0;
2644 #endif
2645 }
2646 
php_date_initialize(php_date_obj * dateobj,char * time_str,size_t time_str_len,char * format,zval * timezone_object,int ctor)2647 PHPAPI int php_date_initialize(php_date_obj *dateobj, /*const*/ char *time_str, size_t time_str_len, char *format, zval *timezone_object, int ctor) /* {{{ */
2648 {
2649 	timelib_time   *now;
2650 	timelib_tzinfo *tzi = NULL;
2651 	timelib_error_container *err = NULL;
2652 	int type = TIMELIB_ZONETYPE_ID, new_dst = 0;
2653 	char *new_abbr = NULL;
2654 	timelib_sll new_offset = 0;
2655 	time_t sec;
2656 	suseconds_t usec;
2657 
2658 	if (dateobj->time) {
2659 		timelib_time_dtor(dateobj->time);
2660 	}
2661 	if (format) {
2662 		dateobj->time = timelib_parse_from_format(format, time_str_len ? time_str : "", time_str_len ? time_str_len : 0, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
2663 	} else {
2664 		dateobj->time = timelib_strtotime(time_str_len ? time_str : "now", time_str_len ? time_str_len : sizeof("now") -1, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
2665 	}
2666 
2667 	/* update last errors and warnings */
2668 	update_errors_warnings(err);
2669 
2670 
2671 	if (ctor && err && err->error_count) {
2672 		/* spit out the first library error message, at least */
2673 		php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
2674 			err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
2675 	}
2676 	if (err && err->error_count) {
2677 		timelib_time_dtor(dateobj->time);
2678 		dateobj->time = 0;
2679 		return 0;
2680 	}
2681 
2682 	if (timezone_object) {
2683 		php_timezone_obj *tzobj;
2684 
2685 		tzobj = Z_PHPTIMEZONE_P(timezone_object);
2686 		switch (tzobj->type) {
2687 			case TIMELIB_ZONETYPE_ID:
2688 				tzi = tzobj->tzi.tz;
2689 				break;
2690 			case TIMELIB_ZONETYPE_OFFSET:
2691 				new_offset = tzobj->tzi.utc_offset;
2692 				break;
2693 			case TIMELIB_ZONETYPE_ABBR:
2694 				new_offset = tzobj->tzi.z.utc_offset;
2695 				new_dst    = tzobj->tzi.z.dst;
2696 				new_abbr   = timelib_strdup(tzobj->tzi.z.abbr);
2697 				break;
2698 		}
2699 		type = tzobj->type;
2700 	} else if (dateobj->time->tz_info) {
2701 		tzi = dateobj->time->tz_info;
2702 	} else {
2703 		tzi = get_timezone_info();
2704 	}
2705 
2706 	now = timelib_time_ctor();
2707 	now->zone_type = type;
2708 	switch (type) {
2709 		case TIMELIB_ZONETYPE_ID:
2710 			now->tz_info = tzi;
2711 			break;
2712 		case TIMELIB_ZONETYPE_OFFSET:
2713 			now->z = new_offset;
2714 			break;
2715 		case TIMELIB_ZONETYPE_ABBR:
2716 			now->z = new_offset;
2717 			now->dst = new_dst;
2718 			now->tz_abbr = new_abbr;
2719 			break;
2720 	}
2721 	php_date_get_current_time_with_fraction(&sec, &usec);
2722 	timelib_unixtime2local(now, (timelib_sll) sec);
2723 	php_date_set_time_fraction(now, usec);
2724 	timelib_fill_holes(dateobj->time, now, TIMELIB_NO_CLONE);
2725 	timelib_update_ts(dateobj->time, tzi);
2726 	timelib_update_from_sse(dateobj->time);
2727 
2728 	dateobj->time->have_relative = 0;
2729 
2730 	timelib_time_dtor(now);
2731 
2732 	return 1;
2733 } /* }}} */
2734 
2735 /* {{{ proto DateTime date_create([string time[, DateTimeZone object]])
2736    Returns new DateTime object
2737 */
PHP_FUNCTION(date_create)2738 PHP_FUNCTION(date_create)
2739 {
2740 	zval           *timezone_object = NULL;
2741 	char           *time_str = NULL;
2742 	size_t          time_str_len = 0;
2743 
2744 	ZEND_PARSE_PARAMETERS_START(0, 2)
2745 		Z_PARAM_OPTIONAL
2746 		Z_PARAM_STRING(time_str, time_str_len)
2747 		Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
2748 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
2749 
2750 	php_date_instantiate(date_ce_date, return_value);
2751 	if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
2752 		zval_ptr_dtor(return_value);
2753 		RETURN_FALSE;
2754 	}
2755 }
2756 /* }}} */
2757 
2758 /* {{{ proto DateTime date_create_immutable([string time[, DateTimeZone object]])
2759    Returns new DateTime object
2760 */
PHP_FUNCTION(date_create_immutable)2761 PHP_FUNCTION(date_create_immutable)
2762 {
2763 	zval           *timezone_object = NULL;
2764 	char           *time_str = NULL;
2765 	size_t          time_str_len = 0;
2766 
2767 	ZEND_PARSE_PARAMETERS_START(0, 2)
2768 		Z_PARAM_OPTIONAL
2769 		Z_PARAM_STRING(time_str, time_str_len)
2770 		Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
2771 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
2772 
2773 	php_date_instantiate(date_ce_immutable, return_value);
2774 	if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
2775 		zval_ptr_dtor(return_value);
2776 		RETURN_FALSE;
2777 	}
2778 }
2779 /* }}} */
2780 
2781 /* {{{ proto DateTime date_create_from_format(string format, string time[, DateTimeZone object])
2782    Returns new DateTime object formatted according to the specified format
2783 */
PHP_FUNCTION(date_create_from_format)2784 PHP_FUNCTION(date_create_from_format)
2785 {
2786 	zval           *timezone_object = NULL;
2787 	char           *time_str = NULL, *format_str = NULL;
2788 	size_t          time_str_len = 0, format_str_len = 0;
2789 
2790 	ZEND_PARSE_PARAMETERS_START(2, 3)
2791 		Z_PARAM_STRING(format_str, format_str_len)
2792 		Z_PARAM_STRING(time_str, time_str_len)
2793 		Z_PARAM_OPTIONAL
2794 		Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
2795 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
2796 
2797 	php_date_instantiate(date_ce_date, return_value);
2798 	if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
2799 		zval_ptr_dtor(return_value);
2800 		RETURN_FALSE;
2801 	}
2802 }
2803 /* }}} */
2804 
2805 /* {{{ proto DateTime date_create_immutable_from_format(string format, string time[, DateTimeZone object])
2806    Returns new DateTime object formatted according to the specified format
2807 */
PHP_FUNCTION(date_create_immutable_from_format)2808 PHP_FUNCTION(date_create_immutable_from_format)
2809 {
2810 	zval           *timezone_object = NULL;
2811 	char           *time_str = NULL, *format_str = NULL;
2812 	size_t          time_str_len = 0, format_str_len = 0;
2813 
2814 	ZEND_PARSE_PARAMETERS_START(2, 3)
2815 		Z_PARAM_STRING(format_str, format_str_len)
2816 		Z_PARAM_STRING(time_str, time_str_len)
2817 		Z_PARAM_OPTIONAL
2818 		Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
2819 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
2820 
2821 	php_date_instantiate(date_ce_immutable, return_value);
2822 	if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
2823 		zval_ptr_dtor(return_value);
2824 		RETURN_FALSE;
2825 	}
2826 }
2827 /* }}} */
2828 
2829 /* {{{ proto DateTime::__construct([string time[, DateTimeZone object]])
2830    Creates new DateTime object
2831 */
PHP_METHOD(DateTime,__construct)2832 PHP_METHOD(DateTime, __construct)
2833 {
2834 	zval *timezone_object = NULL;
2835 	char *time_str = NULL;
2836 	size_t time_str_len = 0;
2837 	zend_error_handling error_handling;
2838 
2839 	ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 2)
2840 		Z_PARAM_OPTIONAL
2841 		Z_PARAM_STRING(time_str, time_str_len)
2842 		Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
2843 	ZEND_PARSE_PARAMETERS_END();
2844 
2845 	zend_replace_error_handling(EH_THROW, NULL, &error_handling);
2846 	php_date_initialize(Z_PHPDATE_P(getThis()), time_str, time_str_len, NULL, timezone_object, 1);
2847 	zend_restore_error_handling(&error_handling);
2848 }
2849 /* }}} */
2850 
2851 /* {{{ proto DateTimeImmutable::__construct([string time[, DateTimeZone object]])
2852    Creates new DateTimeImmutable object
2853 */
PHP_METHOD(DateTimeImmutable,__construct)2854 PHP_METHOD(DateTimeImmutable, __construct)
2855 {
2856 	zval *timezone_object = NULL;
2857 	char *time_str = NULL;
2858 	size_t time_str_len = 0;
2859 	zend_error_handling error_handling;
2860 
2861 	ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 2)
2862 		Z_PARAM_OPTIONAL
2863 		Z_PARAM_STRING(time_str, time_str_len)
2864 		Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
2865 	ZEND_PARSE_PARAMETERS_END();
2866 
2867 	zend_replace_error_handling(EH_THROW, NULL, &error_handling);
2868 	php_date_initialize(Z_PHPDATE_P(getThis()), time_str, time_str_len, NULL, timezone_object, 1);
2869 	zend_restore_error_handling(&error_handling);
2870 }
2871 /* }}} */
2872 
2873 /* {{{ proto DateTime::createFromImmutable(DateTimeImmutable object)
2874    Creates new DateTime object from an existing immutable DateTimeImmutable object.
2875 */
PHP_METHOD(DateTime,createFromImmutable)2876 PHP_METHOD(DateTime, createFromImmutable)
2877 {
2878 	zval *datetimeimmutable_object = NULL;
2879 	php_date_obj *new_obj = NULL;
2880 	php_date_obj *old_obj = NULL;
2881 
2882 	ZEND_PARSE_PARAMETERS_START(1, 1)
2883 		Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable)
2884 	ZEND_PARSE_PARAMETERS_END();
2885 
2886 	php_date_instantiate(date_ce_date, return_value);
2887 	old_obj = Z_PHPDATE_P(datetimeimmutable_object);
2888 	new_obj = Z_PHPDATE_P(return_value);
2889 
2890 	new_obj->time = timelib_time_clone(old_obj->time);
2891 }
2892 /* }}} */
2893 
2894 /* {{{ proto DateTimeImmutable::createFromMutable(DateTime object)
2895    Creates new DateTimeImmutable object from an existing mutable DateTime object.
2896 */
PHP_METHOD(DateTimeImmutable,createFromMutable)2897 PHP_METHOD(D