Coverage Report

Created: 2026-04-01 06:49

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.35k
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
133k
#define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
54
#endif
55
56
PHPAPI time_t php_time(void)
57
93
{
58
93
#ifdef HAVE_GETTIMEOFDAY
59
93
  struct timeval tm;
60
61
93
  if (UNEXPECTED(gettimeofday(&tm, NULL) != SUCCESS)) {
62
    /* fallback, can't reasonably happen */
63
0
    return time(NULL);
64
0
  }
65
66
93
  return tm.tv_sec;
67
#else
68
  return time(NULL);
69
#endif
70
93
}
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
45
  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.21k
{
403
5.21k
  timelib_tzinfo *tzi = (timelib_tzinfo*)Z_PTR_P(zv);
404
405
5.21k
  timelib_tzinfo_dtor(tzi);
406
5.21k
} /* }}} */
407
408
/* {{{ PHP_RINIT_FUNCTION */
409
PHP_RINIT_FUNCTION(date)
410
224k
{
411
224k
  if (DATEG(timezone)) {
412
0
    efree(DATEG(timezone));
413
0
  }
414
224k
  DATEG(timezone) = NULL;
415
224k
  DATEG(tzcache) = NULL;
416
224k
  DATEG(last_errors) = NULL;
417
418
224k
  return SUCCESS;
419
224k
}
420
/* }}} */
421
422
/* {{{ PHP_RSHUTDOWN_FUNCTION */
423
PHP_RSHUTDOWN_FUNCTION(date)
424
224k
{
425
224k
  if (DATEG(timezone)) {
426
5
    efree(DATEG(timezone));
427
5
  }
428
224k
  DATEG(timezone) = NULL;
429
430
224k
  return SUCCESS;
431
224k
}
432
/* }}} */
433
434
ZEND_MODULE_POST_ZEND_DEACTIVATE_D(date)
435
224k
{
436
224k
  if (DATEG(tzcache)) {
437
44.9k
    zend_hash_destroy(DATEG(tzcache));
438
44.9k
    FREE_HASHTABLE(DATEG(tzcache));
439
44.9k
    DATEG(tzcache) = NULL;
440
44.9k
  }
441
442
224k
  if (DATEG(last_errors)) {
443
17.7k
    timelib_error_container_dtor(DATEG(last_errors));
444
17.7k
    DATEG(last_errors) = NULL;
445
17.7k
  }
446
447
224k
  return SUCCESS;
448
224k
}
449
450
442k
#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
315k
{
503
315k
  timelib_tzinfo *tzi;
504
315k
  int dummy_error_code;
505
506
315k
  if(!DATEG(tzcache)) {
507
44.9k
    ALLOC_HASHTABLE(DATEG(tzcache));
508
44.9k
    zend_hash_init(DATEG(tzcache), 4, NULL, _php_date_tzinfo_dtor, 0);
509
44.9k
  }
510
511
315k
  if ((tzi = zend_hash_str_find_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname))) != NULL) {
512
68.3k
    return tzi;
513
68.3k
  }
514
515
247k
  tzi = timelib_parse_tzfile(formal_tzname, tzdb, &dummy_error_code);
516
247k
  if (tzi) {
517
5.21k
    zend_hash_str_add_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname), tzi);
518
5.21k
  }
519
247k
  return tzi;
520
315k
}
521
522
static timelib_tzinfo *php_date_parse_tzfile_wrapper(const char *formal_tzname, const timelib_tzdb *tzdb, int *dummy_error_code)
523
311k
{
524
311k
  return php_date_parse_tzfile(formal_tzname, tzdb);
525
311k
}
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
4.07k
{
553
  /* Checking whether timezone has been set with date_default_timezone_set() */
554
4.07k
  if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
555
1
    return DATEG(timezone);
556
1
  }
557
  /* Check config setting for default timezone */
558
4.07k
  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
4.07k
  } else if (*DATEG(default_timezone)) {
567
4.07k
    return DATEG(default_timezone);
568
4.07k
  }
569
  /* Fallback to UTC */
570
0
  return "UTC";
571
4.07k
}
572
573
PHPAPI timelib_tzinfo *get_timezone_info(void)
574
4.07k
{
575
4.07k
  timelib_tzinfo *tzi;
576
577
4.07k
  const char *tz = guess_timezone(DATE_TIMEZONEDB);
578
4.07k
  tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
579
4.07k
  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
4.07k
  return tzi;
583
4.07k
}
584
585
static void update_property(zend_object *object, zend_string *key, zval *prop_val)
586
30.2k
{
587
30.2k
  if (ZSTR_LEN(key) > 0 && ZSTR_VAL(key)[0] == '\0') { // not public
588
16.0k
    const char *class_name, *prop_name;
589
16.0k
    size_t prop_name_len;
590
591
16.0k
    if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
592
14.8k
      if (class_name[0] != '*') { // private
593
309
        zend_string *cname;
594
309
        zend_class_entry *ce;
595
596
309
        cname = zend_string_init(class_name, strlen(class_name), 0);
597
309
        ce = zend_lookup_class(cname);
598
599
309
        if (ce) {
600
6
          zend_update_property(ce, object, prop_name, prop_name_len, prop_val);
601
6
        }
602
603
309
        zend_string_release_ex(cname, 0);
604
14.5k
      } else { // protected
605
14.5k
        zend_update_property(object->ce, object, prop_name, prop_name_len, prop_val);
606
14.5k
      }
607
14.8k
    }
608
16.0k
    return;
609
16.0k
  }
610
611
  // public
612
14.1k
  zend_update_property(object->ce, object, ZSTR_VAL(key), ZSTR_LEN(key), prop_val);
613
14.1k
}
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
457
{
640
457
  if (number >= 10 && number <= 19) {
641
2
    return "th";
642
455
  } else {
643
455
    switch (number % 10) {
644
417
      case 1: return "st";
645
2
      case 2: return "nd";
646
0
      case 3: return "rd";
647
455
    }
648
455
  }
649
36
  return "th";
650
457
}
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.67k
{
656
1.67k
  timelib_sll day_of_week = timelib_day_of_week(y, m, d);
657
1.67k
  if (day_of_week < 0) {
658
0
    return "Unknown";
659
0
  }
660
1.67k
  return day_full_names[day_of_week];
661
1.67k
}
662
663
static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
664
4.14k
{
665
4.14k
  timelib_sll day_of_week = timelib_day_of_week(y, m, d);
666
4.14k
  if (day_of_week < 0) {
667
0
    return "Unknown";
668
0
  }
669
4.14k
  return day_short_names[day_of_week];
670
4.14k
}
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
289
{
676
289
  smart_str            string = {0};
677
289
  size_t               i;
678
289
  int                  length = 0;
679
289
  char                 buffer[97];
680
289
  timelib_time_offset *offset = NULL;
681
289
  timelib_sll          isoweek, isoyear;
682
289
  bool                 rfc_colon;
683
289
  int                  weekYearSet = 0;
684
685
289
  if (!format_len) {
686
6
    return ZSTR_EMPTY_ALLOC();
687
6
  }
688
689
283
  if (localtime) {
690
283
    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
281
    } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
697
4
      offset = timelib_time_offset_ctor();
698
4
      offset->offset = (t->z);
699
4
      offset->leap_secs = 0;
700
4
      offset->is_dst = 0;
701
4
      offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
702
4
      snprintf(offset->abbr, 9, "GMT%c%02d%02d",
703
4
                                (offset->offset < 0) ? '-' : '+',
704
4
                                abs(offset->offset / 3600),
705
4
                                abs((offset->offset % 3600) / 60));
706
277
    } else if (t->zone_type == TIMELIB_ZONETYPE_ID) {
707
277
      offset = timelib_get_time_zone_info(t->sse, t->tz_info);
708
277
    } else {
709
      /* Shouldn't happen, but code defensively */
710
0
      offset = timelib_time_offset_ctor();
711
0
    }
712
283
  }
713
714
275k
  for (i = 0; i < format_len; i++) {
715
275k
    rfc_colon = false;
716
275k
    switch (format[i]) {
717
      /* day */
718
2.70k
      case 'd': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
719
1.01k
      case 'D': length = slprintf(buffer, sizeof(buffer), "%s", php_date_short_day_name(t->y, t->m, t->d)); break;
720
583
      case 'j': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
721
1.67k
      case 'l': length = slprintf(buffer, sizeof(buffer), "%s", php_date_full_day_name(t->y, t->m, t->d)); break;
722
457
      case 'S': length = slprintf(buffer, sizeof(buffer), "%s", english_suffix(t->d)); break;
723
579
      case 'w': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_week(t->y, t->m, t->d)); break;
724
676
      case 'N': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_iso_day_of_week(t->y, t->m, t->d)); break;
725
536
      case 'z': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_year(t->y, t->m, t->d)); break;
726
727
      /* week */
728
870
      case 'W':
729
870
        if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
730
870
        length = slprintf(buffer, sizeof(buffer), "%02d", (int) isoweek); break; /* iso weeknr */
731
4.16k
      case 'o':
732
4.16k
        if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
733
4.16k
        length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) isoyear); break; /* iso year */
734
735
      /* month */
736
548
      case 'F': length = slprintf(buffer, sizeof(buffer), "%s", mon_full_names[t->m - 1]); break;
737
2.46k
      case 'm': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
738
459
      case 'M': length = slprintf(buffer, sizeof(buffer), "%s", mon_short_names[t->m - 1]); break;
739
3.02k
      case 'n': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
740
3.99k
      case 't': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_days_in_month(t->y, t->m)); break;
741
742
      /* year */
743
410
      case 'L': length = slprintf(buffer, sizeof(buffer), "%d", timelib_is_leap((int) t->y)); break;
744
989
      case 'y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) (t->y % 100)); break;
745
641
      case 'Y': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
746
331
      case 'x': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : (t->y >= 10000 ? "+" : ""), php_date_llabs((timelib_sll) t->y)); break;
747
386
      case 'X': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "+", php_date_llabs((timelib_sll) t->y)); break;
748
749
      /* time */
750
4.20k
      case 'a': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "pm" : "am"); break;
751
2.63k
      case 'A': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "PM" : "AM"); break;
752
532
      case 'B': {
753
532
        int retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
754
532
        if (retval < 0) {
755
12
          retval += 864000;
756
12
        }
757
        /* Make sure to do this on a positive int to avoid rounding errors */
758
532
        retval = (retval / 864)  % 1000;
759
532
        length = slprintf(buffer, sizeof(buffer), "%03d", retval);
760
532
        break;
761
0
      }
762
801
      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.21k
      case 'h': length = slprintf(buffer, sizeof(buffer), "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
765
967
      case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
766
3.51k
      case 'i': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
767
2.62k
      case 's': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->s); break;
768
2.46k
      case 'u': length = slprintf(buffer, sizeof(buffer), "%06d", (int) floor(t->us)); break;
769
749
      case 'v': length = slprintf(buffer, sizeof(buffer), "%03d", (int) floor(t->us / 1000)); break;
770
771
      /* timezone */
772
832
      case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
773
3.17k
      case 'p':
774
3.17k
        if (!localtime || strcmp(offset->abbr, "UTC") == 0 || strcmp(offset->abbr, "Z") == 0 || strcmp(offset->abbr, "GMT+0000") == 0) {
775
3.17k
          length = slprintf(buffer, sizeof(buffer), "%s", "Z");
776
3.17k
          break;
777
3.17k
        }
778
0
        ZEND_FALLTHROUGH;
779
1.27k
      case 'P': rfc_colon = true; ZEND_FALLTHROUGH;
780
2.03k
      case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
781
2.03k
                      localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
782
2.03k
                      localtime ? abs(offset->offset / 3600) : 0,
783
2.03k
                      rfc_colon ? ":" : "",
784
2.03k
                      localtime ? abs((offset->offset % 3600) / 60) : 0
785
2.03k
                );
786
2.03k
            break;
787
1.10k
      case 'T': length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); break;
788
6.41k
      case 'e': if (!localtime) {
789
0
                length = slprintf(buffer, sizeof(buffer), "%s", "UTC");
790
6.41k
            } else {
791
6.41k
              switch (t->zone_type) {
792
6.41k
                case TIMELIB_ZONETYPE_ID:
793
6.41k
                  length = slprintf(buffer, sizeof(buffer), "%s", t->tz_info->name);
794
6.41k
                  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.41k
              }
817
6.41k
            }
818
6.41k
            break;
819
6.41k
      case 'Z': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); break;
820
821
      /* full date/time */
822
2.50k
      case 'c': length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
823
2.50k
                              (zend_long) t->y, (int) t->m, (int) t->d,
824
2.50k
                      (int) t->h, (int) t->i, (int) t->s,
825
2.50k
                      localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
826
2.50k
                      localtime ? abs(offset->offset / 3600) : 0,
827
2.50k
                      localtime ? abs((offset->offset % 3600) / 60) : 0
828
2.50k
                );
829
2.50k
            break;
830
3.12k
      case 'r': length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d",
831
3.12k
                              php_date_short_day_name(t->y, t->m, t->d),
832
3.12k
                      (int) t->d, mon_short_names[t->m - 1],
833
3.12k
                      (zend_long) t->y, (int) t->h, (int) t->i, (int) t->s,
834
3.12k
                      localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
835
3.12k
                      localtime ? abs(offset->offset / 3600) : 0,
836
3.12k
                      localtime ? abs((offset->offset % 3600) / 60) : 0
837
3.12k
                );
838
3.12k
            break;
839
292
      case 'U': length = slprintf(buffer, sizeof(buffer), "%lld", (timelib_sll) t->sse); break;
840
841
861
      case '\\': if (i < format_len) i++; ZEND_FALLTHROUGH;
842
843
209k
      default: buffer[0] = format[i]; buffer[1] = '\0'; length = 1; break;
844
275k
    }
845
275k
    smart_str_appendl(&string, buffer, length);
846
275k
  }
847
848
283
  smart_str_0(&string);
849
850
283
  if (localtime) {
851
283
    timelib_time_offset_dtor(offset);
852
283
  }
853
854
283
  return string.s;
855
283
}
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
260
{
868
260
  zend_string *format;
869
260
  zend_long    ts;
870
260
  bool    ts_is_null = 1;
871
872
779
  ZEND_PARSE_PARAMETERS_START(1, 2)
873
1.03k
    Z_PARAM_STR(format)
874
259
    Z_PARAM_OPTIONAL
875
872
    Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
876
260
  ZEND_PARSE_PARAMETERS_END();
877
878
259
  if (ts_is_null) {
879
85
    ts = php_time();
880
85
  }
881
882
259
  RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
883
259
}
884
/* }}} */
885
886
PHPAPI zend_string *php_format_date(const char *format, size_t format_len, time_t ts, bool localtime) /* {{{ */
887
263
{
888
263
  timelib_time   *t;
889
263
  timelib_tzinfo *tzi;
890
263
  zend_string *string;
891
892
263
  t = timelib_time_ctor();
893
894
263
  if (localtime) {
895
263
    tzi = get_timezone_info();
896
263
    t->tz_info = tzi;
897
263
    t->zone_type = TIMELIB_ZONETYPE_ID;
898
263
    timelib_unixtime2local(t, ts);
899
263
  } else {
900
0
    tzi = NULL;
901
0
    timelib_unixtime2gmt(t, ts);
902
0
  }
903
904
263
  string = date_format(format, format_len, t, localtime);
905
906
263
  timelib_time_dtor(t);
907
263
  return string;
908
263
}
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
260
{
1012
260
  php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1013
260
}
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
9
{
1386
9
  ZEND_PARSE_PARAMETERS_NONE();
1387
1388
8
  RETURN_LONG((zend_long)php_time());
1389
8
}
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
96
{
1707
96
  if (implementor->type == ZEND_USER_CLASS &&
1708
64
    !instanceof_function(implementor, date_ce_date) &&
1709
0
    !instanceof_function(implementor, date_ce_immutable)
1710
96
  ) {
1711
0
    zend_error_noreturn(E_ERROR, "DateTimeInterface can't be implemented by user classes");
1712
0
  }
1713
1714
96
  return SUCCESS;
1715
96
} /* }}} */
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
318k
{
1832
318k
  php_date_obj *intern = zend_object_alloc(sizeof(php_date_obj), class_type);
1833
1834
318k
  zend_object_std_init(&intern->std, class_type);
1835
318k
  object_properties_init(&intern->std, class_type);
1836
1837
318k
  return &intern->std;
1838
318k
} /* }}} */
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.07k
{
1894
1.07k
  *table = NULL;
1895
1.07k
  *n = 0;
1896
1.07k
  return zend_std_get_properties(object);
1897
1.07k
} /* }}} */
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
4
{
1908
4
  int seconds = offset % 60;
1909
4
  size_t size;
1910
4
  const char *format;
1911
1912
4
  if (seconds == 0) {
1913
4
    size = sizeof("+05:00");
1914
4
    format = "%c%02d:%02d";
1915
4
  } else {
1916
0
    size = sizeof("+05:00:01");
1917
0
    format = "%c%02d:%02d:%02d";
1918
0
  }
1919
1920
4
  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
4
  ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format,
1924
4
    offset < 0 ? '-' : '+',
1925
4
    abs((int)(offset / 3600)),
1926
4
    abs((int)(offset % 3600) / 60),
1927
4
    abs(seconds));
