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