Coverage Report

Created: 2025-09-27 06:26

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