1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Derick Rethans <derick@derickrethans.nl> |
14 +----------------------------------------------------------------------+
15 */
16
17 #include "php.h"
18 #include "php_main.h"
19 #include "php_ini.h"
20 #include "ext/standard/info.h"
21 #include "ext/standard/php_versioning.h"
22 #include "php_date.h"
23 #include "zend_attributes.h"
24 #include "zend_interfaces.h"
25 #include "zend_exceptions.h"
26 #include "lib/timelib.h"
27 #include "lib/timelib_private.h"
28 #ifndef PHP_WIN32
29 #include <time.h>
30 #else
31 #include "win32/time.h"
32 #endif
33
34 #ifdef PHP_WIN32
php_date_llabs(__int64 i)35 static __inline __int64 php_date_llabs( __int64 i ) { return i >= 0? i: -i; }
36 #elif defined(__GNUC__) && __GNUC__ < 3
php_date_llabs(__int64_t i)37 static __inline __int64_t php_date_llabs( __int64_t i ) { return i >= 0 ? i : -i; }
38 #else
php_date_llabs(long long i)39 static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i; }
40 #endif
41
42 #ifdef PHP_WIN32
43 #define DATE_I64_BUF_LEN 65
44 # define DATE_I64A(i, s, len) _i64toa_s(i, s, len, 10)
45 # define DATE_A64I(i, s) i = _atoi64(s)
46 #else
47 #define DATE_I64_BUF_LEN 65
48 # define DATE_I64A(i, s, len) \
49 do { \
50 int st = snprintf(s, len, "%lld", i); \
51 s[st] = '\0'; \
52 } while (0);
53 #define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
54 #endif
55
php_time(void)56 PHPAPI time_t php_time(void)
57 {
58 #ifdef HAVE_GETTIMEOFDAY
59 struct timeval tm;
60
61 if (UNEXPECTED(gettimeofday(&tm, NULL) != SUCCESS)) {
62 /* fallback, can't reasonably happen */
63 return time(NULL);
64 }
65
66 return tm.tv_sec;
67 #else
68 return time(NULL);
69 #endif
70 }
71
72 /*
73 * RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
74 * date-time = [ day "," ] date time ; dd mm yy hh:mm:ss zzz
75 * day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
76 * date = 1*2DIGIT month 2DIGIT ; day month year e.g. 20 Jun 82
77 * month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
78 * time = hour zone ; ANSI and Military
79 * hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
80 * zone = "UT" / "GMT" / "EST" / "EDT" / "CST" / "CDT" / "MST" / "MDT" / "PST" / "PDT" / 1ALPHA / ( ("+" / "-") 4DIGIT )
81 */
82 #define DATE_FORMAT_RFC822 "D, d M y H:i:s O"
83
84 /*
85 * RFC850, Section 2.1.4: http://www.ietf.org/rfc/rfc850.txt
86 * Format must be acceptable both to the ARPANET and to the getdate routine.
87 * One format that is acceptable to both is Weekday, DD-Mon-YY HH:MM:SS TIMEZONE
88 * TIMEZONE can be any timezone name (3 or more letters)
89 */
90 #define DATE_FORMAT_RFC850 "l, d-M-y H:i:s T"
91
92 /*
93 * RFC1036, Section 2.1.2: http://www.ietf.org/rfc/rfc1036.txt
94 * Its format must be acceptable both in RFC-822 and to the getdate(3)
95 * Wdy, DD Mon YY HH:MM:SS TIMEZONE
96 * There is no hope of having a complete list of timezones. Universal
97 * Time (GMT), the North American timezones (PST, PDT, MST, MDT, CST,
98 * CDT, EST, EDT) and the +/-hhmm offset specified in RFC-822 should be supported.
99 */
100 #define DATE_FORMAT_RFC1036 "D, d M y H:i:s O"
101
102 /*
103 * RFC1123, Section 5.2.14: http://www.ietf.org/rfc/rfc1123.txt
104 * RFC-822 Date and Time Specification: RFC-822 Section 5
105 * The syntax for the date is hereby changed to: date = 1*2DIGIT month 2*4DIGIT
106 */
107 #define DATE_FORMAT_RFC1123 "D, d M Y H:i:s O"
108
109 /*
110 * RFC7231, Section 7.1.1: http://tools.ietf.org/html/rfc7231
111 */
112 #define DATE_FORMAT_RFC7231 "D, d M Y H:i:s \\G\\M\\T"
113
114 /*
115 * RFC2822, Section 3.3: http://www.ietf.org/rfc/rfc2822.txt
116 * FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space
117 * CFWS = *([FWS] comment) (([FWS] comment) / FWS)
118 *
119 * date-time = [ day-of-week "," ] date FWS time [CFWS]
120 * day-of-week = ([FWS] day-name)
121 * day-name = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
122 * date = day month year
123 * year = 4*DIGIT
124 * month = (FWS month-name FWS)
125 * month-name = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
126 * day = ([FWS] 1*2DIGIT)
127 * time = time-of-day FWS zone
128 * time-of-day = hour ":" minute [ ":" second ]
129 * hour = 2DIGIT
130 * minute = 2DIGIT
131 * second = 2DIGIT
132 * zone = (( "+" / "-" ) 4DIGIT)
133 */
134 #define DATE_FORMAT_RFC2822 "D, d M Y H:i:s O"
135
136 /*
137 * RFC3339, Section 5.6: http://www.ietf.org/rfc/rfc3339.txt
138 * date-fullyear = 4DIGIT
139 * date-month = 2DIGIT ; 01-12
140 * date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
141 *
142 * time-hour = 2DIGIT ; 00-23
143 * time-minute = 2DIGIT ; 00-59
144 * time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
145 *
146 * time-secfrac = "." 1*DIGIT
147 * time-numoffset = ("+" / "-") time-hour ":" time-minute
148 * time-offset = "Z" / time-numoffset
149 *
150 * partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
151 * full-date = date-fullyear "-" date-month "-" date-mday
152 * full-time = partial-time time-offset
153 *
154 * date-time = full-date "T" full-time
155 */
156 #define DATE_FORMAT_RFC3339 "Y-m-d\\TH:i:sP"
157
158 /*
159 * This format does not technically match the ISO 8601 standard, as it does not
160 * use : in the UTC offset format specifier. This is kept for BC reasons. The
161 * DATE_FORMAT_ISO8601_EXPANDED format does correct this, as well as adding
162 * support for years out side of the traditional 0000-9999 range.
163 */
164 #define DATE_FORMAT_ISO8601 "Y-m-d\\TH:i:sO"
165
166 /* ISO 8601:2004(E)
167 *
168 * Section 3.5 Expansion:
169 * By mutual agreement of the partners in information interchange, it is
170 * permitted to expand the component identifying the calendar year, which is
171 * otherwise limited to four digits. This enables reference to dates and times
172 * in calendar years outside the range supported by complete representations,
173 * i.e. before the start of the year [0000] or after the end of the year
174 * [9999]."
175 *
176 * Section 4.1.2.4 Expanded representations:
177 * If, by agreement, expanded representations are used, the formats shall be as
178 * specified below. The interchange parties shall agree the additional number of
179 * digits in the time element year. In the examples below it has been agreed to
180 * expand the time element year with two digits.
181 * Extended format: ±YYYYY-MM-DD
182 * Example: +001985-04-12
183 *
184 * PHP's year expansion digits are variable.
185 */
186 #define DATE_FORMAT_ISO8601_EXPANDED "X-m-d\\TH:i:sP"
187
188 /* Internal Only
189 * This format only extends the year when needed, keeping the 'P' format with
190 * colon for UTC offsets
191 */
192 #define DATE_FORMAT_ISO8601_LARGE_YEAR "x-m-d\\TH:i:sP"
193
194 /*
195 * RFC3339, Appendix A: http://www.ietf.org/rfc/rfc3339.txt
196 * ISO 8601 also requires (in section 5.3.1.3) that a decimal fraction
197 * be proceeded by a "0" if less than unity. Annex B.2 of ISO 8601
198 * gives examples where the decimal fractions are not preceded by a "0".
199 * This grammar assumes section 5.3.1.3 is correct and that Annex B.2 is
200 * in error.
201 */
202 #define DATE_FORMAT_RFC3339_EXTENDED "Y-m-d\\TH:i:s.vP"
203
204 /*
205 * This comes from various sources that like to contradict. I'm going with the
206 * format here because of:
207 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa384321%28v=vs.85%29.aspx
208 * and http://curl.haxx.se/rfc/cookie_spec.html
209 */
210 #define DATE_FORMAT_COOKIE "l, d-M-Y H:i:s T"
211
212 #define SUNFUNCS_RET_TIMESTAMP 0
213 #define SUNFUNCS_RET_STRING 1
214 #define SUNFUNCS_RET_DOUBLE 2
215
216 #define PHP_DATE_TIMEZONE_GROUP_AFRICA 0x0001
217 #define PHP_DATE_TIMEZONE_GROUP_AMERICA 0x0002
218 #define PHP_DATE_TIMEZONE_GROUP_ANTARCTICA 0x0004
219 #define PHP_DATE_TIMEZONE_GROUP_ARCTIC 0x0008
220 #define PHP_DATE_TIMEZONE_GROUP_ASIA 0x0010
221 #define PHP_DATE_TIMEZONE_GROUP_ATLANTIC 0x0020
222 #define PHP_DATE_TIMEZONE_GROUP_AUSTRALIA 0x0040
223 #define PHP_DATE_TIMEZONE_GROUP_EUROPE 0x0080
224 #define PHP_DATE_TIMEZONE_GROUP_INDIAN 0x0100
225 #define PHP_DATE_TIMEZONE_GROUP_PACIFIC 0x0200
226 #define PHP_DATE_TIMEZONE_GROUP_UTC 0x0400
227 #define PHP_DATE_TIMEZONE_GROUP_ALL 0x07FF
228 #define PHP_DATE_TIMEZONE_GROUP_ALL_W_BC 0x0FFF
229 #define PHP_DATE_TIMEZONE_PER_COUNTRY 0x1000
230
231 #define PHP_DATE_PERIOD_EXCLUDE_START_DATE 0x0001
232 #define PHP_DATE_PERIOD_INCLUDE_END_DATE 0x0002
233
234 #include "php_date_arginfo.h"
235
236 static const char* guess_timezone(const timelib_tzdb *tzdb);
237 static void date_register_classes(void);
238 /* }}} */
239
240 ZEND_DECLARE_MODULE_GLOBALS(date)
241 static PHP_GINIT_FUNCTION(date);
242
243 /* True global */
244 timelib_tzdb *php_date_global_timezone_db;
245 int php_date_global_timezone_db_enabled;
246
247 #define DATE_DEFAULT_LATITUDE "31.7667"
248 #define DATE_DEFAULT_LONGITUDE "35.2333"
249
250 /* on 90'50; common sunset declaration (start of sun body appear) */
251 #define DATE_SUNSET_ZENITH "90.833333"
252
253 /* on 90'50; common sunrise declaration (sun body disappeared) */
254 #define DATE_SUNRISE_ZENITH "90.833333"
255
256 static PHP_INI_MH(OnUpdate_date_timezone);
257
258 /* {{{ INI Settings */
259 PHP_INI_BEGIN()
260 STD_PHP_INI_ENTRY("date.timezone", "UTC", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
261 PHP_INI_ENTRY("date.default_latitude", DATE_DEFAULT_LATITUDE, PHP_INI_ALL, NULL)
262 PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL)
263 PHP_INI_ENTRY("date.sunset_zenith", DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL)
264 PHP_INI_ENTRY("date.sunrise_zenith", DATE_SUNRISE_ZENITH, PHP_INI_ALL, NULL)
265 PHP_INI_END()
266 /* }}} */
267
268 static zend_class_entry *date_ce_date, *date_ce_timezone, *date_ce_interval, *date_ce_period;
269 static zend_class_entry *date_ce_immutable, *date_ce_interface;
270 static zend_class_entry *date_ce_date_error, *date_ce_date_object_error, *date_ce_date_range_error;
271 static zend_class_entry *date_ce_date_exception, *date_ce_date_invalid_timezone_exception, *date_ce_date_invalid_operation_exception, *date_ce_date_malformed_string_exception, *date_ce_date_malformed_interval_string_exception, *date_ce_date_malformed_period_string_exception;
272
273
php_date_get_date_ce(void)274 PHPAPI zend_class_entry *php_date_get_date_ce(void)
275 {
276 return date_ce_date;
277 }
278
php_date_get_immutable_ce(void)279 PHPAPI zend_class_entry *php_date_get_immutable_ce(void)
280 {
281 return date_ce_immutable;
282 }
283
php_date_get_interface_ce(void)284 PHPAPI zend_class_entry *php_date_get_interface_ce(void)
285 {
286 return date_ce_interface;
287 }
288
php_date_get_timezone_ce(void)289 PHPAPI zend_class_entry *php_date_get_timezone_ce(void)
290 {
291 return date_ce_timezone;
292 }
293
php_date_get_interval_ce(void)294 PHPAPI zend_class_entry *php_date_get_interval_ce(void)
295 {
296 return date_ce_interval;
297 }
298
php_date_get_period_ce(void)299 PHPAPI zend_class_entry *php_date_get_period_ce(void)
300 {
301 return date_ce_period;
302 }
303
304 static zend_object_handlers date_object_handlers_date;
305 static zend_object_handlers date_object_handlers_immutable;
306 static zend_object_handlers date_object_handlers_timezone;
307 static zend_object_handlers date_object_handlers_interval;
308 static zend_object_handlers date_object_handlers_period;
309
date_throw_uninitialized_error(zend_class_entry * ce)310 static void date_throw_uninitialized_error(zend_class_entry *ce)
311 {
312 if (ce->type == ZEND_INTERNAL_CLASS) {
313 zend_throw_error(date_ce_date_object_error, "Object of type %s has not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name));
314 } else {
315 zend_class_entry *ce_ptr = ce;
316 while (ce_ptr && ce_ptr->parent && ce_ptr->type == ZEND_USER_CLASS) {
317 ce_ptr = ce_ptr->parent;
318 }
319 if (ce_ptr->type != ZEND_INTERNAL_CLASS) {
320 zend_throw_error(date_ce_date_object_error, "Object of type %s not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name));
321 return;
322 }
323 zend_throw_error(date_ce_date_object_error, "Object of type %s (inheriting %s) has not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name), ZSTR_VAL(ce_ptr->name));
324 }
325 }
326
327 #define DATE_CHECK_INITIALIZED(member, ce) \
328 if (UNEXPECTED(!member)) { \
329 date_throw_uninitialized_error(ce); \
330 RETURN_THROWS(); \
331 }
332
333 static void date_object_free_storage_date(zend_object *object);
334 static void date_object_free_storage_timezone(zend_object *object);
335 static void date_object_free_storage_interval(zend_object *object);
336 static void date_object_free_storage_period(zend_object *object);
337
338 static zend_object *date_object_new_date(zend_class_entry *class_type);
339 static zend_object *date_object_new_timezone(zend_class_entry *class_type);
340 static zend_object *date_object_new_interval(zend_class_entry *class_type);
341 static zend_object *date_object_new_period(zend_class_entry *class_type);
342
343 static zend_object *date_object_clone_date(zend_object *this_ptr);
344 static zend_object *date_object_clone_timezone(zend_object *this_ptr);
345 static zend_object *date_object_clone_interval(zend_object *this_ptr);
346 static zend_object *date_object_clone_period(zend_object *this_ptr);
347
348 static int date_object_compare_date(zval *d1, zval *d2);
349 static HashTable *date_object_get_gc(zend_object *object, zval **table, int *n);
350 static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_purpose purpose);
351 static HashTable *date_object_get_gc_interval(zend_object *object, zval **table, int *n);
352 static HashTable *date_object_get_properties_interval(zend_object *object);
353 static HashTable *date_object_get_gc_period(zend_object *object, zval **table, int *n);
354 static HashTable *date_object_get_properties_for_timezone(zend_object *object, zend_prop_purpose purpose);
355 static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table, int *n);
356 static HashTable *date_object_get_debug_info_timezone(zend_object *object, int *is_temp);
357 static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv);
358
359 static int date_interval_compare_objects(zval *o1, zval *o2);
360 static zval *date_interval_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv);
361 static zval *date_interval_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot);
362 static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot);
363 static int date_period_has_property(zend_object *object, zend_string *name, int type, void **cache_slot);
364 static zval *date_period_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv);
365 static zval *date_period_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot);
366 static zval *date_period_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot);
367 static void date_period_unset_property(zend_object *object, zend_string *name, void **cache_slot);
368 static HashTable *date_period_get_properties_for(zend_object *object, zend_prop_purpose purpose);
369 static int date_object_compare_timezone(zval *tz1, zval *tz2);
370
371 /* {{{ Module struct */
372 zend_module_entry date_module_entry = {
373 STANDARD_MODULE_HEADER_EX,
374 NULL,
375 NULL,
376 "date", /* extension name */
377 ext_functions, /* function list */
378 PHP_MINIT(date), /* process startup */
379 PHP_MSHUTDOWN(date), /* process shutdown */
380 PHP_RINIT(date), /* request startup */
381 PHP_RSHUTDOWN(date), /* request shutdown */
382 PHP_MINFO(date), /* extension info */
383 PHP_DATE_VERSION, /* extension version */
384 PHP_MODULE_GLOBALS(date), /* globals descriptor */
385 PHP_GINIT(date), /* globals ctor */
386 NULL, /* globals dtor */
387 ZEND_MODULE_POST_ZEND_DEACTIVATE_N(date), /* post deactivate */
388 STANDARD_MODULE_PROPERTIES_EX
389 };
390 /* }}} */
391
392
393 /* {{{ PHP_GINIT_FUNCTION */
PHP_GINIT_FUNCTION(date)394 static PHP_GINIT_FUNCTION(date)
395 {
396 date_globals->default_timezone = NULL;
397 date_globals->timezone = NULL;
398 date_globals->tzcache = NULL;
399 }
400 /* }}} */
401
402
_php_date_tzinfo_dtor(zval * zv)403 static void _php_date_tzinfo_dtor(zval *zv) /* {{{ */
404 {
405 timelib_tzinfo *tzi = (timelib_tzinfo*)Z_PTR_P(zv);
406
407 timelib_tzinfo_dtor(tzi);
408 } /* }}} */
409
410 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(date)411 PHP_RINIT_FUNCTION(date)
412 {
413 if (DATEG(timezone)) {
414 efree(DATEG(timezone));
415 }
416 DATEG(timezone) = NULL;
417 DATEG(tzcache) = NULL;
418 DATEG(last_errors) = NULL;
419
420 return SUCCESS;
421 }
422 /* }}} */
423
424 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(date)425 PHP_RSHUTDOWN_FUNCTION(date)
426 {
427 if (DATEG(timezone)) {
428 efree(DATEG(timezone));
429 }
430 DATEG(timezone) = NULL;
431
432 return SUCCESS;
433 }
434 /* }}} */
435
ZEND_MODULE_POST_ZEND_DEACTIVATE_D(date)436 ZEND_MODULE_POST_ZEND_DEACTIVATE_D(date)
437 {
438 if (DATEG(tzcache)) {
439 zend_hash_destroy(DATEG(tzcache));
440 FREE_HASHTABLE(DATEG(tzcache));
441 DATEG(tzcache) = NULL;
442 }
443
444 if (DATEG(last_errors)) {
445 timelib_error_container_dtor(DATEG(last_errors));
446 DATEG(last_errors) = NULL;
447 }
448
449 return SUCCESS;
450 }
451
452 #define DATE_TIMEZONEDB php_date_global_timezone_db ? php_date_global_timezone_db : timelib_builtin_db()
453
454 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(date)455 PHP_MINIT_FUNCTION(date)
456 {
457 REGISTER_INI_ENTRIES();
458 date_register_classes();
459 register_php_date_symbols(module_number);
460
461 php_date_global_timezone_db = NULL;
462 php_date_global_timezone_db_enabled = 0;
463 DATEG(last_errors) = NULL;
464 return SUCCESS;
465 }
466 /* }}} */
467
468 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(date)469 PHP_MSHUTDOWN_FUNCTION(date)
470 {
471 UNREGISTER_INI_ENTRIES();
472
473 if (DATEG(last_errors)) {
474 timelib_error_container_dtor(DATEG(last_errors));
475 }
476
477 #ifndef ZTS
478 DATEG(default_timezone) = NULL;
479 #endif
480
481 return SUCCESS;
482 }
483 /* }}} */
484
485 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(date)486 PHP_MINFO_FUNCTION(date)
487 {
488 const timelib_tzdb *tzdb = DATE_TIMEZONEDB;
489
490 php_info_print_table_start();
491 php_info_print_table_row(2, "date/time support", "enabled");
492 php_info_print_table_row(2, "timelib version", TIMELIB_ASCII_VERSION);
493 php_info_print_table_row(2, "\"Olson\" Timezone Database Version", tzdb->version);
494 php_info_print_table_row(2, "Timezone Database", php_date_global_timezone_db_enabled ? "external" : "internal");
495 php_info_print_table_row(2, "Default timezone", guess_timezone(tzdb));
496 php_info_print_table_end();
497
498 DISPLAY_INI_ENTRIES();
499 }
500 /* }}} */
501
502 /* {{{ Timezone Cache functions */
php_date_parse_tzfile(const char * formal_tzname,const timelib_tzdb * tzdb)503 static timelib_tzinfo *php_date_parse_tzfile(const char *formal_tzname, const timelib_tzdb *tzdb)
504 {
505 timelib_tzinfo *tzi;
506 int dummy_error_code;
507
508 if(!DATEG(tzcache)) {
509 ALLOC_HASHTABLE(DATEG(tzcache));
510 zend_hash_init(DATEG(tzcache), 4, NULL, _php_date_tzinfo_dtor, 0);
511 }
512
513 if ((tzi = zend_hash_str_find_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname))) != NULL) {
514 return tzi;
515 }
516
517 tzi = timelib_parse_tzfile(formal_tzname, tzdb, &dummy_error_code);
518 if (tzi) {
519 zend_hash_str_add_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname), tzi);
520 }
521 return tzi;
522 }
523
php_date_parse_tzfile_wrapper(const char * formal_tzname,const timelib_tzdb * tzdb,int * dummy_error_code)524 static timelib_tzinfo *php_date_parse_tzfile_wrapper(const char *formal_tzname, const timelib_tzdb *tzdb, int *dummy_error_code)
525 {
526 return php_date_parse_tzfile(formal_tzname, tzdb);
527 }
528 /* }}} */
529
530 /* Callback to check the date.timezone only when changed increases performance */
531 /* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
PHP_INI_MH(OnUpdate_date_timezone)532 static PHP_INI_MH(OnUpdate_date_timezone)
533 {
534 if (new_value && !timelib_timezone_id_is_valid(ZSTR_VAL(new_value), DATE_TIMEZONEDB)) {
535 php_error_docref(
536 NULL, E_WARNING,
537 "Invalid date.timezone value '%s', using '%s' instead",
538 ZSTR_VAL(new_value),
539 DATEG(default_timezone) ? DATEG(default_timezone) : "UTC"
540 );
541 return FAILURE;
542 }
543
544 if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
545 return FAILURE;
546 }
547
548 return SUCCESS;
549 }
550 /* }}} */
551
552 /* {{{ Helper functions */
guess_timezone(const timelib_tzdb * tzdb)553 static const char* guess_timezone(const timelib_tzdb *tzdb)
554 {
555 /* Checking whether timezone has been set with date_default_timezone_set() */
556 if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
557 return DATEG(timezone);
558 }
559 /* Check config setting for default timezone */
560 if (!DATEG(default_timezone)) {
561 /* Special case: ext/date wasn't initialized yet */
562 zval *ztz;
563
564 if (NULL != (ztz = cfg_get_entry("date.timezone", sizeof("date.timezone")))
565 && Z_TYPE_P(ztz) == IS_STRING && Z_STRLEN_P(ztz) > 0 && timelib_timezone_id_is_valid(Z_STRVAL_P(ztz), tzdb)) {
566 return Z_STRVAL_P(ztz);
567 }
568 } else if (*DATEG(default_timezone)) {
569 return DATEG(default_timezone);
570 }
571 /* Fallback to UTC */
572 return "UTC";
573 }
574
get_timezone_info(void)575 PHPAPI timelib_tzinfo *get_timezone_info(void)
576 {
577 timelib_tzinfo *tzi;
578
579 const char *tz = guess_timezone(DATE_TIMEZONEDB);
580 tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
581 if (! tzi) {
582 zend_throw_error(date_ce_date_error, "Timezone database is corrupt. Please file a bug report as this should never happen");
583 }
584 return tzi;
585 }
586
update_property(zend_object * object,zend_string * key,zval * prop_val)587 static void update_property(zend_object *object, zend_string *key, zval *prop_val)
588 {
589 if (ZSTR_LEN(key) > 0 && ZSTR_VAL(key)[0] == '\0') { // not public
590 const char *class_name, *prop_name;
591 size_t prop_name_len;
592
593 if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
594 if (class_name[0] != '*') { // private
595 zend_string *cname;
596 zend_class_entry *ce;
597
598 cname = zend_string_init(class_name, strlen(class_name), 0);
599 ce = zend_lookup_class(cname);
600
601 if (ce) {
602 zend_update_property(ce, object, prop_name, prop_name_len, prop_val);
603 }
604
605 zend_string_release_ex(cname, 0);
606 } else { // protected
607 zend_update_property(object->ce, object, prop_name, prop_name_len, prop_val);
608 }
609 }
610 return;
611 }
612
613 // public
614 zend_update_property(object->ce, object, ZSTR_VAL(key), ZSTR_LEN(key), prop_val);
615 }
616 /* }}} */
617
618
619 /* {{{ date() and gmdate() data */
620 #include "zend_smart_str.h"
621
622 static const char * const mon_full_names[] = {
623 "January", "February", "March", "April",
624 "May", "June", "July", "August",
625 "September", "October", "November", "December"
626 };
627
628 static const char * const mon_short_names[] = {
629 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
630 };
631
632 static const char * const day_full_names[] = {
633 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
634 };
635
636 static const char * const day_short_names[] = {
637 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
638 };
639
english_suffix(timelib_sll number)640 static const char *english_suffix(timelib_sll number)
641 {
642 if (number >= 10 && number <= 19) {
643 return "th";
644 } else {
645 switch (number % 10) {
646 case 1: return "st";
647 case 2: return "nd";
648 case 3: return "rd";
649 }
650 }
651 return "th";
652 }
653 /* }}} */
654
655 /* {{{ day of week helpers */
php_date_full_day_name(timelib_sll y,timelib_sll m,timelib_sll d)656 static const char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
657 {
658 timelib_sll day_of_week = timelib_day_of_week(y, m, d);
659 if (day_of_week < 0) {
660 return "Unknown";
661 }
662 return day_full_names[day_of_week];
663 }
664
php_date_short_day_name(timelib_sll y,timelib_sll m,timelib_sll d)665 static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
666 {
667 timelib_sll day_of_week = timelib_day_of_week(y, m, d);
668 if (day_of_week < 0) {
669 return "Unknown";
670 }
671 return day_short_names[day_of_week];
672 }
673 /* }}} */
674
675 /* {{{ date_format - (gm)date helper */
date_format(const char * format,size_t format_len,timelib_time * t,bool localtime)676 static zend_string *date_format(const char *format, size_t format_len, timelib_time *t, bool localtime)
677 {
678 smart_str string = {0};
679 size_t i;
680 int length = 0;
681 char buffer[97];
682 timelib_time_offset *offset = NULL;
683 timelib_sll isoweek, isoyear;
684 bool rfc_colon;
685 int weekYearSet = 0;
686
687 if (!format_len) {
688 return ZSTR_EMPTY_ALLOC();
689 }
690
691 if (localtime) {
692 if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
693 offset = timelib_time_offset_ctor();
694 offset->offset = (t->z + (t->dst * 3600));
695 offset->leap_secs = 0;
696 offset->is_dst = t->dst;
697 offset->abbr = timelib_strdup(t->tz_abbr);
698 } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
699 offset = timelib_time_offset_ctor();
700 offset->offset = (t->z);
701 offset->leap_secs = 0;
702 offset->is_dst = 0;
703 offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
704 snprintf(offset->abbr, 9, "GMT%c%02d%02d",
705 (offset->offset < 0) ? '-' : '+',
706 abs(offset->offset / 3600),
707 abs((offset->offset % 3600) / 60));
708 } else if (t->zone_type == TIMELIB_ZONETYPE_ID) {
709 offset = timelib_get_time_zone_info(t->sse, t->tz_info);
710 } else {
711 /* Shouldn't happen, but code defensively */
712 offset = timelib_time_offset_ctor();
713 }
714 }
715
716 for (i = 0; i < format_len; i++) {
717 rfc_colon = 0;
718 switch (format[i]) {
719 /* day */
720 case 'd': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
721 case 'D': length = slprintf(buffer, sizeof(buffer), "%s", php_date_short_day_name(t->y, t->m, t->d)); break;
722 case 'j': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
723 case 'l': length = slprintf(buffer, sizeof(buffer), "%s", php_date_full_day_name(t->y, t->m, t->d)); break;
724 case 'S': length = slprintf(buffer, sizeof(buffer), "%s", english_suffix(t->d)); break;
725 case 'w': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_week(t->y, t->m, t->d)); break;
726 case 'N': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_iso_day_of_week(t->y, t->m, t->d)); break;
727 case 'z': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_year(t->y, t->m, t->d)); break;
728
729 /* week */
730 case 'W':
731 if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
732 length = slprintf(buffer, sizeof(buffer), "%02d", (int) isoweek); break; /* iso weeknr */
733 case 'o':
734 if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
735 length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) isoyear); break; /* iso year */
736
737 /* month */
738 case 'F': length = slprintf(buffer, sizeof(buffer), "%s", mon_full_names[t->m - 1]); break;
739 case 'm': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
740 case 'M': length = slprintf(buffer, sizeof(buffer), "%s", mon_short_names[t->m - 1]); break;
741 case 'n': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
742 case 't': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_days_in_month(t->y, t->m)); break;
743
744 /* year */
745 case 'L': length = slprintf(buffer, sizeof(buffer), "%d", timelib_is_leap((int) t->y)); break;
746 case 'y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) (t->y % 100)); break;
747 case 'Y': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
748 case 'x': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : (t->y >= 10000 ? "+" : ""), php_date_llabs((timelib_sll) t->y)); break;
749 case 'X': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "+", php_date_llabs((timelib_sll) t->y)); break;
750
751 /* time */
752 case 'a': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "pm" : "am"); break;
753 case 'A': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "PM" : "AM"); break;
754 case 'B': {
755 int retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
756 if (retval < 0) {
757 retval += 864000;
758 }
759 /* Make sure to do this on a positive int to avoid rounding errors */
760 retval = (retval / 864) % 1000;
761 length = slprintf(buffer, sizeof(buffer), "%03d", retval);
762 break;
763 }
764 case 'g': length = slprintf(buffer, sizeof(buffer), "%d", (t->h % 12) ? (int) t->h % 12 : 12); break;
765 case 'G': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
766 case 'h': length = slprintf(buffer, sizeof(buffer), "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
767 case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
768 case 'i': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
769 case 's': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->s); break;
770 case 'u': length = slprintf(buffer, sizeof(buffer), "%06d", (int) floor(t->us)); break;
771 case 'v': length = slprintf(buffer, sizeof(buffer), "%03d", (int) floor(t->us / 1000)); break;
772
773 /* timezone */
774 case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
775 case 'p':
776 if (!localtime || strcmp(offset->abbr, "UTC") == 0 || strcmp(offset->abbr, "Z") == 0 || strcmp(offset->abbr, "GMT+0000") == 0) {
777 length = slprintf(buffer, sizeof(buffer), "%s", "Z");
778 break;
779 }
780 ZEND_FALLTHROUGH;
781 case 'P': rfc_colon = 1; ZEND_FALLTHROUGH;
782 case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
783 localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
784 localtime ? abs(offset->offset / 3600) : 0,
785 rfc_colon ? ":" : "",
786 localtime ? abs((offset->offset % 3600) / 60) : 0
787 );
788 break;
789 case 'T': length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); break;
790 case 'e': if (!localtime) {
791 length = slprintf(buffer, sizeof(buffer), "%s", "UTC");
792 } else {
793 switch (t->zone_type) {
794 case TIMELIB_ZONETYPE_ID:
795 length = slprintf(buffer, sizeof(buffer), "%s", t->tz_info->name);
796 break;
797 case TIMELIB_ZONETYPE_ABBR:
798 length = slprintf(buffer, sizeof(buffer), "%s", offset->abbr);
799 break;
800 case TIMELIB_ZONETYPE_OFFSET:
801 length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d",
802 ((offset->offset < 0) ? '-' : '+'),
803 abs(offset->offset / 3600),
804 abs((offset->offset % 3600) / 60)
805 );
806 break;
807 }
808 }
809 break;
810 case 'Z': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); break;
811
812 /* full date/time */
813 case 'c': length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
814 (zend_long) t->y, (int) t->m, (int) t->d,
815 (int) t->h, (int) t->i, (int) t->s,
816 localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
817 localtime ? abs(offset->offset / 3600) : 0,
818 localtime ? abs((offset->offset % 3600) / 60) : 0
819 );
820 break;
821 case 'r': length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d",
822 php_date_short_day_name(t->y, t->m, t->d),
823 (int) t->d, mon_short_names[t->m - 1],
824 (zend_long) t->y, (int) t->h, (int) t->i, (int) t->s,
825 localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
826 localtime ? abs(offset->offset / 3600) : 0,
827 localtime ? abs((offset->offset % 3600) / 60) : 0
828 );
829 break;
830 case 'U': length = slprintf(buffer, sizeof(buffer), "%lld", (timelib_sll) t->sse); break;
831
832 case '\\': if (i < format_len) i++; ZEND_FALLTHROUGH;
833
834 default: buffer[0] = format[i]; buffer[1] = '\0'; length = 1; break;
835 }
836 smart_str_appendl(&string, buffer, length);
837 }
838
839 smart_str_0(&string);
840
841 if (localtime) {
842 timelib_time_offset_dtor(offset);
843 }
844
845 return string.s;
846 }
847
php_format_date_obj(const char * format,size_t format_len,php_date_obj * date_obj)848 PHPAPI zend_string *php_format_date_obj(const char *format, size_t format_len, php_date_obj *date_obj)
849 {
850 if (!date_obj->time) {
851 return NULL;
852 }
853
854 return date_format(format, format_len, date_obj->time, date_obj->time->is_localtime);
855 }
856
php_date(INTERNAL_FUNCTION_PARAMETERS,bool localtime)857 static void php_date(INTERNAL_FUNCTION_PARAMETERS, bool localtime)
858 {
859 zend_string *format;
860 zend_long ts;
861 bool ts_is_null = 1;
862
863 ZEND_PARSE_PARAMETERS_START(1, 2)
864 Z_PARAM_STR(format)
865 Z_PARAM_OPTIONAL
866 Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
867 ZEND_PARSE_PARAMETERS_END();
868
869 if (ts_is_null) {
870 ts = php_time();
871 }
872
873 RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
874 }
875 /* }}} */
876
php_format_date(const char * format,size_t format_len,time_t ts,bool localtime)877 PHPAPI zend_string *php_format_date(const char *format, size_t format_len, time_t ts, bool localtime) /* {{{ */
878 {
879 timelib_time *t;
880 timelib_tzinfo *tzi;
881 zend_string *string;
882
883 t = timelib_time_ctor();
884
885 if (localtime) {
886 tzi = get_timezone_info();
887 t->tz_info = tzi;
888 t->zone_type = TIMELIB_ZONETYPE_ID;
889 timelib_unixtime2local(t, ts);
890 } else {
891 tzi = NULL;
892 timelib_unixtime2gmt(t, ts);
893 }
894
895 string = date_format(format, format_len, t, localtime);
896
897 timelib_time_dtor(t);
898 return string;
899 }
900 /* }}} */
901
902 /* {{{ php_idate */
php_idate(char format,time_t ts,bool localtime)903 PHPAPI int php_idate(char format, time_t ts, bool localtime)
904 {
905 timelib_time *t;
906 timelib_tzinfo *tzi;
907 int retval = -1;
908 timelib_time_offset *offset = NULL;
909 timelib_sll isoweek, isoyear;
910
911 t = timelib_time_ctor();
912
913 if (!localtime) {
914 tzi = get_timezone_info();
915 t->tz_info = tzi;
916 t->zone_type = TIMELIB_ZONETYPE_ID;
917 timelib_unixtime2local(t, ts);
918 } else {
919 tzi = NULL;
920 timelib_unixtime2gmt(t, ts);
921 }
922
923 if (!localtime) {
924 if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
925 offset = timelib_time_offset_ctor();
926 offset->offset = (t->z + (t->dst * 3600));
927 offset->leap_secs = 0;
928 offset->is_dst = t->dst;
929 offset->abbr = timelib_strdup(t->tz_abbr);
930 } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
931 offset = timelib_time_offset_ctor();
932 offset->offset = (t->z + (t->dst * 3600));
933 offset->leap_secs = 0;
934 offset->is_dst = t->dst;
935 offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
936 snprintf(offset->abbr, 9, "GMT%c%02d%02d",
937 (offset->offset < 0) ? '-' : '+',
938 abs(offset->offset / 3600),
939 abs((offset->offset % 3600) / 60));
940 } else {
941 offset = timelib_get_time_zone_info(t->sse, t->tz_info);
942 }
943 }
944
945 timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear);
946
947 switch (format) {
948 /* day */
949 case 'd': case 'j': retval = (int) t->d; break;
950
951 case 'N': retval = (int) timelib_iso_day_of_week(t->y, t->m, t->d); break;
952 case 'w': retval = (int) timelib_day_of_week(t->y, t->m, t->d); break;
953 case 'z': retval = (int) timelib_day_of_year(t->y, t->m, t->d); break;
954
955 /* week */
956 case 'W': retval = (int) isoweek; break; /* iso weeknr */
957
958 /* month */
959 case 'm': case 'n': retval = (int) t->m; break;
960 case 't': retval = (int) timelib_days_in_month(t->y, t->m); break;
961
962 /* year */
963 case 'L': retval = (int) timelib_is_leap((int) t->y); break;
964 case 'y': retval = (int) (t->y % 100); break;
965 case 'Y': retval = (int) t->y; break;
966 case 'o': retval = (int) isoyear; break; /* iso year */
967
968 /* Swatch Beat a.k.a. Internet Time */
969 case 'B':
970 retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
971 if (retval < 0) {
972 retval += 864000;
973 }
974 /* Make sure to do this on a positive int to avoid rounding errors */
975 retval = (retval / 864) % 1000;
976 break;
977
978 /* time */
979 case 'g': case 'h': retval = (int) ((t->h % 12) ? (int) t->h % 12 : 12); break;
980 case 'H': case 'G': retval = (int) t->h; break;
981 case 'i': retval = (int) t->i; break;
982 case 's': retval = (int) t->s; break;
983
984 /* timezone */
985 case 'I': retval = (int) (!localtime ? offset->is_dst : 0); break;
986 case 'Z': retval = (int) (!localtime ? offset->offset : 0); break;
987
988 case 'U': retval = (int) t->sse; break;
989 }
990
991 if (!localtime) {
992 timelib_time_offset_dtor(offset);
993 }
994 timelib_time_dtor(t);
995
996 return retval;
997 }
998 /* }}} */
999
1000 /* {{{ Format a local date/time */
PHP_FUNCTION(date)1001 PHP_FUNCTION(date)
1002 {
1003 php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1004 }
1005 /* }}} */
1006
1007 /* {{{ Format a GMT date/time */
PHP_FUNCTION(gmdate)1008 PHP_FUNCTION(gmdate)
1009 {
1010 php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1011 }
1012 /* }}} */
1013
1014 /* {{{ Format a local time/date as integer */
PHP_FUNCTION(idate)1015 PHP_FUNCTION(idate)
1016 {
1017 zend_string *format;
1018 zend_long ts;
1019 bool ts_is_null = 1;
1020 int ret;
1021
1022 ZEND_PARSE_PARAMETERS_START(1, 2)
1023 Z_PARAM_STR(format)
1024 Z_PARAM_OPTIONAL
1025 Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
1026 ZEND_PARSE_PARAMETERS_END();
1027
1028 if (ZSTR_LEN(format) != 1) {
1029 php_error_docref(NULL, E_WARNING, "idate format is one char");
1030 RETURN_FALSE;
1031 }
1032
1033 if (ts_is_null) {
1034 ts = php_time();
1035 }
1036
1037 ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
1038 if (ret == -1) {
1039 php_error_docref(NULL, E_WARNING, "Unrecognized date format token");
1040 RETURN_FALSE;
1041 }
1042 RETURN_LONG(ret);
1043 }
1044 /* }}} */
1045
1046 /* {{{ php_date_set_tzdb - NOT THREADSAFE */
php_date_set_tzdb(timelib_tzdb * tzdb)1047 PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb)
1048 {
1049 const timelib_tzdb *builtin = timelib_builtin_db();
1050
1051 if (php_version_compare(tzdb->version, builtin->version) > 0) {
1052 php_date_global_timezone_db = tzdb;
1053 php_date_global_timezone_db_enabled = 1;
1054 }
1055 }
1056 /* }}} */
1057
1058 /* {{{ php_parse_date: Backwards compatibility function */
php_parse_date(const char * string,zend_long * now)1059 PHPAPI zend_long php_parse_date(const char *string, zend_long *now)
1060 {
1061 timelib_time *parsed_time;
1062 timelib_error_container *error = NULL;
1063 int error2;
1064 zend_long retval;
1065
1066 parsed_time = timelib_strtotime(string, strlen(string), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
1067 if (error->error_count) {
1068 timelib_time_dtor(parsed_time);
1069 timelib_error_container_dtor(error);
1070 return -1;
1071 }
1072 timelib_error_container_dtor(error);
1073 timelib_update_ts(parsed_time, NULL);
1074 retval = timelib_date_to_int(parsed_time, &error2);
1075 timelib_time_dtor(parsed_time);
1076 if (error2) {
1077 return -1;
1078 }
1079 return retval;
1080 }
1081 /* }}} */
1082
1083 /* {{{ Convert string representation of date and time to a timestamp */
PHP_FUNCTION(strtotime)1084 PHP_FUNCTION(strtotime)
1085 {
1086 zend_string *times;
1087 int parse_error, epoch_does_not_fit_in_zend_long;
1088 timelib_error_container *error;
1089 zend_long preset_ts, ts;
1090 bool preset_ts_is_null = 1;
1091 timelib_time *t, *now;
1092 timelib_tzinfo *tzi;
1093
1094 ZEND_PARSE_PARAMETERS_START(1, 2)
1095 Z_PARAM_STR(times)
1096 Z_PARAM_OPTIONAL
1097 Z_PARAM_LONG_OR_NULL(preset_ts, preset_ts_is_null)
1098 ZEND_PARSE_PARAMETERS_END();
1099
1100 /* timelib_strtotime() expects the string to not be empty */
1101 if (ZSTR_LEN(times) == 0) {
1102 RETURN_FALSE;
1103 }
1104
1105 tzi = get_timezone_info();
1106 if (!tzi) {
1107 return;
1108 }
1109
1110 now = timelib_time_ctor();
1111 now->tz_info = tzi;
1112 now->zone_type = TIMELIB_ZONETYPE_ID;
1113 timelib_unixtime2local(now,
1114 !preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) php_time());
1115
1116 t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
1117 DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
1118 parse_error = error->error_count;
1119 timelib_error_container_dtor(error);
1120 if (parse_error) {
1121 timelib_time_dtor(now);
1122 timelib_time_dtor(t);
1123 RETURN_FALSE;
1124 }
1125
1126 timelib_fill_holes(t, now, TIMELIB_NO_CLONE);
1127 timelib_update_ts(t, tzi);
1128 ts = timelib_date_to_int(t, &epoch_does_not_fit_in_zend_long);
1129
1130 timelib_time_dtor(now);
1131 timelib_time_dtor(t);
1132
1133 if (epoch_does_not_fit_in_zend_long) {
1134 php_error_docref(NULL, E_WARNING, "Epoch doesn't fit in a PHP integer");
1135 RETURN_FALSE;
1136 }
1137
1138 RETURN_LONG(ts);
1139 }
1140 /* }}} */
1141
1142 /* {{{ php_mktime - (gm)mktime helper */
php_mktime(INTERNAL_FUNCTION_PARAMETERS,bool gmt)1143 PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, bool gmt)
1144 {
1145 zend_long hou, min, sec, mon, day, yea;
1146 bool min_is_null = 1, sec_is_null = 1, mon_is_null = 1, day_is_null = 1, yea_is_null = 1;
1147 timelib_time *now;
1148 timelib_tzinfo *tzi = NULL;
1149 zend_long ts, adjust_seconds = 0;
1150 int epoch_does_not_fit_in_zend_long;
1151
1152 ZEND_PARSE_PARAMETERS_START(1, 6)
1153 Z_PARAM_LONG(hou)
1154 Z_PARAM_OPTIONAL
1155 Z_PARAM_LONG_OR_NULL(min, min_is_null)
1156 Z_PARAM_LONG_OR_NULL(sec, sec_is_null)
1157 Z_PARAM_LONG_OR_NULL(mon, mon_is_null)
1158 Z_PARAM_LONG_OR_NULL(day, day_is_null)
1159 Z_PARAM_LONG_OR_NULL(yea, yea_is_null)
1160 ZEND_PARSE_PARAMETERS_END();
1161
1162 /* Initialize structure with current time */
1163 now = timelib_time_ctor();
1164 if (gmt) {
1165 timelib_unixtime2gmt(now, (timelib_sll) php_time());
1166 } else {
1167 tzi = get_timezone_info();
1168 if (!tzi) {
1169 return;
1170 }
1171 now->tz_info = tzi;
1172 now->zone_type = TIMELIB_ZONETYPE_ID;
1173 timelib_unixtime2local(now, (timelib_sll) php_time());
1174 }
1175
1176 now->h = hou;
1177
1178 if (!min_is_null) {
1179 now->i = min;
1180 }
1181
1182 if (!sec_is_null) {
1183 now->s = sec;
1184 }
1185
1186 if (!mon_is_null) {
1187 now->m = mon;
1188 }
1189
1190 if (!day_is_null) {
1191 now->d = day;
1192 }
1193
1194 if (!yea_is_null) {
1195 if (yea >= 0 && yea < 70) {
1196 yea += 2000;
1197 } else if (yea >= 70 && yea <= 100) {
1198 yea += 1900;
1199 }
1200 now->y = yea;
1201 }
1202
1203 /* Update the timestamp */
1204 if (gmt) {
1205 timelib_update_ts(now, NULL);
1206 } else {
1207 timelib_update_ts(now, tzi);
1208 }
1209
1210 /* Clean up and return */
1211 ts = timelib_date_to_int(now, &epoch_does_not_fit_in_zend_long);
1212
1213 if (epoch_does_not_fit_in_zend_long) {
1214 timelib_time_dtor(now);
1215 php_error_docref(NULL, E_WARNING, "Epoch doesn't fit in a PHP integer");
1216 RETURN_FALSE;
1217 }
1218
1219 ts += adjust_seconds;
1220 timelib_time_dtor(now);
1221
1222 RETURN_LONG(ts);
1223 }
1224 /* }}} */
1225
1226 /* {{{ Get UNIX timestamp for a date */
PHP_FUNCTION(mktime)1227 PHP_FUNCTION(mktime)
1228 {
1229 php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1230 }
1231 /* }}} */
1232
1233 /* {{{ Get UNIX timestamp for a GMT date */
PHP_FUNCTION(gmmktime)1234 PHP_FUNCTION(gmmktime)
1235 {
1236 php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1237 }
1238 /* }}} */
1239
1240 /* {{{ Returns true(1) if it is a valid date in gregorian calendar */
PHP_FUNCTION(checkdate)1241 PHP_FUNCTION(checkdate)
1242 {
1243 zend_long m, d, y;
1244
1245 ZEND_PARSE_PARAMETERS_START(3, 3)
1246 Z_PARAM_LONG(m)
1247 Z_PARAM_LONG(d)
1248 Z_PARAM_LONG(y)
1249 ZEND_PARSE_PARAMETERS_END();
1250
1251 if (y < 1 || y > 32767 || !timelib_valid_date(y, m, d)) {
1252 RETURN_FALSE;
1253 }
1254 RETURN_TRUE; /* True : This month, day, year arguments are valid */
1255 }
1256 /* }}} */
1257
1258 /* {{{ php_strftime - (gm)strftime helper */
php_strftime(INTERNAL_FUNCTION_PARAMETERS,bool gmt)1259 PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, bool gmt)
1260 {
1261 zend_string *format;
1262 zend_long timestamp;
1263 bool timestamp_is_null = 1;
1264 struct tm ta;
1265 int max_reallocs = 5;
1266 size_t buf_len = 256, real_len;
1267 timelib_time *ts;
1268 timelib_tzinfo *tzi;
1269 timelib_time_offset *offset = NULL;
1270 zend_string *buf;
1271
1272 ZEND_PARSE_PARAMETERS_START(1, 2)
1273 Z_PARAM_STR(format)
1274 Z_PARAM_OPTIONAL
1275 Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
1276 ZEND_PARSE_PARAMETERS_END();
1277
1278 if (ZSTR_LEN(format) == 0) {
1279 RETURN_FALSE;
1280 }
1281
1282 if (timestamp_is_null) {
1283 timestamp = (zend_long) php_time();
1284 }
1285
1286 ts = timelib_time_ctor();
1287 if (gmt) {
1288 tzi = NULL;
1289 timelib_unixtime2gmt(ts, (timelib_sll) timestamp);
1290 } else {
1291 tzi = get_timezone_info();
1292 if (!tzi) {
1293 return;
1294 }
1295 ts->tz_info = tzi;
1296 ts->zone_type = TIMELIB_ZONETYPE_ID;
1297 timelib_unixtime2local(ts, (timelib_sll) timestamp);
1298 }
1299 ta.tm_sec = ts->s;
1300 ta.tm_min = ts->i;
1301 ta.tm_hour = ts->h;
1302 ta.tm_mday = ts->d;
1303 ta.tm_mon = ts->m - 1;
1304 ta.tm_year = ts->y - 1900;
1305 ta.tm_wday = timelib_day_of_week(ts->y, ts->m, ts->d);
1306 ta.tm_yday = timelib_day_of_year(ts->y, ts->m, ts->d);
1307 if (gmt) {
1308 ta.tm_isdst = 0;
1309 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
1310 ta.tm_gmtoff = 0;
1311 #endif
1312 #ifdef HAVE_STRUCT_TM_TM_ZONE
1313 ta.tm_zone = "GMT";
1314 #endif
1315 } else {
1316 offset = timelib_get_time_zone_info(timestamp, tzi);
1317
1318 ta.tm_isdst = offset->is_dst;
1319 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
1320 ta.tm_gmtoff = offset->offset;
1321 #endif
1322 #ifdef HAVE_STRUCT_TM_TM_ZONE
1323 ta.tm_zone = offset->abbr;
1324 #endif
1325 }
1326
1327 /* VS2012 crt has a bug where strftime crash with %z and %Z format when the
1328 initial buffer is too small. See
1329 http://connect.microsoft.com/VisualStudio/feedback/details/759720/vs2012-strftime-crash-with-z-formatting-code */
1330 buf = zend_string_alloc(buf_len, 0);
1331 while ((real_len = strftime(ZSTR_VAL(buf), buf_len, ZSTR_VAL(format), &ta)) == buf_len || real_len == 0) {
1332 buf_len *= 2;
1333 buf = zend_string_extend(buf, buf_len, 0);
1334 if (!--max_reallocs) {
1335 break;
1336 }
1337 }
1338 #ifdef PHP_WIN32
1339 /* VS2012 strftime() returns number of characters, not bytes.
1340 See VC++11 bug id 766205. */
1341 if (real_len > 0) {
1342 real_len = strlen(buf->val);
1343 }
1344 #endif
1345
1346 timelib_time_dtor(ts);
1347 if (!gmt) {
1348 timelib_time_offset_dtor(offset);
1349 }
1350
1351 if (real_len && real_len != buf_len) {
1352 buf = zend_string_truncate(buf, real_len, 0);
1353 RETURN_NEW_STR(buf);
1354 }
1355 zend_string_efree(buf);
1356 RETURN_FALSE;
1357 }
1358 /* }}} */
1359
1360 /* {{{ Format a local time/date according to locale settings */
PHP_FUNCTION(strftime)1361 PHP_FUNCTION(strftime)
1362 {
1363 php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1364 }
1365 /* }}} */
1366
1367 /* {{{ Format a GMT/UCT time/date according to locale settings */
PHP_FUNCTION(gmstrftime)1368 PHP_FUNCTION(gmstrftime)
1369 {
1370 php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1371 }
1372 /* }}} */
1373
1374 /* {{{ Return current UNIX timestamp */
PHP_FUNCTION(time)1375 PHP_FUNCTION(time)
1376 {
1377 ZEND_PARSE_PARAMETERS_NONE();
1378
1379 RETURN_LONG((zend_long)php_time());
1380 }
1381 /* }}} */
1382
1383 /* {{{ 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)1384 PHP_FUNCTION(localtime)
1385 {
1386 zend_long timestamp;
1387 bool timestamp_is_null = 1;
1388 bool associative = 0;
1389 timelib_tzinfo *tzi;
1390 timelib_time *ts;
1391
1392 ZEND_PARSE_PARAMETERS_START(0, 2)
1393 Z_PARAM_OPTIONAL
1394 Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
1395 Z_PARAM_BOOL(associative)
1396 ZEND_PARSE_PARAMETERS_END();
1397
1398 if (timestamp_is_null) {
1399 timestamp = (zend_long) php_time();
1400 }
1401
1402 tzi = get_timezone_info();
1403 if (!tzi) {
1404 RETURN_THROWS();
1405 }
1406 ts = timelib_time_ctor();
1407 ts->tz_info = tzi;
1408 ts->zone_type = TIMELIB_ZONETYPE_ID;
1409 timelib_unixtime2local(ts, (timelib_sll) timestamp);
1410
1411 array_init(return_value);
1412
1413 if (associative) {
1414 add_assoc_long(return_value, "tm_sec", ts->s);
1415 add_assoc_long(return_value, "tm_min", ts->i);
1416 add_assoc_long(return_value, "tm_hour", ts->h);
1417 add_assoc_long(return_value, "tm_mday", ts->d);
1418 add_assoc_long(return_value, "tm_mon", ts->m - 1);
1419 add_assoc_long(return_value, "tm_year", ts->y - 1900);
1420 add_assoc_long(return_value, "tm_wday", timelib_day_of_week(ts->y, ts->m, ts->d));
1421 add_assoc_long(return_value, "tm_yday", timelib_day_of_year(ts->y, ts->m, ts->d));
1422 add_assoc_long(return_value, "tm_isdst", ts->dst);
1423 } else {
1424 add_next_index_long(return_value, ts->s);
1425 add_next_index_long(return_value, ts->i);
1426 add_next_index_long(return_value, ts->h);
1427 add_next_index_long(return_value, ts->d);
1428 add_next_index_long(return_value, ts->m - 1);
1429 add_next_index_long(return_value, ts->y- 1900);
1430 add_next_index_long(return_value, timelib_day_of_week(ts->y, ts->m, ts->d));
1431 add_next_index_long(return_value, timelib_day_of_year(ts->y, ts->m, ts->d));
1432 add_next_index_long(return_value, ts->dst);
1433 }
1434
1435 timelib_time_dtor(ts);
1436 }
1437 /* }}} */
1438
1439 /* {{{ Get date/time information */
PHP_FUNCTION(getdate)1440 PHP_FUNCTION(getdate)
1441 {
1442 zend_long timestamp;
1443 bool timestamp_is_null = 1;
1444 timelib_tzinfo *tzi;
1445 timelib_time *ts;
1446
1447 ZEND_PARSE_PARAMETERS_START(0, 1)
1448 Z_PARAM_OPTIONAL
1449 Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
1450 ZEND_PARSE_PARAMETERS_END();
1451
1452 if (timestamp_is_null) {
1453 timestamp = (zend_long) php_time();
1454 }
1455
1456 tzi = get_timezone_info();
1457 if (!tzi) {
1458 RETURN_THROWS();
1459 }
1460 ts = timelib_time_ctor();
1461 ts->tz_info = tzi;
1462 ts->zone_type = TIMELIB_ZONETYPE_ID;
1463 timelib_unixtime2local(ts, (timelib_sll) timestamp);
1464
1465 array_init(return_value);
1466
1467 add_assoc_long(return_value, "seconds", ts->s);
1468 add_assoc_long(return_value, "minutes", ts->i);
1469 add_assoc_long(return_value, "hours", ts->h);
1470 add_assoc_long(return_value, "mday", ts->d);
1471 add_assoc_long(return_value, "wday", timelib_day_of_week(ts->y, ts->m, ts->d));
1472 add_assoc_long(return_value, "mon", ts->m);
1473 add_assoc_long(return_value, "year", ts->y);
1474 add_assoc_long(return_value, "yday", timelib_day_of_year(ts->y, ts->m, ts->d));
1475 add_assoc_string(return_value, "weekday", php_date_full_day_name(ts->y, ts->m, ts->d));
1476 add_assoc_string(return_value, "month", mon_full_names[ts->m - 1]);
1477 add_index_long(return_value, 0, timestamp);
1478
1479 timelib_time_dtor(ts);
1480 }
1481 /* }}} */
1482
create_date_period_datetime(timelib_time * datetime,zend_class_entry * ce,zval * zv)1483 static void create_date_period_datetime(timelib_time *datetime, zend_class_entry *ce, zval *zv)
1484 {
1485 if (datetime) {
1486 php_date_obj *date_obj;
1487
1488 object_init_ex(zv, ce);
1489 date_obj = Z_PHPDATE_P(zv);
1490 date_obj->time = timelib_time_clone(datetime);
1491 } else {
1492 ZVAL_NULL(zv);
1493 }
1494 }
1495
create_date_period_interval(timelib_rel_time * interval,zval * zv)1496 static void create_date_period_interval(timelib_rel_time *interval, zval *zv)
1497 {
1498 if (interval) {
1499 php_interval_obj *interval_obj;
1500
1501 object_init_ex(zv, date_ce_interval);
1502 interval_obj = Z_PHPINTERVAL_P(zv);
1503 interval_obj->diff = timelib_rel_time_clone(interval);
1504 interval_obj->initialized = 1;
1505 } else {
1506 ZVAL_NULL(zv);
1507 }
1508 }
1509
1510 /* define an overloaded iterator structure */
1511 typedef struct {
1512 zend_object_iterator intern;
1513 zval current;
1514 php_period_obj *object;
1515 int current_index;
1516 } date_period_it;
1517
1518 /* {{{ date_period_it_invalidate_current */
date_period_it_invalidate_current(zend_object_iterator * iter)1519 static void date_period_it_invalidate_current(zend_object_iterator *iter)
1520 {
1521 date_period_it *iterator = (date_period_it *)iter;
1522
1523 if (Z_TYPE(iterator->current) != IS_UNDEF) {
1524 zval_ptr_dtor(&iterator->current);
1525 ZVAL_UNDEF(&iterator->current);
1526 }
1527 }
1528 /* }}} */
1529
1530 /* {{{ date_period_it_dtor */
date_period_it_dtor(zend_object_iterator * iter)1531 static void date_period_it_dtor(zend_object_iterator *iter)
1532 {
1533 date_period_it *iterator = (date_period_it *)iter;
1534
1535 date_period_it_invalidate_current(iter);
1536
1537 zval_ptr_dtor(&iterator->intern.data);
1538 }
1539 /* }}} */
1540
1541 /* {{{ date_period_it_has_more */
date_period_it_has_more(zend_object_iterator * iter)1542 static zend_result date_period_it_has_more(zend_object_iterator *iter)
1543 {
1544 date_period_it *iterator = (date_period_it *)iter;
1545 php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
1546
1547 if (object->end) {
1548 if (object->current->sse == object->end->sse) {
1549 if (object->include_end_date) {
1550 return object->current->us <= object->end->us ? SUCCESS : FAILURE;
1551 } else {
1552 return object->current->us < object->end->us ? SUCCESS : FAILURE;
1553 }
1554 }
1555
1556 return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
1557 } else {
1558 return (iterator->current_index < object->recurrences) ? SUCCESS : FAILURE;
1559 }
1560 }
1561 /* }}} */
1562
get_base_date_class(zend_class_entry * start_ce)1563 static zend_class_entry *get_base_date_class(zend_class_entry *start_ce)
1564 {
1565 zend_class_entry *tmp = start_ce;
1566
1567 while (tmp != date_ce_date && tmp != date_ce_immutable && tmp->parent) {
1568 tmp = tmp->parent;
1569 }
1570
1571 return tmp;
1572 }
1573
1574 /* {{{ date_period_it_current_data */
date_period_it_current_data(zend_object_iterator * iter)1575 static zval *date_period_it_current_data(zend_object_iterator *iter)
1576 {
1577 date_period_it *iterator = (date_period_it *)iter;
1578 php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
1579 timelib_time *it_time = object->current;
1580 php_date_obj *newdateobj;
1581
1582 /* Create new object */
1583 php_date_instantiate(get_base_date_class(object->start_ce), &iterator->current);
1584 newdateobj = Z_PHPDATE_P(&iterator->current);
1585 newdateobj->time = timelib_time_ctor();
1586 *newdateobj->time = *it_time;
1587 if (it_time->tz_abbr) {
1588 newdateobj->time->tz_abbr = timelib_strdup(it_time->tz_abbr);
1589 }
1590 if (it_time->tz_info) {
1591 newdateobj->time->tz_info = it_time->tz_info;
1592 }
1593
1594 return &iterator->current;
1595 }
1596 /* }}} */
1597
1598 /* {{{ date_period_it_current_key */
date_period_it_current_key(zend_object_iterator * iter,zval * key)1599 static void date_period_it_current_key(zend_object_iterator *iter, zval *key)
1600 {
1601 date_period_it *iterator = (date_period_it *)iter;
1602 ZVAL_LONG(key, iterator->current_index);
1603 }
1604 /* }}} */
1605
date_period_advance(timelib_time * it_time,timelib_rel_time * interval)1606 static void date_period_advance(timelib_time *it_time, timelib_rel_time *interval)
1607 {
1608 it_time->have_relative = 1;
1609 it_time->relative = *interval;
1610 it_time->sse_uptodate = 0;
1611 timelib_update_ts(it_time, NULL);
1612 timelib_update_from_sse(it_time);
1613 }
1614
1615 /* {{{ date_period_it_move_forward */
date_period_it_move_forward(zend_object_iterator * iter)1616 static void date_period_it_move_forward(zend_object_iterator *iter)
1617 {
1618 date_period_it *iterator = (date_period_it *)iter;
1619 php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
1620 timelib_time *it_time = object->current;
1621 zval current_zv;
1622
1623 date_period_advance(it_time, object->interval);
1624
1625 /* rebuild properties */
1626 zend_std_get_properties_ex(&object->std);
1627
1628 create_date_period_datetime(object->current, object->start_ce, ¤t_zv);
1629 zval_ptr_dtor(¤t_zv);
1630
1631 iterator->current_index++;
1632 date_period_it_invalidate_current(iter);
1633 }
1634 /* }}} */
1635
1636 /* {{{ date_period_it_rewind */
date_period_it_rewind(zend_object_iterator * iter)1637 static void date_period_it_rewind(zend_object_iterator *iter)
1638 {
1639 date_period_it *iterator = (date_period_it *)iter;
1640
1641 iterator->current_index = 0;
1642 if (iterator->object->current) {
1643 timelib_time_dtor(iterator->object->current);
1644 }
1645 if (!iterator->object->start) {
1646 date_throw_uninitialized_error(date_ce_period);
1647 return;
1648 }
1649
1650 iterator->object->current = timelib_time_clone(iterator->object->start);
1651
1652 if (!iterator->object->include_start_date) {
1653 date_period_advance(iterator->object->current, iterator->object->interval);
1654 }
1655
1656 date_period_it_invalidate_current(iter);
1657 }
1658 /* }}} */
1659
1660 /* iterator handler table */
1661 static const zend_object_iterator_funcs date_period_it_funcs = {
1662 date_period_it_dtor,
1663 date_period_it_has_more,
1664 date_period_it_current_data,
1665 date_period_it_current_key,
1666 date_period_it_move_forward,
1667 date_period_it_rewind,
1668 date_period_it_invalidate_current,
1669 NULL, /* get_gc */
1670 };
1671
date_object_period_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1672 static zend_object_iterator *date_object_period_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1673 {
1674 date_period_it *iterator;
1675
1676 if (by_ref) {
1677 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
1678 return NULL;
1679 }
1680
1681 iterator = emalloc(sizeof(date_period_it));
1682
1683 zend_iterator_init((zend_object_iterator*)iterator);
1684
1685 ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
1686 iterator->intern.funcs = &date_period_it_funcs;
1687 iterator->object = Z_PHPPERIOD_P(object);
1688 ZVAL_UNDEF(&iterator->current);
1689
1690 return (zend_object_iterator*)iterator;
1691 } /* }}} */
1692
implement_date_interface_handler(zend_class_entry * interface,zend_class_entry * implementor)1693 static int implement_date_interface_handler(zend_class_entry *interface, zend_class_entry *implementor) /* {{{ */
1694 {
1695 if (implementor->type == ZEND_USER_CLASS &&
1696 !instanceof_function(implementor, date_ce_date) &&
1697 !instanceof_function(implementor, date_ce_immutable)
1698 ) {
1699 zend_error_noreturn(E_ERROR, "DateTimeInterface can't be implemented by user classes");
1700 }
1701
1702 return SUCCESS;
1703 } /* }}} */
1704
date_interval_has_property(zend_object * object,zend_string * name,int type,void ** cache_slot)1705 static int date_interval_has_property(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
1706 {
1707 php_interval_obj *obj;
1708 zval rv;
1709 zval *prop;
1710 int retval = 0;
1711
1712 obj = php_interval_obj_from_obj(object);
1713
1714 if (!obj->initialized) {
1715 retval = zend_std_has_property(object, name, type, cache_slot);
1716 return retval;
1717 }
1718
1719 prop = date_interval_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
1720
1721 if (prop != &EG(uninitialized_zval)) {
1722 if (type == 2) {
1723 retval = 1;
1724 } else if (type == 1) {
1725 retval = zend_is_true(prop);
1726 } else if (type == 0) {
1727 retval = (Z_TYPE_P(prop) != IS_NULL);
1728 }
1729 } else {
1730 retval = zend_std_has_property(object, name, type, cache_slot);
1731 }
1732
1733 return retval;
1734
1735 }
1736 /* }}} */
1737
date_register_classes(void)1738 static void date_register_classes(void) /* {{{ */
1739 {
1740 date_ce_interface = register_class_DateTimeInterface();
1741 date_ce_interface->interface_gets_implemented = implement_date_interface_handler;
1742
1743 date_ce_date = register_class_DateTime(date_ce_interface);
1744 date_ce_date->create_object = date_object_new_date;
1745 date_ce_date->default_object_handlers = &date_object_handlers_date;
1746 memcpy(&date_object_handlers_date, &std_object_handlers, sizeof(zend_object_handlers));
1747 date_object_handlers_date.offset = XtOffsetOf(php_date_obj, std);
1748 date_object_handlers_date.free_obj = date_object_free_storage_date;
1749 date_object_handlers_date.clone_obj = date_object_clone_date;
1750 date_object_handlers_date.compare = date_object_compare_date;
1751 date_object_handlers_date.get_properties_for = date_object_get_properties_for;
1752 date_object_handlers_date.get_gc = date_object_get_gc;
1753
1754 date_ce_immutable = register_class_DateTimeImmutable(date_ce_interface);
1755 date_ce_immutable->create_object = date_object_new_date;
1756 date_ce_immutable->default_object_handlers = &date_object_handlers_date;
1757 memcpy(&date_object_handlers_immutable, &std_object_handlers, sizeof(zend_object_handlers));
1758 date_object_handlers_immutable.clone_obj = date_object_clone_date;
1759 date_object_handlers_immutable.compare = date_object_compare_date;
1760 date_object_handlers_immutable.get_properties_for = date_object_get_properties_for;
1761 date_object_handlers_immutable.get_gc = date_object_get_gc;
1762
1763 date_ce_timezone = register_class_DateTimeZone();
1764 date_ce_timezone->create_object = date_object_new_timezone;
1765 date_ce_timezone->default_object_handlers = &date_object_handlers_timezone;
1766 memcpy(&date_object_handlers_timezone, &std_object_handlers, sizeof(zend_object_handlers));
1767 date_object_handlers_timezone.offset = XtOffsetOf(php_timezone_obj, std);
1768 date_object_handlers_timezone.free_obj = date_object_free_storage_timezone;
1769 date_object_handlers_timezone.clone_obj = date_object_clone_timezone;
1770 date_object_handlers_timezone.get_properties_for = date_object_get_properties_for_timezone;
1771 date_object_handlers_timezone.get_gc = date_object_get_gc_timezone;
1772 date_object_handlers_timezone.get_debug_info = date_object_get_debug_info_timezone;
1773 date_object_handlers_timezone.compare = date_object_compare_timezone;
1774
1775 date_ce_interval = register_class_DateInterval();
1776 date_ce_interval->create_object = date_object_new_interval;
1777 date_ce_interval->default_object_handlers = &date_object_handlers_interval;
1778 memcpy(&date_object_handlers_interval, &std_object_handlers, sizeof(zend_object_handlers));
1779 date_object_handlers_interval.offset = XtOffsetOf(php_interval_obj, std);
1780 date_object_handlers_interval.free_obj = date_object_free_storage_interval;
1781 date_object_handlers_interval.clone_obj = date_object_clone_interval;
1782 date_object_handlers_interval.has_property = date_interval_has_property;
1783 date_object_handlers_interval.read_property = date_interval_read_property;
1784 date_object_handlers_interval.write_property = date_interval_write_property;
1785 date_object_handlers_interval.get_properties = date_object_get_properties_interval;
1786 date_object_handlers_interval.get_property_ptr_ptr = date_interval_get_property_ptr_ptr;
1787 date_object_handlers_interval.get_gc = date_object_get_gc_interval;
1788 date_object_handlers_interval.compare = date_interval_compare_objects;
1789
1790 date_ce_period = register_class_DatePeriod(zend_ce_aggregate);
1791 date_ce_period->create_object = date_object_new_period;
1792 date_ce_period->default_object_handlers = &date_object_handlers_period;
1793 date_ce_period->get_iterator = date_object_period_get_iterator;
1794 memcpy(&date_object_handlers_period, &std_object_handlers, sizeof(zend_object_handlers));
1795 date_object_handlers_period.offset = XtOffsetOf(php_period_obj, std);
1796 date_object_handlers_period.free_obj = date_object_free_storage_period;
1797 date_object_handlers_period.clone_obj = date_object_clone_period;
1798 date_object_handlers_period.get_gc = date_object_get_gc_period;
1799 date_object_handlers_period.get_property_ptr_ptr = date_period_get_property_ptr_ptr;
1800 date_object_handlers_period.has_property = date_period_has_property;
1801 date_object_handlers_period.read_property = date_period_read_property;
1802 date_object_handlers_period.write_property = date_period_write_property;
1803 date_object_handlers_period.get_properties_for = date_period_get_properties_for;
1804 date_object_handlers_period.unset_property = date_period_unset_property;
1805
1806 date_ce_date_error = register_class_DateError(zend_ce_error);
1807 date_ce_date_object_error = register_class_DateObjectError(date_ce_date_error);
1808 date_ce_date_range_error = register_class_DateRangeError(date_ce_date_error);
1809
1810 date_ce_date_exception = register_class_DateException(zend_ce_exception);
1811 date_ce_date_invalid_timezone_exception = register_class_DateInvalidTimeZoneException(date_ce_date_exception);
1812 date_ce_date_invalid_operation_exception = register_class_DateInvalidOperationException(date_ce_date_exception);
1813 date_ce_date_malformed_string_exception = register_class_DateMalformedStringException(date_ce_date_exception);
1814 date_ce_date_malformed_interval_string_exception = register_class_DateMalformedIntervalStringException(date_ce_date_exception);
1815 date_ce_date_malformed_period_string_exception = register_class_DateMalformedPeriodStringException(date_ce_date_exception);
1816 } /* }}} */
1817
date_object_new_date(zend_class_entry * class_type)1818 static zend_object *date_object_new_date(zend_class_entry *class_type) /* {{{ */
1819 {
1820 php_date_obj *intern = zend_object_alloc(sizeof(php_date_obj), class_type);
1821
1822 zend_object_std_init(&intern->std, class_type);
1823 object_properties_init(&intern->std, class_type);
1824
1825 return &intern->std;
1826 } /* }}} */
1827
date_object_clone_date(zend_object * this_ptr)1828 static zend_object *date_object_clone_date(zend_object *this_ptr) /* {{{ */
1829 {
1830 php_date_obj *old_obj = php_date_obj_from_obj(this_ptr);
1831 php_date_obj *new_obj = php_date_obj_from_obj(date_object_new_date(old_obj->std.ce));
1832
1833 zend_objects_clone_members(&new_obj->std, &old_obj->std);
1834 if (!old_obj->time) {
1835 return &new_obj->std;
1836 }
1837
1838 /* this should probably moved to a new `timelib_time *timelime_time_clone(timelib_time *)` */
1839 new_obj->time = timelib_time_ctor();
1840 *new_obj->time = *old_obj->time;
1841 if (old_obj->time->tz_abbr) {
1842 new_obj->time->tz_abbr = timelib_strdup(old_obj->time->tz_abbr);
1843 }
1844 if (old_obj->time->tz_info) {
1845 new_obj->time->tz_info = old_obj->time->tz_info;
1846 }
1847
1848 return &new_obj->std;
1849 } /* }}} */
1850
date_clone_immutable(zval * object,zval * new_object)1851 static void date_clone_immutable(zval *object, zval *new_object) /* {{{ */
1852 {
1853 ZVAL_OBJ(new_object, date_object_clone_date(Z_OBJ_P(object)));
1854 } /* }}} */
1855
date_object_compare_date(zval * d1,zval * d2)1856 static int date_object_compare_date(zval *d1, zval *d2) /* {{{ */
1857 {
1858 php_date_obj *o1;
1859 php_date_obj *o2;
1860
1861 ZEND_COMPARE_OBJECTS_FALLBACK(d1, d2);
1862
1863 o1 = Z_PHPDATE_P(d1);
1864 o2 = Z_PHPDATE_P(d2);
1865
1866 if (!o1->time || !o2->time) {
1867 zend_throw_error(date_ce_date_object_error, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
1868 return ZEND_UNCOMPARABLE;
1869 }
1870 if (!o1->time->sse_uptodate) {
1871 timelib_update_ts(o1->time, o1->time->tz_info);
1872 }
1873 if (!o2->time->sse_uptodate) {
1874 timelib_update_ts(o2->time, o2->time->tz_info);
1875 }
1876
1877 return timelib_time_compare(o1->time, o2->time);
1878 } /* }}} */
1879
date_object_get_gc(zend_object * object,zval ** table,int * n)1880 static HashTable *date_object_get_gc(zend_object *object, zval **table, int *n) /* {{{ */
1881 {
1882 *table = NULL;
1883 *n = 0;
1884 return zend_std_get_properties(object);
1885 } /* }}} */
1886
date_object_get_gc_timezone(zend_object * object,zval ** table,int * n)1887 static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table, int *n) /* {{{ */
1888 {
1889 *table = NULL;
1890 *n = 0;
1891 return zend_std_get_properties(object);
1892 } /* }}} */
1893
date_object_to_hash(php_date_obj * dateobj,HashTable * props)1894 static void date_object_to_hash(php_date_obj *dateobj, HashTable *props)
1895 {
1896 zval zv;
1897
1898 /* first we add the date and time in ISO format */
1899 ZVAL_STR(&zv, date_format("x-m-d H:i:s.u", sizeof("x-m-d H:i:s.u")-1, dateobj->time, 1));
1900 zend_hash_str_update(props, "date", sizeof("date")-1, &zv);
1901
1902 /* then we add the timezone name (or similar) */
1903 if (dateobj->time->is_localtime) {
1904 ZVAL_LONG(&zv, dateobj->time->zone_type);
1905 zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
1906
1907 switch (dateobj->time->zone_type) {
1908 case TIMELIB_ZONETYPE_ID:
1909 ZVAL_STRING(&zv, dateobj->time->tz_info->name);
1910 break;
1911 case TIMELIB_ZONETYPE_OFFSET: {
1912 zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
1913 int utc_offset = dateobj->time->z;
1914
1915 ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
1916 utc_offset < 0 ? '-' : '+',
1917 abs(utc_offset / 3600),
1918 abs(((utc_offset % 3600) / 60)));
1919
1920 ZVAL_NEW_STR(&zv, tmpstr);
1921 }
1922 break;
1923 case TIMELIB_ZONETYPE_ABBR:
1924 ZVAL_STRING(&zv, dateobj->time->tz_abbr);
1925 break;
1926 }
1927 zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
1928 }
1929 }
1930
date_object_get_properties_for(zend_object * object,zend_prop_purpose purpose)1931 static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_purpose purpose) /* {{{ */
1932 {
1933 HashTable *props;
1934 php_date_obj *dateobj;
1935
1936 switch (purpose) {
1937 case ZEND_PROP_PURPOSE_DEBUG:
1938 case ZEND_PROP_PURPOSE_SERIALIZE:
1939 case ZEND_PROP_PURPOSE_VAR_EXPORT:
1940 case ZEND_PROP_PURPOSE_JSON:
1941 case ZEND_PROP_PURPOSE_ARRAY_CAST:
1942 break;
1943 default:
1944 return zend_std_get_properties_for(object, purpose);
1945 }
1946
1947 dateobj = php_date_obj_from_obj(object);
1948 props = zend_array_dup(zend_std_get_properties(object));
1949 if (!dateobj->time) {
1950 return props;
1951 }
1952
1953 date_object_to_hash(dateobj, props);
1954
1955 return props;
1956 } /* }}} */
1957
date_object_new_timezone(zend_class_entry * class_type)1958 static zend_object *date_object_new_timezone(zend_class_entry *class_type) /* {{{ */
1959 {
1960 php_timezone_obj *intern = zend_object_alloc(sizeof(php_timezone_obj), class_type);
1961
1962 zend_object_std_init(&intern->std, class_type);
1963 object_properties_init(&intern->std, class_type);
1964
1965 return &intern->std;
1966 } /* }}} */
1967
date_object_clone_timezone(zend_object * this_ptr)1968 static zend_object *date_object_clone_timezone(zend_object *this_ptr) /* {{{ */
1969 {
1970 php_timezone_obj *old_obj = php_timezone_obj_from_obj(this_ptr);
1971 php_timezone_obj *new_obj = php_timezone_obj_from_obj(date_object_new_timezone(old_obj->std.ce));
1972
1973 zend_objects_clone_members(&new_obj->std, &old_obj->std);
1974 if (!old_obj->initialized) {
1975 return &new_obj->std;
1976 }
1977
1978 new_obj->type = old_obj->type;
1979 new_obj->initialized = 1;
1980 switch (new_obj->type) {
1981 case TIMELIB_ZONETYPE_ID:
1982 new_obj->tzi.tz = old_obj->tzi.tz;
1983 break;
1984 case TIMELIB_ZONETYPE_OFFSET:
1985 new_obj->tzi.utc_offset = old_obj->tzi.utc_offset;
1986 break;
1987 case TIMELIB_ZONETYPE_ABBR:
1988 new_obj->tzi.z.utc_offset = old_obj->tzi.z.utc_offset;
1989 new_obj->tzi.z.dst = old_obj->tzi.z.dst;
1990 new_obj->tzi.z.abbr = timelib_strdup(old_obj->tzi.z.abbr);
1991 break;
1992 }
1993
1994 return &new_obj->std;
1995 } /* }}} */
1996
date_object_compare_timezone(zval * tz1,zval * tz2)1997 static int date_object_compare_timezone(zval *tz1, zval *tz2) /* {{{ */
1998 {
1999 php_timezone_obj *o1, *o2;
2000
2001 ZEND_COMPARE_OBJECTS_FALLBACK(tz1, tz2);
2002
2003 o1 = Z_PHPTIMEZONE_P(tz1);
2004 o2 = Z_PHPTIMEZONE_P(tz2);
2005
2006 if (!o1->initialized || !o2->initialized) {
2007 zend_throw_error(date_ce_date_object_error, "Trying to compare uninitialized DateTimeZone objects");
2008 return ZEND_UNCOMPARABLE;
2009 }
2010
2011 if (o1->type != o2->type) {
2012 zend_throw_error(date_ce_date_exception, "Cannot compare two different kinds of DateTimeZone objects");
2013 return ZEND_UNCOMPARABLE;
2014 }
2015
2016 switch (o1->type) {
2017 case TIMELIB_ZONETYPE_OFFSET:
2018 return o1->tzi.utc_offset == o2->tzi.utc_offset ? 0 : 1;
2019 case TIMELIB_ZONETYPE_ABBR:
2020 return strcmp(o1->tzi.z.abbr, o2->tzi.z.abbr) ? 1 : 0;
2021 case TIMELIB_ZONETYPE_ID:
2022 return strcmp(o1->tzi.tz->name, o2->tzi.tz->name) ? 1 : 0;
2023 EMPTY_SWITCH_DEFAULT_CASE();
2024 }
2025 } /* }}} */
2026
php_timezone_to_string(php_timezone_obj * tzobj,zval * zv)2027 static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
2028 {
2029 switch (tzobj->type) {
2030 case TIMELIB_ZONETYPE_ID:
2031 ZVAL_STRING(zv, tzobj->tzi.tz->name);
2032 break;
2033 case TIMELIB_ZONETYPE_OFFSET: {
2034 timelib_sll utc_offset = tzobj->tzi.utc_offset;
2035 int seconds = utc_offset % 60;
2036 size_t size;
2037 const char *format;
2038 if (seconds == 0) {
2039 size = sizeof("+05:00");
2040 format = "%c%02d:%02d";
2041 } else {
2042 size = sizeof("+05:00:01");
2043 format = "%c%02d:%02d:%02d";
2044 }
2045 zend_string *tmpstr = zend_string_alloc(size - 1, 0);
2046
2047 /* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */
2048 ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
2049 utc_offset < 0 ? '-' : '+',
2050 abs((int)(utc_offset / 3600)),
2051 abs((int)(utc_offset % 3600) / 60),
2052 abs(seconds));
2053
2054 ZVAL_NEW_STR(zv, tmpstr);
2055 }
2056 break;
2057 case TIMELIB_ZONETYPE_ABBR:
2058 ZVAL_STRING(zv, tzobj->tzi.z.abbr);
2059 break;
2060 }
2061 }
2062
date_timezone_object_to_hash(php_timezone_obj * tzobj,HashTable * props)2063 static void date_timezone_object_to_hash(php_timezone_obj *tzobj, HashTable *props)
2064 {
2065 zval zv;
2066
2067 ZVAL_LONG(&zv, tzobj->type);
2068 zend_hash_str_update(props, "timezone_type", strlen("timezone_type"), &zv);
2069
2070 php_timezone_to_string(tzobj, &zv);
2071 zend_hash_str_update(props, "timezone", strlen("timezone"), &zv);
2072 }
2073
date_object_get_properties_for_timezone(zend_object * object,zend_prop_purpose purpose)2074 static HashTable *date_object_get_properties_for_timezone(zend_object *object, zend_prop_purpose purpose) /* {{{ */
2075 {
2076 HashTable *props;
2077 php_timezone_obj *tzobj;
2078
2079 switch (purpose) {
2080 case ZEND_PROP_PURPOSE_DEBUG:
2081 case ZEND_PROP_PURPOSE_SERIALIZE:
2082 case ZEND_PROP_PURPOSE_VAR_EXPORT:
2083 case ZEND_PROP_PURPOSE_JSON:
2084 case ZEND_PROP_PURPOSE_ARRAY_CAST:
2085 break;
2086 default:
2087 return zend_std_get_properties_for(object, purpose);
2088 }
2089
2090 tzobj = php_timezone_obj_from_obj(object);
2091 props = zend_array_dup(zend_std_get_properties(object));
2092 if (!tzobj->initialized) {
2093 return props;
2094 }
2095
2096 date_timezone_object_to_hash(tzobj, props);
2097
2098 return props;
2099 } /* }}} */
2100
date_object_get_debug_info_timezone(zend_object * object,int * is_temp)2101 static HashTable *date_object_get_debug_info_timezone(zend_object *object, int *is_temp) /* {{{ */
2102 {
2103 HashTable *ht, *props;
2104 zval zv;
2105 php_timezone_obj *tzobj;
2106
2107 tzobj = php_timezone_obj_from_obj(object);
2108 props = zend_std_get_properties(object);
2109
2110 *is_temp = 1;
2111 ht = zend_array_dup(props);
2112
2113 ZVAL_LONG(&zv, tzobj->type);
2114 zend_hash_str_update(ht, "timezone_type", sizeof("timezone_type")-1, &zv);
2115
2116 php_timezone_to_string(tzobj, &zv);
2117 zend_hash_str_update(ht, "timezone", sizeof("timezone")-1, &zv);
2118
2119 return ht;
2120 } /* }}} */
2121
date_object_new_interval(zend_class_entry * class_type)2122 static zend_object *date_object_new_interval(zend_class_entry *class_type) /* {{{ */
2123 {
2124 php_interval_obj *intern = zend_object_alloc(sizeof(php_interval_obj), class_type);
2125
2126 zend_object_std_init(&intern->std, class_type);
2127 object_properties_init(&intern->std, class_type);
2128
2129 return &intern->std;
2130 } /* }}} */
2131
date_object_clone_interval(zend_object * this_ptr)2132 static zend_object *date_object_clone_interval(zend_object *this_ptr) /* {{{ */
2133 {
2134 php_interval_obj *old_obj = php_interval_obj_from_obj(this_ptr);
2135 php_interval_obj *new_obj = php_interval_obj_from_obj(date_object_new_interval(old_obj->std.ce));
2136
2137 zend_objects_clone_members(&new_obj->std, &old_obj->std);
2138 new_obj->civil_or_wall = old_obj->civil_or_wall;
2139 new_obj->from_string = old_obj->from_string;
2140 if (old_obj->date_string) {
2141 new_obj->date_string = zend_string_copy(old_obj->date_string);
2142 }
2143 new_obj->initialized = old_obj->initialized;
2144 if (old_obj->diff) {
2145 new_obj->diff = timelib_rel_time_clone(old_obj->diff);
2146 }
2147
2148 return &new_obj->std;
2149 } /* }}} */
2150
date_object_get_gc_interval(zend_object * object,zval ** table,int * n)2151 static HashTable *date_object_get_gc_interval(zend_object *object, zval **table, int *n) /* {{{ */
2152 {
2153
2154 *table = NULL;
2155 *n = 0;
2156 return zend_std_get_properties(object);
2157 } /* }}} */
2158
date_interval_object_to_hash(php_interval_obj * intervalobj,HashTable * props)2159 static void date_interval_object_to_hash(php_interval_obj *intervalobj, HashTable *props)
2160 {
2161 zval zv;
2162
2163 /* Records whether this is a special relative interval that needs to be recreated from a string */
2164 if (intervalobj->from_string) {
2165 ZVAL_BOOL(&zv, (bool)intervalobj->from_string);
2166 zend_hash_str_update(props, "from_string", strlen("from_string"), &zv);
2167 ZVAL_STR_COPY(&zv, intervalobj->date_string);
2168 zend_hash_str_update(props, "date_string", strlen("date_string"), &zv);
2169 return;
2170 }
2171
2172 #define PHP_DATE_INTERVAL_ADD_PROPERTY(n,f) \
2173 ZVAL_LONG(&zv, (zend_long)intervalobj->diff->f); \
2174 zend_hash_str_update(props, n, sizeof(n)-1, &zv);
2175
2176 PHP_DATE_INTERVAL_ADD_PROPERTY("y", y);
2177 PHP_DATE_INTERVAL_ADD_PROPERTY("m", m);
2178 PHP_DATE_INTERVAL_ADD_PROPERTY("d", d);
2179 PHP_DATE_INTERVAL_ADD_PROPERTY("h", h);
2180 PHP_DATE_INTERVAL_ADD_PROPERTY("i", i);
2181 PHP_DATE_INTERVAL_ADD_PROPERTY("s", s);
2182 ZVAL_DOUBLE(&zv, (double)intervalobj->diff->us / 1000000.0);
2183 zend_hash_str_update(props, "f", sizeof("f") - 1, &zv);
2184 PHP_DATE_INTERVAL_ADD_PROPERTY("invert", invert);
2185 if (intervalobj->diff->days != TIMELIB_UNSET) {
2186 PHP_DATE_INTERVAL_ADD_PROPERTY("days", days);
2187 } else {
2188 ZVAL_FALSE(&zv);
2189 zend_hash_str_update(props, "days", sizeof("days")-1, &zv);
2190 }
2191 ZVAL_BOOL(&zv, (bool)intervalobj->from_string);
2192 zend_hash_str_update(props, "from_string", strlen("from_string"), &zv);
2193
2194 #undef PHP_DATE_INTERVAL_ADD_PROPERTY
2195 }
2196
date_object_get_properties_interval(zend_object * object)2197 static HashTable *date_object_get_properties_interval(zend_object *object) /* {{{ */
2198 {
2199 HashTable *props;
2200 php_interval_obj *intervalobj;
2201
2202 intervalobj = php_interval_obj_from_obj(object);
2203 props = zend_std_get_properties(object);
2204 if (!intervalobj->initialized) {
2205 return props;
2206 }
2207
2208 date_interval_object_to_hash(intervalobj, props);
2209
2210 return props;
2211 } /* }}} */
2212
date_object_new_period(zend_class_entry * class_type)2213 static zend_object *date_object_new_period(zend_class_entry *class_type) /* {{{ */
2214 {
2215 php_period_obj *intern = zend_object_alloc(sizeof(php_period_obj), class_type);
2216
2217 zend_object_std_init(&intern->std, class_type);
2218 object_properties_init(&intern->std, class_type);
2219
2220 return &intern->std;
2221 } /* }}} */
2222
date_object_clone_period(zend_object * this_ptr)2223 static zend_object *date_object_clone_period(zend_object *this_ptr) /* {{{ */
2224 {
2225 php_period_obj *old_obj = php_period_obj_from_obj(this_ptr);
2226 php_period_obj *new_obj = php_period_obj_from_obj(date_object_new_period(old_obj->std.ce));
2227
2228 zend_objects_clone_members(&new_obj->std, &old_obj->std);
2229 new_obj->initialized = old_obj->initialized;
2230 new_obj->recurrences = old_obj->recurrences;
2231 new_obj->include_start_date = old_obj->include_start_date;
2232 new_obj->include_end_date = old_obj->include_end_date;
2233 new_obj->start_ce = old_obj->start_ce;
2234
2235 if (old_obj->start) {
2236 new_obj->start = timelib_time_clone(old_obj->start);
2237 }
2238 if (old_obj->current) {
2239 new_obj->current = timelib_time_clone(old_obj->current);
2240 }
2241 if (old_obj->end) {
2242 new_obj->end = timelib_time_clone(old_obj->end);
2243 }
2244 if (old_obj->interval) {
2245 new_obj->interval = timelib_rel_time_clone(old_obj->interval);
2246 }
2247 return &new_obj->std;
2248 } /* }}} */
2249
date_object_free_storage_date(zend_object * object)2250 static void date_object_free_storage_date(zend_object *object) /* {{{ */
2251 {
2252 php_date_obj *intern = php_date_obj_from_obj(object);
2253
2254 if (intern->time) {
2255 timelib_time_dtor(intern->time);
2256 }
2257
2258 zend_object_std_dtor(&intern->std);
2259 } /* }}} */
2260
date_object_free_storage_timezone(zend_object * object)2261 static void date_object_free_storage_timezone(zend_object *object) /* {{{ */
2262 {
2263 php_timezone_obj *intern = php_timezone_obj_from_obj(object);
2264
2265 if (intern->type == TIMELIB_ZONETYPE_ABBR) {
2266 timelib_free(intern->tzi.z.abbr);
2267 }
2268 zend_object_std_dtor(&intern->std);
2269 } /* }}} */
2270
date_object_free_storage_interval(zend_object * object)2271 static void date_object_free_storage_interval(zend_object *object) /* {{{ */
2272 {
2273 php_interval_obj *intern = php_interval_obj_from_obj(object);
2274
2275 if (intern->date_string) {
2276 zend_string_release(intern->date_string);
2277 intern->date_string = NULL;
2278 }
2279 timelib_rel_time_dtor(intern->diff);
2280 zend_object_std_dtor(&intern->std);
2281 } /* }}} */
2282
date_object_free_storage_period(zend_object * object)2283 static void date_object_free_storage_period(zend_object *object) /* {{{ */
2284 {
2285 php_period_obj *intern = php_period_obj_from_obj(object);
2286
2287 if (intern->start) {
2288 timelib_time_dtor(intern->start);
2289 }
2290
2291 if (intern->current) {
2292 timelib_time_dtor(intern->current);
2293 }
2294
2295 if (intern->end) {
2296 timelib_time_dtor(intern->end);
2297 }
2298
2299 timelib_rel_time_dtor(intern->interval);
2300 zend_object_std_dtor(&intern->std);
2301 } /* }}} */
2302
add_common_properties(HashTable * myht,zend_object * zobj)2303 static void add_common_properties(HashTable *myht, zend_object *zobj)
2304 {
2305 HashTable *common;
2306 zend_string *name;
2307 zval *prop;
2308
2309 common = zend_std_get_properties(zobj);
2310
2311 ZEND_HASH_FOREACH_STR_KEY_VAL_IND(common, name, prop) {
2312 if (zend_hash_add(myht, name, prop) != NULL) {
2313 Z_TRY_ADDREF_P(prop);
2314 }
2315 } ZEND_HASH_FOREACH_END();
2316 }
2317
2318 /* Advanced Interface */
php_date_instantiate(zend_class_entry * pce,zval * object)2319 PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object) /* {{{ */
2320 {
2321 object_init_ex(object, pce);
2322 return object;
2323 } /* }}} */
2324
2325 /* Helper function used to store the latest found warnings and errors while
2326 * parsing, from either strtotime or parse_from_format. */
update_errors_warnings(timelib_error_container ** last_errors)2327 static void update_errors_warnings(timelib_error_container **last_errors) /* {{{ */
2328 {
2329 if (DATEG(last_errors)) {
2330 timelib_error_container_dtor(DATEG(last_errors));
2331 DATEG(last_errors) = NULL;
2332 }
2333
2334 if (last_errors == NULL || (*last_errors) == NULL) {
2335 return;
2336 }
2337
2338 if ((*last_errors)->warning_count || (*last_errors)->error_count) {
2339 DATEG(last_errors) = *last_errors;
2340 return;
2341 }
2342
2343 timelib_error_container_dtor(*last_errors);
2344 *last_errors = NULL;
2345 } /* }}} */
2346
php_date_set_time_fraction(timelib_time * time,int microsecond)2347 static void php_date_set_time_fraction(timelib_time *time, int microsecond)
2348 {
2349 time->us = microsecond;
2350 }
2351
php_date_get_current_time_with_fraction(time_t * sec,suseconds_t * usec)2352 static void php_date_get_current_time_with_fraction(time_t *sec, suseconds_t *usec)
2353 {
2354 #ifdef HAVE_GETTIMEOFDAY
2355 struct timeval tp = {0}; /* For setting microsecond */
2356
2357 gettimeofday(&tp, NULL);
2358 *sec = tp.tv_sec;
2359 *usec = tp.tv_usec;
2360 #else
2361 *sec = time(NULL);
2362 *usec = 0;
2363 #endif
2364 }
2365
php_date_initialize(php_date_obj * dateobj,const char * time_str,size_t time_str_len,const char * format,zval * timezone_object,int flags)2366 PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, size_t time_str_len, const char *format, zval *timezone_object, int flags) /* {{{ */
2367 {
2368 timelib_time *now;
2369 timelib_tzinfo *tzi = NULL;
2370 timelib_error_container *err = NULL;
2371 int type = TIMELIB_ZONETYPE_ID, new_dst = 0;
2372 char *new_abbr = NULL;
2373 timelib_sll new_offset = 0;
2374 time_t sec;
2375 suseconds_t usec;
2376 int options = 0;
2377
2378 if (dateobj->time) {
2379 timelib_time_dtor(dateobj->time);
2380 }
2381 if (format) {
2382 if (time_str_len == 0) {
2383 time_str = "";
2384 }
2385 dateobj->time = timelib_parse_from_format(format, time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
2386 } else {
2387 if (time_str_len == 0) {
2388 time_str = "now";
2389 time_str_len = sizeof("now") - 1;
2390 }
2391 dateobj->time = timelib_strtotime(time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
2392 }
2393
2394 /* update last errors and warnings */
2395 update_errors_warnings(&err);
2396
2397 /* If called from a constructor throw an exception */
2398 if ((flags & PHP_DATE_INIT_CTOR) && err && err->error_count) {
2399 /* spit out the first library error message, at least */
2400 zend_throw_exception_ex(date_ce_date_malformed_string_exception, 0, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
2401 err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
2402 }
2403 if (err && err->error_count) {
2404 timelib_time_dtor(dateobj->time);
2405 dateobj->time = 0;
2406 return 0;
2407 }
2408
2409 if (timezone_object) {
2410 php_timezone_obj *tzobj;
2411
2412 tzobj = Z_PHPTIMEZONE_P(timezone_object);
2413 switch (tzobj->type) {
2414 case TIMELIB_ZONETYPE_ID:
2415 tzi = tzobj->tzi.tz;
2416 break;
2417 case TIMELIB_ZONETYPE_OFFSET:
2418 new_offset = tzobj->tzi.utc_offset;
2419 break;
2420 case TIMELIB_ZONETYPE_ABBR:
2421 new_offset = tzobj->tzi.z.utc_offset;
2422 new_dst = tzobj->tzi.z.dst;
2423 new_abbr = timelib_strdup(tzobj->tzi.z.abbr);
2424 break;
2425 default:
2426 zend_throw_error(NULL, "The DateTimeZone object has not been correctly initialized by its constructor");
2427 return 0;
2428 }
2429 type = tzobj->type;
2430 } else if (dateobj->time->tz_info) {
2431 tzi = dateobj->time->tz_info;
2432 } else {
2433 tzi = get_timezone_info();
2434 if (!tzi) {
2435 return 0;
2436 }
2437 }
2438
2439 now = timelib_time_ctor();
2440 now->zone_type = type;
2441 switch (type) {
2442 case TIMELIB_ZONETYPE_ID:
2443 now->tz_info = tzi;
2444 break;
2445 case TIMELIB_ZONETYPE_OFFSET:
2446 now->z = new_offset;
2447 break;
2448 case TIMELIB_ZONETYPE_ABBR:
2449 now->z = new_offset;
2450 now->dst = new_dst;
2451 now->tz_abbr = new_abbr;
2452 break;
2453 }
2454 php_date_get_current_time_with_fraction(&sec, &usec);
2455 timelib_unixtime2local(now, (timelib_sll) sec);
2456 php_date_set_time_fraction(now, usec);
2457
2458 if (!format
2459 && time_str_len == sizeof("now") - 1
2460 && memcmp(time_str, "now", sizeof("now") - 1) == 0) {
2461 timelib_time_dtor(dateobj->time);
2462 dateobj->time = now;
2463 return 1;
2464 }
2465
2466 options = TIMELIB_NO_CLONE;
2467 if (flags & PHP_DATE_INIT_FORMAT) {
2468 options |= TIMELIB_OVERRIDE_TIME;
2469 }
2470 timelib_fill_holes(dateobj->time, now, options);
2471
2472 timelib_update_ts(dateobj->time, tzi);
2473 timelib_update_from_sse(dateobj->time);
2474
2475 dateobj->time->have_relative = 0;
2476
2477 timelib_time_dtor(now);
2478
2479 return 1;
2480 } /* }}} */
2481
php_date_initialize_from_ts_long(php_date_obj * dateobj,zend_long sec,int usec)2482 PHPAPI void php_date_initialize_from_ts_long(php_date_obj *dateobj, zend_long sec, int usec) /* {{{ */
2483 {
2484 dateobj->time = timelib_time_ctor();
2485 dateobj->time->zone_type = TIMELIB_ZONETYPE_OFFSET;
2486
2487 timelib_unixtime2gmt(dateobj->time, (timelib_sll)sec);
2488 timelib_update_ts(dateobj->time, NULL);
2489 php_date_set_time_fraction(dateobj->time, usec);
2490 } /* }}} */
2491
php_date_initialize_from_ts_double(php_date_obj * dateobj,double ts)2492 PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts) /* {{{ */
2493 {
2494 double sec_dval = trunc(ts);
2495 zend_long sec;
2496 int usec;
2497
2498 if (UNEXPECTED(isnan(sec_dval) || !PHP_DATE_DOUBLE_FITS_LONG(sec_dval))) {
2499 zend_argument_error(
2500 date_ce_date_range_error,
2501 1,
2502 "must be a finite number between " TIMELIB_LONG_FMT " and " TIMELIB_LONG_FMT ".999999, %g given",
2503 TIMELIB_LONG_MIN,
2504 TIMELIB_LONG_MAX,
2505 ts
2506 );
2507 return false;
2508 }
2509
2510 sec = (zend_long)sec_dval;
2511 usec = (int) round(fmod(ts, 1) * 1000000);
2512
2513 if (UNEXPECTED(abs(usec) == 1000000)) {
2514 sec += usec > 0 ? 1 : -1;
2515 usec = 0;
2516 }
2517
2518 if (UNEXPECTED(usec < 0)) {
2519 if (UNEXPECTED(sec == TIMELIB_LONG_MIN)) {
2520 zend_argument_error(
2521 date_ce_date_range_error,
2522 1,
2523 "must be a finite number between " TIMELIB_LONG_FMT " and " TIMELIB_LONG_FMT ".999999, %g given",
2524 TIMELIB_LONG_MIN,
2525 TIMELIB_LONG_MAX,
2526 ts
2527 );
2528 return false;
2529 }
2530
2531 sec = sec - 1;
2532 usec = 1000000 + usec;
2533 }
2534
2535 php_date_initialize_from_ts_long(dateobj, sec, usec);
2536
2537 return true;
2538 } /* }}} */
2539
2540 /* {{{ Returns new DateTime object */
PHP_FUNCTION(date_create)2541 PHP_FUNCTION(date_create)
2542 {
2543 zval *timezone_object = NULL;
2544 char *time_str = NULL;
2545 size_t time_str_len = 0;
2546
2547 ZEND_PARSE_PARAMETERS_START(0, 2)
2548 Z_PARAM_OPTIONAL
2549 Z_PARAM_STRING(time_str, time_str_len)
2550 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
2551 ZEND_PARSE_PARAMETERS_END();
2552
2553 php_date_instantiate(date_ce_date, return_value);
2554 if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
2555 zval_ptr_dtor(return_value);
2556 RETURN_FALSE;
2557 }
2558 }
2559 /* }}} */
2560
2561 /* {{{ Returns new DateTimeImmutable object */
PHP_FUNCTION(date_create_immutable)2562 PHP_FUNCTION(date_create_immutable)
2563 {
2564 zval *timezone_object = NULL;
2565 char *time_str = NULL;
2566 size_t time_str_len = 0;
2567
2568 ZEND_PARSE_PARAMETERS_START(0, 2)
2569 Z_PARAM_OPTIONAL
2570 Z_PARAM_STRING(time_str, time_str_len)
2571 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
2572 ZEND_PARSE_PARAMETERS_END();
2573
2574 php_date_instantiate(date_ce_immutable, return_value);
2575 if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
2576 zval_ptr_dtor(return_value);
2577 RETURN_FALSE;
2578 }
2579 }
2580 /* }}} */
2581
2582 /* {{{ Returns new DateTime object formatted according to the specified format */
PHP_FUNCTION(date_create_from_format)2583 PHP_FUNCTION(date_create_from_format)
2584 {
2585 zval *timezone_object = NULL;
2586 char *time_str = NULL, *format_str = NULL;
2587 size_t time_str_len = 0, format_str_len = 0;
2588
2589 ZEND_PARSE_PARAMETERS_START(2, 3)
2590 Z_PARAM_STRING(format_str, format_str_len)
2591 Z_PARAM_PATH(time_str, time_str_len)
2592 Z_PARAM_OPTIONAL
2593 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
2594 ZEND_PARSE_PARAMETERS_END();
2595
2596 php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
2597 if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, PHP_DATE_INIT_FORMAT)) {
2598 zval_ptr_dtor(return_value);
2599 RETURN_FALSE;
2600 }
2601 }
2602 /* }}} */
2603
2604 /* {{{ Returns new DateTimeImmutable object formatted according to the specified format */
PHP_FUNCTION(date_create_immutable_from_format)2605 PHP_FUNCTION(date_create_immutable_from_format)
2606 {
2607 zval *timezone_object = NULL;
2608 char *time_str = NULL, *format_str = NULL;
2609 size_t time_str_len = 0, format_str_len = 0;
2610
2611 ZEND_PARSE_PARAMETERS_START(2, 3)
2612 Z_PARAM_STRING(format_str, format_str_len)
2613 Z_PARAM_PATH(time_str, time_str_len)
2614 Z_PARAM_OPTIONAL
2615 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
2616 ZEND_PARSE_PARAMETERS_END();
2617
2618 php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
2619 if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, PHP_DATE_INIT_FORMAT)) {
2620 zval_ptr_dtor(return_value);
2621 RETURN_FALSE;
2622 }
2623 }
2624 /* }}} */
2625
2626 /* {{{ Creates new DateTime object */
PHP_METHOD(DateTime,__construct)2627 PHP_METHOD(DateTime, __construct)
2628 {
2629 zval *timezone_object = NULL;
2630 char *time_str = NULL;
2631 size_t time_str_len = 0;
2632
2633 ZEND_PARSE_PARAMETERS_START(0, 2)
2634 Z_PARAM_OPTIONAL
2635 Z_PARAM_STRING(time_str, time_str_len)
2636 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
2637 ZEND_PARSE_PARAMETERS_END();
2638
2639 php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, PHP_DATE_INIT_CTOR);
2640 }
2641 /* }}} */
2642
2643 /* {{{ Creates new DateTimeImmutable object */
PHP_METHOD(DateTimeImmutable,__construct)2644 PHP_METHOD(DateTimeImmutable, __construct)
2645 {
2646 zval *timezone_object = NULL;
2647 char *time_str = NULL;
2648 size_t time_str_len = 0;
2649
2650 ZEND_PARSE_PARAMETERS_START(0, 2)
2651 Z_PARAM_OPTIONAL
2652 Z_PARAM_STRING(time_str, time_str_len)
2653 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
2654 ZEND_PARSE_PARAMETERS_END();
2655
2656 php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, PHP_DATE_INIT_CTOR);
2657 }
2658 /* }}} */
2659
2660 /* {{{ Creates new DateTime object from an existing immutable DateTimeImmutable object. */
PHP_METHOD(DateTime,createFromImmutable)2661 PHP_METHOD(DateTime, createFromImmutable)
2662 {
2663 zval *datetimeimmutable_object = NULL;
2664 php_date_obj *new_obj = NULL;
2665 php_date_obj *old_obj = NULL;
2666
2667 ZEND_PARSE_PARAMETERS_START(1, 1)
2668 Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable)
2669 ZEND_PARSE_PARAMETERS_END();
2670
2671 old_obj = Z_PHPDATE_P(datetimeimmutable_object);
2672 DATE_CHECK_INITIALIZED(old_obj->time, Z_OBJCE_P(datetimeimmutable_object));
2673
2674 php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
2675 new_obj = Z_PHPDATE_P(return_value);
2676
2677 new_obj->time = timelib_time_clone(old_obj->time);
2678 }
2679 /* }}} */
2680
2681 /* {{{ Creates new DateTime object from an existing DateTimeInterface object. */
PHP_METHOD(DateTime,createFromInterface)2682 PHP_METHOD(DateTime, createFromInterface)
2683 {
2684 zval *datetimeinterface_object = NULL;
2685 php_date_obj *new_obj = NULL;
2686 php_date_obj *old_obj = NULL;
2687
2688 ZEND_PARSE_PARAMETERS_START(1, 1)
2689 Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
2690 ZEND_PARSE_PARAMETERS_END();
2691
2692 old_obj = Z_PHPDATE_P(datetimeinterface_object);
2693 DATE_CHECK_INITIALIZED(old_obj->time, Z_OBJCE_P(datetimeinterface_object));
2694
2695 php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
2696 new_obj = Z_PHPDATE_P(return_value);
2697
2698 new_obj->time = timelib_time_clone(old_obj->time);
2699 }
2700 /* }}} */
2701
2702 /* {{{ Creates new DateTime object from given unix timestamp */
PHP_METHOD(DateTime,createFromTimestamp)2703 PHP_METHOD(DateTime, createFromTimestamp)
2704 {
2705 zval *value;
2706 zval new_object;
2707 php_date_obj *new_dateobj;
2708
2709 ZEND_PARSE_PARAMETERS_START(1, 1)
2710 Z_PARAM_NUMBER(value)
2711 ZEND_PARSE_PARAMETERS_END();
2712
2713 php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, &new_object);
2714 new_dateobj = Z_PHPDATE_P(&new_object);
2715
2716 switch (Z_TYPE_P(value)) {
2717 case IS_LONG:
2718 php_date_initialize_from_ts_long(new_dateobj, Z_LVAL_P(value), 0);
2719 break;
2720
2721 case IS_DOUBLE:
2722 if (!php_date_initialize_from_ts_double(new_dateobj, Z_DVAL_P(value))) {
2723 zval_ptr_dtor(&new_object);
2724 RETURN_THROWS();
2725 }
2726 break;
2727
2728 EMPTY_SWITCH_DEFAULT_CASE();
2729 }
2730
2731 RETURN_OBJ(Z_OBJ(new_object));
2732 }
2733 /* }}} */
2734
2735 /* {{{ Creates new DateTimeImmutable object from an existing mutable DateTime object. */
PHP_METHOD(DateTimeImmutable,createFromMutable)2736 PHP_METHOD(DateTimeImmutable, createFromMutable)
2737 {
2738 zval *datetime_object = NULL;
2739 php_date_obj *new_obj = NULL;
2740 php_date_obj *old_obj = NULL;
2741
2742 ZEND_PARSE_PARAMETERS_START(1, 1)
2743 Z_PARAM_OBJECT_OF_CLASS(datetime_object, date_ce_date)
2744 ZEND_PARSE_PARAMETERS_END();
2745
2746 old_obj = Z_PHPDATE_P(datetime_object);
2747 DATE_CHECK_INITIALIZED(old_obj->time, Z_OBJCE_P(datetime_object));
2748
2749 php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
2750 new_obj = Z_PHPDATE_P(return_value);
2751
2752 new_obj->time = timelib_time_clone(old_obj->time);
2753 }
2754 /* }}} */
2755
2756 /* {{{ Creates new DateTimeImmutable object from an existing DateTimeInterface object. */
PHP_METHOD(DateTimeImmutable,createFromInterface)2757 PHP_METHOD(DateTimeImmutable, createFromInterface)
2758 {
2759 zval *datetimeinterface_object = NULL;
2760 php_date_obj *new_obj = NULL;
2761 php_date_obj *old_obj = NULL;
2762
2763 ZEND_PARSE_PARAMETERS_START(1, 1)
2764 Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
2765 ZEND_PARSE_PARAMETERS_END();
2766
2767 old_obj = Z_PHPDATE_P(datetimeinterface_object);
2768 DATE_CHECK_INITIALIZED(old_obj->time, Z_OBJCE_P(datetimeinterface_object));
2769
2770 php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
2771 new_obj = Z_PHPDATE_P(return_value);
2772
2773 new_obj->time = timelib_time_clone(old_obj->time);
2774 }
2775 /* }}} */
2776
2777 /* {{{ Creates new DateTimeImmutable object from given unix timestamp */
PHP_METHOD(DateTimeImmutable,createFromTimestamp)2778 PHP_METHOD(DateTimeImmutable, createFromTimestamp)
2779 {
2780 zval *value;
2781 zval new_object;
2782 php_date_obj *new_dateobj;
2783
2784 ZEND_PARSE_PARAMETERS_START(1, 1)
2785 Z_PARAM_NUMBER(value)
2786 ZEND_PARSE_PARAMETERS_END();
2787
2788 php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, &new_object);
2789 new_dateobj = Z_PHPDATE_P(&new_object);
2790
2791 switch (Z_TYPE_P(value)) {
2792 case IS_LONG:
2793 php_date_initialize_from_ts_long(new_dateobj, Z_LVAL_P(value), 0);
2794 break;
2795
2796 case IS_DOUBLE:
2797 if (!php_date_initialize_from_ts_double(new_dateobj, Z_DVAL_P(value))) {
2798 zval_ptr_dtor(&new_object);
2799 RETURN_THROWS();
2800 }
2801 break;
2802
2803 EMPTY_SWITCH_DEFAULT_CASE();
2804 }
2805
2806 RETURN_OBJ(Z_OBJ(new_object));
2807 }
2808 /* }}} */
2809
php_date_initialize_from_hash(php_date_obj ** dateobj,HashTable * myht)2810 static bool php_date_initialize_from_hash(php_date_obj **dateobj, HashTable *myht)
2811 {
2812 zval *z_date;
2813 zval *z_timezone_type;
2814 zval *z_timezone;
2815 zval tmp_obj;
2816 timelib_tzinfo *tzi;
2817
2818 z_date = zend_hash_str_find(myht, "date", sizeof("date")-1);
2819 if (!z_date || Z_TYPE_P(z_date) != IS_STRING) {
2820 return false;
2821 }
2822
2823 z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type")-1);
2824 if (!z_timezone_type || Z_TYPE_P(z_timezone_type) != IS_LONG) {
2825 return false;
2826 }
2827
2828 z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone")-1);
2829 if (!z_timezone || Z_TYPE_P(z_timezone) != IS_STRING) {
2830 return false;
2831 }
2832
2833 switch (Z_LVAL_P(z_timezone_type)) {
2834 case TIMELIB_ZONETYPE_OFFSET:
2835 case TIMELIB_ZONETYPE_ABBR: {
2836 zend_string *tmp = zend_string_concat3(
2837 Z_STRVAL_P(z_date), Z_STRLEN_P(z_date), " ", 1,
2838 Z_STRVAL_P(z_timezone), Z_STRLEN_P(z_timezone));
2839 bool ret = php_date_initialize(*dateobj, ZSTR_VAL(tmp), ZSTR_LEN(tmp), NULL, NULL, 0);
2840 zend_string_release(tmp);
2841 return ret;
2842 }
2843
2844 case TIMELIB_ZONETYPE_ID: {
2845 bool ret;
2846 php_timezone_obj *tzobj;
2847
2848 tzi = php_date_parse_tzfile(Z_STRVAL_P(z_timezone), DATE_TIMEZONEDB);
2849
2850 if (tzi == NULL) {
2851 return false;
2852 }
2853
2854 tzobj = Z_PHPTIMEZONE_P(php_date_instantiate(date_ce_timezone, &tmp_obj));
2855 tzobj->type = TIMELIB_ZONETYPE_ID;
2856 tzobj->tzi.tz = tzi;
2857 tzobj->initialized = 1;
2858
2859 ret = php_date_initialize(*dateobj, Z_STRVAL_P(z_date), Z_STRLEN_P(z_date), NULL, &tmp_obj, 0);
2860 zval_ptr_dtor(&tmp_obj);
2861 return ret;
2862 }
2863 }
2864 return false;
2865 } /* }}} */
2866
2867 /* {{{ */
PHP_METHOD(DateTime,__set_state)2868 PHP_METHOD(DateTime, __set_state)
2869 {
2870 php_date_obj *dateobj;
2871 zval *array;
2872 HashTable *myht;
2873
2874 ZEND_PARSE_PARAMETERS_START(1, 1)
2875 Z_PARAM_ARRAY(array)
2876 ZEND_PARSE_PARAMETERS_END();
2877
2878 myht = Z_ARRVAL_P(array);
2879
2880 php_date_instantiate(date_ce_date, return_value);
2881 dateobj = Z_PHPDATE_P(return_value);
2882 if (!php_date_initialize_from_hash(&dateobj, myht)) {
2883 zend_throw_error(NULL, "Invalid serialization data for DateTime object");
2884 RETURN_THROWS();
2885 }
2886 }
2887 /* }}} */
2888
2889 /* {{{ */
PHP_METHOD(DateTimeImmutable,__set_state)2890 PHP_METHOD(DateTimeImmutable, __set_state)
2891 {
2892 php_date_obj *dateobj;
2893 zval *array;
2894 HashTable *myht;
2895
2896 ZEND_PARSE_PARAMETERS_START(1, 1)
2897 Z_PARAM_ARRAY(array)
2898 ZEND_PARSE_PARAMETERS_END();
2899
2900 myht = Z_ARRVAL_P(array);
2901
2902 php_date_instantiate(date_ce_immutable, return_value);
2903 dateobj = Z_PHPDATE_P(return_value);
2904 if (!php_date_initialize_from_hash(&dateobj, myht)) {
2905 zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
2906 RETURN_THROWS();
2907 }
2908 }
2909 /* }}} */
2910
2911 /* {{{ */
PHP_METHOD(DateTime,__serialize)2912 PHP_METHOD(DateTime, __serialize)
2913 {
2914 zval *object = ZEND_THIS;
2915 php_date_obj *dateobj;
2916 HashTable *myht;
2917
2918 ZEND_PARSE_PARAMETERS_NONE();
2919
2920 dateobj = Z_PHPDATE_P(object);
2921 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
2922
2923 array_init(return_value);
2924 myht = Z_ARRVAL_P(return_value);
2925 date_object_to_hash(dateobj, myht);
2926
2927 add_common_properties(myht, &dateobj->std);
2928 }
2929 /* }}} */
2930
2931 /* {{{ */
PHP_METHOD(DateTimeImmutable,__serialize)2932 PHP_METHOD(DateTimeImmutable, __serialize)
2933 {
2934 zval *object = ZEND_THIS;
2935 php_date_obj *dateobj;
2936 HashTable *myht;
2937
2938 ZEND_PARSE_PARAMETERS_NONE();
2939
2940 dateobj = Z_PHPDATE_P(object);
2941 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
2942
2943 array_init(return_value);
2944 myht = Z_ARRVAL_P(return_value);
2945 date_object_to_hash(dateobj, myht);
2946
2947 add_common_properties(myht, &dateobj->std);
2948 }
2949 /* }}} */
2950
date_time_is_internal_property(zend_string * name)2951 static bool date_time_is_internal_property(zend_string *name)
2952 {
2953 if (
2954 zend_string_equals_literal(name, "date") ||
2955 zend_string_equals_literal(name, "timezone_type") ||
2956 zend_string_equals_literal(name, "timezone")
2957 ) {
2958 return 1;
2959 }
2960 return 0;
2961 }
2962
restore_custom_datetime_properties(zval * object,HashTable * myht)2963 static void restore_custom_datetime_properties(zval *object, HashTable *myht)
2964 {
2965 zend_string *prop_name;
2966 zval *prop_val;
2967
2968 ZEND_HASH_FOREACH_STR_KEY_VAL(myht, prop_name, prop_val) {
2969 if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_time_is_internal_property(prop_name)) {
2970 continue;
2971 }
2972 update_property(Z_OBJ_P(object), prop_name, prop_val);
2973 } ZEND_HASH_FOREACH_END();
2974 }
2975
2976 /* {{{ */
PHP_METHOD(DateTime,__unserialize)2977 PHP_METHOD(DateTime, __unserialize)
2978 {
2979 zval *object = ZEND_THIS;
2980 php_date_obj *dateobj;
2981 zval *array;
2982 HashTable *myht;
2983
2984 ZEND_PARSE_PARAMETERS_START(1, 1)
2985 Z_PARAM_ARRAY(array)
2986 ZEND_PARSE_PARAMETERS_END();
2987
2988 dateobj = Z_PHPDATE_P(object);
2989 myht = Z_ARRVAL_P(array);
2990
2991 if (!php_date_initialize_from_hash(&dateobj, myht)) {
2992 zend_throw_error(NULL, "Invalid serialization data for DateTime object");
2993 RETURN_THROWS();
2994 }
2995
2996 restore_custom_datetime_properties(object, myht);
2997 }
2998 /* }}} */
2999
3000 /* {{{ */
PHP_METHOD(DateTimeImmutable,__unserialize)3001 PHP_METHOD(DateTimeImmutable, __unserialize)
3002 {
3003 zval *object = ZEND_THIS;
3004 php_date_obj *dateobj;
3005 zval *array;
3006 HashTable *myht;
3007
3008 ZEND_PARSE_PARAMETERS_START(1, 1)
3009 Z_PARAM_ARRAY(array)
3010 ZEND_PARSE_PARAMETERS_END();
3011
3012 dateobj = Z_PHPDATE_P(object);
3013 myht = Z_ARRVAL_P(array);
3014
3015 if (!php_date_initialize_from_hash(&dateobj, myht)) {
3016 zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
3017 RETURN_THROWS();
3018 }
3019
3020 restore_custom_datetime_properties(object, myht);
3021 }
3022 /* }}} */
3023
3024 /* {{{
3025 * Common implementation for DateTime::__wakeup() and DateTimeImmutable::__wakeup() */
php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAMETERS,const char * class_name)3026 static void php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAMETERS, const char *class_name)
3027 {
3028 zval *object = ZEND_THIS;
3029 php_date_obj *dateobj;
3030 HashTable *myht;
3031
3032 ZEND_PARSE_PARAMETERS_NONE();
3033
3034 dateobj = Z_PHPDATE_P(object);
3035
3036 myht = Z_OBJPROP_P(object);
3037
3038 if (!php_date_initialize_from_hash(&dateobj, myht)) {
3039 zend_throw_error(NULL, "Invalid serialization data for %s object", class_name);
3040 RETURN_THROWS();
3041 }
3042 }
3043 /* }}} */
3044
3045 /* {{{ */
PHP_METHOD(DateTime,__wakeup)3046 PHP_METHOD(DateTime, __wakeup)
3047 {
3048 php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTime");
3049 }
3050 /* }}} */
3051
3052 /* {{{ */
PHP_METHOD(DateTimeImmutable,__wakeup)3053 PHP_METHOD(DateTimeImmutable, __wakeup)
3054 {
3055 php_do_date_time_wakeup(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DateTimeImmutable");
3056 }
3057 /* }}} */
3058
3059 /* Helper function used to add an associative array of warnings and errors to a zval */
zval_from_error_container(zval * z,timelib_error_container * error)3060 static void zval_from_error_container(zval *z, timelib_error_container *error) /* {{{ */
3061 {
3062 int i;
3063 zval element;
3064
3065 add_assoc_long(z, "warning_count", error->warning_count);
3066 array_init(&element);
3067 for (i = 0; i < error->warning_count; i++) {
3068 add_index_string(&element, error->warning_messages[i].position, error->warning_messages[i].message);
3069 }
3070 add_assoc_zval(z, "warnings", &element);
3071
3072 add_assoc_long(z, "error_count", error->error_count);
3073 array_init(&element);
3074 for (i = 0; i < error->error_count; i++) {
3075 add_index_string(&element, error->error_messages[i].position, error->error_messages[i].message);
3076 }
3077 add_assoc_zval(z, "errors", &element);
3078 } /* }}} */
3079
3080 /* {{{ Returns the warnings and errors found while parsing a date/time string. */
PHP_FUNCTION(date_get_last_errors)3081 PHP_FUNCTION(date_get_last_errors)
3082 {
3083 ZEND_PARSE_PARAMETERS_NONE();
3084
3085 if (DATEG(last_errors)) {
3086 array_init(return_value);
3087 zval_from_error_container(return_value, DATEG(last_errors));
3088 } else {
3089 RETURN_FALSE;
3090 }
3091 }
3092 /* }}} */
3093
php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAMETERS,timelib_time * parsed_time,timelib_error_container * error)3094 static void php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAMETERS, timelib_time *parsed_time, timelib_error_container *error) /* {{{ */
3095 {
3096 zval element;
3097
3098 array_init(return_value);
3099 #define PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(name, elem) \
3100 if (parsed_time->elem == TIMELIB_UNSET) { \
3101 add_assoc_bool(return_value, #name, 0); \
3102 } else { \
3103 add_assoc_long(return_value, #name, parsed_time->elem); \
3104 }
3105 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(year, y);
3106 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(month, m);
3107 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(day, d);
3108 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(hour, h);
3109 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(minute, i);
3110 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(second, s);
3111
3112 if (parsed_time->us == TIMELIB_UNSET) {
3113 add_assoc_bool(return_value, "fraction", 0);
3114 } else {
3115 add_assoc_double(return_value, "fraction", (double)parsed_time->us / 1000000.0);
3116 }
3117
3118 zval_from_error_container(return_value, error);
3119
3120 timelib_error_container_dtor(error);
3121
3122 add_assoc_bool(return_value, "is_localtime", parsed_time->is_localtime);
3123
3124 if (parsed_time->is_localtime) {
3125 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone_type, zone_type);
3126 switch (parsed_time->zone_type) {
3127 case TIMELIB_ZONETYPE_OFFSET:
3128 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone, z);
3129 add_assoc_bool(return_value, "is_dst", parsed_time->dst);
3130 break;
3131 case TIMELIB_ZONETYPE_ID:
3132 if (parsed_time->tz_abbr) {
3133 add_assoc_string(return_value, "tz_abbr", parsed_time->tz_abbr);
3134 }
3135 if (parsed_time->tz_info) {
3136 add_assoc_string(return_value, "tz_id", parsed_time->tz_info->name);
3137 }
3138 break;
3139 case TIMELIB_ZONETYPE_ABBR:
3140 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone, z);
3141 add_assoc_bool(return_value, "is_dst", parsed_time->dst);
3142 add_assoc_string(return_value, "tz_abbr", parsed_time->tz_abbr);
3143 break;
3144 }
3145 }
3146 if (parsed_time->have_relative) {
3147 array_init(&element);
3148 add_assoc_long(&element, "year", parsed_time->relative.y);
3149 add_assoc_long(&element, "month", parsed_time->relative.m);
3150 add_assoc_long(&element, "day", parsed_time->relative.d);
3151 add_assoc_long(&element, "hour", parsed_time->relative.h);
3152 add_assoc_long(&element, "minute", parsed_time->relative.i);
3153 add_assoc_long(&element, "second", parsed_time->relative.s);
3154 if (parsed_time->relative.have_weekday_relative) {
3155 add_assoc_long(&element, "weekday", parsed_time->relative.weekday);
3156 }
3157 if (parsed_time->relative.have_special_relative && (parsed_time->relative.special.type == TIMELIB_SPECIAL_WEEKDAY)) {
3158 add_assoc_long(&element, "weekdays", parsed_time->relative.special.amount);
3159 }
3160 if (parsed_time->relative.first_last_day_of) {
3161 add_assoc_bool(&element, parsed_time->relative.first_last_day_of == TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH ? "first_day_of_month" : "last_day_of_month", 1);
3162 }
3163 add_assoc_zval(return_value, "relative", &element);
3164 }
3165 timelib_time_dtor(parsed_time);
3166 } /* }}} */
3167
3168 /* {{{ Returns associative array with detailed info about given date */
PHP_FUNCTION(date_parse)3169 PHP_FUNCTION(date_parse)
3170 {
3171 zend_string *date;
3172 timelib_error_container *error;
3173 timelib_time *parsed_time;
3174
3175 ZEND_PARSE_PARAMETERS_START(1, 1)
3176 Z_PARAM_STR(date)
3177 ZEND_PARSE_PARAMETERS_END();
3178
3179 parsed_time = timelib_strtotime(ZSTR_VAL(date), ZSTR_LEN(date), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
3180 php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAM_PASSTHRU, parsed_time, error);
3181 }
3182 /* }}} */
3183
3184 /* {{{ Returns associative array with detailed info about given date */
PHP_FUNCTION(date_parse_from_format)3185 PHP_FUNCTION(date_parse_from_format)
3186 {
3187 zend_string *date, *format;
3188 timelib_error_container *error;
3189 timelib_time *parsed_time;
3190
3191 ZEND_PARSE_PARAMETERS_START(2, 2)
3192 Z_PARAM_STR(format)
3193 Z_PARAM_PATH_STR(date)
3194 ZEND_PARSE_PARAMETERS_END();
3195
3196 parsed_time = timelib_parse_from_format(ZSTR_VAL(format), ZSTR_VAL(date), ZSTR_LEN(date), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
3197 php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAM_PASSTHRU, parsed_time, error);
3198 }
3199 /* }}} */
3200
3201 /* {{{ Returns date formatted according to given format */
PHP_FUNCTION(date_format)3202 PHP_FUNCTION(date_format)
3203 {
3204 zval *object;
3205 php_date_obj *dateobj;
3206 char *format;
3207 size_t format_len;
3208
3209 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_interface, &format, &format_len) == FAILURE) {
3210 RETURN_THROWS();
3211 }
3212 dateobj = Z_PHPDATE_P(object);
3213 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3214 RETURN_STR(date_format(format, format_len, dateobj->time, dateobj->time->is_localtime));
3215 }
3216 /* }}} */
3217
php_date_modify(zval * object,char * modify,size_t modify_len)3218 static bool php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{ */
3219 {
3220 php_date_obj *dateobj;
3221 timelib_time *tmp_time;
3222 timelib_error_container *err = NULL;
3223
3224 dateobj = Z_PHPDATE_P(object);
3225
3226 if (!(dateobj->time)) {
3227 date_throw_uninitialized_error(Z_OBJCE_P(object));
3228 return 0;
3229 }
3230
3231 tmp_time = timelib_strtotime(modify, modify_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
3232
3233 /* update last errors and warnings */
3234 update_errors_warnings(&err);
3235
3236 if (err && err->error_count) {
3237 /* spit out the first library error message, at least */
3238 php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
3239 err->error_messages[0].position,
3240 err->error_messages[0].character ? err->error_messages[0].character : ' ',
3241 err->error_messages[0].message);
3242 timelib_time_dtor(tmp_time);
3243 return 0;
3244 }
3245
3246 memcpy(&dateobj->time->relative, &tmp_time->relative, sizeof(timelib_rel_time));
3247 dateobj->time->have_relative = tmp_time->have_relative;
3248 dateobj->time->sse_uptodate = 0;
3249
3250 if (tmp_time->y != TIMELIB_UNSET) {
3251 dateobj->time->y = tmp_time->y;
3252 }
3253 if (tmp_time->m != TIMELIB_UNSET) {
3254 dateobj->time->m = tmp_time->m;
3255 }
3256 if (tmp_time->d != TIMELIB_UNSET) {
3257 dateobj->time->d = tmp_time->d;
3258 }
3259
3260 if (tmp_time->h != TIMELIB_UNSET) {
3261 dateobj->time->h = tmp_time->h;
3262 if (tmp_time->i != TIMELIB_UNSET) {
3263 dateobj->time->i = tmp_time->i;
3264 if (tmp_time->s != TIMELIB_UNSET) {
3265 dateobj->time->s = tmp_time->s;
3266 } else {
3267 dateobj->time->s = 0;
3268 }
3269 } else {
3270 dateobj->time->i = 0;
3271 dateobj->time->s = 0;
3272 }
3273 }
3274
3275 if (tmp_time->us != TIMELIB_UNSET) {
3276 dateobj->time->us = tmp_time->us;
3277 }
3278
3279 /* Reset timezone to UTC if we detect a "@<ts>" modification */
3280 if (
3281 tmp_time->y == 1970 && tmp_time->m == 1 && tmp_time->d == 1 &&
3282 tmp_time->h == 0 && tmp_time->i == 0 && tmp_time->s == 0 && tmp_time->us == 0 &&
3283 tmp_time->have_zone && tmp_time->zone_type == TIMELIB_ZONETYPE_OFFSET &&
3284 tmp_time->z == 0 && tmp_time->dst == 0
3285 ) {
3286 timelib_set_timezone_from_offset(dateobj->time, 0);
3287 }
3288
3289 timelib_time_dtor(tmp_time);
3290
3291 timelib_update_ts(dateobj->time, NULL);
3292 timelib_update_from_sse(dateobj->time);
3293 dateobj->time->have_relative = 0;
3294 memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
3295
3296 return 1;
3297 } /* }}} */
3298
3299 /* {{{ Alters the timestamp. */
PHP_FUNCTION(date_modify)3300 PHP_FUNCTION(date_modify)
3301 {
3302 zval *object;
3303 char *modify;
3304 size_t modify_len;
3305
3306 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_date, &modify, &modify_len) == FAILURE) {
3307 RETURN_THROWS();
3308 }
3309
3310 if (!php_date_modify(object, modify, modify_len)) {
3311 RETURN_FALSE;
3312 }
3313
3314 RETURN_OBJ_COPY(Z_OBJ_P(object));
3315 }
3316 /* }}} */
3317
3318 /* {{{ */
PHP_METHOD(DateTime,modify)3319 PHP_METHOD(DateTime, modify)
3320 {
3321 zval *object;
3322 char *modify;
3323 size_t modify_len;
3324 zend_error_handling zeh;
3325
3326 object = ZEND_THIS;
3327 ZEND_PARSE_PARAMETERS_START(1, 1)
3328 Z_PARAM_STRING(modify, modify_len)
3329 ZEND_PARSE_PARAMETERS_END();
3330
3331 zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
3332 if (!php_date_modify(object, modify, modify_len)) {
3333 zend_restore_error_handling(&zeh);
3334 RETURN_THROWS();
3335 }
3336
3337 zend_restore_error_handling(&zeh);
3338
3339 RETURN_OBJ_COPY(Z_OBJ_P(object));
3340 }
3341 /* }}} */
3342
3343 /* {{{ */
PHP_METHOD(DateTimeImmutable,modify)3344 PHP_METHOD(DateTimeImmutable, modify)
3345 {
3346 zval *object, new_object;
3347 char *modify;
3348 size_t modify_len;
3349 zend_error_handling zeh;
3350
3351 object = ZEND_THIS;
3352 ZEND_PARSE_PARAMETERS_START(1, 1)
3353 Z_PARAM_STRING(modify, modify_len)
3354 ZEND_PARSE_PARAMETERS_END();
3355
3356 date_clone_immutable(object, &new_object);
3357
3358 zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
3359 if (!php_date_modify(&new_object, modify, modify_len)) {
3360 zval_ptr_dtor(&new_object);
3361 zend_restore_error_handling(&zeh);
3362 RETURN_THROWS();
3363 }
3364
3365 zend_restore_error_handling(&zeh);
3366
3367 RETURN_OBJ(Z_OBJ(new_object));
3368 }
3369 /* }}} */
3370
php_date_add(zval * object,zval * interval,zval * return_value)3371 static void php_date_add(zval *object, zval *interval, zval *return_value) /* {{{ */
3372 {
3373 php_date_obj *dateobj;
3374 php_interval_obj *intobj;
3375 timelib_time *new_time;
3376
3377 dateobj = Z_PHPDATE_P(object);
3378 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3379 intobj = Z_PHPINTERVAL_P(interval);
3380 DATE_CHECK_INITIALIZED(intobj->initialized, Z_OBJCE_P(interval));
3381
3382 if (intobj->civil_or_wall == PHP_DATE_WALL) {
3383 new_time = timelib_add_wall(dateobj->time, intobj->diff);
3384 } else {
3385 new_time = timelib_add(dateobj->time, intobj->diff);
3386 }
3387 timelib_time_dtor(dateobj->time);
3388 dateobj->time = new_time;
3389 } /* }}} */
3390
3391 /* {{{ Adds an interval to the current date in object. */
PHP_FUNCTION(date_add)3392 PHP_FUNCTION(date_add)
3393 {
3394 zval *object, *interval;
3395
3396 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
3397 RETURN_THROWS();
3398 }
3399
3400 php_date_add(object, interval, return_value);
3401
3402 RETURN_OBJ_COPY(Z_OBJ_P(object));
3403 }
3404 /* }}} */
3405
3406 /* {{{ */
PHP_METHOD(DateTimeImmutable,add)3407 PHP_METHOD(DateTimeImmutable, add)
3408 {
3409 zval *object, *interval, new_object;
3410
3411 object = ZEND_THIS;
3412 ZEND_PARSE_PARAMETERS_START(1, 1)
3413 Z_PARAM_OBJECT_OF_CLASS(interval, date_ce_interval)
3414 ZEND_PARSE_PARAMETERS_END();
3415
3416 date_clone_immutable(object, &new_object);
3417 php_date_add(&new_object, interval, return_value);
3418
3419 RETURN_OBJ(Z_OBJ(new_object));
3420 }
3421 /* }}} */
3422
php_date_sub(zval * object,zval * interval,zval * return_value)3423 static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{{ */
3424 {
3425 php_date_obj *dateobj;
3426 php_interval_obj *intobj;
3427 timelib_time *new_time;
3428
3429 dateobj = Z_PHPDATE_P(object);
3430 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3431 intobj = Z_PHPINTERVAL_P(interval);
3432 DATE_CHECK_INITIALIZED(intobj->initialized, Z_OBJCE_P(interval));
3433
3434 if (intobj->diff->have_weekday_relative || intobj->diff->have_special_relative) {
3435 php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
3436 return;
3437 }
3438
3439 if (intobj->civil_or_wall == PHP_DATE_WALL) {
3440 new_time = timelib_sub_wall(dateobj->time, intobj->diff);
3441 } else {
3442 new_time = timelib_sub(dateobj->time, intobj->diff);
3443 }
3444 timelib_time_dtor(dateobj->time);
3445 dateobj->time = new_time;
3446 } /* }}} */
3447
3448 /* {{{ Subtracts an interval to the current date in object. */
PHP_FUNCTION(date_sub)3449 PHP_FUNCTION(date_sub)
3450 {
3451 zval *object, *interval;
3452
3453 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
3454 RETURN_THROWS();
3455 }
3456
3457 php_date_sub(object, interval, return_value);
3458 RETURN_OBJ_COPY(Z_OBJ_P(object));
3459 }
3460 /* }}} */
3461
3462 /* {{{ Subtracts an interval to the current date in object. */
PHP_METHOD(DateTime,sub)3463 PHP_METHOD(DateTime, sub)
3464 {
3465 zval *object, *interval;
3466 zend_error_handling zeh;
3467
3468 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
3469 RETURN_THROWS();
3470 }
3471
3472 zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
3473 php_date_sub(object, interval, return_value);
3474 zend_restore_error_handling(&zeh);
3475
3476 RETURN_OBJ_COPY(Z_OBJ_P(object));
3477 }
3478 /* }}} */
3479
3480 /* {{{ */
PHP_METHOD(DateTimeImmutable,sub)3481 PHP_METHOD(DateTimeImmutable, sub)
3482 {
3483 zval *object, *interval, new_object;
3484 zend_error_handling zeh;
3485
3486 object = ZEND_THIS;
3487 ZEND_PARSE_PARAMETERS_START(1, 1)
3488 Z_PARAM_OBJECT_OF_CLASS(interval, date_ce_interval)
3489 ZEND_PARSE_PARAMETERS_END();
3490
3491 date_clone_immutable(object, &new_object);
3492
3493 zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
3494 php_date_sub(&new_object, interval, return_value);
3495 zend_restore_error_handling(&zeh);
3496
3497 RETURN_OBJ(Z_OBJ(new_object));
3498 }
3499 /* }}} */
3500
set_timezone_from_timelib_time(php_timezone_obj * tzobj,timelib_time * t)3501 static void set_timezone_from_timelib_time(php_timezone_obj *tzobj, timelib_time *t)
3502 {
3503 /* Free abbreviation if already set */
3504 if (tzobj->initialized && tzobj->type == TIMELIB_ZONETYPE_ABBR) {
3505 timelib_free(tzobj->tzi.z.abbr);
3506 }
3507
3508 /* Set new values */
3509 tzobj->initialized = 1;
3510 tzobj->type = t->zone_type;
3511
3512 switch (t->zone_type) {
3513 case TIMELIB_ZONETYPE_ID:
3514 tzobj->tzi.tz = t->tz_info;
3515 break;
3516 case TIMELIB_ZONETYPE_OFFSET:
3517 tzobj->tzi.utc_offset = t->z;
3518 break;
3519 case TIMELIB_ZONETYPE_ABBR:
3520 tzobj->tzi.z.utc_offset = t->z;
3521 tzobj->tzi.z.dst = t->dst;
3522 tzobj->tzi.z.abbr = timelib_strdup(t->tz_abbr);
3523 break;
3524 }
3525 }
3526
3527
3528 /* {{{ Return new DateTimeZone object relative to give DateTime */
PHP_FUNCTION(date_timezone_get)3529 PHP_FUNCTION(date_timezone_get)
3530 {
3531 zval *object;
3532 php_date_obj *dateobj;
3533
3534 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
3535 RETURN_THROWS();
3536 }
3537 dateobj = Z_PHPDATE_P(object);
3538 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3539 if (dateobj->time->is_localtime) {
3540 php_timezone_obj *tzobj;
3541 php_date_instantiate(date_ce_timezone, return_value);
3542 tzobj = Z_PHPTIMEZONE_P(return_value);
3543 set_timezone_from_timelib_time(tzobj, dateobj->time);
3544 } else {
3545 RETURN_FALSE;
3546 }
3547 }
3548 /* }}} */
3549
php_date_timezone_set(zval * object,zval * timezone_object,zval * return_value)3550 static void php_date_timezone_set(zval *object, zval *timezone_object, zval *return_value) /* {{{ */
3551 {
3552 php_date_obj *dateobj;
3553 php_timezone_obj *tzobj;
3554
3555 dateobj = Z_PHPDATE_P(object);
3556 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3557 tzobj = Z_PHPTIMEZONE_P(timezone_object);
3558
3559 switch (tzobj->type) {
3560 case TIMELIB_ZONETYPE_OFFSET:
3561 timelib_set_timezone_from_offset(dateobj->time, tzobj->tzi.utc_offset);
3562 break;
3563 case TIMELIB_ZONETYPE_ABBR:
3564 timelib_set_timezone_from_abbr(dateobj->time, tzobj->tzi.z);
3565 break;
3566 case TIMELIB_ZONETYPE_ID:
3567 timelib_set_timezone(dateobj->time, tzobj->tzi.tz);
3568 break;
3569 }
3570 timelib_unixtime2local(dateobj->time, dateobj->time->sse);
3571 } /* }}} */
3572
3573 /* {{{ Sets the timezone for the DateTime object. */
PHP_FUNCTION(date_timezone_set)3574 PHP_FUNCTION(date_timezone_set)
3575 {
3576 zval *object;
3577 zval *timezone_object;
3578
3579 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &timezone_object, date_ce_timezone) == FAILURE) {
3580 RETURN_THROWS();
3581 }
3582
3583 php_date_timezone_set(object, timezone_object, return_value);
3584
3585 RETURN_OBJ_COPY(Z_OBJ_P(object));
3586 }
3587 /* }}} */
3588
3589 /* {{{ */
PHP_METHOD(DateTimeImmutable,setTimezone)3590 PHP_METHOD(DateTimeImmutable, setTimezone)
3591 {
3592 zval *object, new_object;
3593 zval *timezone_object;
3594
3595 object = ZEND_THIS;
3596 ZEND_PARSE_PARAMETERS_START(1, 1)
3597 Z_PARAM_OBJECT_OF_CLASS(timezone_object, date_ce_timezone)
3598 ZEND_PARSE_PARAMETERS_END();
3599
3600 date_clone_immutable(object, &new_object);
3601 php_date_timezone_set(&new_object, timezone_object, return_value);
3602
3603 RETURN_OBJ(Z_OBJ(new_object));
3604 }
3605 /* }}} */
3606
3607 /* {{{ Returns the DST offset. */
PHP_FUNCTION(date_offset_get)3608 PHP_FUNCTION(date_offset_get)
3609 {
3610 zval *object;
3611 php_date_obj *dateobj;
3612 timelib_time_offset *offset;
3613
3614 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
3615 RETURN_THROWS();
3616 }
3617 dateobj = Z_PHPDATE_P(object);
3618 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3619 if (dateobj->time->is_localtime) {
3620 switch (dateobj->time->zone_type) {
3621 case TIMELIB_ZONETYPE_ID:
3622 offset = timelib_get_time_zone_info(dateobj->time->sse, dateobj->time->tz_info);
3623 RETVAL_LONG(offset->offset);
3624 timelib_time_offset_dtor(offset);
3625 break;
3626 case TIMELIB_ZONETYPE_OFFSET:
3627 RETVAL_LONG(dateobj->time->z);
3628 break;
3629 case TIMELIB_ZONETYPE_ABBR:
3630 RETVAL_LONG((dateobj->time->z + (3600 * dateobj->time->dst)));
3631 break;
3632 }
3633 return;
3634 } else {
3635 RETURN_LONG(0);
3636 }
3637 }
3638 /* }}} */
3639
php_date_time_set(zval * object,zend_long h,zend_long i,zend_long s,zend_long ms,zval * return_value)3640 static void php_date_time_set(zval *object, zend_long h, zend_long i, zend_long s, zend_long ms, zval *return_value) /* {{{ */
3641 {
3642 php_date_obj *dateobj;
3643
3644 dateobj = Z_PHPDATE_P(object);
3645 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3646 dateobj->time->h = h;
3647 dateobj->time->i = i;
3648 dateobj->time->s = s;
3649 dateobj->time->us = ms;
3650 timelib_update_ts(dateobj->time, NULL);
3651 timelib_update_from_sse(dateobj->time);
3652 } /* }}} */
3653
3654 /* {{{ Sets the time. */
PHP_FUNCTION(date_time_set)3655 PHP_FUNCTION(date_time_set)
3656 {
3657 zval *object;
3658 zend_long h, i, s = 0, ms = 0;
3659
3660 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|ll", &object, date_ce_date, &h, &i, &s, &ms) == FAILURE) {
3661 RETURN_THROWS();
3662 }
3663
3664 php_date_time_set(object, h, i, s, ms, return_value);
3665
3666 RETURN_OBJ_COPY(Z_OBJ_P(object));
3667 }
3668 /* }}} */
3669
3670 /* {{{ */
PHP_METHOD(DateTimeImmutable,setTime)3671 PHP_METHOD(DateTimeImmutable, setTime)
3672 {
3673 zval *object, new_object;
3674 zend_long h, i, s = 0, ms = 0;
3675
3676 object = ZEND_THIS;
3677 ZEND_PARSE_PARAMETERS_START(2, 4)
3678 Z_PARAM_LONG(h)
3679 Z_PARAM_LONG(i)
3680 Z_PARAM_OPTIONAL
3681 Z_PARAM_LONG(s)
3682 Z_PARAM_LONG(ms)
3683 ZEND_PARSE_PARAMETERS_END();
3684
3685 date_clone_immutable(object, &new_object);
3686 php_date_time_set(&new_object, h, i, s, ms, return_value);
3687
3688 RETURN_OBJ(Z_OBJ(new_object));
3689 }
3690 /* }}} */
3691
php_date_date_set(zval * object,zend_long y,zend_long m,zend_long d,zval * return_value)3692 static void php_date_date_set(zval *object, zend_long y, zend_long m, zend_long d, zval *return_value) /* {{{ */
3693 {
3694 php_date_obj *dateobj;
3695
3696 dateobj = Z_PHPDATE_P(object);
3697 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3698 dateobj->time->y = y;
3699 dateobj->time->m = m;
3700 dateobj->time->d = d;
3701 timelib_update_ts(dateobj->time, NULL);
3702 } /* }}} */
3703
3704 /* {{{ Sets the date. */
PHP_FUNCTION(date_date_set)3705 PHP_FUNCTION(date_date_set)
3706 {
3707 zval *object;
3708 zend_long y, m, d;
3709
3710 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olll", &object, date_ce_date, &y, &m, &d) == FAILURE) {
3711 RETURN_THROWS();
3712 }
3713
3714 php_date_date_set(object, y, m, d, return_value);
3715
3716 RETURN_OBJ_COPY(Z_OBJ_P(object));
3717 }
3718 /* }}} */
3719
3720 /* {{{ */
PHP_METHOD(DateTimeImmutable,setDate)3721 PHP_METHOD(DateTimeImmutable, setDate)
3722 {
3723 zval *object, new_object;
3724 zend_long y, m, d;
3725
3726 object = ZEND_THIS;
3727 ZEND_PARSE_PARAMETERS_START(3, 3)
3728 Z_PARAM_LONG(y)
3729 Z_PARAM_LONG(m)
3730 Z_PARAM_LONG(d)
3731 ZEND_PARSE_PARAMETERS_END();
3732
3733 date_clone_immutable(object, &new_object);
3734 php_date_date_set(&new_object, y, m, d, return_value);
3735
3736 RETURN_OBJ(Z_OBJ(new_object));
3737 }
3738 /* }}} */
3739
php_date_isodate_set(zval * object,zend_long y,zend_long w,zend_long d,zval * return_value)3740 static void php_date_isodate_set(zval *object, zend_long y, zend_long w, zend_long d, zval *return_value) /* {{{ */
3741 {
3742 php_date_obj *dateobj;
3743
3744 dateobj = Z_PHPDATE_P(object);
3745 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3746 dateobj->time->y = y;
3747 dateobj->time->m = 1;
3748 dateobj->time->d = 1;
3749 memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
3750 dateobj->time->relative.d = timelib_daynr_from_weeknr(y, w, d);
3751 dateobj->time->have_relative = 1;
3752
3753 timelib_update_ts(dateobj->time, NULL);
3754 } /* }}} */
3755
3756 /* {{{ Sets the ISO date. */
PHP_FUNCTION(date_isodate_set)3757 PHP_FUNCTION(date_isodate_set)
3758 {
3759 zval *object;
3760 zend_long y, w, d = 1;
3761
3762 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|l", &object, date_ce_date, &y, &w, &d) == FAILURE) {
3763 RETURN_THROWS();
3764 }
3765
3766 php_date_isodate_set(object, y, w, d, return_value);
3767
3768 RETURN_OBJ_COPY(Z_OBJ_P(object));
3769 }
3770 /* }}} */
3771
3772 /* {{{ */
PHP_METHOD(DateTimeImmutable,setISODate)3773 PHP_METHOD(DateTimeImmutable, setISODate)
3774 {
3775 zval *object, new_object;
3776 zend_long y, w, d = 1;
3777
3778 object = ZEND_THIS;
3779 ZEND_PARSE_PARAMETERS_START(2, 3)
3780 Z_PARAM_LONG(y)
3781 Z_PARAM_LONG(w)
3782 Z_PARAM_OPTIONAL
3783 Z_PARAM_LONG(d)
3784 ZEND_PARSE_PARAMETERS_END();
3785
3786 date_clone_immutable(object, &new_object);
3787 php_date_isodate_set(&new_object, y, w, d, return_value);
3788
3789 RETURN_OBJ(Z_OBJ(new_object));
3790 }
3791 /* }}} */
3792
php_date_timestamp_set(zval * object,zend_long timestamp,zval * return_value)3793 static void php_date_timestamp_set(zval *object, zend_long timestamp, zval *return_value) /* {{{ */
3794 {
3795 php_date_obj *dateobj;
3796
3797 dateobj = Z_PHPDATE_P(object);
3798 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3799 timelib_unixtime2local(dateobj->time, (timelib_sll)timestamp);
3800 timelib_update_ts(dateobj->time, NULL);
3801 php_date_set_time_fraction(dateobj->time, 0);
3802 } /* }}} */
3803
3804 /* {{{ Sets the date and time based on an Unix timestamp. */
PHP_FUNCTION(date_timestamp_set)3805 PHP_FUNCTION(date_timestamp_set)
3806 {
3807 zval *object;
3808 zend_long timestamp;
3809
3810 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &object, date_ce_date, ×tamp) == FAILURE) {
3811 RETURN_THROWS();
3812 }
3813
3814 php_date_timestamp_set(object, timestamp, return_value);
3815
3816 RETURN_OBJ_COPY(Z_OBJ_P(object));
3817 }
3818 /* }}} */
3819
3820 /* {{{ */
PHP_METHOD(DateTimeImmutable,setTimestamp)3821 PHP_METHOD(DateTimeImmutable, setTimestamp)
3822 {
3823 zval *object, new_object;
3824 zend_long timestamp;
3825
3826 object = ZEND_THIS;
3827 ZEND_PARSE_PARAMETERS_START(1, 1)
3828 Z_PARAM_LONG(timestamp)
3829 ZEND_PARSE_PARAMETERS_END();
3830
3831 date_clone_immutable(object, &new_object);
3832 php_date_timestamp_set(&new_object, timestamp, return_value);
3833
3834 RETURN_OBJ(Z_OBJ(new_object));
3835 }
3836 /* }}} */
3837
3838 /* {{{ */
PHP_METHOD(DateTimeImmutable,setMicrosecond)3839 PHP_METHOD(DateTimeImmutable, setMicrosecond)
3840 {
3841 zval *object, new_object;
3842 php_date_obj *dateobj, *new_dateobj;
3843 zend_long us;
3844
3845 ZEND_PARSE_PARAMETERS_START(1, 1)
3846 Z_PARAM_LONG(us)
3847 ZEND_PARSE_PARAMETERS_END();
3848
3849 if (UNEXPECTED(us < 0 || us > 999999)) {
3850 zend_argument_error(
3851 date_ce_date_range_error,
3852 1,
3853 "must be between 0 and 999999, " ZEND_LONG_FMT " given",
3854 us
3855 );
3856 RETURN_THROWS();
3857 }
3858
3859 object = ZEND_THIS;
3860 dateobj = Z_PHPDATE_P(object);
3861 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3862
3863 date_clone_immutable(object, &new_object);
3864 new_dateobj = Z_PHPDATE_P(&new_object);
3865
3866 php_date_set_time_fraction(new_dateobj->time, (int)us);
3867
3868 RETURN_OBJ(Z_OBJ(new_object));
3869 }
3870 /* }}} */
3871
3872 /* {{{ */
PHP_METHOD(DateTime,setMicrosecond)3873 PHP_METHOD(DateTime, setMicrosecond)
3874 {
3875 zval *object;
3876 php_date_obj *dateobj;
3877 zend_long us;
3878
3879 ZEND_PARSE_PARAMETERS_START(1, 1)
3880 Z_PARAM_LONG(us)
3881 ZEND_PARSE_PARAMETERS_END();
3882
3883 if (UNEXPECTED(us < 0 || us > 999999)) {
3884 zend_argument_error(
3885 date_ce_date_range_error,
3886 1,
3887 "must be between 0 and 999999, " ZEND_LONG_FMT " given",
3888 us
3889 );
3890 RETURN_THROWS();
3891 }
3892
3893 object = ZEND_THIS;
3894 dateobj = Z_PHPDATE_P(object);
3895 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3896 php_date_set_time_fraction(dateobj->time, (int)us);
3897
3898 RETURN_OBJ_COPY(Z_OBJ_P(object));
3899 }
3900 /* }}} */
3901
3902 /* {{{ Gets the Unix timestamp. */
PHP_FUNCTION(date_timestamp_get)3903 PHP_FUNCTION(date_timestamp_get)
3904 {
3905 zval *object;
3906 php_date_obj *dateobj;
3907 zend_long timestamp;
3908 int epoch_does_not_fit_in_zend_long;
3909
3910 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
3911 RETURN_THROWS();
3912 }
3913 dateobj = Z_PHPDATE_P(object);
3914 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3915
3916 if (!dateobj->time->sse_uptodate) {
3917 timelib_update_ts(dateobj->time, NULL);
3918 }
3919
3920 timestamp = timelib_date_to_int(dateobj->time, &epoch_does_not_fit_in_zend_long);
3921
3922 if (epoch_does_not_fit_in_zend_long) {
3923 zend_throw_error(date_ce_date_range_error, "Epoch doesn't fit in a PHP integer");
3924 RETURN_THROWS();
3925 }
3926
3927 RETURN_LONG(timestamp);
3928 }
3929 /* }}} */
3930
PHP_METHOD(DateTime,getMicrosecond)3931 PHP_METHOD(DateTime, getMicrosecond) /* {{{ */
3932 {
3933 zval *object;
3934 php_date_obj *dateobj;
3935
3936 ZEND_PARSE_PARAMETERS_NONE();
3937
3938 object = ZEND_THIS;
3939 dateobj = Z_PHPDATE_P(object);
3940 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3941
3942 RETURN_LONG((zend_long)dateobj->time->us);
3943 }
3944 /* }}} */
3945
3946 /* {{{ Returns the difference between two DateTime objects. */
PHP_FUNCTION(date_diff)3947 PHP_FUNCTION(date_diff)
3948 {
3949 zval *object1, *object2;
3950 php_date_obj *dateobj1, *dateobj2;
3951 php_interval_obj *interval;
3952 bool absolute = 0;
3953
3954 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO|b", &object1, date_ce_interface, &object2, date_ce_interface, &absolute) == FAILURE) {
3955 RETURN_THROWS();
3956 }
3957 dateobj1 = Z_PHPDATE_P(object1);
3958 dateobj2 = Z_PHPDATE_P(object2);
3959 DATE_CHECK_INITIALIZED(dateobj1->time, Z_OBJCE_P(object1));
3960 DATE_CHECK_INITIALIZED(dateobj2->time, Z_OBJCE_P(object2));
3961
3962 php_date_instantiate(date_ce_interval, return_value);
3963 interval = Z_PHPINTERVAL_P(return_value);
3964 interval->diff = timelib_diff(dateobj1->time, dateobj2->time);
3965 if (absolute) {
3966 interval->diff->invert = 0;
3967 }
3968 interval->initialized = 1;
3969 interval->civil_or_wall = PHP_DATE_CIVIL;
3970 }
3971 /* }}} */
3972
timezone_initialize(php_timezone_obj * tzobj,const char * tz,size_t tz_len,char ** warning_message)3973 static bool timezone_initialize(php_timezone_obj *tzobj, const char *tz, size_t tz_len, char **warning_message) /* {{{ */
3974 {
3975 timelib_time *dummy_t = ecalloc(1, sizeof(timelib_time));
3976 int dst, not_found;
3977 const char *orig_tz = tz;
3978
3979 if (strlen(tz) != tz_len) {
3980 if (warning_message) {
3981 spprintf(warning_message, 0, "Timezone must not contain null bytes");
3982 }
3983 efree(dummy_t);
3984 return false;
3985 }
3986
3987 dummy_t->z = timelib_parse_zone(&tz, &dst, dummy_t, ¬_found, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
3988 if ((dummy_t->z >= (100 * 60 * 60)) || (dummy_t->z <= (-100 * 60 * 60))) {
3989 if (warning_message) {
3990 spprintf(warning_message, 0, "Timezone offset is out of range (%s)", orig_tz);
3991 }
3992 timelib_free(dummy_t->tz_abbr);
3993 efree(dummy_t);
3994 return false;
3995 }
3996 dummy_t->dst = dst;
3997 if (!not_found && (*tz != '\0')) {
3998 if (warning_message) {
3999 spprintf(warning_message, 0, "Unknown or bad timezone (%s)", orig_tz);
4000 }
4001 timelib_free(dummy_t->tz_abbr);
4002 efree(dummy_t);
4003 return false;
4004 }
4005 if (not_found) {
4006 if (warning_message) {
4007 spprintf(warning_message, 0, "Unknown or bad timezone (%s)", orig_tz);
4008 }
4009 efree(dummy_t);
4010 return false;
4011 } else {
4012 set_timezone_from_timelib_time(tzobj, dummy_t);
4013 timelib_free(dummy_t->tz_abbr);
4014 efree(dummy_t);
4015 return true;
4016 }
4017 } /* }}} */
4018
4019 /* {{{ Returns new DateTimeZone object */
PHP_FUNCTION(timezone_open)4020 PHP_FUNCTION(timezone_open)
4021 {
4022 zend_string *tz;
4023 php_timezone_obj *tzobj;
4024 char *warning_message;
4025
4026 ZEND_PARSE_PARAMETERS_START(1, 1)
4027 Z_PARAM_PATH_STR(tz) /* To prevent null bytes */
4028 ZEND_PARSE_PARAMETERS_END();
4029
4030 tzobj = Z_PHPTIMEZONE_P(php_date_instantiate(date_ce_timezone, return_value));
4031 if (!timezone_initialize(tzobj, ZSTR_VAL(tz), ZSTR_LEN(tz), &warning_message)) {
4032 php_error_docref(NULL, E_WARNING, "%s", warning_message);
4033 efree(warning_message);
4034 zval_ptr_dtor(return_value);
4035 RETURN_FALSE;
4036 }
4037 }
4038 /* }}} */
4039
4040 /* {{{ Creates new DateTimeZone object. */
PHP_METHOD(DateTimeZone,__construct)4041 PHP_METHOD(DateTimeZone, __construct)
4042 {
4043 zend_string *tz;
4044 php_timezone_obj *tzobj;
4045 char *exception_message;
4046
4047 ZEND_PARSE_PARAMETERS_START(1, 1)
4048 Z_PARAM_PATH_STR(tz) /* To prevent null bytes */
4049 ZEND_PARSE_PARAMETERS_END();
4050
4051 tzobj = Z_PHPTIMEZONE_P(ZEND_THIS);
4052 if (!timezone_initialize(tzobj, ZSTR_VAL(tz), ZSTR_LEN(tz), &exception_message)) {
4053 zend_throw_exception_ex(date_ce_date_invalid_timezone_exception, 0, "DateTimeZone::__construct(): %s", exception_message);
4054 efree(exception_message);
4055 RETURN_THROWS();
4056 }
4057 }
4058 /* }}} */
4059
php_date_timezone_initialize_from_hash(zval ** return_value,php_timezone_obj ** tzobj,HashTable * myht)4060 static bool php_date_timezone_initialize_from_hash(zval **return_value, php_timezone_obj **tzobj, HashTable *myht) /* {{{ */
4061 {
4062 zval *z_timezone_type;
4063
4064 if ((z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type") - 1)) == NULL) {
4065 return false;
4066 }
4067
4068 zval *z_timezone;
4069
4070 if ((z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone") - 1)) == NULL) {
4071 return false;
4072 }
4073
4074 if (Z_TYPE_P(z_timezone_type) != IS_LONG) {
4075 return false;
4076 }
4077 if (Z_LVAL_P(z_timezone_type) < TIMELIB_ZONETYPE_OFFSET || Z_LVAL_P(z_timezone_type) > TIMELIB_ZONETYPE_ID) {
4078 return false;
4079 }
4080 if (Z_TYPE_P(z_timezone) != IS_STRING) {
4081 return false;
4082 }
4083 return timezone_initialize(*tzobj, Z_STRVAL_P(z_timezone), Z_STRLEN_P(z_timezone), NULL);
4084 } /* }}} */
4085
4086 /* {{{ */
PHP_METHOD(DateTimeZone,__set_state)4087 PHP_METHOD(DateTimeZone, __set_state)
4088 {
4089 php_timezone_obj *tzobj;
4090 zval *array;
4091 HashTable *myht;
4092
4093 ZEND_PARSE_PARAMETERS_START(1, 1)
4094 Z_PARAM_ARRAY(array)
4095 ZEND_PARSE_PARAMETERS_END();
4096
4097 myht = Z_ARRVAL_P(array);
4098
4099 php_date_instantiate(date_ce_timezone, return_value);
4100 tzobj = Z_PHPTIMEZONE_P(return_value);
4101 if (!php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht)) {
4102 zend_throw_error(NULL, "Invalid serialization data for DateTimeZone object");
4103 RETURN_THROWS();
4104 }
4105 }
4106 /* }}} */
4107
4108 /* {{{ */
PHP_METHOD(DateTimeZone,__wakeup)4109 PHP_METHOD(DateTimeZone, __wakeup)
4110 {
4111 zval *object = ZEND_THIS;
4112 php_timezone_obj *tzobj;
4113 HashTable *myht;
4114
4115 ZEND_PARSE_PARAMETERS_NONE();
4116
4117 tzobj = Z_PHPTIMEZONE_P(object);
4118
4119 myht = Z_OBJPROP_P(object);
4120
4121 if (!php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht)) {
4122 zend_throw_error(NULL, "Invalid serialization data for DateTimeZone object");
4123 RETURN_THROWS();
4124 }
4125 }
4126 /* }}} */
4127
4128 /* {{{ */
PHP_METHOD(DateTimeZone,__serialize)4129 PHP_METHOD(DateTimeZone, __serialize)
4130 {
4131 zval *object = ZEND_THIS;
4132 php_timezone_obj *tzobj;
4133 HashTable *myht;
4134
4135 ZEND_PARSE_PARAMETERS_NONE();
4136
4137 tzobj = Z_PHPTIMEZONE_P(object);
4138 DATE_CHECK_INITIALIZED(tzobj->initialized, Z_OBJCE_P(object));
4139
4140 array_init(return_value);
4141 myht = Z_ARRVAL_P(return_value);
4142 date_timezone_object_to_hash(tzobj, myht);
4143
4144 add_common_properties(myht, &tzobj->std);
4145 }
4146 /* }}} */
4147
date_timezone_is_internal_property(zend_string * name)4148 static bool date_timezone_is_internal_property(zend_string *name)
4149 {
4150 if (
4151 zend_string_equals_literal(name, "timezone_type") ||
4152 zend_string_equals_literal(name, "timezone")
4153 ) {
4154 return 1;
4155 }
4156 return 0;
4157 }
4158
restore_custom_datetimezone_properties(zval * object,HashTable * myht)4159 static void restore_custom_datetimezone_properties(zval *object, HashTable *myht)
4160 {
4161 zend_string *prop_name;
4162 zval *prop_val;
4163
4164 ZEND_HASH_FOREACH_STR_KEY_VAL(myht, prop_name, prop_val) {
4165 if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_timezone_is_internal_property(prop_name)) {
4166 continue;
4167 }
4168 update_property(Z_OBJ_P(object), prop_name, prop_val);
4169 } ZEND_HASH_FOREACH_END();
4170 }
4171
4172 /* {{{ */
PHP_METHOD(DateTimeZone,__unserialize)4173 PHP_METHOD(DateTimeZone, __unserialize)
4174 {
4175 zval *object = ZEND_THIS;
4176 php_timezone_obj *tzobj;
4177 zval *array;
4178 HashTable *myht;
4179
4180 ZEND_PARSE_PARAMETERS_START(1, 1)
4181 Z_PARAM_ARRAY(array)
4182 ZEND_PARSE_PARAMETERS_END();
4183
4184 tzobj = Z_PHPTIMEZONE_P(object);
4185 myht = Z_ARRVAL_P(array);
4186
4187 if (!php_date_timezone_initialize_from_hash(&object, &tzobj, myht)) {
4188 zend_throw_error(NULL, "Invalid serialization data for DateTimeZone object");
4189 RETURN_THROWS();
4190 }
4191
4192 restore_custom_datetimezone_properties(object, myht);
4193 }
4194 /* }}} */
4195
4196 /* {{{ Returns the name of the timezone. */
PHP_FUNCTION(timezone_name_get)4197 PHP_FUNCTION(timezone_name_get)
4198 {
4199 zval *object;
4200 php_timezone_obj *tzobj;
4201
4202 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_timezone) == FAILURE) {
4203 RETURN_THROWS();
4204 }
4205 tzobj = Z_PHPTIMEZONE_P(object);
4206 DATE_CHECK_INITIALIZED(tzobj->initialized, Z_OBJCE_P(object));
4207 php_timezone_to_string(tzobj, return_value);
4208 }
4209 /* }}} */
4210
4211 /* {{{ Returns the timezone name from abbreviation */
PHP_FUNCTION(timezone_name_from_abbr)4212 PHP_FUNCTION(timezone_name_from_abbr)
4213 {
4214 zend_string *abbr;
4215 const char *tzid;
4216 zend_long gmtoffset = -1;
4217 zend_long isdst = -1;
4218
4219 ZEND_PARSE_PARAMETERS_START(1, 3)
4220 Z_PARAM_STR(abbr)
4221 Z_PARAM_OPTIONAL
4222 Z_PARAM_LONG(gmtoffset)
4223 Z_PARAM_LONG(isdst)
4224 ZEND_PARSE_PARAMETERS_END();
4225
4226 tzid = timelib_timezone_id_from_abbr(ZSTR_VAL(abbr), gmtoffset, isdst);
4227
4228 if (tzid) {
4229 RETURN_STRING(tzid);
4230 } else {
4231 RETURN_FALSE;
4232 }
4233 }
4234 /* }}} */
4235
4236 /* {{{ Returns the timezone offset. */
PHP_FUNCTION(timezone_offset_get)4237 PHP_FUNCTION(timezone_offset_get)
4238 {
4239 zval *object, *dateobject;
4240 php_timezone_obj *tzobj;
4241 php_date_obj *dateobj;
4242 timelib_time_offset *offset;
4243
4244 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_timezone, &dateobject, date_ce_interface) == FAILURE) {
4245 RETURN_THROWS();
4246 }
4247 tzobj = Z_PHPTIMEZONE_P(object);
4248 DATE_CHECK_INITIALIZED(tzobj->initialized, Z_OBJCE_P(object));
4249 dateobj = Z_PHPDATE_P(dateobject);
4250 DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(dateobject));
4251
4252 switch (tzobj->type) {
4253 case TIMELIB_ZONETYPE_ID:
4254 offset = timelib_get_time_zone_info(dateobj->time->sse, tzobj->tzi.tz);
4255 RETVAL_LONG(offset->offset);
4256 timelib_time_offset_dtor(offset);
4257 break;
4258 case TIMELIB_ZONETYPE_OFFSET:
4259 RETURN_LONG(tzobj->tzi.utc_offset);
4260 break;
4261 case TIMELIB_ZONETYPE_ABBR:
4262 RETURN_LONG(tzobj->tzi.z.utc_offset + (tzobj->tzi.z.dst * 3600));
4263 break;
4264 }
4265 }
4266 /* }}} */
4267
4268 /* {{{ Returns numerically indexed array containing associative array for all transitions in the specified range for the timezone. */
PHP_FUNCTION(timezone_transitions_get)4269 PHP_FUNCTION(timezone_transitions_get)
4270 {
4271 zval *object, element;
4272 php_timezone_obj *tzobj;
4273 uint64_t begin = 0;
4274 bool found;
4275 zend_long timestamp_begin = ZEND_LONG_MIN, timestamp_end = INT32_MAX;
4276
4277 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ll", &object, date_ce_timezone, ×tamp_begin, ×tamp_end) == FAILURE) {
4278 RETURN_THROWS();
4279 }
4280 tzobj = Z_PHPTIMEZONE_P(object);
4281 DATE_CHECK_INITIALIZED(tzobj->initialized, Z_OBJCE_P(object));
4282 if (tzobj->type != TIMELIB_ZONETYPE_ID) {
4283 RETURN_FALSE;
4284 }
4285
4286 #define add_nominal() \
4287 array_init(&element); \
4288 add_assoc_long(&element, "ts", timestamp_begin); \
4289 add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601_LARGE_YEAR, 13, timestamp_begin, 0)); \
4290 add_assoc_long(&element, "offset", tzobj->tzi.tz->type[0].offset); \
4291 add_assoc_bool(&element, "isdst", tzobj->tzi.tz->type[0].isdst); \
4292 add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[0].abbr_idx]); \
4293 add_next_index_zval(return_value, &element);
4294
4295 #define add(i,ts) \
4296 array_init(&element); \
4297 add_assoc_long(&element, "ts", ts); \
4298 add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601_LARGE_YEAR, 13, ts, 0)); \
4299 add_assoc_long(&element, "offset", tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].offset); \
4300 add_assoc_bool(&element, "isdst", tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].isdst); \
4301 add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].abbr_idx]); \
4302 add_next_index_zval(return_value, &element);
4303
4304 #define add_by_index(i,ts) \
4305 array_init(&element); \
4306 add_assoc_long(&element, "ts", ts); \
4307 add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601_LARGE_YEAR, 13, ts, 0)); \
4308 add_assoc_long(&element, "offset", tzobj->tzi.tz->type[i].offset); \
4309 add_assoc_bool(&element, "isdst", tzobj->tzi.tz->type[i].isdst); \
4310 add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[i].abbr_idx]); \
4311 add_next_index_zval(return_value, &element);
4312
4313 #define add_from_tto(to,ts) \
4314 array_init(&element); \
4315 add_assoc_long(&element, "ts", ts); \
4316 add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601_LARGE_YEAR, 13, ts, 0)); \
4317 add_assoc_long(&element, "offset", (to)->offset); \
4318 add_assoc_bool(&element, "isdst", (to)->is_dst); \
4319 add_assoc_string(&element, "abbr", (to)->abbr); \
4320 add_next_index_zval(return_value, &element);
4321
4322 #define add_last() add(tzobj->tzi.tz->bit64.timecnt - 1, timestamp_begin)
4323
4324 array_init(return_value);
4325
4326 if (timestamp_begin == ZEND_LONG_MIN) {
4327 add_nominal();
4328 begin = 0;
4329 found = 1;
4330 } else {
4331 begin = 0;
4332 found = 0;
4333 if (tzobj->tzi.tz->bit64.timecnt > 0) {
4334 do {
4335 if (tzobj->tzi.tz->trans[begin] > timestamp_begin) {
4336 if (begin > 0) {
4337 add(begin - 1, timestamp_begin);
4338 } else {
4339 add_nominal();
4340 }
4341 found = 1;
4342 break;
4343 }
4344 begin++;
4345 } while (begin < tzobj->tzi.tz->bit64.timecnt);
4346 }
4347 }
4348
4349 if (!found) {
4350 if (tzobj->tzi.tz->bit64.timecnt > 0) {
4351 if (tzobj->tzi.tz->posix_info && tzobj->tzi.tz->posix_info->dst_end) {
4352 timelib_time_offset *tto = timelib_get_time_zone_info(timestamp_begin, tzobj->tzi.tz);
4353 add_from_tto(tto, timestamp_begin);
4354 timelib_time_offset_dtor(tto);
4355 } else {
4356 add_last();
4357 }
4358 } else {
4359 add_nominal();
4360 }
4361 } else {
4362 for (uint64_t i = begin; i < tzobj->tzi.tz->bit64.timecnt; ++i) {
4363 if (tzobj->tzi.tz->trans[i] < timestamp_end) {
4364 add(i, tzobj->tzi.tz->trans[i]);
4365 } else {
4366 return;
4367 }
4368 }
4369 }
4370 if (tzobj->tzi.tz->posix_info && tzobj->tzi.tz->posix_info->dst_end) {
4371 timelib_sll start_y, end_y, dummy_m, dummy_d;
4372 timelib_sll last_transition_ts = tzobj->tzi.tz->trans[tzobj->tzi.tz->bit64.timecnt - 1];
4373
4374 /* Find out year for last transition */
4375 timelib_unixtime2date(last_transition_ts, &start_y, &dummy_m, &dummy_d);
4376
4377 /* Find out year for final boundary timestamp */
4378 timelib_unixtime2date(timestamp_end, &end_y, &dummy_m, &dummy_d);
4379
4380 for (timelib_sll i = start_y; i <= end_y; i++) {
4381 timelib_posix_transitions transitions = { 0 };
4382
4383 timelib_get_transitions_for_year(tzobj->tzi.tz, i, &transitions);
4384
4385 for (size_t j = 0; j < transitions.count; j++) {
4386 if (transitions.times[j] <= last_transition_ts) {
4387 continue;
4388 }
4389 if (transitions.times[j] < timestamp_begin) {
4390 continue;
4391 }
4392 if (transitions.times[j] > timestamp_end) {
4393 return;
4394 }
4395 add_by_index(transitions.types[j], transitions.times[j]);
4396 }
4397 }
4398 }
4399 }
4400 /* }}} */
4401
4402 /* {{{ Returns location information for a timezone, including country code, latitude/longitude and comments */
PHP_FUNCTION(timezone_location_get)4403 PHP_FUNCTION(timezone_location_get)
4404 {
4405 zval *object;
4406 php_timezone_obj *tzobj;
4407
4408 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_timezone) == FAILURE) {
4409 RETURN_THROWS();
4410 }
4411 tzobj = Z_PHPTIMEZONE_P(object);
4412 DATE_CHECK_INITIALIZED(tzobj->initialized, Z_OBJCE_P(object));
4413 if (tzobj->type != TIMELIB_ZONETYPE_ID) {
4414 RETURN_FALSE;
4415 }
4416
4417 array_init(return_value);
4418 add_assoc_string(return_value, "country_code", tzobj->tzi.tz->location.country_code);
4419 add_assoc_double(return_value, "latitude", tzobj->tzi.tz->location.latitude);
4420 add_assoc_double(return_value, "longitude", tzobj->tzi.tz->location.longitude);
4421 add_assoc_string(return_value, "comments", tzobj->tzi.tz->location.comments);
4422 }
4423 /* }}} */
4424
date_interval_initialize(timelib_rel_time ** rt,char * format,size_t format_length)4425 static bool date_interval_initialize(timelib_rel_time **rt, /*const*/ char *format, size_t format_length) /* {{{ */
4426 {
4427 timelib_time *b = NULL, *e = NULL;
4428 timelib_rel_time *p = NULL;
4429 int r = 0;
4430 bool retval = false;
4431 timelib_error_container *errors;
4432
4433 timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
4434
4435 if (errors->error_count > 0) {
4436 zend_throw_exception_ex(date_ce_date_malformed_interval_string_exception, 0, "Unknown or bad format (%s)", format);
4437 retval = false;
4438 if (p) {
4439 timelib_rel_time_dtor(p);
4440 }
4441 } else {
4442 if (p) {
4443 *rt = p;
4444 retval = true;
4445 } else {
4446 if (b && e) {
4447 timelib_update_ts(b, NULL);
4448 timelib_update_ts(e, NULL);
4449 *rt = timelib_diff(b, e);
4450 retval = true;
4451 } else {
4452 zend_throw_exception_ex(date_ce_date_malformed_interval_string_exception, 0, "Failed to parse interval (%s)", format);
4453 retval = false;
4454 }
4455 }
4456 }
4457 timelib_error_container_dtor(errors);
4458 timelib_free(b);
4459 timelib_free(e);
4460 return retval;
4461 } /* }}} */
4462
date_interval_compare_objects(zval * o1,zval * o2)4463 static int date_interval_compare_objects(zval *o1, zval *o2)
4464 {
4465 ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
4466 /* There is no well defined way to compare intervals like P1M and P30D, which may compare
4467 * smaller, equal or greater depending on the point in time at which the interval starts. As
4468 * such, we treat DateInterval objects are non-comparable and emit a warning. */
4469 zend_error(E_WARNING, "Cannot compare DateInterval objects");
4470 return ZEND_UNCOMPARABLE;
4471 }
4472
4473 /* {{{ date_interval_read_property */
date_interval_read_property(zend_object * object,zend_string * name,int type,void ** cache_slot,zval * rv)4474 static zval *date_interval_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
4475 {
4476 php_interval_obj *obj;
4477 zval *retval;
4478 timelib_sll value = -1;
4479 double fvalue = -1;
4480
4481 obj = php_interval_obj_from_obj(object);
4482
4483 if (!obj->initialized) {
4484 retval = zend_std_read_property(object, name, type, cache_slot, rv);
4485 return retval;
4486 }
4487
4488 #define GET_VALUE_FROM_STRUCT(n,m) \
4489 if (zend_string_equals_literal(name, m)) { \
4490 value = obj->diff->n; \
4491 break; \
4492 }
4493 do {
4494 GET_VALUE_FROM_STRUCT(y, "y");
4495 GET_VALUE_FROM_STRUCT(m, "m");
4496 GET_VALUE_FROM_STRUCT(d, "d");
4497 GET_VALUE_FROM_STRUCT(h, "h");
4498 GET_VALUE_FROM_STRUCT(i, "i");
4499 GET_VALUE_FROM_STRUCT(s, "s");
4500 if (zend_string_equals_literal(name, "f")) {
4501 fvalue = obj->diff->us / 1000000.0;
4502 break;
4503 }
4504 GET_VALUE_FROM_STRUCT(invert, "invert");
4505 GET_VALUE_FROM_STRUCT(days, "days");
4506 /* didn't find any */
4507 retval = zend_std_read_property(object, name, type, cache_slot, rv);
4508
4509 return retval;
4510 } while(0);
4511
4512 retval = rv;
4513
4514 if (fvalue != -1) {
4515 ZVAL_DOUBLE(retval, fvalue);
4516 } else if (value != TIMELIB_UNSET) {
4517 ZVAL_LONG(retval, value);
4518 } else {
4519 ZVAL_FALSE(retval);
4520 }
4521
4522 return retval;
4523 }
4524 /* }}} */
4525
4526 /* {{{ date_interval_write_property */
date_interval_write_property(zend_object * object,zend_string * name,zval * value,void ** cache_slot)4527 static zval *date_interval_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
4528 {
4529 php_interval_obj *obj;
4530
4531 obj = php_interval_obj_from_obj(object);
4532
4533 if (!obj->initialized) {
4534 return zend_std_write_property(object, name, value, cache_slot);
4535 }
4536
4537 #define SET_VALUE_FROM_STRUCT(n,m) \
4538 if (zend_string_equals_literal(name, m)) { \
4539 obj->diff->n = zval_get_long(value); \
4540 break; \
4541 }
4542
4543 do {
4544 SET_VALUE_FROM_STRUCT(y, "y");
4545 SET_VALUE_FROM_STRUCT(m, "m");
4546 SET_VALUE_FROM_STRUCT(d, "d");
4547 SET_VALUE_FROM_STRUCT(h, "h");
4548 SET_VALUE_FROM_STRUCT(i, "i");
4549 SET_VALUE_FROM_STRUCT(s, "s");
4550 if (zend_string_equals_literal(name, "f")) {
4551 obj->diff->us = zend_dval_to_lval(zval_get_double(value) * 1000000.0);
4552 break;
4553 }
4554 SET_VALUE_FROM_STRUCT(invert, "invert");
4555 /* didn't find any */
4556 value = zend_std_write_property(object, name, value, cache_slot);
4557 } while(0);
4558
4559 return value;
4560 }
4561 /* }}} */
4562
4563 /* {{{ date_interval_get_property_ptr_ptr */
date_interval_get_property_ptr_ptr(zend_object * object,zend_string * name,int type,void ** cache_slot)4564 static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
4565 {
4566 zval *ret;
4567
4568 if (
4569 zend_string_equals_literal(name, "y") ||
4570 zend_string_equals_literal(name, "m") ||
4571 zend_string_equals_literal(name, "d") ||
4572 zend_string_equals_literal(name, "h") ||
4573 zend_string_equals_literal(name, "i") ||
4574 zend_string_equals_literal(name, "s") ||
4575 zend_string_equals_literal(name, "f") ||
4576 zend_string_equals_literal(name, "days") ||
4577 zend_string_equals_literal(name, "invert") ) {
4578 /* Fallback to read_property. */
4579 ret = NULL;
4580 } else {
4581 ret = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
4582 }
4583
4584 return ret;
4585 }
4586 /* }}} */
4587
4588 /* {{{ Creates new DateInterval object. */
PHP_METHOD(DateInterval,__construct)4589 PHP_METHOD(DateInterval, __construct)
4590 {
4591 zend_string *interval_string = NULL;
4592 timelib_rel_time *reltime;
4593
4594 ZEND_PARSE_PARAMETERS_START(1, 1)
4595 Z_PARAM_STR(interval_string)
4596 ZEND_PARSE_PARAMETERS_END();
4597
4598 if (!date_interval_initialize(&reltime, ZSTR_VAL(interval_string), ZSTR_LEN(interval_string))) {
4599 RETURN_THROWS();
4600 }
4601
4602 php_interval_obj *diobj = Z_PHPINTERVAL_P(ZEND_THIS);
4603 diobj->diff = reltime;
4604 diobj->initialized = 1;
4605 diobj->civil_or_wall = PHP_DATE_WALL;
4606 }
4607 /* }}} */
4608
php_date_interval_initialize_from_hash(zval ** return_value,php_interval_obj ** intobj,HashTable * myht)4609 static void php_date_interval_initialize_from_hash(zval **return_value, php_interval_obj **intobj, HashTable *myht) /* {{{ */
4610 {
4611 /* If we have a date_string, use that instead */
4612 zval *date_str = zend_hash_str_find(myht, "date_string", strlen("date_string"));
4613 if (date_str && Z_TYPE_P(date_str) == IS_STRING) {
4614 timelib_time *time;
4615 timelib_error_container *err = NULL;
4616
4617 time = timelib_strtotime(Z_STRVAL_P(date_str), Z_STRLEN_P(date_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
4618
4619 if (err->error_count > 0) {
4620 zend_throw_error(NULL,
4621 "Unknown or bad format (%s) at position %d (%c) while unserializing: %s",
4622 Z_STRVAL_P(date_str),
4623 err->error_messages[0].position,
4624 err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
4625 timelib_time_dtor(time);
4626 timelib_error_container_dtor(err);
4627 return;
4628 }
4629
4630 /* If ->diff is already set, then we need to free it first */
4631 if ((*intobj)->diff) {
4632 timelib_rel_time_dtor((*intobj)->diff);
4633 }
4634
4635 (*intobj)->diff = timelib_rel_time_clone(&time->relative);
4636 (*intobj)->initialized = 1;
4637 (*intobj)->civil_or_wall = PHP_DATE_CIVIL;
4638 (*intobj)->from_string = true;
4639 (*intobj)->date_string = zend_string_copy(Z_STR_P(date_str));
4640
4641 timelib_time_dtor(time);
4642 timelib_error_container_dtor(err);
4643
4644 return;
4645 }
4646
4647 /* If ->diff is already set, then we need to free it first */
4648 if ((*intobj)->diff) {
4649 timelib_rel_time_dtor((*intobj)->diff);
4650 }
4651
4652 /* Set new value */
4653 (*intobj)->diff = timelib_rel_time_ctor();
4654
4655 #define PHP_DATE_INTERVAL_READ_PROPERTY(element, member, itype, def) \
4656 do { \
4657 zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
4658 if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
4659 (*intobj)->diff->member = (itype)zval_get_long(z_arg); \
4660 } else { \
4661 (*intobj)->diff->member = (itype)def; \
4662 } \
4663 } while (0);
4664
4665 #define PHP_DATE_INTERVAL_READ_PROPERTY_I64(element, member) \
4666 do { \
4667 zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
4668 if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
4669 zend_string *tmp_str; \
4670 zend_string *str = zval_get_tmp_string(z_arg, &tmp_str); \
4671 DATE_A64I((*intobj)->diff->member, ZSTR_VAL(str)); \
4672 zend_tmp_string_release(tmp_str); \
4673 } else { \
4674 (*intobj)->diff->member = -1LL; \
4675 } \
4676 } while (0);
4677
4678 #define PHP_DATE_INTERVAL_READ_PROPERTY_DAYS(member) \
4679 do { \
4680 zval *z_arg = zend_hash_str_find(myht, "days", sizeof("days") - 1); \
4681 if (z_arg && Z_TYPE_P(z_arg) == IS_FALSE) { \
4682 (*intobj)->diff->member = TIMELIB_UNSET; \
4683 } else if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
4684 zend_string *str = zval_get_string(z_arg); \
4685 DATE_A64I((*intobj)->diff->member, ZSTR_VAL(str)); \
4686 zend_string_release(str); \
4687 } else { \
4688 (*intobj)->diff->member = -1LL; \
4689 } \
4690 } while (0);
4691
4692 #define PHP_DATE_INTERVAL_READ_PROPERTY_DOUBLE(element, member, def) \
4693 do { \
4694 zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
4695 if (z_arg) { \
4696 (*intobj)->diff->member = (double)zval_get_double(z_arg); \
4697 } else { \
4698 (*intobj)->diff->member = (double)def; \
4699 } \
4700 } while (0);
4701
4702 PHP_DATE_INTERVAL_READ_PROPERTY("y", y, timelib_sll, -1)
4703 PHP_DATE_INTERVAL_READ_PROPERTY("m", m, timelib_sll, -1)
4704 PHP_DATE_INTERVAL_READ_PROPERTY("d", d, timelib_sll, -1)
4705 PHP_DATE_INTERVAL_READ_PROPERTY("h", h, timelib_sll, -1)
4706 PHP_DATE_INTERVAL_READ_PROPERTY("i", i, timelib_sll, -1)
4707 PHP_DATE_INTERVAL_READ_PROPERTY("s", s, timelib_sll, -1)
4708 {
4709 zval *z_arg = zend_hash_str_find(myht, "f", sizeof("f") - 1);
4710 if (z_arg) {
4711 (*intobj)->diff->us = zend_dval_to_lval(zval_get_double(z_arg) * 1000000.0);
4712 }
4713 }
4714 PHP_DATE_INTERVAL_READ_PROPERTY("weekday", weekday, int, -1)
4715 PHP_DATE_INTERVAL_READ_PROPERTY("weekday_behavior", weekday_behavior, int, -1)
4716 PHP_DATE_INTERVAL_READ_PROPERTY("first_last_day_of", first_last_day_of, int, -1)
4717 PHP_DATE_INTERVAL_READ_PROPERTY("invert", invert, int, 0);
4718 PHP_DATE_INTERVAL_READ_PROPERTY_DAYS(days);
4719 PHP_DATE_INTERVAL_READ_PROPERTY("special_type", special.type, unsigned int, 0);
4720 PHP_DATE_INTERVAL_READ_PROPERTY_I64("special_amount", special.amount);
4721 PHP_DATE_INTERVAL_READ_PROPERTY("have_weekday_relative", have_weekday_relative, unsigned int, 0);
4722 PHP_DATE_INTERVAL_READ_PROPERTY("have_special_relative", have_special_relative, unsigned int, 0);
4723 {
4724 zval *z_arg = zend_hash_str_find(myht, "civil_or_wall", sizeof("civil_or_wall") - 1);
4725 (*intobj)->civil_or_wall = PHP_DATE_CIVIL;
4726 if (z_arg) {
4727 zend_long val = zval_get_long(z_arg);
4728 (*intobj)->civil_or_wall = val;
4729 }
4730 }
4731
4732 (*intobj)->initialized = 1;
4733 } /* }}} */
4734
4735 /* {{{ */
PHP_METHOD(DateInterval,__set_state)4736 PHP_METHOD(DateInterval, __set_state)
4737 {
4738 php_interval_obj *intobj;
4739 zval *array;
4740 HashTable *myht;
4741
4742 ZEND_PARSE_PARAMETERS_START(1, 1)
4743 Z_PARAM_ARRAY(array)
4744 ZEND_PARSE_PARAMETERS_END();
4745
4746 myht = Z_ARRVAL_P(array);
4747
4748 php_date_instantiate(date_ce_interval, return_value);
4749 intobj = Z_PHPINTERVAL_P(return_value);
4750 php_date_interval_initialize_from_hash(&return_value, &intobj, myht);
4751 }
4752 /* }}} */
4753
4754 /* {{{ */
PHP_METHOD(DateInterval,__serialize)4755 PHP_METHOD(DateInterval, __serialize)
4756 {
4757 zval *object = ZEND_THIS;
4758 php_interval_obj *intervalobj;
4759 HashTable *myht;
4760
4761 ZEND_PARSE_PARAMETERS_NONE();
4762
4763 intervalobj = Z_PHPINTERVAL_P(object);
4764 DATE_CHECK_INITIALIZED(intervalobj->initialized, Z_OBJCE_P(object));
4765
4766 array_init(return_value);
4767 myht = Z_ARRVAL_P(return_value);
4768 date_interval_object_to_hash(intervalobj, myht);
4769
4770 add_common_properties(myht, &intervalobj->std);
4771 }
4772 /* }}} */
4773
date_interval_is_internal_property(zend_string * name)4774 static bool date_interval_is_internal_property(zend_string *name)
4775 {
4776 if (
4777 zend_string_equals_literal(name, "date_string") ||
4778 zend_string_equals_literal(name, "from_string") ||
4779 zend_string_equals_literal(name, "y") ||
4780 zend_string_equals_literal(name, "m") ||
4781 zend_string_equals_literal(name, "d") ||
4782 zend_string_equals_literal(name, "h") ||
4783 zend_string_equals_literal(name, "i") ||
4784 zend_string_equals_literal(name, "s") ||
4785 zend_string_equals_literal(name, "f") ||
4786 zend_string_equals_literal(name, "invert") ||
4787 zend_string_equals_literal(name, "days")
4788 ) {
4789 return 1;
4790 }
4791 return 0;
4792 }
4793
restore_custom_dateinterval_properties(zval * object,HashTable * myht)4794 static void restore_custom_dateinterval_properties(zval *object, HashTable *myht)
4795 {
4796 zend_string *prop_name;
4797 zval *prop_val;
4798
4799 ZEND_HASH_FOREACH_STR_KEY_VAL(myht, prop_name, prop_val) {
4800 if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_interval_is_internal_property(prop_name)) {
4801 continue;
4802 }
4803 update_property(Z_OBJ_P(object), prop_name, prop_val);
4804 } ZEND_HASH_FOREACH_END();
4805 }
4806
4807
4808 /* {{{ */
PHP_METHOD(DateInterval,__unserialize)4809 PHP_METHOD(DateInterval, __unserialize)
4810 {
4811 zval *object = ZEND_THIS;
4812 php_interval_obj *intervalobj;
4813 zval *array;
4814 HashTable *myht;
4815
4816 ZEND_PARSE_PARAMETERS_START(1, 1)
4817 Z_PARAM_ARRAY(array)
4818 ZEND_PARSE_PARAMETERS_END();
4819
4820 intervalobj = Z_PHPINTERVAL_P(object);
4821 myht = Z_ARRVAL_P(array);
4822
4823 php_date_interval_initialize_from_hash(&object, &intervalobj, myht);
4824 restore_custom_dateinterval_properties(object, myht);
4825 }
4826 /* }}} */
4827
4828 /* {{{ */
PHP_METHOD(DateInterval,__wakeup)4829 PHP_METHOD(DateInterval, __wakeup)
4830 {
4831 zval *object = ZEND_THIS;
4832 php_interval_obj *intobj;
4833 HashTable *myht;
4834
4835 ZEND_PARSE_PARAMETERS_NONE();
4836
4837 intobj = Z_PHPINTERVAL_P(object);
4838
4839 myht = Z_OBJPROP_P(object);
4840
4841 php_date_interval_initialize_from_hash(&return_value, &intobj, myht);
4842 }
4843 /* }}} */
4844
date_interval_instantiate_from_time(zval * return_value,timelib_time * time,zend_string * time_str)4845 static void date_interval_instantiate_from_time(zval *return_value, timelib_time *time, zend_string *time_str)
4846 {
4847 php_interval_obj *diobj;
4848
4849 php_date_instantiate(date_ce_interval, return_value);
4850 diobj = Z_PHPINTERVAL_P(return_value);
4851 diobj->diff = timelib_rel_time_clone(&time->relative);
4852 diobj->initialized = 1;
4853 diobj->civil_or_wall = PHP_DATE_CIVIL;
4854 diobj->from_string = true;
4855 diobj->date_string = zend_string_copy(time_str);
4856 }
4857
4858 /* {{{ Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string */
PHP_FUNCTION(date_interval_create_from_date_string)4859 PHP_FUNCTION(date_interval_create_from_date_string)
4860 {
4861 zend_string *time_str = NULL;
4862 timelib_time *time;
4863 timelib_error_container *err = NULL;
4864
4865 ZEND_PARSE_PARAMETERS_START(1, 1)
4866 Z_PARAM_STR(time_str)
4867 ZEND_PARSE_PARAMETERS_END();
4868
4869 time = timelib_strtotime(ZSTR_VAL(time_str), ZSTR_LEN(time_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
4870
4871 if (err->error_count > 0) {
4872 php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s) at position %d (%c): %s", ZSTR_VAL(time_str),
4873 err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
4874 RETVAL_FALSE;
4875 goto cleanup;
4876 }
4877
4878 if (time->have_date || time->have_time || time->have_zone) {
4879 php_error_docref(NULL, E_WARNING, "String '%s' contains non-relative elements", ZSTR_VAL(time_str));
4880 RETVAL_FALSE;
4881 goto cleanup;
4882 }
4883
4884 date_interval_instantiate_from_time(return_value, time, time_str);
4885
4886 cleanup:
4887 timelib_time_dtor(time);
4888 timelib_error_container_dtor(err);
4889 }
4890 /* }}} */
4891
4892 /* {{{ Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string */
PHP_METHOD(DateInterval,createFromDateString)4893 PHP_METHOD(DateInterval, createFromDateString)
4894 {
4895 zend_string *time_str = NULL;
4896 timelib_time *time;
4897 timelib_error_container *err = NULL;
4898
4899 ZEND_PARSE_PARAMETERS_START(1, 1)
4900 Z_PARAM_STR(time_str)
4901 ZEND_PARSE_PARAMETERS_END();
4902
4903 time = timelib_strtotime(ZSTR_VAL(time_str), ZSTR_LEN(time_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
4904
4905 if (err->error_count > 0) {
4906 zend_throw_error(date_ce_date_malformed_interval_string_exception, "Unknown or bad format (%s) at position %d (%c): %s", ZSTR_VAL(time_str),
4907 err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
4908 goto cleanup;
4909 }
4910
4911 if (time->have_date || time->have_time || time->have_zone) {
4912 zend_throw_error(date_ce_date_malformed_interval_string_exception, "String '%s' contains non-relative elements", ZSTR_VAL(time_str));
4913 goto cleanup;
4914 }
4915
4916 date_interval_instantiate_from_time(return_value, time, time_str);
4917
4918 cleanup:
4919 timelib_time_dtor(time);
4920 timelib_error_container_dtor(err);
4921 }
4922 /* }}} */
4923
4924 /* {{{ date_interval_format - */
date_interval_format(char * format,size_t format_len,timelib_rel_time * t)4925 static zend_string *date_interval_format(char *format, size_t format_len, timelib_rel_time *t)
4926 {
4927 smart_str string = {0};
4928 size_t i;
4929 int length, have_format_spec = 0;
4930 char buffer[33];
4931
4932 if (!format_len) {
4933 return ZSTR_EMPTY_ALLOC();
4934 }
4935
4936 for (i = 0; i < format_len; i++) {
4937 if (have_format_spec) {
4938 switch (format[i]) {
4939 case 'Y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->y); break;
4940 case 'y': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->y); break;
4941
4942 case 'M': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
4943 case 'm': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
4944
4945 case 'D': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
4946 case 'd': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
4947
4948 case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
4949 case 'h': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
4950
4951 case 'I': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
4952 case 'i': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->i); break;
4953
4954 case 'S': length = slprintf(buffer, sizeof(buffer), "%02" ZEND_LONG_FMT_SPEC, (zend_long) t->s); break;
4955 case 's': length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) t->s); break;
4956
4957 case 'F': length = slprintf(buffer, sizeof(buffer), "%06" ZEND_LONG_FMT_SPEC, (zend_long) t->us); break;
4958 case 'f': length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) t->us); break;
4959
4960 case 'a': {
4961 if ((int) t->days != TIMELIB_UNSET) {
4962 length = slprintf(buffer, sizeof(buffer), "%d", (int) t->days);
4963 } else {
4964 length = slprintf(buffer, sizeof(buffer), "(unknown)");
4965 }
4966 } break;
4967 case 'r': length = slprintf(buffer, sizeof(buffer), "%s", t->invert ? "-" : ""); break;
4968 case 'R': length = slprintf(buffer, sizeof(buffer), "%c", t->invert ? '-' : '+'); break;
4969
4970 case '%': length = slprintf(buffer, sizeof(buffer), "%%"); break;
4971 default: buffer[0] = '%'; buffer[1] = format[i]; buffer[2] = '\0'; length = 2; break;
4972 }
4973 smart_str_appendl(&string, buffer, length);
4974 have_format_spec = 0;
4975 } else {
4976 if (format[i] == '%') {
4977 have_format_spec = 1;
4978 } else {
4979 smart_str_appendc(&string, format[i]);
4980 }
4981 }
4982 }
4983
4984 smart_str_0(&string);
4985
4986 if (string.s == NULL) {
4987 return ZSTR_EMPTY_ALLOC();
4988 }
4989
4990 return string.s;
4991 }
4992 /* }}} */
4993
4994 /* {{{ Formats the interval. */
PHP_FUNCTION(date_interval_format)4995 PHP_FUNCTION(date_interval_format)
4996 {
4997 zval *object;
4998 php_interval_obj *diobj;
4999 char *format;
5000 size_t format_len;
5001
5002 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_interval, &format, &format_len) == FAILURE) {
5003 RETURN_THROWS();
5004 }
5005 diobj = Z_PHPINTERVAL_P(object);
5006 DATE_CHECK_INITIALIZED(diobj->initialized, Z_OBJCE_P(object));
5007
5008 RETURN_STR(date_interval_format(format, format_len, diobj->diff));
5009 }
5010 /* }}} */
5011
date_period_initialize(timelib_time ** st,timelib_time ** et,timelib_rel_time ** d,zend_long * recurrences,char * format,size_t format_length)5012 static bool date_period_initialize(timelib_time **st, timelib_time **et, timelib_rel_time **d, zend_long *recurrences, /*const*/ char *format, size_t format_length) /* {{{ */
5013 {
5014 timelib_time *b = NULL, *e = NULL;
5015 timelib_rel_time *p = NULL;
5016 int r = 0;
5017 timelib_error_container *errors;
5018 bool retval = false;
5019
5020 timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
5021
5022 if (errors->error_count > 0) {
5023 retval = false;
5024 zend_throw_exception_ex(date_ce_date_malformed_period_string_exception, 0, "Unknown or bad format (%s)", format);
5025 if (b) {
5026 timelib_time_dtor(b);
5027 }
5028 if (e) {
5029 timelib_time_dtor(e);
5030 }
5031 if (p) {
5032 timelib_rel_time_dtor(p);
5033 }
5034 } else {
5035 *st = b;
5036 *et = e;
5037 *d = p;
5038 *recurrences = r;
5039 retval = true;
5040 }
5041 timelib_error_container_dtor(errors);
5042 return retval;
5043 } /* }}} */
5044
date_period_init_iso8601_string(php_period_obj * dpobj,zend_class_entry * base_ce,char * isostr,size_t isostr_len,zend_long options,zend_long * recurrences)5045 static bool date_period_init_iso8601_string(php_period_obj *dpobj, zend_class_entry* base_ce, char *isostr, size_t isostr_len, zend_long options, zend_long *recurrences)
5046 {
5047 if (!date_period_initialize(&(dpobj->start), &(dpobj->end), &(dpobj->interval), recurrences, isostr, isostr_len)) {
5048 return false;
5049 }
5050
5051 if (dpobj->start == NULL) {
5052 zend_string *func = get_active_function_or_method_name();
5053 zend_throw_exception_ex(date_ce_date_malformed_period_string_exception, 0, "%s(): ISO interval must contain a start date, \"%s\" given", ZSTR_VAL(func), isostr);
5054 zend_string_release(func);
5055 return false;
5056 }
5057 if (dpobj->interval == NULL) {
5058 zend_string *func = get_active_function_or_method_name();
5059 zend_throw_exception_ex(date_ce_date_malformed_period_string_exception, 0, "%s(): ISO interval must contain an interval, \"%s\" given", ZSTR_VAL(func), isostr);
5060 zend_string_release(func);
5061 return false;
5062 }
5063 if (dpobj->end == NULL && recurrences == 0) {
5064 zend_string *func = get_active_function_or_method_name();
5065 zend_throw_exception_ex(date_ce_date_malformed_period_string_exception, 0, "%s(): ISO interval must contain an end date or a recurrence count, \"%s\" given", ZSTR_VAL(func), isostr);
5066 zend_string_release(func);
5067 return false;
5068 }
5069
5070 if (dpobj->start) {
5071 timelib_update_ts(dpobj->start, NULL);
5072 }
5073 if (dpobj->end) {
5074 timelib_update_ts(dpobj->end, NULL);
5075 }
5076 dpobj->start_ce = base_ce;
5077
5078 return true;
5079 }
5080
date_period_init_finish(php_period_obj * dpobj,zend_long options,zend_long recurrences)5081 static bool date_period_init_finish(php_period_obj *dpobj, zend_long options, zend_long recurrences)
5082 {
5083 const zend_long max_recurrences = (INT_MAX - 8);
5084
5085 if (dpobj->end == NULL && (recurrences < 1 || recurrences > max_recurrences)) {
5086 zend_string *func = get_active_function_or_method_name();
5087 zend_throw_exception_ex(date_ce_date_malformed_period_string_exception, 0, "%s(): Recurrence count must be greater or equal to 1 and lower than " ZEND_LONG_FMT, ZSTR_VAL(func), max_recurrences + 1);
5088 zend_string_release(func);
5089 return false;
5090 }
5091
5092 /* options */
5093 dpobj->include_start_date = !(options & PHP_DATE_PERIOD_EXCLUDE_START_DATE);
5094 dpobj->include_end_date = options & PHP_DATE_PERIOD_INCLUDE_END_DATE;
5095
5096 /* recurrences */
5097 recurrences += dpobj->include_start_date + dpobj->include_end_date;
5098
5099 if (UNEXPECTED(recurrences > max_recurrences)) {
5100 zend_string *func = get_active_function_or_method_name();
5101 zend_throw_exception_ex(date_ce_date_malformed_string_exception, 0, "%s(): Recurrence count must be greater or equal to 1 and lower than " ZEND_LONG_FMT " (including options)", ZSTR_VAL(func), max_recurrences + 1);
5102 zend_string_release(func);
5103 return false;
5104 }
5105
5106 dpobj->recurrences = (int)recurrences;
5107
5108 dpobj->initialized = 1;
5109
5110 return true;
5111 }
5112
PHP_METHOD(DatePeriod,createFromISO8601String)5113 PHP_METHOD(DatePeriod, createFromISO8601String)
5114 {
5115 php_period_obj *dpobj;
5116 zend_long recurrences = 0, options = 0;
5117 char *isostr = NULL;
5118 size_t isostr_len = 0;
5119
5120 ZEND_PARSE_PARAMETERS_START(1, 2)
5121 Z_PARAM_STRING(isostr, isostr_len)
5122 Z_PARAM_OPTIONAL
5123 Z_PARAM_LONG(options)
5124 ZEND_PARSE_PARAMETERS_END();
5125
5126 object_init_ex(return_value, execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_period);
5127 dpobj = Z_PHPPERIOD_P(return_value);
5128
5129 dpobj->current = NULL;
5130
5131 if (!date_period_init_iso8601_string(dpobj, date_ce_immutable, isostr, isostr_len, options, &recurrences)) {
5132 RETURN_THROWS();
5133 }
5134
5135 if (!date_period_init_finish(dpobj, options, recurrences)) {
5136 RETURN_THROWS();
5137 }
5138 }
5139
5140 /* {{{ Creates new DatePeriod object. */
PHP_METHOD(DatePeriod,__construct)5141 PHP_METHOD(DatePeriod, __construct)
5142 {
5143 php_period_obj *dpobj;
5144 php_date_obj *dateobj;
5145 zval *start, *end = NULL, *interval;
5146 zend_long recurrences = 0, options = 0;
5147 char *isostr = NULL;
5148 size_t isostr_len = 0;
5149 timelib_time *clone;
5150
5151 if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOl|l", &start, date_ce_interface, &interval, date_ce_interval, &recurrences, &options) == FAILURE) {
5152 if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOO|l", &start, date_ce_interface, &interval, date_ce_interval, &end, date_ce_interface, &options) == FAILURE) {
5153 if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "s|l", &isostr, &isostr_len, &options) == FAILURE) {
5154 zend_type_error("DatePeriod::__construct() accepts (DateTimeInterface, DateInterval, int [, int]), or (DateTimeInterface, DateInterval, DateTime [, int]), or (string [, int]) as arguments");
5155 RETURN_THROWS();
5156 }
5157 }
5158 }
5159
5160 dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5161 dpobj->current = NULL;
5162
5163 if (isostr) {
5164 zend_error(E_DEPRECATED, "Calling DatePeriod::__construct(string $isostr, int $options = 0) is deprecated, "
5165 "use DatePeriod::createFromISO8601String() instead");
5166 if (UNEXPECTED(EG(exception))) {
5167 RETURN_THROWS();
5168 }
5169
5170 if (!date_period_init_iso8601_string(dpobj, date_ce_date, isostr, isostr_len, options, &recurrences)) {
5171 RETURN_THROWS();
5172 }
5173 } else {
5174 /* check initialisation */
5175 DATE_CHECK_INITIALIZED(Z_PHPDATE_P(start)->time, date_ce_interface);
5176 if (end) {
5177 DATE_CHECK_INITIALIZED(Z_PHPDATE_P(end)->time, date_ce_interface);
5178 }
5179
5180 /* init */
5181 php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
5182
5183 /* start date */
5184 dateobj = Z_PHPDATE_P(start);
5185 clone = timelib_time_ctor();
5186 memcpy(clone, dateobj->time, sizeof(timelib_time));
5187 if (dateobj->time->tz_abbr) {
5188 clone->tz_abbr = timelib_strdup(dateobj->time->tz_abbr);
5189 }
5190 if (dateobj->time->tz_info) {
5191 clone->tz_info = dateobj->time->tz_info;
5192 }
5193 dpobj->start = clone;
5194 dpobj->start_ce = Z_OBJCE_P(start);
5195
5196 /* interval */
5197 dpobj->interval = timelib_rel_time_clone(intobj->diff);
5198
5199 /* end date */
5200 if (end) {
5201 dateobj = Z_PHPDATE_P(end);
5202 clone = timelib_time_clone(dateobj->time);
5203 dpobj->end = clone;
5204 }
5205 }
5206
5207 if (!date_period_init_finish(dpobj, options, recurrences)) {
5208 RETURN_THROWS();
5209 }
5210 }
5211 /* }}} */
5212
5213 /* {{{ Get start date. */
PHP_METHOD(DatePeriod,getStartDate)5214 PHP_METHOD(DatePeriod, getStartDate)
5215 {
5216 php_period_obj *dpobj;
5217 php_date_obj *dateobj;
5218
5219 ZEND_PARSE_PARAMETERS_NONE();
5220
5221 dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5222 DATE_CHECK_INITIALIZED(dpobj->start, Z_OBJCE_P(ZEND_THIS));
5223
5224 php_date_instantiate(dpobj->start_ce, return_value);
5225 dateobj = Z_PHPDATE_P(return_value);
5226 dateobj->time = timelib_time_ctor();
5227 *dateobj->time = *dpobj->start;
5228 if (dpobj->start->tz_abbr) {
5229 dateobj->time->tz_abbr = timelib_strdup(dpobj->start->tz_abbr);
5230 }
5231 if (dpobj->start->tz_info) {
5232 dateobj->time->tz_info = dpobj->start->tz_info;
5233 }
5234 }
5235 /* }}} */
5236
5237 /* {{{ Get end date. */
PHP_METHOD(DatePeriod,getEndDate)5238 PHP_METHOD(DatePeriod, getEndDate)
5239 {
5240 php_period_obj *dpobj;
5241 php_date_obj *dateobj;
5242
5243 ZEND_PARSE_PARAMETERS_NONE();
5244
5245 dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5246
5247 if (!dpobj->end) {
5248 return;
5249 }
5250
5251 php_date_instantiate(dpobj->start_ce, return_value);
5252 dateobj = Z_PHPDATE_P(return_value);
5253 dateobj->time = timelib_time_ctor();
5254 *dateobj->time = *dpobj->end;
5255 if (dpobj->end->tz_abbr) {
5256 dateobj->time->tz_abbr = timelib_strdup(dpobj->end->tz_abbr);
5257 }
5258 if (dpobj->end->tz_info) {
5259 dateobj->time->tz_info = dpobj->end->tz_info;
5260 }
5261 }
5262 /* }}} */
5263
5264 /* {{{ Get date interval. */
PHP_METHOD(DatePeriod,getDateInterval)5265 PHP_METHOD(DatePeriod, getDateInterval)
5266 {
5267 php_period_obj *dpobj;
5268 php_interval_obj *diobj;
5269
5270 ZEND_PARSE_PARAMETERS_NONE();
5271
5272 dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5273 DATE_CHECK_INITIALIZED(dpobj->interval, Z_OBJCE_P(ZEND_THIS));
5274
5275 php_date_instantiate(date_ce_interval, return_value);
5276 diobj = Z_PHPINTERVAL_P(return_value);
5277 diobj->diff = timelib_rel_time_clone(dpobj->interval);
5278 diobj->initialized = 1;
5279 }
5280 /* }}} */
5281
5282 /* {{{ Get recurrences. */
PHP_METHOD(DatePeriod,getRecurrences)5283 PHP_METHOD(DatePeriod, getRecurrences)
5284 {
5285 php_period_obj *dpobj;
5286
5287 ZEND_PARSE_PARAMETERS_NONE();
5288
5289 dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5290
5291 if (0 == dpobj->recurrences - dpobj->include_start_date - dpobj->include_end_date) {
5292 return;
5293 }
5294
5295 RETURN_LONG(dpobj->recurrences - dpobj->include_start_date - dpobj->include_end_date);
5296 }
5297 /* }}} */
5298
PHP_METHOD(DatePeriod,getIterator)5299 PHP_METHOD(DatePeriod, getIterator)
5300 {
5301 ZEND_PARSE_PARAMETERS_NONE();
5302
5303 zend_create_internal_iterator_zval(return_value, ZEND_THIS);
5304 }
5305
check_id_allowed(char * id,zend_long what)5306 static int check_id_allowed(char *id, zend_long what) /* {{{ */
5307 {
5308 if ((what & PHP_DATE_TIMEZONE_GROUP_AFRICA) && strncasecmp(id, "Africa/", 7) == 0) return 1;
5309 if ((what & PHP_DATE_TIMEZONE_GROUP_AMERICA) && strncasecmp(id, "America/", 8) == 0) return 1;
5310 if ((what & PHP_DATE_TIMEZONE_GROUP_ANTARCTICA) && strncasecmp(id, "Antarctica/", 11) == 0) return 1;
5311 if ((what & PHP_DATE_TIMEZONE_GROUP_ARCTIC) && strncasecmp(id, "Arctic/", 7) == 0) return 1;
5312 if ((what & PHP_DATE_TIMEZONE_GROUP_ASIA) && strncasecmp(id, "Asia/", 5) == 0) return 1;
5313 if ((what & PHP_DATE_TIMEZONE_GROUP_ATLANTIC) && strncasecmp(id, "Atlantic/", 9) == 0) return 1;
5314 if ((what & PHP_DATE_TIMEZONE_GROUP_AUSTRALIA) && strncasecmp(id, "Australia/", 10) == 0) return 1;
5315 if ((what & PHP_DATE_TIMEZONE_GROUP_EUROPE) && strncasecmp(id, "Europe/", 7) == 0) return 1;
5316 if ((what & PHP_DATE_TIMEZONE_GROUP_INDIAN) && strncasecmp(id, "Indian/", 7) == 0) return 1;
5317 if ((what & PHP_DATE_TIMEZONE_GROUP_PACIFIC) && strncasecmp(id, "Pacific/", 8) == 0) return 1;
5318 if ((what & PHP_DATE_TIMEZONE_GROUP_UTC) && strncasecmp(id, "UTC", 3) == 0) return 1;
5319 return 0;
5320 } /* }}} */
5321
5322 /* {{{ Returns numerically index array with all timezone identifiers. */
PHP_FUNCTION(timezone_identifiers_list)5323 PHP_FUNCTION(timezone_identifiers_list)
5324 {
5325 const timelib_tzdb *tzdb;
5326 const timelib_tzdb_index_entry *table;
5327 int i, item_count;
5328 zend_long what = PHP_DATE_TIMEZONE_GROUP_ALL;
5329 char *option = NULL;
5330 size_t option_len = 0;
5331
5332 ZEND_PARSE_PARAMETERS_START(0, 2)
5333 Z_PARAM_OPTIONAL
5334 Z_PARAM_LONG(what)
5335 Z_PARAM_STRING_OR_NULL(option, option_len)
5336 ZEND_PARSE_PARAMETERS_END();
5337
5338 /* Extra validation */
5339 if (what == PHP_DATE_TIMEZONE_PER_COUNTRY && option_len != 2) {
5340 zend_argument_value_error(2, "must be a two-letter ISO 3166-1 compatible country code "
5341 "when argument #1 ($timezoneGroup) is DateTimeZone::PER_COUNTRY");
5342 RETURN_THROWS();
5343 }
5344
5345 tzdb = DATE_TIMEZONEDB;
5346 table = timelib_timezone_identifiers_list((timelib_tzdb*) tzdb, &item_count);
5347
5348 array_init(return_value);
5349
5350 for (i = 0; i < item_count; ++i) {
5351 if (what == PHP_DATE_TIMEZONE_PER_COUNTRY) {
5352 if (tzdb->data[table[i].pos + 5] == option[0] && tzdb->data[table[i].pos + 6] == option[1]) {
5353 add_next_index_string(return_value, table[i].id);
5354 }
5355 } else if (what == PHP_DATE_TIMEZONE_GROUP_ALL_W_BC || (check_id_allowed(table[i].id, what) && (tzdb->data[table[i].pos + 4] == '\1'))) {
5356 add_next_index_string(return_value, table[i].id);
5357 }
5358 };
5359 }
5360 /* }}} */
5361
5362 /* {{{ Returns the Olson database version number. */
PHP_FUNCTION(timezone_version_get)5363 PHP_FUNCTION(timezone_version_get)
5364 {
5365 const timelib_tzdb *tzdb;
5366
5367 ZEND_PARSE_PARAMETERS_NONE();
5368
5369 tzdb = DATE_TIMEZONEDB;
5370 RETURN_STRING(tzdb->version);
5371 }
5372 /* }}} */
5373
5374 /* {{{ Returns associative array containing dst, offset and the timezone name */
PHP_FUNCTION(timezone_abbreviations_list)5375 PHP_FUNCTION(timezone_abbreviations_list)
5376 {
5377 const timelib_tz_lookup_table *table, *entry;
5378 zval element, *abbr_array_p, abbr_array;
5379
5380 ZEND_PARSE_PARAMETERS_NONE();
5381
5382 table = timelib_timezone_abbreviations_list();
5383 array_init(return_value);
5384 entry = table;
5385
5386 do {
5387 array_init(&element);
5388 add_assoc_bool_ex(&element, "dst", sizeof("dst") -1, entry->type);
5389 add_assoc_long_ex(&element, "offset", sizeof("offset") - 1, entry->gmtoffset);
5390 if (entry->full_tz_name) {
5391 add_assoc_string_ex(&element, "timezone_id", sizeof("timezone_id") - 1, entry->full_tz_name);
5392 } else {
5393 add_assoc_null_ex(&element, "timezone_id", sizeof("timezone_id") - 1);
5394 }
5395
5396 abbr_array_p = zend_hash_str_find(Z_ARRVAL_P(return_value), entry->name, strlen(entry->name));
5397 if (!abbr_array_p) {
5398 array_init(&abbr_array);
5399 add_assoc_zval(return_value, entry->name, &abbr_array);
5400 } else {
5401 ZVAL_COPY_VALUE(&abbr_array, abbr_array_p);
5402 }
5403 add_next_index_zval(&abbr_array, &element);
5404 entry++;
5405 } while (entry->name);
5406 }
5407 /* }}} */
5408
5409 /* {{{ Sets the default timezone used by all date/time functions in a script */
PHP_FUNCTION(date_default_timezone_set)5410 PHP_FUNCTION(date_default_timezone_set)
5411 {
5412 char *zone;
5413 size_t zone_len;
5414
5415 ZEND_PARSE_PARAMETERS_START(1, 1)
5416 Z_PARAM_STRING(zone, zone_len)
5417 ZEND_PARSE_PARAMETERS_END();
5418
5419 if (!timelib_timezone_id_is_valid(zone, DATE_TIMEZONEDB)) {
5420 php_error_docref(NULL, E_NOTICE, "Timezone ID '%s' is invalid", zone);
5421 RETURN_FALSE;
5422 }
5423 if (DATEG(timezone)) {
5424 efree(DATEG(timezone));
5425 DATEG(timezone) = NULL;
5426 }
5427 DATEG(timezone) = estrndup(zone, zone_len);
5428 RETURN_TRUE;
5429 }
5430 /* }}} */
5431
5432 /* {{{ Gets the default timezone used by all date/time functions in a script */
PHP_FUNCTION(date_default_timezone_get)5433 PHP_FUNCTION(date_default_timezone_get)
5434 {
5435 timelib_tzinfo *default_tz;
5436 ZEND_PARSE_PARAMETERS_NONE();
5437
5438 default_tz = get_timezone_info();
5439 if (!default_tz) {
5440 RETURN_THROWS();
5441 }
5442 RETVAL_STRING(default_tz->name);
5443 }
5444 /* }}} */
5445
5446 /* {{{ php_do_date_sunrise_sunset
5447 * Common for date_sunrise() and date_sunset() functions
5448 */
php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS,bool calc_sunset)5449 static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, bool calc_sunset)
5450 {
5451 double latitude, longitude, zenith, gmt_offset, altitude;
5452 bool latitude_is_null = 1, longitude_is_null = 1, zenith_is_null = 1, gmt_offset_is_null = 1;
5453 double h_rise, h_set, N;
5454 timelib_sll rise, set, transit;
5455 zend_long time, retformat = SUNFUNCS_RET_STRING;
5456 int rs;
5457 timelib_time *t;
5458 timelib_tzinfo *tzi;
5459 zend_string *retstr;
5460
5461 ZEND_PARSE_PARAMETERS_START(1, 6)
5462 Z_PARAM_LONG(time)
5463 Z_PARAM_OPTIONAL
5464 Z_PARAM_LONG(retformat)
5465 Z_PARAM_DOUBLE_OR_NULL(latitude, latitude_is_null)
5466 Z_PARAM_DOUBLE_OR_NULL(longitude, longitude_is_null)
5467 Z_PARAM_DOUBLE_OR_NULL(zenith, zenith_is_null)
5468 Z_PARAM_DOUBLE_OR_NULL(gmt_offset, gmt_offset_is_null)
5469 ZEND_PARSE_PARAMETERS_END();
5470
5471 if (latitude_is_null) {
5472 latitude = INI_FLT("date.default_latitude");
5473 }
5474
5475 if (longitude_is_null) {
5476 longitude = INI_FLT("date.default_longitude");
5477 }
5478
5479 if (zenith_is_null) {
5480 if (calc_sunset) {
5481 zenith = INI_FLT("date.sunset_zenith");
5482 } else {
5483 zenith = INI_FLT("date.sunrise_zenith");
5484 }
5485 }
5486
5487 if (retformat != SUNFUNCS_RET_TIMESTAMP &&
5488 retformat != SUNFUNCS_RET_STRING &&
5489 retformat != SUNFUNCS_RET_DOUBLE)
5490 {
5491 zend_argument_value_error(2, "must be one of SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING, or SUNFUNCS_RET_DOUBLE");
5492 RETURN_THROWS();
5493 }
5494 altitude = 90 - zenith;
5495
5496 if (!zend_finite(latitude) || !zend_finite(longitude)) {
5497 RETURN_FALSE;
5498 }
5499
5500 /* Initialize time struct */
5501 tzi = get_timezone_info();
5502 if (!tzi) {
5503 RETURN_THROWS();
5504 }
5505 t = timelib_time_ctor();
5506 t->tz_info = tzi;
5507 t->zone_type = TIMELIB_ZONETYPE_ID;
5508
5509 if (gmt_offset_is_null) {
5510 gmt_offset = timelib_get_current_offset(t) / 3600;
5511 }
5512
5513 timelib_unixtime2local(t, time);
5514 rs = timelib_astro_rise_set_altitude(t, longitude, latitude, altitude, 1, &h_rise, &h_set, &rise, &set, &transit);
5515 timelib_time_dtor(t);
5516
5517 if (rs != 0) {
5518 RETURN_FALSE;
5519 }
5520
5521 if (retformat == SUNFUNCS_RET_TIMESTAMP) {
5522 RETURN_LONG(calc_sunset ? set : rise);
5523 }
5524 N = (calc_sunset ? h_set : h_rise) + gmt_offset;
5525
5526 if (N > 24 || N < 0) {
5527 N -= floor(N / 24) * 24;
5528 }
5529 if (N > 24 || N < 0) {
5530 RETURN_FALSE;
5531 }
5532
5533 switch (retformat) {
5534 case SUNFUNCS_RET_STRING:
5535 retstr = strpprintf(0, "%02d:%02d", (int) N, (int) (60 * (N - (int) N)));
5536 RETURN_NEW_STR(retstr);
5537 break;
5538 case SUNFUNCS_RET_DOUBLE:
5539 RETURN_DOUBLE(N);
5540 break;
5541 }
5542 }
5543 /* }}} */
5544
5545 /* {{{ Returns time of sunrise for a given day and location */
PHP_FUNCTION(date_sunrise)5546 PHP_FUNCTION(date_sunrise)
5547 {
5548 php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
5549 }
5550 /* }}} */
5551
5552 /* {{{ Returns time of sunset for a given day and location */
PHP_FUNCTION(date_sunset)5553 PHP_FUNCTION(date_sunset)
5554 {
5555 php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
5556 }
5557 /* }}} */
5558
5559 /* {{{ Returns an array with information about sun set/rise and twilight begin/end */
PHP_FUNCTION(date_sun_info)5560 PHP_FUNCTION(date_sun_info)
5561 {
5562 zend_long time;
5563 double latitude, longitude;
5564 timelib_time *t, *t2;
5565 timelib_tzinfo *tzi;
5566 int rs;
5567 timelib_sll rise, set, transit;
5568 int dummy;
5569 double ddummy;
5570
5571 ZEND_PARSE_PARAMETERS_START(3, 3)
5572 Z_PARAM_LONG(time)
5573 Z_PARAM_DOUBLE(latitude)
5574 Z_PARAM_DOUBLE(longitude)
5575 ZEND_PARSE_PARAMETERS_END();
5576
5577 if (!zend_finite(latitude)) {
5578 zend_argument_value_error(2, "must be finite");
5579 RETURN_THROWS();
5580 }
5581 if (!zend_finite(longitude)) {
5582 zend_argument_value_error(3, "must be finite");
5583 RETURN_THROWS();
5584 }
5585
5586 /* Initialize time struct */
5587 tzi = get_timezone_info();
5588 if (!tzi) {
5589 RETURN_THROWS();
5590 }
5591 t = timelib_time_ctor();
5592 t->tz_info = tzi;
5593 t->zone_type = TIMELIB_ZONETYPE_ID;
5594 timelib_unixtime2local(t, time);
5595
5596 /* Setup */
5597 t2 = timelib_time_ctor();
5598 array_init(return_value);
5599
5600 /* Get sun up/down and transit */
5601 rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -50.0/60, 1, &ddummy, &ddummy, &rise, &set, &transit);
5602 switch (rs) {
5603 case -1: /* always below */
5604 add_assoc_bool(return_value, "sunrise", 0);
5605 add_assoc_bool(return_value, "sunset", 0);
5606 break;
5607 case 1: /* always above */
5608 add_assoc_bool(return_value, "sunrise", 1);
5609 add_assoc_bool(return_value, "sunset", 1);
5610 break;
5611 default:
5612 t2->sse = rise;
5613 add_assoc_long(return_value, "sunrise", timelib_date_to_int(t2, &dummy));
5614 t2->sse = set;
5615 add_assoc_long(return_value, "sunset", timelib_date_to_int(t2, &dummy));
5616 }
5617 t2->sse = transit;
5618 add_assoc_long(return_value, "transit", timelib_date_to_int(t2, &dummy));
5619
5620 /* Get civil twilight */
5621 rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -6.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
5622 switch (rs) {
5623 case -1: /* always below */
5624 add_assoc_bool(return_value, "civil_twilight_begin", 0);
5625 add_assoc_bool(return_value, "civil_twilight_end", 0);
5626 break;
5627 case 1: /* always above */
5628 add_assoc_bool(return_value, "civil_twilight_begin", 1);
5629 add_assoc_bool(return_value, "civil_twilight_end", 1);
5630 break;
5631 default:
5632 t2->sse = rise;
5633 add_assoc_long(return_value, "civil_twilight_begin", timelib_date_to_int(t2, &dummy));
5634 t2->sse = set;
5635 add_assoc_long(return_value, "civil_twilight_end", timelib_date_to_int(t2, &dummy));
5636 }
5637
5638 /* Get nautical twilight */
5639 rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -12.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
5640 switch (rs) {
5641 case -1: /* always below */
5642 add_assoc_bool(return_value, "nautical_twilight_begin", 0);
5643 add_assoc_bool(return_value, "nautical_twilight_end", 0);
5644 break;
5645 case 1: /* always above */
5646 add_assoc_bool(return_value, "nautical_twilight_begin", 1);
5647 add_assoc_bool(return_value, "nautical_twilight_end", 1);
5648 break;
5649 default:
5650 t2->sse = rise;
5651 add_assoc_long(return_value, "nautical_twilight_begin", timelib_date_to_int(t2, &dummy));
5652 t2->sse = set;
5653 add_assoc_long(return_value, "nautical_twilight_end", timelib_date_to_int(t2, &dummy));
5654 }
5655
5656 /* Get astronomical twilight */
5657 rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -18.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
5658 switch (rs) {
5659 case -1: /* always below */
5660 add_assoc_bool(return_value, "astronomical_twilight_begin", 0);
5661 add_assoc_bool(return_value, "astronomical_twilight_end", 0);
5662 break;
5663 case 1: /* always above */
5664 add_assoc_bool(return_value, "astronomical_twilight_begin", 1);
5665 add_assoc_bool(return_value, "astronomical_twilight_end", 1);
5666 break;
5667 default:
5668 t2->sse = rise;
5669 add_assoc_long(return_value, "astronomical_twilight_begin", timelib_date_to_int(t2, &dummy));
5670 t2->sse = set;
5671 add_assoc_long(return_value, "astronomical_twilight_end", timelib_date_to_int(t2, &dummy));
5672 }
5673 timelib_time_dtor(t);
5674 timelib_time_dtor(t2);
5675 }
5676 /* }}} */
5677
date_object_get_gc_period(zend_object * object,zval ** table,int * n)5678 static HashTable *date_object_get_gc_period(zend_object *object, zval **table, int *n) /* {{{ */
5679 {
5680 *table = NULL;
5681 *n = 0;
5682 return zend_std_get_properties(object);
5683 } /* }}} */
5684
date_period_object_to_hash(php_period_obj * period_obj,HashTable * props)5685 static void date_period_object_to_hash(php_period_obj *period_obj, HashTable *props)
5686 {
5687 zval zv;
5688
5689 create_date_period_datetime(period_obj->start, period_obj->start_ce, &zv);
5690 zend_hash_str_update(props, "start", sizeof("start")-1, &zv);
5691
5692 create_date_period_datetime(period_obj->current, period_obj->start_ce, &zv);
5693 zend_hash_str_update(props, "current", sizeof("current")-1, &zv);
5694
5695 create_date_period_datetime(period_obj->end, period_obj->start_ce, &zv);
5696 zend_hash_str_update(props, "end", sizeof("end")-1, &zv);
5697
5698 create_date_period_interval(period_obj->interval, &zv);
5699 zend_hash_str_update(props, "interval", sizeof("interval")-1, &zv);
5700
5701 /* converted to larger type (int->long); must check when unserializing */
5702 ZVAL_LONG(&zv, (zend_long) period_obj->recurrences);
5703 zend_hash_str_update(props, "recurrences", sizeof("recurrences")-1, &zv);
5704
5705 ZVAL_BOOL(&zv, period_obj->include_start_date);
5706 zend_hash_str_update(props, "include_start_date", sizeof("include_start_date")-1, &zv);
5707
5708 ZVAL_BOOL(&zv, period_obj->include_end_date);
5709 zend_hash_str_update(props, "include_end_date", sizeof("include_end_date")-1, &zv);
5710 }
5711
php_date_period_initialize_from_hash(php_period_obj * period_obj,HashTable * myht)5712 static bool php_date_period_initialize_from_hash(php_period_obj *period_obj, HashTable *myht) /* {{{ */
5713 {
5714 zval *ht_entry;
5715
5716 /* this function does no rollback on error */
5717
5718 ht_entry = zend_hash_str_find(myht, "start", sizeof("start")-1);
5719 if (ht_entry) {
5720 if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
5721 php_date_obj *date_obj;
5722 date_obj = Z_PHPDATE_P(ht_entry);
5723
5724 if (!date_obj->time) {
5725 return 0;
5726 }
5727
5728 if (period_obj->start != NULL) {
5729 timelib_time_dtor(period_obj->start);
5730 }
5731 period_obj->start = timelib_time_clone(date_obj->time);
5732 period_obj->start_ce = Z_OBJCE_P(ht_entry);
5733 } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
5734 return 0;
5735 }
5736 } else {
5737 return 0;
5738 }
5739
5740 ht_entry = zend_hash_str_find(myht, "end", sizeof("end")-1);
5741 if (ht_entry) {
5742 if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
5743 php_date_obj *date_obj;
5744 date_obj = Z_PHPDATE_P(ht_entry);
5745
5746 if (!date_obj->time) {
5747 return 0;
5748 }
5749
5750 if (period_obj->end != NULL) {
5751 timelib_time_dtor(period_obj->end);
5752 }
5753 period_obj->end = timelib_time_clone(date_obj->time);
5754 } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
5755 return 0;
5756 }
5757 } else {
5758 return 0;
5759 }
5760
5761 ht_entry = zend_hash_str_find(myht, "current", sizeof("current")-1);
5762 if (ht_entry) {
5763 if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
5764 php_date_obj *date_obj;
5765 date_obj = Z_PHPDATE_P(ht_entry);
5766
5767 if (!date_obj->time) {
5768 return 0;
5769 }
5770
5771 if (period_obj->current != NULL) {
5772 timelib_time_dtor(period_obj->current);
5773 }
5774 period_obj->current = timelib_time_clone(date_obj->time);
5775 } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
5776 return 0;
5777 }
5778 } else {
5779 return 0;
5780 }
5781
5782 ht_entry = zend_hash_str_find(myht, "interval", sizeof("interval")-1);
5783 if (ht_entry) {
5784 if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_interval) {
5785 php_interval_obj *interval_obj;
5786 interval_obj = Z_PHPINTERVAL_P(ht_entry);
5787
5788 if (!interval_obj->initialized) {
5789 return 0;
5790 }
5791
5792 if (period_obj->interval != NULL) {
5793 timelib_rel_time_dtor(period_obj->interval);
5794 }
5795 period_obj->interval = timelib_rel_time_clone(interval_obj->diff);
5796 } else { /* interval is required */
5797 return 0;
5798 }
5799 } else {
5800 return 0;
5801 }
5802
5803 ht_entry = zend_hash_str_find(myht, "recurrences", sizeof("recurrences")-1);
5804 if (ht_entry &&
5805 Z_TYPE_P(ht_entry) == IS_LONG && Z_LVAL_P(ht_entry) >= 0 && Z_LVAL_P(ht_entry) <= INT_MAX) {
5806 period_obj->recurrences = Z_LVAL_P(ht_entry);
5807 } else {
5808 return 0;
5809 }
5810
5811 ht_entry = zend_hash_str_find(myht, "include_start_date", sizeof("include_start_date")-1);
5812 if (ht_entry &&
5813 (Z_TYPE_P(ht_entry) == IS_FALSE || Z_TYPE_P(ht_entry) == IS_TRUE)) {
5814 period_obj->include_start_date = (Z_TYPE_P(ht_entry) == IS_TRUE);
5815 } else {
5816 return 0;
5817 }
5818
5819 ht_entry = zend_hash_str_find(myht, "include_end_date", sizeof("include_end_date")-1);
5820 if (ht_entry &&
5821 (Z_TYPE_P(ht_entry) == IS_FALSE || Z_TYPE_P(ht_entry) == IS_TRUE)) {
5822 period_obj->include_end_date = (Z_TYPE_P(ht_entry) == IS_TRUE);
5823 } else {
5824 return 0;
5825 }
5826
5827 period_obj->initialized = 1;
5828
5829 return 1;
5830 } /* }}} */
5831
5832 /* {{{ */
PHP_METHOD(DatePeriod,__set_state)5833 PHP_METHOD(DatePeriod, __set_state)
5834 {
5835 php_period_obj *period_obj;
5836 zval *array;
5837 HashTable *myht;
5838
5839 ZEND_PARSE_PARAMETERS_START(1, 1)
5840 Z_PARAM_ARRAY(array)
5841 ZEND_PARSE_PARAMETERS_END();
5842
5843 myht = Z_ARRVAL_P(array);
5844
5845 object_init_ex(return_value, date_ce_period);
5846 period_obj = Z_PHPPERIOD_P(return_value);
5847 if (!php_date_period_initialize_from_hash(period_obj, myht)) {
5848 zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
5849 RETURN_THROWS();
5850 }
5851 }
5852 /* }}} */
5853
5854 /* {{{ */
PHP_METHOD(DatePeriod,__serialize)5855 PHP_METHOD(DatePeriod, __serialize)
5856 {
5857 zval *object = ZEND_THIS;
5858 php_period_obj *period_obj;
5859 HashTable *myht;
5860
5861 ZEND_PARSE_PARAMETERS_NONE();
5862
5863 period_obj = Z_PHPPERIOD_P(object);
5864 DATE_CHECK_INITIALIZED(period_obj->start, Z_OBJCE_P(object));
5865
5866 array_init(return_value);
5867 myht = Z_ARRVAL_P(return_value);
5868 date_period_object_to_hash(period_obj, myht);
5869
5870 add_common_properties(myht, &period_obj->std);
5871 }
5872 /* }}} */
5873
5874 /* {{{ date_period_is_internal_property
5875 * Common for date_period_read_property(), date_period_write_property(), and
5876 * restore_custom_dateperiod_properties functions
5877 */
date_period_is_internal_property(zend_string * name)5878 static bool date_period_is_internal_property(zend_string *name)
5879 {
5880 if (
5881 zend_string_equals_literal(name, "start") ||
5882 zend_string_equals_literal(name, "current") ||
5883 zend_string_equals_literal(name, "end") ||
5884 zend_string_equals_literal(name, "interval") ||
5885 zend_string_equals_literal(name, "recurrences") ||
5886 zend_string_equals_literal(name, "include_start_date") ||
5887 zend_string_equals_literal(name, "include_end_date")
5888 ) {
5889 return 1;
5890 }
5891 return 0;
5892 }
5893 /* }}} */
5894
restore_custom_dateperiod_properties(zval * object,HashTable * myht)5895 static void restore_custom_dateperiod_properties(zval *object, HashTable *myht)
5896 {
5897 zend_string *prop_name;
5898 zval *prop_val;
5899
5900 ZEND_HASH_FOREACH_STR_KEY_VAL(myht, prop_name, prop_val) {
5901 if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_period_is_internal_property(prop_name)) {
5902 continue;
5903 }
5904 update_property(Z_OBJ_P(object), prop_name, prop_val);
5905 } ZEND_HASH_FOREACH_END();
5906 }
5907
5908 /* {{{ */
PHP_METHOD(DatePeriod,__unserialize)5909 PHP_METHOD(DatePeriod, __unserialize)
5910 {
5911 zval *object = ZEND_THIS;
5912 php_period_obj *period_obj;
5913 zval *array;
5914 HashTable *myht;
5915
5916 ZEND_PARSE_PARAMETERS_START(1, 1)
5917 Z_PARAM_ARRAY(array)
5918 ZEND_PARSE_PARAMETERS_END();
5919
5920 period_obj = Z_PHPPERIOD_P(object);
5921 myht = Z_ARRVAL_P(array);
5922
5923 if (!php_date_period_initialize_from_hash(period_obj, myht)) {
5924 zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
5925 RETURN_THROWS();
5926 }
5927 restore_custom_dateperiod_properties(object, myht);
5928 }
5929 /* }}} */
5930
5931 /* {{{ */
PHP_METHOD(DatePeriod,__wakeup)5932 PHP_METHOD(DatePeriod, __wakeup)
5933 {
5934 zval *object = ZEND_THIS;
5935 php_period_obj *period_obj;
5936 HashTable *myht;
5937
5938 ZEND_PARSE_PARAMETERS_NONE();
5939
5940 period_obj = Z_PHPPERIOD_P(object);
5941
5942 myht = Z_OBJPROP_P(object);
5943
5944 if (!php_date_period_initialize_from_hash(period_obj, myht)) {
5945 zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
5946 RETURN_THROWS();
5947 }
5948
5949 restore_custom_dateperiod_properties(object, myht);
5950 }
5951 /* }}} */
5952
date_period_has_property(zend_object * object,zend_string * name,int type,void ** cache_slot)5953 static int date_period_has_property(zend_object *object, zend_string *name, int type, void **cache_slot)
5954 {
5955 zval rv;
5956 zval *prop;
5957
5958 if (!date_period_is_internal_property(name)) {
5959 return zend_std_has_property(object, name, type, cache_slot);
5960 }
5961
5962 php_period_obj *period_obj = php_period_obj_from_obj(object);
5963 if (!period_obj->initialized) {
5964 switch (type) {
5965 case ZEND_PROPERTY_ISSET: /* Intentional fallthrough */
5966 case ZEND_PROPERTY_NOT_EMPTY:
5967 return 0;
5968 case ZEND_PROPERTY_EXISTS:
5969 return 1;
5970 EMPTY_SWITCH_DEFAULT_CASE()
5971 }
5972 }
5973
5974 if (type == ZEND_PROPERTY_EXISTS) {
5975 return 1;
5976 }
5977
5978 prop = date_period_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
5979 ZEND_ASSERT(prop != &EG(uninitialized_zval));
5980
5981 bool result;
5982
5983 if (type == ZEND_PROPERTY_NOT_EMPTY) {
5984 result = zend_is_true(prop);
5985 } else if (type == ZEND_PROPERTY_ISSET) {
5986 result = Z_TYPE_P(prop) != IS_NULL;
5987 } else {
5988 ZEND_UNREACHABLE();
5989 }
5990
5991 zval_ptr_dtor(prop);
5992
5993 return result;
5994 }
5995
5996 /* {{{ date_period_read_property */
date_period_read_property(zend_object * object,zend_string * name,int type,void ** cache_slot,zval * rv)5997 static zval *date_period_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
5998 {
5999 if (date_period_is_internal_property(name)) {
6000 if (type == BP_VAR_IS || type == BP_VAR_R) {
6001 php_period_obj *period_obj = php_period_obj_from_obj(object);
6002
6003 if (zend_string_equals_literal(name, "start")) {
6004 create_date_period_datetime(period_obj->start, period_obj->start_ce, rv);
6005 return rv;
6006 } else if (zend_string_equals_literal(name, "current")) {
6007 create_date_period_datetime(period_obj->current, period_obj->start_ce, rv);
6008 return rv;
6009 } else if (zend_string_equals_literal(name, "end")) {
6010 create_date_period_datetime(period_obj->end, period_obj->start_ce, rv);
6011 return rv;
6012 } else if (zend_string_equals_literal(name, "interval")) {
6013 create_date_period_interval(period_obj->interval, rv);
6014 return rv;
6015 } else if (zend_string_equals_literal(name, "recurrences")) {
6016 ZVAL_LONG(rv, period_obj->recurrences);
6017 return rv;
6018 } else if (zend_string_equals_literal(name, "include_start_date")) {
6019 ZVAL_BOOL(rv, period_obj->include_start_date);
6020 return rv;
6021 } else if (zend_string_equals_literal(name, "include_end_date")) {
6022 ZVAL_BOOL(rv, period_obj->include_end_date);
6023 return rv;
6024 }
6025 } else {
6026 zend_readonly_property_modification_error_ex("DatePeriod", ZSTR_VAL(name));
6027 return &EG(uninitialized_zval);
6028 }
6029 }
6030
6031 return zend_std_read_property(object, name, type, cache_slot, rv);
6032 }
6033 /* }}} */
6034
date_period_write_property(zend_object * object,zend_string * name,zval * value,void ** cache_slot)6035 static zval *date_period_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
6036 {
6037 if (date_period_is_internal_property(name)) {
6038 zend_readonly_property_modification_error_ex("DatePeriod", ZSTR_VAL(name));
6039 return value;
6040 }
6041
6042 return zend_std_write_property(object, name, value, cache_slot);
6043 }
6044
date_period_get_property_ptr_ptr(zend_object * object,zend_string * name,int type,void ** cache_slot)6045 static zval *date_period_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
6046 {
6047 if (date_period_is_internal_property(name)) {
6048 zend_readonly_property_modification_error_ex("DatePeriod", ZSTR_VAL(name));
6049 return &EG(error_zval);
6050 }
6051
6052 return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
6053 }
6054
date_period_get_properties_for(zend_object * object,zend_prop_purpose purpose)6055 static HashTable *date_period_get_properties_for(zend_object *object, zend_prop_purpose purpose)
6056 {
6057 php_period_obj *period_obj = php_period_obj_from_obj(object);
6058 HashTable *props = zend_array_dup(zend_std_get_properties(object));
6059 if (!period_obj->initialized) {
6060 return props;
6061 }
6062
6063 date_period_object_to_hash(period_obj, props);
6064
6065 return props;
6066 }
6067
date_period_unset_property(zend_object * object,zend_string * name,void ** cache_slot)6068 static void date_period_unset_property(zend_object *object, zend_string *name, void **cache_slot)
6069 {
6070 if (date_period_is_internal_property(name)) {
6071 zend_throw_error(NULL, "Cannot unset %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
6072 return;
6073 }
6074
6075 zend_std_unset_property(object, name, cache_slot);
6076 }
6077