Coverage Report

Created: 2025-11-16 06:23

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