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