1928
1929
4
  return tmpstr;
1930
4
}
1931
1932
static void date_object_to_hash(php_date_obj *dateobj, HashTable *props)
1933
26
{
1934
26
  zval zv;
1935
1936
  /* first we add the date and time in ISO format */
1937
26
  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
26
  zend_hash_str_update(props, "date", sizeof("date")-1, &zv);
1939
1940
  /* then we add the timezone name (or similar) */
1941
26
  if (dateobj->time->is_localtime) {
1942
26
    ZVAL_LONG(&zv, dateobj->time->zone_type);
1943
26
    zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
1944
1945
26
    switch (dateobj->time->zone_type) {
1946
20
      case TIMELIB_ZONETYPE_ID:
1947
20
        ZVAL_STRING(&zv, dateobj->time->tz_info->name);
1948
20
        break;
1949
4
      case TIMELIB_ZONETYPE_OFFSET:
1950
4
        ZVAL_NEW_STR(&zv, date_create_tz_offset_str(dateobj->time->z));
1951
4
        break;
1952
2
      case TIMELIB_ZONETYPE_ABBR:
1953
2
        ZVAL_STRING(&zv, dateobj->time->tz_abbr);
1954
2
        break;
1955
26
    }
1956
26
    zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
1957
26
  }
1958
26
}
1959
1960
static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_purpose purpose) /* {{{ */
1961
22
{
1962
22
  HashTable *props;
1963
22
  php_date_obj *dateobj;
1964
1965
22
  switch (purpose) {
1966
9
    case ZEND_PROP_PURPOSE_DEBUG:
1967
9
    case ZEND_PROP_PURPOSE_SERIALIZE:
1968
9
    case ZEND_PROP_PURPOSE_VAR_EXPORT:
1969
9
    case ZEND_PROP_PURPOSE_JSON:
1970
22
    case ZEND_PROP_PURPOSE_ARRAY_CAST:
1971
22
      break;
1972
0
    default:
1973
0
      return zend_std_get_properties_for(object, purpose);
1974
22
  }
1975
1976
22
  dateobj = php_date_obj_from_obj(object);
1977
22
  props = zend_array_dup(zend_std_get_properties(object));
1978
22
  if (!dateobj->time) {
1979
0
    return props;
1980
0
  }
1981
1982
22
  date_object_to_hash(dateobj, props);
1983
1984
22
  return props;
1985
22
} /* }}} */
1986
1987
static zend_object *date_object_new_timezone(zend_class_entry *class_type) /* {{{ */
1988
67.3k
{
1989
67.3k
  php_timezone_obj *intern = zend_object_alloc(sizeof(php_timezone_obj), class_type);
1990
1991
67.3k
  zend_object_std_init(&intern->std, class_type);
1992
67.3k
  object_properties_init(&intern->std, class_type);
1993
1994
67.3k
  return &intern->std;
1995
67.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
314k
{
2132
314k
  php_interval_obj *intern = zend_object_alloc(sizeof(php_interval_obj), class_type);
2133
2134
314k
  zend_object_std_init(&intern->std, class_type);
2135
314k
  object_properties_init(&intern->std, class_type);
2136
2137
314k
  return &intern->std;
2138
314k
} /* }}} */
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
424k
{
2161
2162
424k
  *table = NULL;
2163
424k
  *n = 0;
2164
424k
  return zend_std_get_properties(object);
2165
424k
} /* }}} */
2166
2167
static void date_interval_object_to_hash(php_interval_obj *intervalobj, HashTable *props)
2168
10
{
2169
10
  zval zv;
2170
2171
  /* Records whether this is a special relative interval that needs to be recreated from a string */
2172
10
  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
10
#define PHP_DATE_INTERVAL_ADD_PROPERTY(n,f) \
2181
80
  ZVAL_LONG(&zv, (zend_long)intervalobj->diff->f); \
2182
10
  zend_hash_str_update(props, n, sizeof(n)-1, &zv);
2183
2184
10
  PHP_DATE_INTERVAL_ADD_PROPERTY("y", y);
2185
10
  PHP_DATE_INTERVAL_ADD_PROPERTY("m", m);
2186
10
  PHP_DATE_INTERVAL_ADD_PROPERTY("d", d);
2187
10
  PHP_DATE_INTERVAL_ADD_PROPERTY("h", h);
2188
10
  PHP_DATE_INTERVAL_ADD_PROPERTY("i", i);
2189
10
  PHP_DATE_INTERVAL_ADD_PROPERTY("s", s);
2190
10
  ZVAL_DOUBLE(&zv, (double)intervalobj->diff->us / 1000000.0);
2191
10
  zend_hash_str_update(props, "f", sizeof("f") - 1, &zv);
2192
10
  PHP_DATE_INTERVAL_ADD_PROPERTY("invert", invert);
2193
10
  if (intervalobj->diff->days != TIMELIB_UNSET) {
2194
10
    PHP_DATE_INTERVAL_ADD_PROPERTY("days", days);
2195
10
  } else {
2196
0
    ZVAL_FALSE(&zv);
2197
0
    zend_hash_str_update(props, "days", sizeof("days")-1, &zv);
2198
0
  }
2199
10
  ZVAL_BOOL(&zv, (bool)intervalobj->from_string);
2200
10
  zend_hash_str_update(props, "from_string", strlen("from_string"), &zv);
2201
2202
10
#undef PHP_DATE_INTERVAL_ADD_PROPERTY
2203
10
}
2204
2205
static HashTable *date_object_get_properties_interval(zend_object *object) /* {{{ */
2206
10
{
2207
10
  HashTable *props;
2208
10
  php_interval_obj *intervalobj;
2209
2210
10
  intervalobj = php_interval_obj_from_obj(object);
2211
10
  props = zend_std_get_properties(object);
2212
10
  if (!intervalobj->initialized) {
2213
0
    return props;
2214
0
  }
2215
2216
10
  date_interval_object_to_hash(intervalobj, props);
2217
2218
10
  return props;
2219
10
} /* }}} */
2220
2221
static zend_object *date_object_new_period(zend_class_entry *class_type) /* {{{ */
2222
1.99k
{
2223
1.99k
  php_period_obj *intern = zend_object_alloc(sizeof(php_period_obj), class_type);
2224
2225
1.99k
  zend_object_std_init(&intern->std, class_type);
2226
1.99k
  object_properties_init(&intern->std, class_type);
2227
2228
1.99k
  return &intern->std;
2229
1.99k
} /* }}} */
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
318k
{
2260
318k
  php_date_obj *intern = php_date_obj_from_obj(object);
2261
2262
318k
  if (intern->time) {
2263
3.97k
    timelib_time_dtor(intern->time);
2264
3.97k
  }
2265
2266
318k
  zend_object_std_dtor(&intern->std);
2267
318k
} /* }}} */
2268
2269
static void date_object_free_storage_timezone(zend_object *object) /* {{{ */
2270
67.3k
{
2271
67.3k
  php_timezone_obj *intern = php_timezone_obj_from_obj(object);
2272
2273
67.3k
  if (intern->type == TIMELIB_ZONETYPE_ABBR) {
2274
423
    timelib_free(intern->tzi.z.abbr);
2275
423
  }
2276
67.3k
  zend_object_std_dtor(&intern->std);
2277
67.3k
} /* }}} */
2278
2279
static void date_object_free_storage_interval(zend_object *object) /* {{{ */
2280
314k
{
2281
314k
  php_interval_obj *intern = php_interval_obj_from_obj(object);
2282
2283
314k
  if (intern->date_string) {
2284
58
    zend_string_release(intern->date_string);
2285
58
    intern->date_string = NULL;
2286
58
  }
2287
314k
  timelib_rel_time_dtor(intern->diff);
2288
314k
  zend_object_std_dtor(&intern->std);
2289
314k
} /* }}} */
2290
2291
static void date_object_free_storage_period(zend_object *object) /* {{{ */
2292
1.99k
{
2293
1.99k
  php_period_obj *intern = php_period_obj_from_obj(object);
2294
2295
1.99k
  if (intern->start) {
2296
7
    timelib_time_dtor(intern->start);
2297
7
  }
2298
2299
1.99k
  if (intern->current) {
2300
0
    timelib_time_dtor(intern->current);
2301
0
  }
2302
2303
1.99k
  if (intern->end) {
2304
0
    timelib_time_dtor(intern->end);
2305
0
  }
2306
2307
1.99k
  timelib_rel_time_dtor(intern->interval);
2308
1.99k
  zend_object_std_dtor(&intern->std);
2309
1.99k
} /* }}} */
2310
2311
static void add_common_properties(HashTable *myht, zend_object *zobj)
2312
4
{
2313
4
  HashTable *common;
2314
4
  zend_string *name;
2315
4
  zval *prop;
2316
2317
4
  common = zend_std_get_properties(zobj);
2318
2319
4
  ZEND_HASH_FOREACH_STR_KEY_VAL_IND(common, name, prop) {
2320
4
    if (zend_hash_add(myht, name, prop) != NULL) {
2321
0
      Z_TRY_ADDREF_P(prop);
2322
0
    }
2323
4
  } ZEND_HASH_FOREACH_END();
2324
4
}
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
14
{
2330
14
  object_init_ex(object, pce);
2331
14
  return object;
2332
14
} /* }}} */
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
310k
{
2338
310k
  if (DATEG(last_errors)) {
2339
288k
    timelib_error_container_dtor(DATEG(last_errors));
2340
288k
    DATEG(last_errors) = NULL;
2341
288k
  }
2342
2343
310k
  if (last_errors == NULL || (*last_errors) == NULL) {
2344
0
    return;
2345
0
  }
2346
2347
310k
  if ((*last_errors)->warning_count || (*last_errors)->error_count) {
2348
306k
    DATEG(last_errors) = *last_errors;
2349
306k
    return;
2350
306k
  }
2351
2352
3.95k
  timelib_error_container_dtor(*last_errors);
2353
3.95k
  *last_errors = NULL;
2354
3.95k
} /* }}} */
2355
2356
static void php_date_set_time_fraction(timelib_time *time, int microsecond)
2357
3.97k
{
2358
3.97k
  time->us = microsecond;
2359
3.97k
}
2360
2361
static void php_date_get_current_time_with_fraction(time_t *sec, suseconds_t *usec)
2362
3.96k
{
2363
3.96k
#ifdef HAVE_GETTIMEOFDAY
2364
3.96k
  struct timeval tp = {0}; /* For setting microsecond */
2365
2366
3.96k
  gettimeofday(&tp, NULL);
2367
3.96k
  *sec = tp.tv_sec;
2368
3.96k
  *usec = tp.tv_usec;
2369
#else
2370
  *sec = time(NULL);
2371
  *usec = 0;
2372
#endif
2373
3.96k
}
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
310k
{
2377
310k
  timelib_time   *now;
2378
310k
  timelib_tzinfo *tzi = NULL;
2379
310k
  timelib_error_container *err = NULL;
2380
310k
  int type = TIMELIB_ZONETYPE_ID, new_dst = 0;
2381
310k
  char *new_abbr = NULL;
2382
310k
  timelib_sll new_offset = 0;
2383
310k
  time_t sec;
2384
310k
  suseconds_t usec;
2385
310k
  int options = 0;
2386
2387
310k
  if (dateobj->time) {
2388
0
    timelib_time_dtor(dateobj->time);
2389
0
  }
2390
310k
  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
310k
  } else {
2396
310k
    if (time_str_len == 0) {
2397
3.65k
      time_str = "now";
2398
3.65k
      time_str_len = sizeof("now") - 1;
2399
3.65k
    }
2400
310k
    dateobj->time = timelib_strtotime(time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
2401
310k
  }
2402
2403
  /* update last errors and warnings */
2404
310k
  update_errors_warnings(&err);
2405
2406
  /* If called from a constructor throw an exception */
2407
310k
  if ((flags & PHP_DATE_INIT_CTOR) && err && err->error_count) {
2408
    /* spit out the first library error message, at least */
2409
306k
    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
306k
      err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
2411
306k
  }
2412
310k
  if (err && err->error_count) {
2413
306k
    timelib_time_dtor(dateobj->time);
2414
306k
    dateobj->time = 0;
2415
306k
    return 0;
2416
306k
  }
2417
2418
3.96k
  if (timezone_object) {
2419
155
    php_timezone_obj *tzobj;
2420
2421
155
    tzobj = Z_PHPTIMEZONE_P(timezone_object);
2422
155
    switch (tzobj->type) {
2423
129
      case TIMELIB_ZONETYPE_ID:
2424
129
        tzi = tzobj->tzi.tz;
2425
129
        break;
2426
0
      case TIMELIB_ZONETYPE_OFFSET:
2427
0
        new_offset = tzobj->tzi.utc_offset;
2428
0
        break;
2429
26
      case TIMELIB_ZONETYPE_ABBR:
2430
26
        new_offset = tzobj->tzi.z.utc_offset;
2431
26
        new_dst    = tzobj->tzi.z.dst;
2432
26
        new_abbr   = timelib_strdup(tzobj->tzi.z.abbr);
2433
26
        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
155
    }
2438
155
    type = tzobj->type;
2439
3.81k
  } else if (dateobj->time->tz_info) {
2440
2
    tzi = dateobj->time->tz_info;
2441
3.81k
  } else {
2442
3.81k
    tzi = get_timezone_info();
2443
3.81k
    if (!tzi) {
2444
0
      return 0;
2445
0
    }
2446
3.81k
  }
2447
2448
3.96k
  now = timelib_time_ctor();
2449
3.96k
  now->zone_type = type;
2450
3.96k
  switch (type) {
2451
3.93k
    case TIMELIB_ZONETYPE_ID:
2452
3.93k
      now->tz_info = tzi;
2453
3.93k
      break;
2454
0
    case TIMELIB_ZONETYPE_OFFSET:
2455
0
      now->z = new_offset;
2456
0
      break;
2457
26
    case TIMELIB_ZONETYPE_ABBR:
2458
26
      now->z = new_offset;
2459
26
      now->dst = new_dst;
2460
26
      now->tz_abbr = new_abbr;
2461
26
      break;
2462
3.96k
  }
2463
3.96k
  php_date_get_current_time_with_fraction(&sec, &usec);
2464
3.96k
  timelib_unixtime2local(now, (timelib_sll) sec);
2465
3.96k
  php_date_set_time_fraction(now, usec);
2466
2467
3.96k
  if (!format
2468
3.96k
   && time_str_len == sizeof("now") - 1
2469
3.67k
   && memcmp(time_str, "now", sizeof("now") - 1) == 0) {
2470
3.66k
    timelib_time_dtor(dateobj->time);
2471
3.66k
    dateobj->time = now;
2472
3.66k
    return 1;
2473
3.66k
  }
2474
2475
304
  options = TIMELIB_NO_CLONE;
2476
304
  if (flags & PHP_DATE_INIT_FORMAT) {
2477
0
    options |= TIMELIB_OVERRIDE_TIME;
2478
0
  }
2479
304
  timelib_fill_holes(dateobj->time, now, options);
2480
2481
304
  timelib_update_ts(dateobj->time, tzi);
2482
304
  timelib_update_from_sse(dateobj->time);
2483
2484
304
  dateobj->time->have_relative = 0;
2485
2486
304
  timelib_time_dtor(now);
2487
2488
304
  return 1;
2489
3.96k
} /* }}} */
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
310k
{
2642
310k
  zval *timezone_object = NULL;
2643
310k
  char *time_str = NULL;
2644
310k
  size_t time_str_len = 0;
2645
2646
931k
  ZEND_PARSE_PARAMETERS_START(0, 2)
2647
931k
    Z_PARAM_OPTIONAL
2648
1.24M
    Z_PARAM_STRING(time_str, time_str_len)
2649
1.54M
    Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
2650
310k
  ZEND_PARSE_PARAMETERS_END();
2651
2652
310k
  php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, PHP_DATE_INIT_CTOR);
2653
310k
}
2654
/* }}} */
2655
2656
/* {{{ Creates new DateTimeImmutable object */
2657
PHP_METHOD(DateTimeImmutable, __construct)
2658
81
{
2659
81
  zval *timezone_object = NULL;
2660
81
  char *time_str = NULL;
2661
81
  size_t time_str_len = 0;
2662
2663
243
  ZEND_PARSE_PARAMETERS_START(0, 2)
2664
243
    Z_PARAM_OPTIONAL
2665
268
    Z_PARAM_STRING(time_str, time_str_len)
2666
161
    Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
2667
81
  ZEND_PARSE_PARAMETERS_END();
2668
2669
80
  php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, PHP_DATE_INIT_CTOR);
