Coverage Report

Created: 2026-02-14 06:52

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