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