2670
80
}
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
8
{
2837
8
  zval             *z_date;
2838
8
  zval             *z_timezone_type;
2839
8
  zval             *z_timezone;
2840
8
  zval              tmp_obj;
2841
8
  timelib_tzinfo   *tzi;
2842
2843
8
  z_date = zend_hash_str_find(myht, "date", sizeof("date")-1);
2844
8
  if (!z_date || Z_TYPE_P(z_date) != IS_STRING) {
2845
4
    return false;
2846
4
  }
2847
2848
4
  z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type")-1);
2849
4
  if (!z_timezone_type || Z_TYPE_P(z_timezone_type) != IS_LONG) {
2850
0
    return false;
2851
0
  }
2852
2853
4
  z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone")-1);
2854
4
  if (!z_timezone || Z_TYPE_P(z_timezone) != IS_STRING) {
2855
0
    return false;
2856
0
  }
2857
2858
4
  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
4
    case TIMELIB_ZONETYPE_ID: {
2870
4
      bool ret;
2871
4
      php_timezone_obj *tzobj;
2872
2873
4
      tzi = php_date_parse_tzfile(Z_STRVAL_P(z_timezone), DATE_TIMEZONEDB);
2874
2875
4
      if (tzi == NULL) {
2876
0
        return false;
2877
0
      }
2878
2879
4
      tzobj = Z_PHPTIMEZONE_P(php_date_instantiate(date_ce_timezone, &tmp_obj));
2880
4
      tzobj->type = TIMELIB_ZONETYPE_ID;
2881
4
      tzobj->tzi.tz = tzi;
2882
4
      tzobj->initialized = true;
2883
2884
4
      ret = php_date_initialize(*dateobj, Z_STRVAL_P(z_date), Z_STRLEN_P(z_date), NULL, &tmp_obj, 0);
2885
4
      zval_ptr_dtor(&tmp_obj);
2886
4
      return ret;
2887
4
    }
2888
4
  }
2889
0
  return false;
2890
4
} /* }}} */
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
4
{
2939
4
  zval             *object = ZEND_THIS;
2940
4
  php_date_obj     *dateobj;
2941
4
  HashTable        *myht;
2942
2943
4
  ZEND_PARSE_PARAMETERS_NONE();
2944
2945
4
  dateobj = Z_PHPDATE_P(object);
2946
4
  DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
2947
2948
4
  array_init(return_value);
2949
4
  myht = Z_ARRVAL_P(return_value);
2950
4
  date_object_to_hash(dateobj, myht);
2951
2952
4
  add_common_properties(myht, &dateobj->std);
2953
4
}
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
12
{
2978
12
  if (
2979
12
    zend_string_equals_literal(name, "date") ||
2980
8
    zend_string_equals_literal(name, "timezone_type") ||
2981
4
    zend_string_equals_literal(name, "timezone")
2982
12
  ) {
2983
12
    return true;
2984
12
  }
2985
0
  return false;
2986
12
}
2987
2988
static void restore_custom_datetime_properties(zval *object, const HashTable *myht)
2989
4
{
2990
4
  zend_string      *prop_name;
2991
4
  zval             *prop_val;
2992
2993
28
  ZEND_HASH_FOREACH_STR_KEY_VAL(myht, prop_name, prop_val) {
2994
28
    if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_time_is_internal_property(prop_name)) {
2995
12
      continue;
2996
12
    }
2997
0
    update_property(Z_OBJ_P(object), prop_name, prop_val);
2998
0
  } ZEND_HASH_FOREACH_END();
2999
4
}
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
4
    zend_throw_error(NULL, "Invalid serialization data for DateTime object");
3016
4
    RETURN_THROWS();
3017
4
  }
3018
3019
4
  restore_custom_datetime_properties(object, myht);
3020
4
}
3021
/* }}} */
3022
3023
/* {{{ */
3024
PHP_METHOD(DateTimeImmutable, __unserialize)
3025
0
{
3026
0
  zval             *object = ZEND_THIS;
3027
0
  php_date_obj     *dateobj;
3028
0
  HashTable        *myht;
3029
3030
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
3031
0
    Z_PARAM_ARRAY_HT(myht)
3032
0
  ZEND_PARSE_PARAMETERS_END();
3033
3034
0
  dateobj = Z_PHPDATE_P(object);
3035
3036
0
  if (!php_date_initialize_from_hash(&dateobj, myht)) {
3037
0
    zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
3038
0
    RETURN_THROWS();
3039
0
  }
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
16
{
3241
16
  php_date_obj *dateobj;
3242
16
  timelib_time *tmp_time;
3243
16
  timelib_error_container *err = NULL;
3244
3245
16
  dateobj = Z_PHPDATE_P(object);
3246
3247
16
  if (!(dateobj->time)) {
3248
0
    date_throw_uninitialized_error(Z_OBJCE_P(object));
3249
0
    return false;
3250
0
  }
3251
3252
16
  tmp_time = timelib_strtotime(modify, modify_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
3253
3254
  /* update last errors and warnings */
3255
16
  update_errors_warnings(&err);
3256
3257
16
  if (err && err->error_count) {
3258
    /* spit out the first library error message, at least */
3259
7
    php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
3260
7
      err->error_messages[0].position,
3261
7
      err->error_messages[0].character ? err->error_messages[0].character : ' ',
3262
7
      err->error_messages[0].message);
3263
7
    timelib_time_dtor(tmp_time);
3264
7
    return false;
3265
7
  }
3266
3267
9
  memcpy(&dateobj->time->relative, &tmp_time->relative, sizeof(timelib_rel_time));
3268
9
  dateobj->time->have_relative = tmp_time->have_relative;
3269
9
  dateobj->time->sse_uptodate = 0;
3270
3271
9
  if (tmp_time->y != TIMELIB_UNSET) {
3272
0
    dateobj->time->y = tmp_time->y;
3273
0
  }
3274
9
  if (tmp_time->m != TIMELIB_UNSET) {
3275
0
    dateobj->time->m = tmp_time->m;
3276
0
  }
3277
9
  if (tmp_time->d != TIMELIB_UNSET) {
3278
0
    dateobj->time->d = tmp_time->d;
3279
0
  }
3280
3281
9
  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
9
  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
9
  if (
3302
9
    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
9
  ) {
3307
0
    timelib_set_timezone_from_offset(dateobj->time, 0);
3308
0
  }
3309
3310
9
  timelib_time_dtor(tmp_time);
3311
3312
9
  timelib_update_ts(dateobj->time, NULL);
3313
9
  timelib_update_from_sse(dateobj->time);
3314
9
  dateobj->time->have_relative = 0;
3315
9
  memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
3316
3317
9
  return true;
3318
16
} /* }}} */
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
16
{
3342
16
  zval                *object;
3343
16
  char                *modify;
3344
16
  size_t               modify_len;
3345
16
  zend_error_handling  zeh;
3346
3347
16
  object = ZEND_THIS;
3348
48
  ZEND_PARSE_PARAMETERS_START(1, 1)
3349
64
    Z_PARAM_STRING(modify, modify_len)
3350
16
  ZEND_PARSE_PARAMETERS_END();
3351
3352
16
  zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
3353
16
  if (!php_date_modify(object, modify, modify_len)) {
3354
7
    zend_restore_error_handling(&zeh);
3355
7
    RETURN_THROWS();
3356
7
  }
3357
3358
9
  zend_restore_error_handling(&zeh);
3359
3360
9
  RETURN_OBJ_COPY(Z_OBJ_P(object));
3361
9
}
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
61.9k
{
3524
  /* Free abbreviation if already set */
3525
61.9k
  if (tzobj->initialized && tzobj->type == TIMELIB_ZONETYPE_ABBR) {
3526
0
    timelib_free(tzobj->tzi.z.abbr);
3527
0
  }
3528
3529
  /* Set new values */
3530
61.9k
  tzobj->initialized = true;
3531
61.9k
  tzobj->type = t->zone_type;
3532
3533
61.9k
  switch (t->zone_type) {
3534
61.5k
    case TIMELIB_ZONETYPE_ID:
3535
61.5k
      tzobj->tzi.tz = t->tz_info;
3536
61.5k
      break;
3537
0
    case TIMELIB_ZONETYPE_OFFSET:
3538
0
      tzobj->tzi.utc_offset = t->z;
3539
0
      break;
3540
423
    case TIMELIB_ZONETYPE_ABBR:
3541
423
      tzobj->tzi.z.utc_offset = t->z;
3542
423
      tzobj->tzi.z.dst = t->dst;
3543
423
      tzobj->tzi.z.abbr = timelib_strdup(t->tz_abbr);
3544
423
      break;
3545
61.9k
  }
3546
61.9k
}
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
10
{
3970
10
  zval         *object1, *object2;
3971
10
  php_date_obj *dateobj1, *dateobj2;
3972
10
  php_interval_obj *interval;
3973
10
  bool      absolute = false;
3974
3975
10
  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
10
  dateobj1 = Z_PHPDATE_P(object1);
3979
10
  dateobj2 = Z_PHPDATE_P(object2);
3980
10
  DATE_CHECK_INITIALIZED(dateobj1->time, Z_OBJCE_P(object1));
3981
10
  DATE_CHECK_INITIALIZED(dateobj2->time, Z_OBJCE_P(object2));
3982
3983
10
  php_date_instantiate(date_ce_interval, return_value);
3984
10
  interval = Z_PHPINTERVAL_P(return_value);
3985
10
  interval->diff = timelib_diff(dateobj1->time, dateobj2->time);
3986
10
  if (absolute) {
3987
0
    interval->diff->invert = 0;
3988
0
  }
3989
10
  interval->initialized = true;
3990
10
  interval->civil_or_wall = PHP_DATE_CIVIL;
3991
10
}
3992
/* }}} */
3993
3994
static bool timezone_initialize(php_timezone_obj *tzobj, const zend_string *tz_zstr, char **warning_message) /* {{{ */
3995
61.9k
{
3996
61.9k
  timelib_time  dummy_t = {0};
3997
61.9k
  int           dst, not_found;
3998
61.9k
  const char *tz = ZSTR_VAL(tz_zstr);
3999
4000
61.9k
  ZEND_ASSERT(!zend_str_has_nul_byte(tz_zstr) && "timezone should have been checked to not have null bytes");
4001
4002
61.9k
  dummy_t.z = timelib_parse_zone(&tz, &dst, &dummy_t, &not_found, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
4003
61.9k
  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
61.9k
  dummy_t.dst = dst;
4011
61.9k
  if (!not_found && (*tz != '\0')) {
4012
3
    if (warning_message) {
4013
3
      spprintf(warning_message, 0, "Unknown or bad timezone (%s)", ZSTR_VAL(tz_zstr));
4014
3
    }
4015
3
    timelib_free(dummy_t.tz_abbr);
4016
3
    return false;
4017
3
  }
4018
61.9k
  if (not_found) {
4019
1
    if (warning_message) {
4020
1
      spprintf(warning_message, 0, "Unknown or bad timezone (%s)", ZSTR_VAL(tz_zstr));
4021
1
    }
4022
1
    return false;
4023
61.9k
  } else {
4024
61.9k
    set_timezone_from_timelib_time(tzobj, &dummy_t);
4025
61.9k
    timelib_free(dummy_t.tz_abbr);
4026
61.9k
    return true;
4027
61.9k
  }
4028
61.9k
} /* }}} */
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.0k
{
4054
62.0k
  zend_string *tz;
4055
62.0k
  php_timezone_obj *tzobj;
4056
62.0k
  char *exception_message;
4057
4058
186k
  ZEND_PARSE_PARAMETERS_START(1, 1)
4059
247k
    Z_PARAM_PATH_STR(tz) /* To prevent null bytes */
4060
62.0k
  ZEND_PARSE_PARAMETERS_END();
4061
4062
61.9k
  tzobj = Z_PHPTIMEZONE_P(ZEND_THIS);
4063
61.9k
  if (!timezone_initialize(tzobj, tz, &exception_message)) {
4064
4
    zend_throw_exception_ex(date_ce_date_invalid_timezone_exception, 0, "DateTimeZone::__construct(): %s", exception_message);
4065
4
    efree(exception_message);
4066
4
    RETURN_THROWS();
4067
4
  }
4068
61.9k
}
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
    case TIMELIB_ZONETYPE_ABBR:
4270
0
      RETURN_LONG(tzobj->tzi.z.utc_offset + (tzobj->tzi.z.dst * 3600));
4271
0
  }
4272
0
}
4273
/* }}} */
4274
4275
/* {{{ Returns numerically indexed array containing associative array for all transitions in the specified range for the timezone. */
4276
PHP_FUNCTION(timezone_transitions_get)
4277
0
{
4278
0
  zval                *object, element;
4279
0
  php_timezone_obj    *tzobj;
4280
0
  uint64_t             begin = 0;
4281
0
  bool                 found;
4282
0
  zend_long            timestamp_begin = ZEND_LONG_MIN, timestamp_end = INT32_MAX;
4283
4284
0
  if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ll", &object, date_ce_timezone, &timestamp_begin, &timestamp_end) == FAILURE) {
4285
0
    RETURN_THROWS();
4286
0
  }
4287
0
  tzobj = Z_PHPTIMEZONE_P(object);
4288
0
  DATE_CHECK_INITIALIZED(tzobj->initialized, Z_OBJCE_P(object));
4289
0
  if (tzobj->type != TIMELIB_ZONETYPE_ID) {
4290
0
    RETURN_FALSE;
4291
0
  }
4292
4293
0
#define add_nominal() \
4294
0
    array_init_size(&element, 5); \
4295
0
    add_assoc_long(&element, "ts",     timestamp_begin); \
4296
0
    add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601_LARGE_YEAR, 13, timestamp_begin, 0)); \
4297
0
    add_assoc_long(&element, "offset", tzobj->tzi.tz->type[0].offset); \
4298
0
    add_assoc_bool(&element, "isdst",  tzobj->tzi.tz->type[0].isdst); \
