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