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