4299
0
    add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[0].abbr_idx]); \
4300
0
    add_next_index_zval(return_value, &element);
4301
4302
0
#define add(i,ts) \
4303
0
    array_init_size(&element, 5); \
4304
0
    add_assoc_long(&element, "ts",     ts); \
4305
0
    add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601_LARGE_YEAR, 13, ts, 0)); \
4306
0
    add_assoc_long(&element, "offset", tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].offset); \
4307
0
    add_assoc_bool(&element, "isdst",  tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].isdst); \
4308
0
    add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].abbr_idx]); \
4309
0
    add_next_index_zval(return_value, &element);
4310
4311
0
#define add_by_index(i,ts) \
4312
0
    array_init_size(&element, 5); \
4313
0
    add_assoc_long(&element, "ts",     ts); \
4314
0
    add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601_LARGE_YEAR, 13, ts, 0)); \
4315
0
    add_assoc_long(&element, "offset", tzobj->tzi.tz->type[i].offset); \
4316
0
    add_assoc_bool(&element, "isdst",  tzobj->tzi.tz->type[i].isdst); \
4317
0
    add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[i].abbr_idx]); \
4318
0
    add_next_index_zval(return_value, &element);
4319
4320
0
#define add_from_tto(to,ts) \
4321
0
    array_init_size(&element, 5); \
4322
0
    add_assoc_long(&element, "ts",     ts); \
4323
0
    add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601_LARGE_YEAR, 13, ts, 0)); \
4324
0
    add_assoc_long(&element, "offset", (to)->offset); \
4325
0
    add_assoc_bool(&element, "isdst",  (to)->is_dst); \
4326
0
    add_assoc_string(&element, "abbr", (to)->abbr); \
4327
0
    add_next_index_zval(return_value, &element);
4328
4329
0
#define add_last() add(tzobj->tzi.tz->bit64.timecnt - 1, timestamp_begin)
4330
4331
0
  array_init(return_value);
4332
4333
0
  if (timestamp_begin == ZEND_LONG_MIN) {
4334
0
    add_nominal();
4335
0
    begin = 0;
4336
0
    found = true;
4337
0
  } else {
4338
0
    begin = 0;
4339
0
    found = false;
4340
0
    if (tzobj->tzi.tz->bit64.timecnt > 0) {
4341
0
      do {
4342
0
        if (tzobj->tzi.tz->trans[begin] > timestamp_begin) {
4343
0
          if (begin > 0) {
4344
0
            add(begin - 1, timestamp_begin);
4345
0
          } else {
4346
0
            add_nominal();
4347
0
          }
4348
0
          found = true;
4349
0
          break;
4350
0
        }
4351
0
        begin++;
4352
0
      } while (begin < tzobj->tzi.tz->bit64.timecnt);
4353
0
    }
4354
0
  }
4355
4356
0
  if (!found) {
4357
0
    if (tzobj->tzi.tz->bit64.timecnt > 0) {
4358
0
      if (tzobj->tzi.tz->posix_info && tzobj->tzi.tz->posix_info->dst_end) {
4359
0
        timelib_time_offset *tto = timelib_get_time_zone_info(timestamp_begin, tzobj->tzi.tz);
4360
0
        add_from_tto(tto, timestamp_begin);
4361
0
        timelib_time_offset_dtor(tto);
4362
0
      } else {
4363
0
        add_last();
4364
0
      }
4365
0
    } else {
4366
0
      add_nominal();
4367
0
    }
4368
0
  } else {
4369
0
    for (uint64_t i = begin; i < tzobj->tzi.tz->bit64.timecnt; ++i) {
4370
0
      if (tzobj->tzi.tz->trans[i] < timestamp_end) {
4371
0
        add(i, tzobj->tzi.tz->trans[i]);
4372
0
      } else {
4373
0
        return;
4374
0
      }
4375
0
    }
4376
0
  }
4377
0
  if (tzobj->tzi.tz->posix_info && tzobj->tzi.tz->posix_info->dst_end) {
4378
0
    timelib_sll start_y, end_y, dummy_m, dummy_d;
4379
0
    timelib_sll last_transition_ts = tzobj->tzi.tz->trans[tzobj->tzi.tz->bit64.timecnt - 1];
4380
4381
    /* Find out year for last transition */
4382
0
    timelib_unixtime2date(last_transition_ts, &start_y, &dummy_m, &dummy_d);
4383
4384
    /* Find out year for final boundary timestamp */
4385
0
    timelib_unixtime2date(timestamp_end, &end_y, &dummy_m, &dummy_d);
4386
4387
0
    for (timelib_sll i = start_y; i <= end_y; i++) {
4388
0
      timelib_posix_transitions transitions = { 0 };
4389
4390
0
      timelib_get_transitions_for_year(tzobj->tzi.tz, i, &transitions);
4391
4392
0
      for (size_t j = 0; j < transitions.count; j++) {
4393
0
        if (transitions.times[j] <= last_transition_ts) {
4394
0
          continue;
4395
0
        }
4396
0
        if (transitions.times[j] < timestamp_begin) {
4397
0
          continue;
4398
0
        }
4399
0
        if (transitions.times[j] > timestamp_end) {
4400
0
          return;
4401
0
        }
4402
0
        add_by_index(transitions.types[j], transitions.times[j]);
4403
0
      }
4404
0
    }
4405
0
  }
4406
0
}
4407
/* }}} */
4408
4409
/* {{{ Returns location information for a timezone, including country code, latitude/longitude and comments */
4410
PHP_FUNCTION(timezone_location_get)
4411
0
{
4412
0
  zval                *object;
4413
0
  php_timezone_obj    *tzobj;
4414
4415
0
  if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_timezone) == FAILURE) {
4416
0
    RETURN_THROWS();
4417
0
  }
4418
0
  tzobj = Z_PHPTIMEZONE_P(object);
4419
0
  DATE_CHECK_INITIALIZED(tzobj->initialized, Z_OBJCE_P(object));
4420
0
  if (tzobj->type != TIMELIB_ZONETYPE_ID) {
4421
0
    RETURN_FALSE;
4422
0
  }
4423
4424
0
  array_init(return_value);
4425
0
  add_assoc_string(return_value, "country_code", tzobj->tzi.tz->location.country_code);
4426
0
  add_assoc_double(return_value, "latitude", tzobj->tzi.tz->location.latitude);
4427
0
  add_assoc_double(return_value, "longitude", tzobj->tzi.tz->location.longitude);
4428
0
  add_assoc_string(return_value, "comments", tzobj->tzi.tz->location.comments);
4429
0
}
4430
/* }}} */
4431
4432
static bool date_interval_initialize(timelib_rel_time **rt, const char *format, size_t format_length) /* {{{ */
4433
35
{
4434
35
  timelib_time     *b = NULL, *e = NULL;
4435
35
  timelib_rel_time *p = NULL;
4436
35
  int               r = 0;
4437
35
  bool              retval = false;
4438
35
  timelib_error_container *errors;
4439
4440
35
  timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
4441
4442
35
  if (errors->error_count > 0) {
4443
9
    zend_throw_exception_ex(date_ce_date_malformed_interval_string_exception, 0, "Unknown or bad format (%s)", format);
4444
9
    retval = false;
4445
9
    if (p) {
4446
4
      timelib_rel_time_dtor(p);
4447
4
    }
4448
26
  } else {
4449
26
    if (p) {
4450
26
      *rt = p;
4451
26
      retval = true;
4452
26
    } else {
4453
0
      if (b && e) {
4454
0
        timelib_update_ts(b, NULL);
4455
0
        timelib_update_ts(e, NULL);
4456
0
        *rt = timelib_diff(b, e);
4457
0
        retval = true;
4458
0
      } else {
4459
0
        zend_throw_exception_ex(date_ce_date_malformed_interval_string_exception, 0, "Failed to parse interval (%s)", format);
4460
0
        retval = false;
4461
0
      }
4462
0
    }
4463
26
  }
4464
35
  timelib_error_container_dtor(errors);
4465
35
  timelib_free(b);
4466
35
  timelib_free(e);
4467
35
  return retval;
4468
35
} /* }}} */
4469
4470
static int date_interval_compare_objects(zval *o1, zval *o2)
4471
0
{
4472
0
  ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
4473
  /* There is no well defined way to compare intervals like P1M and P30D, which may compare
4474
   * smaller, equal or greater depending on the point in time at which the interval starts. As
4475
   * such, we treat DateInterval objects are non-comparable and emit a warning. */
4476
0
  zend_error(E_WARNING, "Cannot compare DateInterval objects");
4477
0
  return ZEND_UNCOMPARABLE;
4478
0
}
4479
4480
/* {{{ date_interval_read_property */
4481
static zval *date_interval_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
4482
11
{
4483
11
  php_interval_obj *obj;
4484
11
  zval *retval;
4485
11
  timelib_sll value = -1;
4486
11
  double      fvalue = -1;
4487
4488
11
  obj = php_interval_obj_from_obj(object);
4489
4490
11
  if (!obj->initialized) {
4491
0
    retval = zend_std_read_property(object, name, type, cache_slot, rv);
4492
0
    return retval;
4493
0
  }
4494
4495
11
#define GET_VALUE_FROM_STRUCT(n,m)            \
4496
88
  if (zend_string_equals_literal(name, m)) { \
4497
0
    value = obj->diff->n; \
4498
0
    break; \
4499
0
  }
4500
11
  do {
4501
11
    GET_VALUE_FROM_STRUCT(y, "y");
4502
11
    GET_VALUE_FROM_STRUCT(m, "m");
4503
11
    GET_VALUE_FROM_STRUCT(d, "d");
4504
11
    GET_VALUE_FROM_STRUCT(h, "h");
4505
11
    GET_VALUE_FROM_STRUCT(i, "i");
4506
11
    GET_VALUE_FROM_STRUCT(s, "s");
4507
11
    if (zend_string_equals_literal(name, "f")) {
4508
0
      fvalue = obj->diff->us / 1000000.0;
4509
0
      break;
4510
0
    }
4511
11
    GET_VALUE_FROM_STRUCT(invert, "invert");
4512
11
    GET_VALUE_FROM_STRUCT(days, "days");
4513
    /* didn't find any */
4514
11
    retval = zend_std_read_property(object, name, type, cache_slot, rv);
4515
4516
11
    return retval;
4517
11
  } while(0);
4518
4519
0
  retval = rv;
4520
4521
0
  if (fvalue != -1) {
4522
0
    ZVAL_DOUBLE(retval, fvalue);
4523
0
  } else if (value != TIMELIB_UNSET) {
4524
0
    ZVAL_LONG(retval, value);
4525
0
  } else {
4526
0
    ZVAL_FALSE(retval);
4527
0
  }
4528
4529
0
  return retval;
4530
11
}
4531
/* }}} */
4532
4533
/* {{{ date_interval_write_property */
4534
static zval *date_interval_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
4535
28.7k
{
4536
28.7k
  php_interval_obj *obj;
4537
4538
28.7k
  obj = php_interval_obj_from_obj(object);
4539
4540
28.7k
  if (!obj->initialized) {
4541
6
    return zend_std_write_property(object, name, value, cache_slot);
4542
6
  }
4543
4544
28.7k
#define SET_VALUE_FROM_STRUCT(n,m) \
4545
169k
  if (zend_string_equals_literal(name, m)) { \
4546
5.40k
    obj->diff->n = zval_get_long(value); \
4547
5.40k
    break; \
4548
5.40k
  }
4549
4550
28.7k
  do {
4551
28.7k
    SET_VALUE_FROM_STRUCT(y, "y");
4552
27.9k
    SET_VALUE_FROM_STRUCT(m, "m");
4553
27.9k
    SET_VALUE_FROM_STRUCT(d, "d");
4554
23.4k
    SET_VALUE_FROM_STRUCT(h, "h");
4555
23.3k
    SET_VALUE_FROM_STRUCT(i, "i");
4556
23.3k
    SET_VALUE_FROM_STRUCT(s, "s");
4557
23.3k
    if (zend_string_equals_literal(name, "f")) {
4558
8.96k
      obj->diff->us = zend_dval_to_lval(zval_get_double(value) * 1000000.0);
4559
8.96k
      break;
4560
8.96k
    }
4561
14.3k
    SET_VALUE_FROM_STRUCT(invert, "invert");
4562
    /* didn't find any */
4563
14.3k
    value = zend_std_write_property(object, name, value, cache_slot);
4564
14.3k
  } while(0);
4565
4566
28.7k
  return value;
4567
28.7k
}
4568
/* }}} */
4569
4570
/* {{{ date_interval_get_property_ptr_ptr */
4571
static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
4572
0
{
4573
0
  zval *ret;
4574
4575
0
  if (
4576
0
    zend_string_equals_literal(name, "y") ||
4577
0
    zend_string_equals_literal(name, "m") ||
4578
0
    zend_string_equals_literal(name, "d") ||
4579
0
    zend_string_equals_literal(name, "h") ||
4580
0
    zend_string_equals_literal(name, "i") ||
4581
0
    zend_string_equals_literal(name, "s") ||
4582
0
    zend_string_equals_literal(name, "f") ||
4583
0
    zend_string_equals_literal(name, "days") ||
4584
0
    zend_string_equals_literal(name, "invert") ) {
4585
    /* Fallback to read_property. */
4586
0
    if (cache_slot) {
4587
0
      cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
4588
0
    }
4589
0
    ret = NULL;
4590
0
  } else {
4591
0
    ret = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
4592
0
  }
4593
4594
0
  return ret;
4595
0
}
4596
/* }}} */
4597
4598
/* {{{ Creates new DateInterval object. */
4599
PHP_METHOD(DateInterval, __construct)
4600
39
{
4601
39
  zend_string *interval_string = NULL;
4602
39
  timelib_rel_time *reltime;
4603
4604
113
  ZEND_PARSE_PARAMETERS_START(1, 1)
4605
140
    Z_PARAM_STR(interval_string)
4606
39
  ZEND_PARSE_PARAMETERS_END();
4607
4608
35
  if (!date_interval_initialize(&reltime, ZSTR_VAL(interval_string), ZSTR_LEN(interval_string))) {
4609
9
    RETURN_THROWS();
4610
9
  }
4611
4612
26
  php_interval_obj *diobj = Z_PHPINTERVAL_P(ZEND_THIS);
4613
26
  diobj->diff = reltime;
4614
26
  diobj->initialized = 1;
4615
26
  diobj->civil_or_wall = PHP_DATE_WALL;
4616
26
}
4617
/* }}} */
4618
4619
static void php_date_interval_initialize_from_hash(php_interval_obj *intobj, const HashTable *myht) /* {{{ */
4620
312k
{
4621
  /* If we have a date_string, use that instead */
4622
312k
  const zval *date_str = zend_hash_str_find(myht, "date_string", strlen("date_string"));
4623
312k
  if (date_str && Z_TYPE_P(date_str) == IS_STRING) {
4624
61.5k
    timelib_time   *time;
4625
61.5k
    timelib_error_container *err = NULL;
4626
4627
61.5k
    time = timelib_strtotime(Z_STRVAL_P(date_str), Z_STRLEN_P(date_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
4628
4629
61.5k
    if (err->error_count > 0)  {
4630
61.5k
      zend_throw_error(NULL,
4631
61.5k
        "Unknown or bad format (%s) at position %d (%c) while unserializing: %s",
4632
61.5k
        Z_STRVAL_P(date_str),
4633
61.5k
        err->error_messages[0].position,
4634
61.5k
        err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
4635
61.5k
        timelib_time_dtor(time);
4636
61.5k
        timelib_error_container_dtor(err);
4637
61.5k
        return;
4638
61.5k
    }
4639
4640
    /* If ->diff is already set, then we need to free it first */
4641
58
    if (intobj->diff) {
4642
0
      timelib_rel_time_dtor(intobj->diff);
4643
0
    }
4644
4645
58
    intobj->diff = timelib_rel_time_clone(&time->relative);
4646
58
    intobj->initialized = true;
4647
58
    intobj->civil_or_wall = PHP_DATE_CIVIL;
4648
58
    intobj->from_string = true;
4649
58
    intobj->date_string = zend_string_copy(Z_STR_P(date_str));
4650
4651
58
    timelib_time_dtor(time);
4652
58
    timelib_error_container_dtor(err);
4653
4654
58
    return;
4655
61.5k
  }
4656
4657
  /* If ->diff is already set, then we need to free it first */
4658
250k
  if (intobj->diff) {
4659
0
    timelib_rel_time_dtor(intobj->diff);
4660
0
  }
4661
4662
  /* Set new value */
4663
250k
  intobj->diff = timelib_rel_time_ctor();
4664
4665
250k
#define PHP_DATE_INTERVAL_READ_PROPERTY(element, member, itype, def) \
4666
3.26M
  do { \
4667
3.26M
    zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
4668
3.26M
    if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
4669
68.4k
      intobj->diff->member = (itype)zval_get_long(z_arg); \
4670
3.19M
    } else { \
4671
3.19M
      intobj->diff->member = (itype)def; \
4672
3.19M
    } \
4673
3.26M
  } while (0);
4674
4675
250k
#define PHP_DATE_INTERVAL_READ_PROPERTY_I64(element, member) \
4676
250k
  do { \
4677
250k
    zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
4678
250k
    if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
4679
0
      zend_string *tmp_str; \
4680
0
      zend_string *str = zval_get_tmp_string(z_arg, &tmp_str); \
4681
0
      DATE_A64I(intobj->diff->member, ZSTR_VAL(str)); \
4682
0
      zend_tmp_string_release(tmp_str); \
4683
250k
    } else { \
4684
250k
      intobj->diff->member = -1LL; \
4685
250k
    } \
4686
250k
  } while (0);
4687
4688
250k
#define PHP_DATE_INTERVAL_READ_PROPERTY_DAYS(member) \
4689
250k
  do { \
4690
250k
    zval *z_arg = zend_hash_str_find(myht, "days", sizeof("days") - 1); \
4691
250k
    if (z_arg && Z_TYPE_P(z_arg) == IS_FALSE) { \
4692
7
      intobj->diff->member = TIMELIB_UNSET; \
4693
250k
    } else if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
4694
133k
      zend_string *tmp_str; \
4695
133k
      zend_string *str = zval_get_tmp_string(z_arg, &tmp_str); \
4696
133k
      DATE_A64I(intobj->diff->member, ZSTR_VAL(str)); \
4697
133k
      zend_tmp_string_release(tmp_str); \
4698
133k
    } else { \
4699
117k
      intobj->diff->member = -1LL; \
4700
117k
    } \
4701
250k
  } while (0);
4702
4703
250k
#define PHP_DATE_INTERVAL_READ_PROPERTY_DOUBLE(element, member, def) \
4704
250k
  do { \
4705
250k
    zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
4706
250k
    if (z_arg) { \
4707
250k
      intobj->diff->member = (double)zval_get_double(z_arg); \
4708
250k
    } else { \
4709
250k
      intobj->diff->member = (double)def; \
4710
250k
    } \
4711
250k
  } while (0);
4712
4713
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("y", y, timelib_sll, -1)
4714
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("m", m, timelib_sll, -1)
4715
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("d", d, timelib_sll, -1)
4716
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("h", h, timelib_sll, -1)
4717
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("i", i, timelib_sll, -1)
4718
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("s", s, timelib_sll, -1)
4719
250k
  {
4720
250k
    const zval *z_arg = zend_hash_str_find(myht, "f", sizeof("f") - 1);
4721
250k
    if (z_arg) {
4722
7.51k
      intobj->diff->us = zend_dval_to_lval(zval_get_double(z_arg) * 1000000.0);
4723
7.51k
    }
4724
250k
  }
4725
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("weekday", weekday, int, -1)
4726
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("weekday_behavior", weekday_behavior, int, -1)
4727
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("first_last_day_of", first_last_day_of, int, -1)
4728
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("invert", invert, int, 0);
4729
250k
  PHP_DATE_INTERVAL_READ_PROPERTY_DAYS(days);
4730
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("special_type", special.type, unsigned int, 0);
4731
250k
  PHP_DATE_INTERVAL_READ_PROPERTY_I64("special_amount", special.amount);
4732
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("have_weekday_relative", have_weekday_relative, unsigned int, 0);
4733
250k
  PHP_DATE_INTERVAL_READ_PROPERTY("have_special_relative", have_special_relative, unsigned int, 0);
4734
250k
  {
4735
250k
    const zval *z_arg = zend_hash_str_find(myht, "civil_or_wall", sizeof("civil_or_wall") - 1);
4736
250k
    intobj->civil_or_wall = PHP_DATE_CIVIL;
4737
250k
    if (z_arg) {
4738
0
      zend_long val = zval_get_long(z_arg);
4739
0
      intobj->civil_or_wall = val;
4740
0
    }
4741
250k
  }
4742
4743
250k
  intobj->initialized = true;
4744
250k
} /* }}} */
4745
4746
/* {{{ */
4747
PHP_METHOD(DateInterval, __set_state)
4748
0
{
4749
0
  php_interval_obj *intobj;
4750
0
  HashTable        *myht;
4751
4752
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
4753
0
    Z_PARAM_ARRAY_HT(myht)
4754
0
  ZEND_PARSE_PARAMETERS_END();
4755
4756
0
  php_date_instantiate(date_ce_interval, return_value);
4757
0
  intobj = Z_PHPINTERVAL_P(return_value);
4758
0
  php_date_interval_initialize_from_hash(intobj, myht);
4759
0
}
4760
/* }}} */
4761
4762
/* {{{ */
4763
PHP_METHOD(DateInterval, __serialize)
4764
0
{
4765
0
  zval             *object = ZEND_THIS;
4766
0
  php_interval_obj *intervalobj;
4767
0
  HashTable        *myht;
4768
4769
0
  ZEND_PARSE_PARAMETERS_NONE();
4770
4771
0
  intervalobj = Z_PHPINTERVAL_P(object);
4772
0
  DATE_CHECK_INITIALIZED(intervalobj->initialized, Z_OBJCE_P(object));
4773
4774
0
  array_init(return_value);
4775
0
  myht = Z_ARRVAL_P(return_value);
4776
0
  date_interval_object_to_hash(intervalobj, myht);
4777
4778
0
  add_common_properties(myht, &intervalobj->std);
4779
0
}
4780
/* }}} */
4781
4782
static bool date_interval_is_internal_property(const zend_string *name)
4783
294k
{
4784
294k
  if (
4785
294k
    zend_string_equals_literal(name, "date_string") ||
4786
233k
    zend_string_equals_literal(name, "from_string") ||
4787
233k
    zend_string_equals_literal(name, "y") ||
4788
233k
    zend_string_equals_literal(name, "m") ||
4789
220k
    zend_string_equals_literal(name, "d") ||
4790
202k
    zend_string_equals_literal(name, "h") ||
4791
167k
    zend_string_equals_literal(name, "i") ||
4792
167k
    zend_string_equals_literal(name, "s") ||
4793
164k
    zend_string_equals_literal(name, "f") ||
4794
163k
    zend_string_equals_literal(name, "invert") ||
4795
163k
    zend_string_equals_literal(name, "days")
4796
294k
  ) {
4797
264k
    return true;
4798
264k
  }
4799
30.2k
  return false;
4800
294k
}
4801
4802
static void restore_custom_dateinterval_properties(zval *object, const HashTable *myht)
4803
312k
{
4804
312k
  zend_string      *prop_name;
4805
312k
  zval             *prop_val;
4806
4807
934k
  ZEND_HASH_FOREACH_STR_KEY_VAL(myht, prop_name, prop_val) {
4808
934k
    if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_interval_is_internal_property(prop_name)) {
4809
280k
      continue;
4810
280k
    }
4811
30.2k
    update_property(Z_OBJ_P(object), prop_name, prop_val);
4812
30.2k
  } ZEND_HASH_FOREACH_END();
