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