Coverage Report

Created: 2026-07-25 06:39

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