4813
312k
}
4814
4815
4816
/* {{{ */
4817
PHP_METHOD(DateInterval, __unserialize)
4818
312k
{
4819
312k
  zval             *object = ZEND_THIS;
4820
312k
  php_interval_obj *intervalobj;
4821
312k
  HashTable        *myht;
4822
4823
937k
  ZEND_PARSE_PARAMETERS_START(1, 1)
4824
1.24M
    Z_PARAM_ARRAY_HT(myht)
4825
312k
  ZEND_PARSE_PARAMETERS_END();
4826
4827
312k
  intervalobj = Z_PHPINTERVAL_P(object);
4828
4829
312k
  php_date_interval_initialize_from_hash(intervalobj, myht);
4830
312k
  restore_custom_dateinterval_properties(object, myht);
4831
312k
}
4832
/* }}} */
4833
4834
/* {{{ */
4835
PHP_METHOD(DateInterval, __wakeup)
4836
0
{
4837
0
  zval             *object = ZEND_THIS;
4838
0
  php_interval_obj *intobj;
4839
0
  const HashTable  *myht;
4840
4841
0
  ZEND_PARSE_PARAMETERS_NONE();
4842
4843
0
  intobj = Z_PHPINTERVAL_P(object);
4844
4845
0
  myht = Z_OBJPROP_P(object);
4846
4847
0
  php_date_interval_initialize_from_hash(intobj, myht);
4848
0
}
4849
/* }}} */
4850
4851
static void date_interval_instantiate_from_time(zval *return_value, timelib_time *time, zend_string *time_str)
4852
0
{
4853
0
  php_interval_obj *diobj;
4854
4855
0
  php_date_instantiate(date_ce_interval, return_value);
4856
0
  diobj = Z_PHPINTERVAL_P(return_value);
4857
0
  diobj->diff = timelib_rel_time_clone(&time->relative);
4858
0
  diobj->initialized = true;
4859
0
  diobj->civil_or_wall = PHP_DATE_CIVIL;
4860
0
  diobj->from_string = true;
4861
0
  diobj->date_string = zend_string_copy(time_str);
4862
0
}
4863
4864
/* {{{ Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string */
4865
PHP_FUNCTION(date_interval_create_from_date_string)
4866
0
{
4867
0
  zend_string    *time_str = NULL;
4868
0
  timelib_time   *time;
4869
0
  timelib_error_container *err = NULL;
4870
4871
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
4872
0
    Z_PARAM_STR(time_str)
4873
0
  ZEND_PARSE_PARAMETERS_END();
4874
4875
0
  time = timelib_strtotime(ZSTR_VAL(time_str), ZSTR_LEN(time_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
4876
4877
0
  if (err->error_count > 0)  {
4878
0
    php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s) at position %d (%c): %s", ZSTR_VAL(time_str),
4879
0
      err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
4880
0
    RETVAL_FALSE;
4881
0
    goto cleanup;
4882
0
  }
4883
4884
0
  if (time->have_date || time->have_time || time->have_zone) {
4885
0
    php_error_docref(NULL, E_WARNING, "String '%s' contains non-relative elements", ZSTR_VAL(time_str));
4886
0
    RETVAL_FALSE;
4887
0
    goto cleanup;
4888
0
  }
4889
4890
0
  date_interval_instantiate_from_time(return_value, time, time_str);
4891
4892
0
cleanup:
4893
0
  timelib_time_dtor(time);
4894
0
  timelib_error_container_dtor(err);
4895
0
}
4896
/* }}} */
4897
4898
/* {{{ Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string */
4899
PHP_METHOD(DateInterval, createFromDateString)
4900
0
{
4901
0
  zend_string    *time_str = NULL;
4902
0
  timelib_time   *time;
4903
0
  timelib_error_container *err = NULL;
4904
4905
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
4906
0
    Z_PARAM_STR(time_str)
4907
0
  ZEND_PARSE_PARAMETERS_END();
4908
4909
0
  time = timelib_strtotime(ZSTR_VAL(time_str), ZSTR_LEN(time_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
4910
4911
0
  if (err->error_count > 0)  {
4912
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),
4913
0
      err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
4914
0
    goto cleanup;
4915
0
  }
4916
4917
0
  if (time->have_date || time->have_time || time->have_zone) {
4918
0
    zend_throw_error(date_ce_date_malformed_interval_string_exception, "String '%s' contains non-relative elements", ZSTR_VAL(time_str));
4919
0
    goto cleanup;
4920
0
  }
4921
4922
0
  date_interval_instantiate_from_time(return_value, time, time_str);
4923
4924
0
cleanup:
4925
0
  timelib_time_dtor(time);
4926
0
  timelib_error_container_dtor(err);
4927
0
}
4928
/* }}} */
4929
4930
/* {{{ date_interval_format -  */
4931
static zend_string *date_interval_format(const char *format, size_t format_len, timelib_rel_time *t)
4932
0
{
4933
0
  smart_str            string = {0};
4934
0
  size_t               i;
4935
0
  int                  length, have_format_spec = 0;
4936
0
  char                 buffer[33];
4937
4938
0
  if (!format_len) {
4939
0
    return ZSTR_EMPTY_ALLOC();
4940
0
  }
4941
4942
0
  for (i = 0; i < format_len; i++) {
4943
0
    if (have_format_spec) {
4944
0
      switch (format[i]) {
4945
0
        case 'Y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->y); break;
4946
0
        case 'y': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->y); break;
4947
4948
0
        case 'M': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
4949
0
        case 'm': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
4950
4951
0
        case 'D': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
4952
0
        case 'd': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
4953
4954
0
        case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
4955
0
        case 'h': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
4956
4957
0
        case 'I': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
4958
0
        case 'i': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->i); break;
4959
4960
0
        case 'S': length = slprintf(buffer, sizeof(buffer), "%02" ZEND_LONG_FMT_SPEC, (zend_long) t->s); break;
4961
0
        case 's': length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) t->s); break;
4962
4963
0
        case 'F': length = slprintf(buffer, sizeof(buffer), "%06" ZEND_LONG_FMT_SPEC, (zend_long) t->us); break;
4964
0
        case 'f': length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) t->us); break;
4965
4966
0
        case 'a': {
4967
0
          if ((int) t->days != TIMELIB_UNSET) {
4968
0
            length = slprintf(buffer, sizeof(buffer), "%d", (int) t->days);
4969
0
          } else {
4970
0
            length = slprintf(buffer, sizeof(buffer), "(unknown)");
4971
0
          }
4972
0
        } break;
4973
0
        case 'r': length = slprintf(buffer, sizeof(buffer), "%s", t->invert ? "-" : ""); break;
4974
0
        case 'R': length = slprintf(buffer, sizeof(buffer), "%c", t->invert ? '-' : '+'); break;
4975
4976
0
        case '%': length = slprintf(buffer, sizeof(buffer), "%%"); break;
4977
0
        default: buffer[0] = '%'; buffer[1] = format[i]; buffer[2] = '\0'; length = 2; break;
4978
0
      }
4979
0
      smart_str_appendl(&string, buffer, length);
4980
0
      have_format_spec = 0;
4981
0
    } else {
4982
0
      if (format[i] == '%') {
4983
0
        have_format_spec = 1;
4984
0
      } else {
4985
0
        smart_str_appendc(&string, format[i]);
4986
0
      }
4987
0
    }
4988
0
  }
4989
4990
0
  smart_str_0(&string);
4991
4992
0
  if (string.s == NULL) {
4993
0
    return ZSTR_EMPTY_ALLOC();
4994
0
  }
4995
4996
0
  return string.s;
4997
0
}
4998
/* }}} */
4999
5000
/* {{{ Formats the interval. */
5001
PHP_FUNCTION(date_interval_format)
5002
0
{
5003
0
  zval             *object;
5004
0
  php_interval_obj *diobj;
5005
0
  const char       *format;
5006
0
  size_t            format_len;
5007
5008
0
  if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_interval, &format, &format_len) == FAILURE) {
5009
0
    RETURN_THROWS();
5010
0
  }
