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