Coverage Report

Created: 2026-10-28 06:20

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