5011
0
  diobj = Z_PHPINTERVAL_P(object);
5012
0
  DATE_CHECK_INITIALIZED(diobj->initialized, Z_OBJCE_P(object));
5013
5014
0
  RETURN_STR(date_interval_format(format, format_len, diobj->diff));
5015
0
}
5016
/* }}} */
5017
5018
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) /* {{{ */
5019
0
{
5020
0
  timelib_time     *b = NULL, *e = NULL;
5021
0
  timelib_rel_time *p = NULL;
5022
0
  int               r = 0;
5023
0
  timelib_error_container *errors;
5024
0
  bool              retval = false;
5025
5026
0
  timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
5027
5028
0
  if (errors->error_count > 0) {
5029
0
    retval = false;
5030
0
    zend_throw_exception_ex(date_ce_date_malformed_period_string_exception, 0, "Unknown or bad format (%s)", format);
5031
0
    if (b) {
5032
0
      timelib_time_dtor(b);
5033
0
    }
5034
0
    if (e) {
5035
0
      timelib_time_dtor(e);
5036
0
    }
5037
0
    if (p) {
5038
0
      timelib_rel_time_dtor(p);
5039
0
    }
5040
0
  } else {
5041
0
    *st = b;
5042
0
    *et = e;
5043
0
    *d  = p;
5044
0
    *recurrences = r;
5045
0
    retval = true;
5046
0
  }
5047
0
  timelib_error_container_dtor(errors);
5048
0
  return retval;
5049
0
} /* }}} */
5050
5051
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)
5052
0
{
5053
0
  if (!date_period_initialize(&(dpobj->start), &(dpobj->end), &(dpobj->interval), recurrences, isostr, isostr_len)) {
5054
0
    return false;
5055
0
  }
5056
5057
0
  if (dpobj->start == NULL) {
5058
0
    zend_string *func = get_active_function_or_method_name();
5059
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);
5060
0
    zend_string_release(func);
5061
0
    return false;
5062
0
  }
5063
0
  if (dpobj->interval == NULL) {
5064
0
    zend_string *func = get_active_function_or_method_name();
5065
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);
5066
0
    zend_string_release(func);
5067
0
    return false;
5068
0
  }
5069
0
  if (dpobj->end == NULL && recurrences == 0) {
5070
0
    zend_string *func = get_active_function_or_method_name();
5071
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);
5072
0
    zend_string_release(func);
5073
0
    return false;
5074
0
  }
5075
5076
0
  if (dpobj->start) {
5077
0
    timelib_update_ts(dpobj->start, NULL);
5078
0
  }
5079
0
  if (dpobj->end) {
5080
0
    timelib_update_ts(dpobj->end, NULL);
5081
0
  }
5082
0
  dpobj->start_ce = base_ce;
5083
5084
0
  return true;
5085
0
}
5086
5087
static bool date_period_init_finish(php_period_obj *dpobj, zend_long options, zend_long recurrences)
5088
7
{
5089
7
  const zend_long max_recurrences = (INT_MAX - 8);
5090
5091
7
  if (dpobj->end == NULL && (recurrences < 1 || recurrences > max_recurrences)) {
5092
2
    zend_string *func = get_active_function_or_method_name();
5093
2
    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);
5094
2
    zend_string_release(func);
5095
2
    return false;
5096
2
  }
5097
5098
  /* options */
5099
5
  dpobj->include_start_date = !(options & PHP_DATE_PERIOD_EXCLUDE_START_DATE);
5100
5
  dpobj->include_end_date = options & PHP_DATE_PERIOD_INCLUDE_END_DATE;
5101
5102
  /* recurrences */
5103
5
  recurrences += dpobj->include_start_date + dpobj->include_end_date;
5104
5105
5
  if (UNEXPECTED(recurrences > max_recurrences)) {
5106
0
    zend_string *func = get_active_function_or_method_name();
5107
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);
5108
0
    zend_string_release(func);
5109
0
    return false;
5110
0
  }
5111
5112
5
  dpobj->recurrences = (int)recurrences;
5113
5114
5
  dpobj->initialized = true;
5115
5116
5
  return true;
5117
5
}
5118
5119
PHP_METHOD(DatePeriod, createFromISO8601String)
5120
0
{
5121
0
  php_period_obj *dpobj;
5122
0
  zend_long recurrences = 0, options = 0;
5123
0
  char *isostr = NULL;
5124
0
  size_t isostr_len = 0;
5125
5126
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
5127
0
    Z_PARAM_STRING(isostr, isostr_len)
5128
0
    Z_PARAM_OPTIONAL
5129
0
    Z_PARAM_LONG(options)
5130
0
  ZEND_PARSE_PARAMETERS_END();
5131
5132
0
  if (object_init_ex(return_value, execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_period) != SUCCESS) {
5133
0
    RETURN_THROWS();
5134
0
  }
5135
0
  dpobj = Z_PHPPERIOD_P(return_value);
5136
5137
0
  dpobj->current = NULL;
5138
5139
0
  if (!date_period_init_iso8601_string(dpobj, date_ce_immutable, isostr, isostr_len, &recurrences)) {
5140
0
    RETURN_THROWS();
5141
0
  }
5142
5143
0
  if (!date_period_init_finish(dpobj, options, recurrences)) {
5144
0
    RETURN_THROWS();
5145
0
  }
5146
0
}
5147
5148
/* {{{ Creates new DatePeriod object. */
5149
PHP_METHOD(DatePeriod, __construct)
5150
14
{
5151
14
  php_period_obj   *dpobj;
5152
14
  php_date_obj     *dateobj;
5153
14
  zval *start, *end = NULL, *interval;
5154
14
  zend_long  recurrences = 0, options = 0;
5155
14
  char *isostr = NULL;
5156
14
  size_t   isostr_len = 0;
5157
14
  timelib_time *clone;
5158
5159
14
  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) {
5160
7
    if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOO|l", &start, date_ce_interface, &interval, date_ce_interval, &end, date_ce_interface, &options) == FAILURE) {
5161
7
      if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "s|l", &isostr, &isostr_len, &options) == FAILURE) {
5162
7
        zend_type_error("DatePeriod::__construct() accepts (DateTimeInterface, DateInterval, int [, int]), or (DateTimeInterface, DateInterval, DateTime [, int]), or (string [, int]) as arguments");
5163
7
        RETURN_THROWS();
5164
7
      }
5165
7
    }
5166
7
  }
5167
5168
7
  dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5169
7
  dpobj->current = NULL;
5170
5171
7
  if (isostr) {
5172
0
    zend_error(E_DEPRECATED, "Calling DatePeriod::__construct(string $isostr, int $options = 0) is deprecated, "
5173
0
      "use DatePeriod::createFromISO8601String() instead");
5174
0
    if (UNEXPECTED(EG(exception))) {
5175
0
      RETURN_THROWS();
5176
0
    }
5177
5178
0
    if (!date_period_init_iso8601_string(dpobj, date_ce_date, isostr, isostr_len, &recurrences)) {
5179
0
      RETURN_THROWS();
5180
0
    }
5181
7
  } else {
5182
    /* check initialisation */
5183
7
    DATE_CHECK_INITIALIZED(Z_PHPDATE_P(start)->time, date_ce_interface);
5184
7
    if (end) {
5185
0
      DATE_CHECK_INITIALIZED(Z_PHPDATE_P(end)->time, date_ce_interface);
5186
0
    }
5187
5188
    /* init */
5189
7
    php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
5190
5191
    /* start date */
5192
7
    dateobj = Z_PHPDATE_P(start);
5193
7
    clone = timelib_time_ctor();
5194
7
    memcpy(clone, dateobj->time, sizeof(timelib_time));
5195
7
    if (dateobj->time->tz_abbr) {
5196
7
      clone->tz_abbr = timelib_strdup(dateobj->time->tz_abbr);
5197
7
    }
5198
7
    if (dateobj->time->tz_info) {
5199
7
      clone->tz_info = dateobj->time->tz_info;
5200
7
    }
5201
7
    dpobj->start = clone;
5202
7
    dpobj->start_ce = Z_OBJCE_P(start);
5203
5204
    /* interval */
5205
7
    dpobj->interval = timelib_rel_time_clone(intobj->diff);
5206
5207
    /* end date */
5208
7
    if (end) {
5209
0
      dateobj = Z_PHPDATE_P(end);
5210
0
      clone = timelib_time_clone(dateobj->time);
5211
0
      dpobj->end = clone;
5212
0
    }
5213
7
  }
5214
5215
7
  if (!date_period_init_finish(dpobj, options, recurrences)) {
5216
2
    RETURN_THROWS();
5217
2
  }
5218
7
}
5219
/* }}} */
5220
5221
/* {{{ Get start date. */
5222
PHP_METHOD(DatePeriod, getStartDate)
5223
0
{
5224
0
  php_period_obj   *dpobj;
5225
0
  php_date_obj     *dateobj;
5226
5227
0
  ZEND_PARSE_PARAMETERS_NONE();
5228
5229
0
  dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5230
0
  DATE_CHECK_INITIALIZED(dpobj->start, Z_OBJCE_P(ZEND_THIS));
5231
5232
0
  php_date_instantiate(dpobj->start_ce, return_value);
5233
0
  dateobj = Z_PHPDATE_P(return_value);
5234
0
  dateobj->time = timelib_time_ctor();
5235
0
  *dateobj->time = *dpobj->start;
5236
0
  if (dpobj->start->tz_abbr) {
5237
0
    dateobj->time->tz_abbr = timelib_strdup(dpobj->start->tz_abbr);
5238
0
  }
5239
0
  if (dpobj->start->tz_info) {
5240
0
    dateobj->time->tz_info = dpobj->start->tz_info;
5241
0
  }
5242
0
}
5243
/* }}} */
5244
5245
/* {{{ Get end date. */
5246
PHP_METHOD(DatePeriod, getEndDate)
5247
0
{
5248
0
  php_period_obj   *dpobj;
5249
0
  php_date_obj     *dateobj;
5250
5251
0
  ZEND_PARSE_PARAMETERS_NONE();
5252
5253
0
  dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5254
5255
0
  if (!dpobj->end) {
5256
0
    return;
5257
0
  }
5258
5259
0
  php_date_instantiate(dpobj->start_ce, return_value);
5260
0
  dateobj = Z_PHPDATE_P(return_value);
5261
0
  dateobj->time = timelib_time_ctor();
5262
0
  *dateobj->time = *dpobj->end;
5263
0
  if (dpobj->end->tz_abbr) {
5264
0
      dateobj->time->tz_abbr = timelib_strdup(dpobj->end->tz_abbr);
5265
0
  }
5266
0
  if (dpobj->end->tz_info) {
5267
0
      dateobj->time->tz_info = dpobj->end->tz_info;
5268
0
  }
5269
0
}
5270
/* }}} */
5271
5272
/* {{{ Get date interval. */
5273
PHP_METHOD(DatePeriod, getDateInterval)
5274
0
{
5275
0
  php_period_obj   *dpobj;
5276
0
  php_interval_obj *diobj;
5277
5278
0
  ZEND_PARSE_PARAMETERS_NONE();
5279
5280
0
  dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5281
0
  DATE_CHECK_INITIALIZED(dpobj->interval, Z_OBJCE_P(ZEND_THIS));
5282
5283
0
  php_date_instantiate(date_ce_interval, return_value);
5284
0
  diobj = Z_PHPINTERVAL_P(return_value);
5285
0
  diobj->diff = timelib_rel_time_clone(dpobj->interval);
5286
0
  diobj->initialized = true;
5287
0
}
5288
/* }}} */
5289
5290
/* {{{ Get recurrences. */
5291
PHP_METHOD(DatePeriod, getRecurrences)
5292
0
{
5293
0
  php_period_obj   *dpobj;
5294
5295
0
  ZEND_PARSE_PARAMETERS_NONE();
5296
5297
0
  dpobj = Z_PHPPERIOD_P(ZEND_THIS);
5298
5299
0
  if (0 == dpobj->recurrences - dpobj->include_start_date - dpobj->include_end_date) {
5300
0
    return;
5301
0
  }
5302
5303
0
  RETURN_LONG(dpobj->recurrences - dpobj->include_start_date - dpobj->include_end_date);
5304
0
}
5305
/* }}} */
5306
5307
PHP_METHOD(DatePeriod, getIterator)
5308
0
{
5309
0
  ZEND_PARSE_PARAMETERS_NONE();
5310
5311
0
  zend_create_internal_iterator_zval(return_value, ZEND_THIS);
5312
0
}
5313
5314
static bool check_id_allowed(const char *id, zend_long what) /* {{{ */
5315
1.19k
{
5316
1.19k
  if ((what & PHP_DATE_TIMEZONE_GROUP_AFRICA)     && strncasecmp(id, "Africa/",      7) == 0) return true;
5317
1.08k
  if ((what & PHP_DATE_TIMEZONE_GROUP_AMERICA)    && strncasecmp(id, "America/",     8) == 0) return true;
5318
750
  if ((what & PHP_DATE_TIMEZONE_GROUP_ANTARCTICA) && strncasecmp(id, "Antarctica/", 11) == 0) return true;
5319
726
  if ((what & PHP_DATE_TIMEZONE_GROUP_ARCTIC)     && strncasecmp(id, "Arctic/",      7) == 0) return true;
5320
724
  if ((what & PHP_DATE_TIMEZONE_GROUP_ASIA)       && strncasecmp(id, "Asia/",        5) == 0) return true;
5321
526
  if ((what & PHP_DATE_TIMEZONE_GROUP_ATLANTIC)   && strncasecmp(id, "Atlantic/",    9) == 0) return true;
5322
502
  if ((what & PHP_DATE_TIMEZONE_GROUP_AUSTRALIA)  && strncasecmp(id, "Australia/",  10) == 0) return true;
5323
456
  if ((what & PHP_DATE_TIMEZONE_GROUP_EUROPE)     && strncasecmp(id, "Europe/",      7) == 0) return true;
5324
328
  if ((what & PHP_DATE_TIMEZONE_GROUP_INDIAN)     && strncasecmp(id, "Indian/",      7) == 0) return true;
5325
306
  if ((what & PHP_DATE_TIMEZONE_GROUP_PACIFIC)    && strncasecmp(id, "Pacific/",     8) == 0) return true;
5326
218
  if ((what & PHP_DATE_TIMEZONE_GROUP_UTC)        && strncasecmp(id, "UTC",          3) == 0) return true;
5327
216
  return false;
5328
218
} /* }}} */
5329
5330
/* {{{ Returns numerically index array with all timezone identifiers. */
5331
PHP_FUNCTION(timezone_identifiers_list)
5332
2
{
5333
2
  const timelib_tzdb             *tzdb;
5334
2
  const timelib_tzdb_index_entry *table;
5335
2
  int                             i, item_count;
5336
2
  zend_long                       what = PHP_DATE_TIMEZONE_GROUP_ALL;
5337
2
  char                           *option = NULL;
5338
2
  size_t                          option_len = 0;
5339
5340
6
  ZEND_PARSE_PARAMETERS_START(0, 2)
5341
6
    Z_PARAM_OPTIONAL
5342
6
    Z_PARAM_LONG(what)
5343
0
    Z_PARAM_STRING_OR_NULL(option, option_len)
5344
2
  ZEND_PARSE_PARAMETERS_END();
5345
5346
  /* Extra validation */
5347
2
  if (what == PHP_DATE_TIMEZONE_PER_COUNTRY && option_len != 2) {
5348
0
    zend_argument_value_error(2, "must be a two-letter ISO 3166-1 compatible country code "
5349
0
      "when argument #1 ($timezoneGroup) is DateTimeZone::PER_COUNTRY");
5350
0
    RETURN_THROWS();
5351
0
  }
5352
5353
2
  tzdb = DATE_TIMEZONEDB;
5354
2
  table = timelib_timezone_identifiers_list((timelib_tzdb*) tzdb, &item_count);
5355
5356
2
  array_init(return_value);
5357
2
  zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
5358
5359
1.19k
  for (i = 0; i < item_count; ++i) {
5360
1.19k
    if (what == PHP_DATE_TIMEZONE_PER_COUNTRY) {
5361
0
      if (tzdb->data[table[i].pos + 5] == option[0] && tzdb->data[table[i].pos + 6] == option[1]) {
5362
0
        add_next_index_string(return_value, table[i].id);
5363
0
      }
5364
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'))) {
5365
838
      add_next_index_string(return_value, table[i].id);
5366
838
    }
5367
1.19k
  };
5368
2
}
5369
/* }}} */
5370
5371
/* {{{ Returns the Olson database version number. */
5372
PHP_FUNCTION(timezone_version_get)
5373
0
{
5374
0
  const timelib_tzdb *tzdb;
5375
5376
0
  ZEND_PARSE_PARAMETERS_NONE();
5377
5378
0
  tzdb = DATE_TIMEZONEDB;
5379
0
  RETURN_STRING(tzdb->version);
5380
0
}
5381
/* }}} */
5382
5383
/* {{{ Returns associative array containing dst, offset and the timezone name */
5384
PHP_FUNCTION(timezone_abbreviations_list)
5385
0
{
5386
0
  const timelib_tz_lookup_table *table, *entry;
5387
0
  zval                          element, *abbr_array_p, abbr_array;
5388
5389
0
  ZEND_PARSE_PARAMETERS_NONE();
5390
5391
0
  table = timelib_timezone_abbreviations_list();
5392
0
  array_init(return_value);
5393
0
  entry = table;
5394
5395
0
  do {
5396
0
    array_init(&element);
5397
0
    add_assoc_bool_ex(&element, "dst", sizeof("dst") -1, entry->type);
5398
0
    add_assoc_long_ex(&element, "offset", sizeof("offset") - 1, entry->gmtoffset);
5399
0
    if (entry->full_tz_name) {
5400
0
      add_assoc_string_ex(&element, "timezone_id", sizeof("timezone_id") - 1, entry->full_tz_name);
5401
0
    } else {
5402
0
      add_assoc_null_ex(&element, "timezone_id", sizeof("timezone_id") - 1);
5403
0
    }
5404
5405
0
    abbr_array_p = zend_hash_str_find(Z_ARRVAL_P(return_value), entry->name, strlen(entry->name));
5406
0
    if (!abbr_array_p) {
5407
0
      array_init(&abbr_array);
5408
0
      add_assoc_zval(return_value, entry->name, &abbr_array);
5409
0
    } else {
5410
0
      ZVAL_COPY_VALUE(&abbr_array, abbr_array_p);
5411
0
    }
5412
0
    add_next_index_zval(&abbr_array, &element);
5413
0
    entry++;
5414
0
  } while (entry->name);
5415
0
}
5416
/* }}} */
5417
5418
/* {{{ Sets the default timezone used by all date/time functions in a script */
5419
PHP_FUNCTION(date_default_timezone_set)
5420
6
{
5421
6
  char *zone;
5422
6
  size_t   zone_len;
5423
5424
18
  ZEND_PARSE_PARAMETERS_START(1, 1)
5425
24
    Z_PARAM_STRING(zone, zone_len)
5426
6
  ZEND_PARSE_PARAMETERS_END();
5427
5428
6
  if (!timelib_timezone_id_is_valid(zone, DATE_TIMEZONEDB)) {
5429
1
    php_error_docref(NULL, E_NOTICE, "Timezone ID '%s' is invalid", zone);
5430
1
    RETURN_FALSE;
5431
1
  }
5432
5
  if (DATEG(timezone)) {
5433
0
    efree(DATEG(timezone));
5434
0
    DATEG(timezone) = NULL;
5435
0
  }
5436
5
  DATEG(timezone) = estrndup(zone, zone_len);
5437
5
  RETURN_TRUE;
5438
5
}
5439
/* }}} */
5440
5441
/* {{{ Gets the default timezone used by all date/time functions in a script */
5442
PHP_FUNCTION(date_default_timezone_get)
5443
0
{
5444
0
  timelib_tzinfo *default_tz;
5445
0
  ZEND_PARSE_PARAMETERS_NONE();
5446
5447
0
  default_tz = get_timezone_info();
5448
0
  if (!default_tz) {
5449
0
    RETURN_THROWS();
5450
0
  }
5451
0
  RETVAL_STRING(default_tz->name);
5452
0
}
5453
/* }}} */
5454
5455
/* {{{ php_do_date_sunrise_sunset
5456
 *  Common for date_sunrise() and date_sunset() functions
5457
 */
