Coverage Report

Created: 2026-09-14 06:25

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