5458
static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, bool calc_sunset)
5459
0
{
5460
0
  double latitude, longitude, zenith, gmt_offset, altitude;
5461
0
  bool latitude_is_null = 1, longitude_is_null = 1, zenith_is_null = 1, gmt_offset_is_null = 1;
5462
0
  double h_rise, h_set, N;
5463
0
  timelib_sll rise, set, transit;
5464
0
  zend_long time, retformat = SUNFUNCS_RET_STRING;
5465
0
  int             rs;
5466
0
  timelib_time   *t;
5467
0
  timelib_tzinfo *tzi;
5468
0
  zend_string    *retstr;
5469
5470
0
  ZEND_PARSE_PARAMETERS_START(1, 6)
5471
0
    Z_PARAM_LONG(time)
5472
0
    Z_PARAM_OPTIONAL
5473
0
    Z_PARAM_LONG(retformat)
5474
0
    Z_PARAM_DOUBLE_OR_NULL(latitude, latitude_is_null)
5475
0
    Z_PARAM_DOUBLE_OR_NULL(longitude, longitude_is_null)
5476
0
    Z_PARAM_DOUBLE_OR_NULL(zenith, zenith_is_null)
5477
0
    Z_PARAM_DOUBLE_OR_NULL(gmt_offset, gmt_offset_is_null)
5478
0
  ZEND_PARSE_PARAMETERS_END();
5479
5480
0
  if (latitude_is_null) {
5481
0
    latitude = zend_ini_double_literal("date.default_latitude");
5482
0
  }
5483
5484
0
  if (longitude_is_null) {
5485
0
    longitude = zend_ini_double_literal("date.default_longitude");
5486
0
  }
5487
5488
0
  if (zenith_is_null) {
5489
0
    if (calc_sunset) {
5490
0
      zenith = zend_ini_double_literal("date.sunset_zenith");
5491
0
    } else {
5492
0
      zenith = zend_ini_double_literal("date.sunrise_zenith");
5493
0
    }
5494
0
  }
5495
5496
0
  if (retformat != SUNFUNCS_RET_TIMESTAMP &&
5497
0
    retformat != SUNFUNCS_RET_STRING &&
5498
0
    retformat != SUNFUNCS_RET_DOUBLE)
5499
0
  {
5500
0
    zend_argument_value_error(2, "must be one of SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING, or SUNFUNCS_RET_DOUBLE");
5501
0
    RETURN_THROWS();
5502
0
  }
5503
0
  altitude = 90 - zenith;
5504
5505
0
  if (!zend_finite(latitude) || !zend_finite(longitude)) {
5506
0
    RETURN_FALSE;
5507
0
  }
5508
5509
  /* Initialize time struct */
5510
0
  tzi = get_timezone_info();
5511
0
  if (!tzi) {
5512
0
    RETURN_THROWS();
5513
0
  }
5514
0
  t = timelib_time_ctor();
5515
0
  t->tz_info = tzi;
5516
0
  t->zone_type = TIMELIB_ZONETYPE_ID;
5517
5518
0
  if (gmt_offset_is_null) {
5519
0
    gmt_offset = timelib_get_current_offset(t) / 3600.0;
5520
0
  }
5521
5522
0
  timelib_unixtime2local(t, time);
5523
0
  rs = timelib_astro_rise_set_altitude(t, longitude, latitude, altitude, 1, &h_rise, &h_set, &rise, &set, &transit);
5524
0
  timelib_time_dtor(t);
5525
5526
0
  if (rs != 0) {
5527
0
    RETURN_FALSE;
5528
0
  }
5529
5530
0
  if (retformat == SUNFUNCS_RET_TIMESTAMP) {
5531
0
    RETURN_LONG(calc_sunset ? set : rise);
5532
0
  }
5533
0
  N = (calc_sunset ? h_set : h_rise) + gmt_offset;
5534
5535
0
  if (N > 24 || N < 0) {
5536
0
    N -= floor(N / 24) * 24;
5537
0
  }
5538
0
  if (!(N <= 24 && N >= 0)) {
5539
0
    RETURN_FALSE;
5540
0
  }
5541
5542
0
  switch (retformat) {
5543
0
    case SUNFUNCS_RET_STRING:
5544
0
      retstr = strpprintf(0, "%02d:%02d", (int) N, (int) (60 * (N - (int) N)));
5545
0
      RETURN_NEW_STR(retstr);
5546
0
    case SUNFUNCS_RET_DOUBLE:
5547
0
      RETURN_DOUBLE(N);
5548
0
  }
5549
0
}
5550
/* }}} */
5551
5552
/* {{{ Returns time of sunrise for a given day and location */
5553
PHP_FUNCTION(date_sunrise)
5554
0
{
5555
0
  php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
5556
0
}
5557
/* }}} */
5558
5559
/* {{{ Returns time of sunset for a given day and location */
5560
PHP_FUNCTION(date_sunset)
5561
0
{
5562
0
  php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
5563
0
}
5564
/* }}} */
5565
5566
/* {{{ Returns an array with information about sun set/rise and twilight begin/end */
5567
PHP_FUNCTION(date_sun_info)
5568
0
{
5569
0
  zend_long       time;
5570
0
  double          latitude, longitude;
5571
0
  timelib_time   *t, *t2;
5572
0
  timelib_tzinfo *tzi;
5573
0
  int             rs;
5574
0
  timelib_sll     rise, set, transit;
5575
0
  int             dummy;
5576
0
  double          ddummy;
5577
5578
0
  ZEND_PARSE_PARAMETERS_START(3, 3)
5579
0
    Z_PARAM_LONG(time)
5580
0
    Z_PARAM_DOUBLE(latitude)
5581
0
    Z_PARAM_DOUBLE(longitude)
5582
0
  ZEND_PARSE_PARAMETERS_END();
5583
5584
0
  if (!zend_finite(latitude)) {
5585
0
    zend_argument_value_error(2, "must be finite");
5586
0
    RETURN_THROWS();
5587
0
  }
5588
0
  if (!zend_finite(longitude)) {
5589
0
    zend_argument_value_error(3, "must be finite");
5590
0
    RETURN_THROWS();
5591
0
  }
5592
5593
  /* Initialize time struct */
5594
0
  tzi = get_timezone_info();
5595
0
  if (!tzi) {
5596
0
    RETURN_THROWS();
5597
0
  }
5598
0
  t = timelib_time_ctor();
5599
0
  t->tz_info = tzi;
5600
0
  t->zone_type = TIMELIB_ZONETYPE_ID;
5601
0
  timelib_unixtime2local(t, time);
5602
5603
  /* Setup */
5604
0
  t2 = timelib_time_ctor();
5605
0
  array_init(return_value);
5606
5607
  /* Get sun up/down and transit */
5608
0
  rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -35.0/60, 1, &ddummy, &ddummy, &rise, &set, &transit);
5609
0
  switch (rs) {
5610
0
    case -1: /* always below */
5611
0
      add_assoc_bool(return_value, "sunrise", 0);
5612
0
      add_assoc_bool(return_value, "sunset", 0);
5613
0
      break;
5614
0
    case 1: /* always above */
5615
0
      add_assoc_bool(return_value, "sunrise", 1);
5616
0
      add_assoc_bool(return_value, "sunset", 1);
5617
0
      break;
5618
0
    default:
5619
0
      t2->sse = rise;
5620
0
      add_assoc_long(return_value, "sunrise", timelib_date_to_int(t2, &dummy));
5621
0
      t2->sse = set;
5622
0
      add_assoc_long(return_value, "sunset", timelib_date_to_int(t2, &dummy));
5623
0
  }
5624
0
  t2->sse = transit;
5625
0
  add_assoc_long(return_value, "transit", timelib_date_to_int(t2, &dummy));
5626
5627
  /* Get civil twilight */
5628
0
  rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -6.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
5629
0
  switch (rs) {
5630
0
    case -1: /* always below */
5631
0
      add_assoc_bool(return_value, "civil_twilight_begin", 0);
5632
0
      add_assoc_bool(return_value, "civil_twilight_end", 0);
5633
0
      break;
5634
0
    case 1: /* always above */
5635
0
      add_assoc_bool(return_value, "civil_twilight_begin", 1);
5636
0
      add_assoc_bool(return_value, "civil_twilight_end", 1);
5637
0
      break;
5638
0
    default:
5639
0
      t2->sse = rise;
5640
0
      add_assoc_long(return_value, "civil_twilight_begin", timelib_date_to_int(t2, &dummy));
5641
0
      t2->sse = set;
5642
0
      add_assoc_long(return_value, "civil_twilight_end", timelib_date_to_int(t2, &dummy));
5643
0
  }
5644
5645
  /* Get nautical twilight */
5646
0
  rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -12.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
5647
0
  switch (rs) {
5648
0
    case -1: /* always below */
5649
0
      add_assoc_bool(return_value, "nautical_twilight_begin", 0);
5650
0
      add_assoc_bool(return_value, "nautical_twilight_end", 0);
5651
0
      break;
5652
0
    case 1: /* always above */
5653
0
      add_assoc_bool(return_value, "nautical_twilight_begin", 1);
5654
0
      add_assoc_bool(return_value, "nautical_twilight_end", 1);
5655
0
      break;
5656
0
    default:
5657
0
      t2->sse = rise;
5658
0
      add_assoc_long(return_value, "nautical_twilight_begin", timelib_date_to_int(t2, &dummy));
5659
0
      t2->sse = set;
5660
0
      add_assoc_long(return_value, "nautical_twilight_end", timelib_date_to_int(t2, &dummy));
5661
0
  }
5662
5663
  /* Get astronomical twilight */
5664
0
  rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -18.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
5665
0
  switch (rs) {
5666
0
    case -1: /* always below */
5667
0
      add_assoc_bool(return_value, "astronomical_twilight_begin", 0);
5668
0
      add_assoc_bool(return_value, "astronomical_twilight_end", 0);
5669
0
      break;
5670
0
    case 1: /* always above */
5671
0
      add_assoc_bool(return_value, "astronomical_twilight_begin", 1);
5672
0
      add_assoc_bool(return_value, "astronomical_twilight_end", 1);
5673
0
      break;
5674
0
    default:
5675
0
      t2->sse = rise;
5676
0
      add_assoc_long(return_value, "astronomical_twilight_begin", timelib_date_to_int(t2, &dummy));
5677
0
      t2->sse = set;
5678
0
      add_assoc_long(return_value, "astronomical_twilight_end", timelib_date_to_int(t2, &dummy));
5679
0
  }
5680
0
  timelib_time_dtor(t);
5681
0
  timelib_time_dtor(t2);
5682
0
}
5683
/* }}} */
5684
5685
static HashTable *date_object_get_gc_period(zend_object *object, zval **table, int *n) /* {{{ */
5686
3.49k
{
5687
3.49k
  *table = NULL;
5688
3.49k
  *n = 0;
5689
3.49k
  return zend_std_get_properties(object);
5690
3.49k
} /* }}} */
5691
5692
static void date_period_object_to_hash(php_period_obj *period_obj, HashTable *props)
5693
0
{
5694
0
  zval zv;
5695
5696
0
  create_date_period_datetime(period_obj->start, period_obj->start_ce, &zv);
5697
0
  zend_hash_str_update(props, "start", sizeof("start")-1, &zv);
5698
5699
0
  create_date_period_datetime(period_obj->current, period_obj->start_ce, &zv);
5700
0
  zend_hash_str_update(props, "current", sizeof("current")-1, &zv);
5701
5702
0
  create_date_period_datetime(period_obj->end, period_obj->start_ce, &zv);
5703
0
  zend_hash_str_update(props, "end", sizeof("end")-1, &zv);
5704
5705
0
  create_date_period_interval(period_obj->interval, &zv);
5706
0
  zend_hash_str_update(props, "interval", sizeof("interval")-1, &zv);
5707
5708
  /* converted to larger type (int->long); must check when unserializing */
5709
0
  ZVAL_LONG(&zv, (zend_long) period_obj->recurrences);
5710
0
  zend_hash_str_update(props, "recurrences", sizeof("recurrences")-1, &zv);
5711
5712
0
  ZVAL_BOOL(&zv, period_obj->include_start_date);
5713
0
  zend_hash_str_update(props, "include_start_date", sizeof("include_start_date")-1, &zv);
5714
5715
0
  ZVAL_BOOL(&zv, period_obj->include_end_date);
5716
0
  zend_hash_str_update(props, "include_end_date", sizeof("include_end_date")-1, &zv);
5717
0
}
5718
5719
static bool php_date_period_initialize_from_hash(php_period_obj *period_obj, const HashTable *myht) /* {{{ */
5720
50
{
5721
50
  zval *ht_entry;
5722
5723
  /* this function does no rollback on error */
5724
5725
50
  ht_entry = zend_hash_str_find(myht, "start", sizeof("start")-1);
5726
50
  if (ht_entry) {
5727
0
    if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
5728
0
      php_date_obj *date_obj;
5729
0
      date_obj = Z_PHPDATE_P(ht_entry);
5730
5731
0
      if (!date_obj->time) {
5732
0
        return false;
5733
0
      }
5734
5735
0
      if (period_obj->start != NULL) {
5736
0
        timelib_time_dtor(period_obj->start);
5737
0
      }
5738
0
      period_obj->start = timelib_time_clone(date_obj->time);
5739
0
      period_obj->start_ce = Z_OBJCE_P(ht_entry);
5740
0
    } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
5741
0
      return false;
5742
0
    }
5743
50
  } else {
5744
50
    return false;
5745
50
  }
5746
5747
0
  ht_entry = zend_hash_str_find(myht, "end", sizeof("end")-1);
5748
0
  if (ht_entry) {
5749
0
    if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
5750
0
      php_date_obj *date_obj;
5751
0
      date_obj = Z_PHPDATE_P(ht_entry);
5752
5753
0
      if (!date_obj->time || !period_obj->start_ce) {
5754
0
        return false;
5755
0
      }
5756
5757
0
      if (period_obj->end != NULL) {
5758
0
        timelib_time_dtor(period_obj->end);
5759
0
      }
5760
0
      period_obj->end = timelib_time_clone(date_obj->time);
5761
0
    } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
5762
0
      return false;
5763
0
    }
5764
0
  } else {
5765
0
    return false;
5766
0
  }
5767
5768
0
  ht_entry = zend_hash_str_find(myht, "current", sizeof("current")-1);
5769
0
  if (ht_entry) {
5770
0
    if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
5771
0
      php_date_obj *date_obj;
5772
0
      date_obj = Z_PHPDATE_P(ht_entry);
5773
5774
0
      if (!date_obj->time || !period_obj->start_ce) {
5775
0
        return false;
5776
0
      }
5777
5778
0
      if (period_obj->current != NULL) {
5779
0
        timelib_time_dtor(period_obj->current);
5780
0
      }
5781
0
      period_obj->current = timelib_time_clone(date_obj->time);
5782
0
    } else if (Z_TYPE_P(ht_entry) != IS_NULL)  {
5783
0
      return false;
5784
0
    }
5785
0
  } else {
5786
0
    return false;
5787
0
  }
5788
5789
0
  ht_entry = zend_hash_str_find(myht, "interval", sizeof("interval")-1);
5790
0
  if (ht_entry) {
5791
0
    if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_interval) {
5792
0
      php_interval_obj *interval_obj;
5793
0
      interval_obj = Z_PHPINTERVAL_P(ht_entry);
5794
5795
0
      if (!interval_obj->initialized) {
5796
0
        return false;
5797
0
      }
5798
5799
0
      if (period_obj->interval != NULL) {
5800
0
        timelib_rel_time_dtor(period_obj->interval);
5801
0
      }
5802
0
      period_obj->interval = timelib_rel_time_clone(interval_obj->diff);
5803
0
    } else { /* interval is required */
5804
0
      return false;
5805
0
    }
5806
0
  } else {
5807
0
    return false;
5808
0
  }
5809
5810
0
  ht_entry = zend_hash_str_find(myht, "recurrences", sizeof("recurrences")-1);
5811
0
  if (ht_entry &&
5812
0
      Z_TYPE_P(ht_entry) == IS_LONG && Z_LVAL_P(ht_entry) >= 0 && Z_LVAL_P(ht_entry) <= INT_MAX) {
5813
0
    period_obj->recurrences = Z_LVAL_P(ht_entry);
5814
0
  } else {
5815
0
    return false;
5816
0
  }
5817
5818
0
  ht_entry = zend_hash_str_find(myht, "include_start_date", sizeof("include_start_date")-1);
5819
0
  if (ht_entry &&
5820
0
      (Z_TYPE_P(ht_entry) == IS_FALSE || Z_TYPE_P(ht_entry) == IS_TRUE)) {
5821
0
    period_obj->include_start_date = (Z_TYPE_P(ht_entry) == IS_TRUE);
5822
0
  } else {
5823
0
    return false;
5824
0
  }
5825
5826
0
  ht_entry = zend_hash_str_find(myht, "include_end_date", sizeof("include_end_date")-1);
5827
0
  if (ht_entry &&
5828
0
      (Z_TYPE_P(ht_entry) == IS_FALSE || Z_TYPE_P(ht_entry) == IS_TRUE)) {
5829
0
    period_obj->include_end_date = (Z_TYPE_P(ht_entry) == IS_TRUE);
5830
0
  } else {
5831
0
    return false;
5832
0
  }
5833
5834
0
  period_obj->initialized = true;
5835
5836
0
  return true;
5837
0
} /* }}} */
5838
5839
/* {{{ */
5840
PHP_METHOD(DatePeriod, __set_state)
5841
0
{
5842
0
  php_period_obj   *period_obj;
5843
0
  HashTable        *myht;
5844
5845
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
5846
0
    Z_PARAM_ARRAY_HT(myht)
5847
0
  ZEND_PARSE_PARAMETERS_END();
5848
5849
0
  object_init_ex(return_value, date_ce_period);
5850
0
  period_obj = Z_PHPPERIOD_P(return_value);
5851
0
  if (!php_date_period_initialize_from_hash(period_obj, myht)) {
5852
0
    zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
5853
0
    RETURN_THROWS();
5854
0
  }
5855
0
}
5856
/* }}} */
5857
5858
/* {{{ */
5859
PHP_METHOD(DatePeriod, __serialize)
5860
0
{
5861
0
  zval             *object = ZEND_THIS;
5862
0
  php_period_obj   *period_obj;
5863
0
  HashTable        *myht;
5864
5865
0
  ZEND_PARSE_PARAMETERS_NONE();
5866
5867
0
  period_obj = Z_PHPPERIOD_P(object);
5868
0
  DATE_CHECK_INITIALIZED(period_obj->start, Z_OBJCE_P(object));
5869
5870
0
  array_init(return_value);
5871
0
  myht = Z_ARRVAL_P(return_value);
5872
0
  date_period_object_to_hash(period_obj, myht);
5873
5874
0
  add_common_properties(myht, &period_obj->std);
5875
0
}
5876
/* }}} */
5877
5878
/* {{{ date_period_is_internal_property
5879
 *  Common for date_period_read_property(), date_period_write_property(), and
5880
 *  restore_custom_dateperiod_properties functions
5881
 */
5882
static bool date_period_is_internal_property(const zend_string *name)
5883
15
{
5884
15
  if (
5885
15
    zend_string_equals_literal(name, "start") ||
5886
15
    zend_string_equals_literal(name, "current") ||
5887
15
    zend_string_equals_literal(name, "end") ||
5888
15
    zend_string_equals_literal(name, "interval") ||
5889
15
    zend_string_equals_literal(name, "recurrences") ||
5890
15
    zend_string_equals_literal(name, "include_start_date") ||
5891
15
    zend_string_equals_literal(name, "include_end_date")
5892
15
  ) {
5893
0
    return true;
5894
0
  }
5895
15
  return false;
5896
15
}
5897
/* }}} */
5898
5899
static void restore_custom_dateperiod_properties(zval *object, const HashTable *myht)
5900
0
{
5901
0
  zend_string      *prop_name;
5902
0
  zval             *prop_val;
5903
5904
0
  ZEND_HASH_FOREACH_STR_KEY_VAL(myht, prop_name, prop_val) {
5905
0
    if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_period_is_internal_property(prop_name)) {
5906
0
      continue;
5907
0
    }
5908
0
    update_property(Z_OBJ_P(object), prop_name, prop_val);
5909
0
  } ZEND_HASH_FOREACH_END();
5910
0
}
5911
5912
/* {{{ */
5913
PHP_METHOD(DatePeriod, __unserialize)
5914
50
{
5915
50
  zval             *object = ZEND_THIS;
5916
50
  php_period_obj   *period_obj;
5917
50
  HashTable        *myht;
5918
5919
150
  ZEND_PARSE_PARAMETERS_START(1, 1)
5920
200
    Z_PARAM_ARRAY_HT(myht)
5921
50
  ZEND_PARSE_PARAMETERS_END();
5922
5923
50
  period_obj = Z_PHPPERIOD_P(object);
5924
5925
50
  if (!php_date_period_initialize_from_hash(period_obj, myht)) {
5926
50
    zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
5927
50
    RETURN_THROWS();
5928
50
  }
5929
0
  restore_custom_dateperiod_properties(object, myht);
5930
0
}
5931
/* }}} */
5932
5933
/* {{{ */
5934
PHP_METHOD(DatePeriod, __wakeup)
5935
0
{
5936
0
  zval             *object = ZEND_THIS;
5937
0
  php_period_obj   *period_obj;
5938
0
  const HashTable  *myht;
5939
5940
0
  ZEND_PARSE_PARAMETERS_NONE();
5941
5942
0
  period_obj = Z_PHPPERIOD_P(object);
5943
5944
0
  myht = Z_OBJPROP_P(object);
5945
5946
0
  if (!php_date_period_initialize_from_hash(period_obj, myht)) {
5947
0
    zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
5948
0
    RETURN_THROWS();
5949
0
  }
5950
5951
0
  restore_custom_dateperiod_properties(object, myht);
5952
0
}
5953
/* }}} */
5954
5955
static int date_period_has_property(zend_object *object, zend_string *name, int type, void **cache_slot)
5956
0
{
5957
0
  zval rv;
5958
0
  zval *prop;
5959
5960
0
  if (!date_period_is_internal_property(name)) {
5961
0
    return zend_std_has_property(object, name, type, cache_slot);
5962
0
  }
5963
5964
0
  php_period_obj *period_obj = php_period_obj_from_obj(object);
5965
0
  if (!period_obj->initialized) {
5966
0
    switch (type) {
5967
0
      case ZEND_PROPERTY_ISSET: /* Intentional fallthrough */
5968
0
      case ZEND_PROPERTY_NOT_EMPTY:
5969
0
        return 0;
5970
0
      case ZEND_PROPERTY_EXISTS:
5971
0
        return 1;
5972
0
      EMPTY_SWITCH_DEFAULT_CASE()
5973
0
    }
5974
0
  }
5975
5976
0
  if (type == ZEND_PROPERTY_EXISTS) {
5977
0
    return 1;
5978
0
  }
5979
5980
0
  prop = date_period_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
5981
0
  ZEND_ASSERT(prop != &EG(uninitialized_zval));
5982
5983
0
  bool result;
5984
5985
0
  if (type == ZEND_PROPERTY_NOT_EMPTY) {
5986
0
    result = zend_is_true(prop);
5987
0
  } else if (type == ZEND_PROPERTY_ISSET) {
5988
0
    result = Z_TYPE_P(prop) != IS_NULL;
5989
0
  } else {
5990
0
    ZEND_UNREACHABLE();
5991
0
  }
5992
5993
0
  zval_ptr_dtor(prop);
5994
5995
0
  return result;
5996
0
}
5997
5998
/* {{{ date_period_read_property */
5999
static zval *date_period_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
6000
0
{
6001
0
  if (date_period_is_internal_property(name)) {
6002
0
    if (type == BP_VAR_IS || type == BP_VAR_R) {
6003
0
      php_period_obj *period_obj = php_period_obj_from_obj(object);
6004
6005
0
      if (zend_string_equals_literal(name, "start")) {
6006
0
        create_date_period_datetime(period_obj->start, period_obj->start_ce, rv);
6007
0
        return rv;
6008
0
      } else if (zend_string_equals_literal(name, "current")) {
6009
0
        create_date_period_datetime(period_obj->current, period_obj->start_ce, rv);
6010
0
        return rv;
6011
0
      } else if (zend_string_equals_literal(name, "end")) {
6012
0
        create_date_period_datetime(period_obj->end, period_obj->start_ce, rv);
6013
0
        return rv;
6014
0
      } else if (zend_string_equals_literal(name, "interval")) {
6015
0
        create_date_period_interval(period_obj->interval, rv);
6016
0
        return rv;
6017
0
      } else if (zend_string_equals_literal(name, "recurrences")) {
6018
0
        ZVAL_LONG(rv, period_obj->recurrences);
6019
0
        return rv;
6020
0
      } else if (zend_string_equals_literal(name, "include_start_date")) {
6021
0
        ZVAL_BOOL(rv, period_obj->include_start_date);
6022
0
        return rv;
6023
0
      } else if (zend_string_equals_literal(name, "include_end_date")) {
6024
0
        ZVAL_BOOL(rv, period_obj->include_end_date);
6025
0
        return rv;
6026
0
      }
6027
0
    } else {
6028
0
      zend_readonly_property_modification_error_ex("DatePeriod", ZSTR_VAL(name));
6029
0
      return &EG(uninitialized_zval);
6030
0
    }
6031
0
  }
6032
6033
0
  return zend_std_read_property(object, name, type, cache_slot, rv);
6034
0
}
6035
/* }}} */
6036
6037
static zval *date_period_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
6038
15
{
6039
15
  if (date_period_is_internal_property(name)) {
6040
0
    zend_readonly_property_modification_error_ex("DatePeriod", ZSTR_VAL(name));
6041
0
    return value;
6042
0
  }
6043
6044
15
  return zend_std_write_property(object, name, value, cache_slot);
6045
15
}
6046
6047
static zval *date_period_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
6048
0
{
6049
0
  if (date_period_is_internal_property(name)) {
6050
0
    zend_readonly_property_modification_error_ex("DatePeriod", ZSTR_VAL(name));
6051
0
    return &EG(error_zval);
6052
0
  }
6053
6054
0
  return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
6055
0
}
6056
6057
static HashTable *date_period_get_properties_for(zend_object *object, zend_prop_purpose purpose)
6058
0
{
6059
0
  php_period_obj *period_obj = php_period_obj_from_obj(object);
6060
0
  HashTable *props = zend_array_dup(zend_std_get_properties(object));
6061
0
  if (!period_obj->initialized) {
6062
0
    return props;
6063
0
  }
6064
6065
0
  date_period_object_to_hash(period_obj, props);
6066
6067
0
  return props;
6068
0
}
6069
6070
static void date_period_unset_property(zend_object *object, zend_string *name, void **cache_slot)
6071
0
{
6072
0
  if (date_period_is_internal_property(name)) {
6073
0
    zend_throw_error(NULL, "Cannot unset %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
6074
0
    return;
6075
0
  }
6076
6077
0
  zend_std_unset_property(object, name, cache_slot);
6078
0
}