PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/date - php_date.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1877 2095 89.6 %
Date: 2016-09-18 Functions: 151 166 91.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2016 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "php.h"
      22             : #include "php_streams.h"
      23             : #include "php_main.h"
      24             : #include "php_globals.h"
      25             : #include "php_ini.h"
      26             : #include "ext/standard/info.h"
      27             : #include "ext/standard/php_versioning.h"
      28             : #include "ext/standard/php_math.h"
      29             : #include "php_date.h"
      30             : #include "zend_interfaces.h"
      31             : #include "lib/timelib.h"
      32             : #include <time.h>
      33             : 
      34             : #ifdef PHP_WIN32
      35             : static __inline __int64 php_date_llabs( __int64 i ) { return i >= 0? i: -i; }
      36             : #elif defined(__GNUC__) && __GNUC__ < 3
      37             : static __inline __int64_t php_date_llabs( __int64_t i ) { return i >= 0 ? i : -i; }
      38             : #else
      39        5773 : static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i; }
      40             : #endif
      41             : 
      42             : #ifdef PHP_WIN32
      43             : #define DATE_I64_BUF_LEN 65
      44             : # define DATE_I64A(i, s, len) _i64toa_s(i, s, len, 10)
      45             : # define DATE_A64I(i, s) i = _atoi64(s)
      46             : #else
      47             : #define DATE_I64_BUF_LEN 65
      48             : # define DATE_I64A(i, s, len) \
      49             :         do { \
      50             :                 int st = snprintf(s, len, "%lld", i); \
      51             :                 s[st] = '\0'; \
      52             :         } while (0);
      53             : #ifdef HAVE_ATOLL
      54             : # define DATE_A64I(i, s) i = atoll(s)
      55             : #else
      56             : # define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
      57             : #endif
      58             : #endif
      59             : 
      60             : /* {{{ arginfo */
      61             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date, 0, 0, 1)
      62             :         ZEND_ARG_INFO(0, format)
      63             :         ZEND_ARG_INFO(0, timestamp)
      64             : ZEND_END_ARG_INFO()
      65             : 
      66             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmdate, 0, 0, 1)
      67             :         ZEND_ARG_INFO(0, format)
      68             :         ZEND_ARG_INFO(0, timestamp)
      69             : ZEND_END_ARG_INFO()
      70             : 
      71             : ZEND_BEGIN_ARG_INFO_EX(arginfo_idate, 0, 0, 1)
      72             :         ZEND_ARG_INFO(0, format)
      73             :         ZEND_ARG_INFO(0, timestamp)
      74             : ZEND_END_ARG_INFO()
      75             : 
      76             : ZEND_BEGIN_ARG_INFO_EX(arginfo_strtotime, 0, 0, 1)
      77             :         ZEND_ARG_INFO(0, time)
      78             :         ZEND_ARG_INFO(0, now)
      79             : ZEND_END_ARG_INFO()
      80             : 
      81             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mktime, 0, 0, 0)
      82             :         ZEND_ARG_INFO(0, hour)
      83             :         ZEND_ARG_INFO(0, min)
      84             :         ZEND_ARG_INFO(0, sec)
      85             :         ZEND_ARG_INFO(0, mon)
      86             :         ZEND_ARG_INFO(0, day)
      87             :         ZEND_ARG_INFO(0, year)
      88             : ZEND_END_ARG_INFO()
      89             : 
      90             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmmktime, 0, 0, 0)
      91             :         ZEND_ARG_INFO(0, hour)
      92             :         ZEND_ARG_INFO(0, min)
      93             :         ZEND_ARG_INFO(0, sec)
      94             :         ZEND_ARG_INFO(0, mon)
      95             :         ZEND_ARG_INFO(0, day)
      96             :         ZEND_ARG_INFO(0, year)
      97             : ZEND_END_ARG_INFO()
      98             : 
      99             : ZEND_BEGIN_ARG_INFO(arginfo_checkdate, 0)
     100             :         ZEND_ARG_INFO(0, month)
     101             :         ZEND_ARG_INFO(0, day)
     102             :         ZEND_ARG_INFO(0, year)
     103             : ZEND_END_ARG_INFO()
     104             : 
     105             : ZEND_BEGIN_ARG_INFO_EX(arginfo_strftime, 0, 0, 1)
     106             :         ZEND_ARG_INFO(0, format)
     107             :         ZEND_ARG_INFO(0, timestamp)
     108             : ZEND_END_ARG_INFO()
     109             : 
     110             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmstrftime, 0, 0, 1)
     111             :         ZEND_ARG_INFO(0, format)
     112             :         ZEND_ARG_INFO(0, timestamp)
     113             : ZEND_END_ARG_INFO()
     114             : 
     115             : ZEND_BEGIN_ARG_INFO(arginfo_time, 0)
     116             : ZEND_END_ARG_INFO()
     117             : 
     118             : ZEND_BEGIN_ARG_INFO_EX(arginfo_localtime, 0, 0, 0)
     119             :         ZEND_ARG_INFO(0, timestamp)
     120             :         ZEND_ARG_INFO(0, associative_array)
     121             : ZEND_END_ARG_INFO()
     122             : 
     123             : ZEND_BEGIN_ARG_INFO_EX(arginfo_getdate, 0, 0, 0)
     124             :         ZEND_ARG_INFO(0, timestamp)
     125             : ZEND_END_ARG_INFO()
     126             : 
     127             : ZEND_BEGIN_ARG_INFO(arginfo_date_default_timezone_set, 0)
     128             :         ZEND_ARG_INFO(0, timezone_identifier)
     129             : ZEND_END_ARG_INFO()
     130             : 
     131             : ZEND_BEGIN_ARG_INFO(arginfo_date_default_timezone_get, 0)
     132             : ZEND_END_ARG_INFO()
     133             : 
     134             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_sunrise, 0, 0, 1)
     135             :         ZEND_ARG_INFO(0, time)
     136             :         ZEND_ARG_INFO(0, format)
     137             :         ZEND_ARG_INFO(0, latitude)
     138             :         ZEND_ARG_INFO(0, longitude)
     139             :         ZEND_ARG_INFO(0, zenith)
     140             :         ZEND_ARG_INFO(0, gmt_offset)
     141             : ZEND_END_ARG_INFO()
     142             : 
     143             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_sunset, 0, 0, 1)
     144             :         ZEND_ARG_INFO(0, time)
     145             :         ZEND_ARG_INFO(0, format)
     146             :         ZEND_ARG_INFO(0, latitude)
     147             :         ZEND_ARG_INFO(0, longitude)
     148             :         ZEND_ARG_INFO(0, zenith)
     149             :         ZEND_ARG_INFO(0, gmt_offset)
     150             : ZEND_END_ARG_INFO()
     151             : 
     152             : ZEND_BEGIN_ARG_INFO(arginfo_date_sun_info, 0)
     153             :         ZEND_ARG_INFO(0, time)
     154             :         ZEND_ARG_INFO(0, latitude)
     155             :         ZEND_ARG_INFO(0, longitude)
     156             : ZEND_END_ARG_INFO()
     157             : 
     158             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_create, 0, 0, 0)
     159             :         ZEND_ARG_INFO(0, time)
     160             :         ZEND_ARG_INFO(0, object)
     161             : ZEND_END_ARG_INFO()
     162             : 
     163             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_create_from_format, 0, 0, 2)
     164             :         ZEND_ARG_INFO(0, format)
     165             :         ZEND_ARG_INFO(0, time)
     166             :         ZEND_ARG_INFO(0, object)
     167             : ZEND_END_ARG_INFO()
     168             : 
     169             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_parse, 0, 0, 1)
     170             :         ZEND_ARG_INFO(0, date)
     171             : ZEND_END_ARG_INFO()
     172             : 
     173             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_parse_from_format, 0, 0, 2)
     174             :         ZEND_ARG_INFO(0, format)
     175             :         ZEND_ARG_INFO(0, date)
     176             : ZEND_END_ARG_INFO()
     177             : 
     178             : ZEND_BEGIN_ARG_INFO(arginfo_date_get_last_errors, 0)
     179             : ZEND_END_ARG_INFO()
     180             : 
     181             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_format, 0, 0, 2)
     182             :         ZEND_ARG_INFO(0, object)
     183             :         ZEND_ARG_INFO(0, format)
     184             : ZEND_END_ARG_INFO()
     185             : 
     186             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_format, 0, 0, 1)
     187             :         ZEND_ARG_INFO(0, format)
     188             : ZEND_END_ARG_INFO()
     189             : 
     190             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_modify, 0, 0, 2)
     191             :         ZEND_ARG_INFO(0, object)
     192             :         ZEND_ARG_INFO(0, modify)
     193             : ZEND_END_ARG_INFO()
     194             : 
     195             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_modify, 0, 0, 1)
     196             :         ZEND_ARG_INFO(0, modify)
     197             : ZEND_END_ARG_INFO()
     198             : 
     199             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_add, 0, 0, 2)
     200             :         ZEND_ARG_INFO(0, object)
     201             :         ZEND_ARG_INFO(0, interval)
     202             : ZEND_END_ARG_INFO()
     203             : 
     204             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_add, 0, 0, 1)
     205             :         ZEND_ARG_INFO(0, interval)
     206             : ZEND_END_ARG_INFO()
     207             : 
     208             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_sub, 0, 0, 2)
     209             :         ZEND_ARG_INFO(0, object)
     210             :         ZEND_ARG_INFO(0, interval)
     211             : ZEND_END_ARG_INFO()
     212             : 
     213             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_sub, 0, 0, 1)
     214             :         ZEND_ARG_INFO(0, interval)
     215             : ZEND_END_ARG_INFO()
     216             : 
     217             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_timezone_get, 0, 0, 1)
     218             :         ZEND_ARG_INFO(0, object)
     219             : ZEND_END_ARG_INFO()
     220             : 
     221             : ZEND_BEGIN_ARG_INFO(arginfo_date_method_timezone_get, 0)
     222             : ZEND_END_ARG_INFO()
     223             : 
     224             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_timezone_set, 0, 0, 2)
     225             :         ZEND_ARG_INFO(0, object)
     226             :         ZEND_ARG_INFO(0, timezone)
     227             : ZEND_END_ARG_INFO()
     228             : 
     229             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_timezone_set, 0, 0, 1)
     230             :         ZEND_ARG_INFO(0, timezone)
     231             : ZEND_END_ARG_INFO()
     232             : 
     233             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_offset_get, 0, 0, 1)
     234             :         ZEND_ARG_INFO(0, object)
     235             : ZEND_END_ARG_INFO()
     236             : 
     237             : ZEND_BEGIN_ARG_INFO(arginfo_date_method_offset_get, 0)
     238             : ZEND_END_ARG_INFO()
     239             : 
     240             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_diff, 0, 0, 2)
     241             :         ZEND_ARG_INFO(0, object)
     242             :         ZEND_ARG_INFO(0, object2)
     243             :         ZEND_ARG_INFO(0, absolute)
     244             : ZEND_END_ARG_INFO()
     245             : 
     246             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_diff, 0, 0, 1)
     247             :         ZEND_ARG_INFO(0, object)
     248             :         ZEND_ARG_INFO(0, absolute)
     249             : ZEND_END_ARG_INFO()
     250             : 
     251             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_time_set, 0, 0, 3)
     252             :         ZEND_ARG_INFO(0, object)
     253             :         ZEND_ARG_INFO(0, hour)
     254             :         ZEND_ARG_INFO(0, minute)
     255             :         ZEND_ARG_INFO(0, second)
     256             : ZEND_END_ARG_INFO()
     257             : 
     258             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_time_set, 0, 0, 2)
     259             :         ZEND_ARG_INFO(0, hour)
     260             :         ZEND_ARG_INFO(0, minute)
     261             :         ZEND_ARG_INFO(0, second)
     262             : ZEND_END_ARG_INFO()
     263             : 
     264             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_date_set, 0, 0, 4)
     265             :         ZEND_ARG_INFO(0, object)
     266             :         ZEND_ARG_INFO(0, year)
     267             :         ZEND_ARG_INFO(0, month)
     268             :         ZEND_ARG_INFO(0, day)
     269             : ZEND_END_ARG_INFO()
     270             : 
     271             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_date_set, 0, 0, 3)
     272             :         ZEND_ARG_INFO(0, year)
     273             :         ZEND_ARG_INFO(0, month)
     274             :         ZEND_ARG_INFO(0, day)
     275             : ZEND_END_ARG_INFO()
     276             : 
     277             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_isodate_set, 0, 0, 3)
     278             :         ZEND_ARG_INFO(0, object)
     279             :         ZEND_ARG_INFO(0, year)
     280             :         ZEND_ARG_INFO(0, week)
     281             :         ZEND_ARG_INFO(0, day)
     282             : ZEND_END_ARG_INFO()
     283             : 
     284             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_isodate_set, 0, 0, 2)
     285             :         ZEND_ARG_INFO(0, year)
     286             :         ZEND_ARG_INFO(0, week)
     287             :         ZEND_ARG_INFO(0, day)
     288             : ZEND_END_ARG_INFO()
     289             : 
     290             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_timestamp_set, 0, 0, 2)
     291             :         ZEND_ARG_INFO(0, object)
     292             :         ZEND_ARG_INFO(0, unixtimestamp)
     293             : ZEND_END_ARG_INFO()
     294             : 
     295             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_timestamp_set, 0, 0, 1)
     296             :         ZEND_ARG_INFO(0, unixtimestamp)
     297             : ZEND_END_ARG_INFO()
     298             : 
     299             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_timestamp_get, 0, 0, 1)
     300             :         ZEND_ARG_INFO(0, object)
     301             : ZEND_END_ARG_INFO()
     302             : 
     303             : ZEND_BEGIN_ARG_INFO(arginfo_date_method_timestamp_get, 0)
     304             : ZEND_END_ARG_INFO()
     305             : 
     306             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_mutable, 0, 0, 1)
     307             :         ZEND_ARG_INFO(0, DateTime)
     308             : ZEND_END_ARG_INFO()
     309             : 
     310             : ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_open, 0, 0, 1)
     311             :         ZEND_ARG_INFO(0, timezone)
     312             : ZEND_END_ARG_INFO()
     313             : 
     314             : ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_name_get, 0, 0, 1)
     315             :         ZEND_ARG_INFO(0, object)
     316             : ZEND_END_ARG_INFO()
     317             : 
     318             : ZEND_BEGIN_ARG_INFO(arginfo_timezone_method_name_get, 0)
     319             : ZEND_END_ARG_INFO()
     320             : 
     321             : ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_name_from_abbr, 0, 0, 1)
     322             :         ZEND_ARG_INFO(0, abbr)
     323             :         ZEND_ARG_INFO(0, gmtoffset)
     324             :         ZEND_ARG_INFO(0, isdst)
     325             : ZEND_END_ARG_INFO()
     326             : 
     327             : ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_offset_get, 0, 0, 2)
     328             :         ZEND_ARG_OBJ_INFO(0, object, DateTimeZone, 0)
     329             :         ZEND_ARG_OBJ_INFO(0, datetime, DateTimeInterface, 0)
     330             : ZEND_END_ARG_INFO()
     331             : 
     332             : ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_method_offset_get, 0, 0, 1)
     333             :         ZEND_ARG_INFO(0, object)
     334             : ZEND_END_ARG_INFO()
     335             : 
     336             : ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_transitions_get, 0, 0, 1)
     337             :         ZEND_ARG_INFO(0, object)
     338             :         ZEND_ARG_INFO(0, timestamp_begin)
     339             :         ZEND_ARG_INFO(0, timestamp_end)
     340             : ZEND_END_ARG_INFO()
     341             : 
     342             : ZEND_BEGIN_ARG_INFO(arginfo_timezone_method_transitions_get, 0)
     343             :         ZEND_ARG_INFO(0, timestamp_begin)
     344             :         ZEND_ARG_INFO(0, timestamp_end)
     345             : ZEND_END_ARG_INFO()
     346             : 
     347             : ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_location_get, 0, 0, 1)
     348             :         ZEND_ARG_INFO(0, object)
     349             : ZEND_END_ARG_INFO()
     350             : 
     351             : ZEND_BEGIN_ARG_INFO(arginfo_timezone_method_location_get, 0)
     352             : ZEND_END_ARG_INFO()
     353             : 
     354             : ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_identifiers_list, 0, 0, 0)
     355             :         ZEND_ARG_INFO(0, what)
     356             :         ZEND_ARG_INFO(0, country)
     357             : ZEND_END_ARG_INFO()
     358             : 
     359             : ZEND_BEGIN_ARG_INFO(arginfo_timezone_abbreviations_list, 0)
     360             : ZEND_END_ARG_INFO()
     361             : 
     362             : ZEND_BEGIN_ARG_INFO(arginfo_timezone_version_get, 0)
     363             : ZEND_END_ARG_INFO()
     364             : 
     365             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_interval_create_from_date_string, 0, 0, 1)
     366             :         ZEND_ARG_INFO(0, time)
     367             : ZEND_END_ARG_INFO()
     368             : 
     369             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_interval_format, 0, 0, 2)
     370             :         ZEND_ARG_INFO(0, object)
     371             :         ZEND_ARG_INFO(0, format)
     372             : ZEND_END_ARG_INFO()
     373             : 
     374             : ZEND_BEGIN_ARG_INFO(arginfo_date_method_interval_format, 0)
     375             :         ZEND_ARG_INFO(0, format)
     376             : ZEND_END_ARG_INFO()
     377             : 
     378             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_period_construct, 0, 0, 3)
     379             :         ZEND_ARG_INFO(0, start)
     380             :         ZEND_ARG_INFO(0, interval)
     381             :         ZEND_ARG_INFO(0, end)
     382             : ZEND_END_ARG_INFO()
     383             : 
     384             : ZEND_BEGIN_ARG_INFO_EX(arginfo_date_interval_construct, 0, 0, 1)
     385             :         ZEND_ARG_INFO(0, interval_spec)
     386             : ZEND_END_ARG_INFO()
     387             : /* }}} */
     388             : 
     389             : /* {{{ Function table */
     390             : const zend_function_entry date_functions[] = {
     391             :         PHP_FE(strtotime, arginfo_strtotime)
     392             :         PHP_FE(date, arginfo_date)
     393             :         PHP_FE(idate, arginfo_idate)
     394             :         PHP_FE(gmdate, arginfo_gmdate)
     395             :         PHP_FE(mktime, arginfo_mktime)
     396             :         PHP_FE(gmmktime, arginfo_gmmktime)
     397             :         PHP_FE(checkdate, arginfo_checkdate)
     398             : 
     399             : #ifdef HAVE_STRFTIME
     400             :         PHP_FE(strftime, arginfo_strftime)
     401             :         PHP_FE(gmstrftime, arginfo_gmstrftime)
     402             : #endif
     403             : 
     404             :         PHP_FE(time, arginfo_time)
     405             :         PHP_FE(localtime, arginfo_localtime)
     406             :         PHP_FE(getdate, arginfo_getdate)
     407             : 
     408             :         /* Advanced Interface */
     409             :         PHP_FE(date_create, arginfo_date_create)
     410             :         PHP_FE(date_create_immutable, arginfo_date_create)
     411             :         PHP_FE(date_create_from_format, arginfo_date_create_from_format)
     412             :         PHP_FE(date_create_immutable_from_format, arginfo_date_create_from_format)
     413             :         PHP_FE(date_parse, arginfo_date_parse)
     414             :         PHP_FE(date_parse_from_format, arginfo_date_parse_from_format)
     415             :         PHP_FE(date_get_last_errors, arginfo_date_get_last_errors)
     416             :         PHP_FE(date_format, arginfo_date_format)
     417             :         PHP_FE(date_modify, arginfo_date_modify)
     418             :         PHP_FE(date_add, arginfo_date_add)
     419             :         PHP_FE(date_sub, arginfo_date_sub)
     420             :         PHP_FE(date_timezone_get, arginfo_date_timezone_get)
     421             :         PHP_FE(date_timezone_set, arginfo_date_timezone_set)
     422             :         PHP_FE(date_offset_get, arginfo_date_offset_get)
     423             :         PHP_FE(date_diff, arginfo_date_diff)
     424             : 
     425             :         PHP_FE(date_time_set, arginfo_date_time_set)
     426             :         PHP_FE(date_date_set, arginfo_date_date_set)
     427             :         PHP_FE(date_isodate_set, arginfo_date_isodate_set)
     428             :         PHP_FE(date_timestamp_set, arginfo_date_timestamp_set)
     429             :         PHP_FE(date_timestamp_get, arginfo_date_timestamp_get)
     430             : 
     431             :         PHP_FE(timezone_open, arginfo_timezone_open)
     432             :         PHP_FE(timezone_name_get, arginfo_timezone_name_get)
     433             :         PHP_FE(timezone_name_from_abbr, arginfo_timezone_name_from_abbr)
     434             :         PHP_FE(timezone_offset_get, arginfo_timezone_offset_get)
     435             :         PHP_FE(timezone_transitions_get, arginfo_timezone_transitions_get)
     436             :         PHP_FE(timezone_location_get, arginfo_timezone_location_get)
     437             :         PHP_FE(timezone_identifiers_list, arginfo_timezone_identifiers_list)
     438             :         PHP_FE(timezone_abbreviations_list, arginfo_timezone_abbreviations_list)
     439             :         PHP_FE(timezone_version_get, arginfo_timezone_version_get)
     440             : 
     441             :         PHP_FE(date_interval_create_from_date_string, arginfo_date_interval_create_from_date_string)
     442             :         PHP_FE(date_interval_format, arginfo_date_interval_format)
     443             : 
     444             :         /* Options and Configuration */
     445             :         PHP_FE(date_default_timezone_set, arginfo_date_default_timezone_set)
     446             :         PHP_FE(date_default_timezone_get, arginfo_date_default_timezone_get)
     447             : 
     448             :         /* Astronomical functions */
     449             :         PHP_FE(date_sunrise, arginfo_date_sunrise)
     450             :         PHP_FE(date_sunset, arginfo_date_sunset)
     451             :         PHP_FE(date_sun_info, arginfo_date_sun_info)
     452             :         PHP_FE_END
     453             : };
     454             : 
     455             : static const zend_function_entry date_funcs_interface[] = {
     456             :         PHP_ABSTRACT_ME(DateTimeInterface, format, arginfo_date_method_format)
     457             :         PHP_ABSTRACT_ME(DateTimeInterface, getTimezone, arginfo_date_method_timezone_get)
     458             :         PHP_ABSTRACT_ME(DateTimeInterface, getOffset, arginfo_date_method_offset_get)
     459             :         PHP_ABSTRACT_ME(DateTimeInterface, getTimestamp, arginfo_date_method_timestamp_get)
     460             :         PHP_ABSTRACT_ME(DateTimeInterface, diff, arginfo_date_method_diff)
     461             :         PHP_ABSTRACT_ME(DateTimeInterface, __wakeup, NULL)
     462             :         PHP_FE_END
     463             : };
     464             : 
     465             : const zend_function_entry date_funcs_date[] = {
     466             :         PHP_ME(DateTime,                        __construct,            arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
     467             :         PHP_ME(DateTime,                        __wakeup,                       NULL, ZEND_ACC_PUBLIC)
     468             :         PHP_ME(DateTime,                        __set_state,            NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     469             :         PHP_ME_MAPPING(createFromFormat, date_create_from_format,       arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     470             :         PHP_ME_MAPPING(getLastErrors, date_get_last_errors,     arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     471             :         PHP_ME_MAPPING(format,          date_format,            arginfo_date_method_format, 0)
     472             :         PHP_ME_MAPPING(modify,          date_modify,            arginfo_date_method_modify, 0)
     473             :         PHP_ME_MAPPING(add,                     date_add,                       arginfo_date_method_add, 0)
     474             :         PHP_ME_MAPPING(sub,                     date_sub,                       arginfo_date_method_sub, 0)
     475             :         PHP_ME_MAPPING(getTimezone, date_timezone_get,  arginfo_date_method_timezone_get, 0)
     476             :         PHP_ME_MAPPING(setTimezone, date_timezone_set,  arginfo_date_method_timezone_set, 0)
     477             :         PHP_ME_MAPPING(getOffset,       date_offset_get,        arginfo_date_method_offset_get, 0)
     478             :         PHP_ME_MAPPING(setTime,         date_time_set,          arginfo_date_method_time_set, 0)
     479             :         PHP_ME_MAPPING(setDate,         date_date_set,          arginfo_date_method_date_set, 0)
     480             :         PHP_ME_MAPPING(setISODate,      date_isodate_set,       arginfo_date_method_isodate_set, 0)
     481             :         PHP_ME_MAPPING(setTimestamp,    date_timestamp_set, arginfo_date_method_timestamp_set, 0)
     482             :         PHP_ME_MAPPING(getTimestamp,    date_timestamp_get, arginfo_date_method_timestamp_get, 0)
     483             :         PHP_ME_MAPPING(diff,                    date_diff, arginfo_date_method_diff, 0)
     484             :         PHP_FE_END
     485             : };
     486             : 
     487             : const zend_function_entry date_funcs_immutable[] = {
     488             :         PHP_ME(DateTimeImmutable, __construct,   arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
     489             :         PHP_ME(DateTime, __wakeup,       NULL, ZEND_ACC_PUBLIC)
     490             :         PHP_ME(DateTimeImmutable, __set_state,   NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     491             :         PHP_ME_MAPPING(createFromFormat, date_create_immutable_from_format, arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     492             :         PHP_ME_MAPPING(getLastErrors,    date_get_last_errors,    arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     493             :         PHP_ME_MAPPING(format,           date_format,             arginfo_date_method_format, 0)
     494             :         PHP_ME_MAPPING(getTimezone, date_timezone_get,  arginfo_date_method_timezone_get, 0)
     495             :         PHP_ME_MAPPING(getOffset,       date_offset_get,        arginfo_date_method_offset_get, 0)
     496             :         PHP_ME_MAPPING(getTimestamp,    date_timestamp_get, arginfo_date_method_timestamp_get, 0)
     497             :         PHP_ME_MAPPING(diff,                    date_diff, arginfo_date_method_diff, 0)
     498             :         PHP_ME(DateTimeImmutable, modify,        arginfo_date_method_modify, 0)
     499             :         PHP_ME(DateTimeImmutable, add,           arginfo_date_method_add, 0)
     500             :         PHP_ME(DateTimeImmutable, sub,           arginfo_date_method_sub, 0)
     501             :         PHP_ME(DateTimeImmutable, setTimezone,   arginfo_date_method_timezone_set, 0)
     502             :         PHP_ME(DateTimeImmutable, setTime,       arginfo_date_method_time_set, 0)
     503             :         PHP_ME(DateTimeImmutable, setDate,       arginfo_date_method_date_set, 0)
     504             :         PHP_ME(DateTimeImmutable, setISODate,    arginfo_date_method_isodate_set, 0)
     505             :         PHP_ME(DateTimeImmutable, setTimestamp,  arginfo_date_method_timestamp_set, 0)
     506             :         PHP_ME(DateTimeImmutable, createFromMutable, arginfo_date_method_create_from_mutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     507             :         PHP_FE_END
     508             : };
     509             : 
     510             : const zend_function_entry date_funcs_timezone[] = {
     511             :         PHP_ME(DateTimeZone,              __construct,                 arginfo_timezone_open, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
     512             :         PHP_ME(DateTimeZone,              __wakeup,                    NULL, ZEND_ACC_PUBLIC)
     513             :         PHP_ME(DateTimeZone,              __set_state,                 NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     514             :         PHP_ME_MAPPING(getName,           timezone_name_get,           arginfo_timezone_method_name_get, 0)
     515             :         PHP_ME_MAPPING(getOffset,         timezone_offset_get,         arginfo_timezone_method_offset_get, 0)
     516             :         PHP_ME_MAPPING(getTransitions,    timezone_transitions_get,    arginfo_timezone_method_transitions_get, 0)
     517             :         PHP_ME_MAPPING(getLocation,       timezone_location_get,       arginfo_timezone_method_location_get, 0)
     518             :         PHP_ME_MAPPING(listAbbreviations, timezone_abbreviations_list, arginfo_timezone_abbreviations_list, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     519             :         PHP_ME_MAPPING(listIdentifiers,   timezone_identifiers_list,   arginfo_timezone_identifiers_list, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     520             :         PHP_FE_END
     521             : };
     522             : 
     523             : const zend_function_entry date_funcs_interval[] = {
     524             :         PHP_ME(DateInterval,              __construct,                 arginfo_date_interval_construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
     525             :         PHP_ME(DateInterval,              __wakeup,                    NULL, ZEND_ACC_PUBLIC)
     526             :         PHP_ME(DateInterval,              __set_state,                 NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     527             :         PHP_ME_MAPPING(format,            date_interval_format,        arginfo_date_method_interval_format, 0)
     528             :         PHP_ME_MAPPING(createFromDateString, date_interval_create_from_date_string,     arginfo_date_interval_create_from_date_string, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     529             :         PHP_FE_END
     530             : };
     531             : 
     532             : const zend_function_entry date_funcs_period[] = {
     533             :         PHP_ME(DatePeriod,                __construct,                 arginfo_date_period_construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
     534             :         PHP_ME(DatePeriod,                __wakeup,                    NULL, ZEND_ACC_PUBLIC)
     535             :         PHP_ME(DatePeriod,                __set_state,                 NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
     536             :         PHP_ME(DatePeriod,                getStartDate,                NULL, ZEND_ACC_PUBLIC)
     537             :         PHP_ME(DatePeriod,                getEndDate,                  NULL, ZEND_ACC_PUBLIC)
     538             :         PHP_ME(DatePeriod,                getDateInterval,             NULL, ZEND_ACC_PUBLIC)
     539             :         PHP_FE_END
     540             : };
     541             : 
     542             : static char* guess_timezone(const timelib_tzdb *tzdb);
     543             : static void date_register_classes(void);
     544             : /* }}} */
     545             : 
     546             : ZEND_DECLARE_MODULE_GLOBALS(date)
     547             : static PHP_GINIT_FUNCTION(date);
     548             : 
     549             : /* True global */
     550             : timelib_tzdb *php_date_global_timezone_db;
     551             : int php_date_global_timezone_db_enabled;
     552             : 
     553             : #define DATE_DEFAULT_LATITUDE "31.7667"
     554             : #define DATE_DEFAULT_LONGITUDE "35.2333"
     555             : 
     556             : /* on 90'35; common sunset declaration (start of sun body appear) */
     557             : #define DATE_SUNSET_ZENITH "90.583333"
     558             : 
     559             : /* on 90'35; common sunrise declaration (sun body disappeared) */
     560             : #define DATE_SUNRISE_ZENITH "90.583333"
     561             : 
     562             : static PHP_INI_MH(OnUpdate_date_timezone);
     563             : 
     564             : /* {{{ INI Settings */
     565             : PHP_INI_BEGIN()
     566             :         STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
     567             :         PHP_INI_ENTRY("date.default_latitude",           DATE_DEFAULT_LATITUDE,        PHP_INI_ALL, NULL)
     568             :         PHP_INI_ENTRY("date.default_longitude",          DATE_DEFAULT_LONGITUDE,       PHP_INI_ALL, NULL)
     569             :         PHP_INI_ENTRY("date.sunset_zenith",              DATE_SUNSET_ZENITH,           PHP_INI_ALL, NULL)
     570             :         PHP_INI_ENTRY("date.sunrise_zenith",             DATE_SUNRISE_ZENITH,          PHP_INI_ALL, NULL)
     571             : PHP_INI_END()
     572             : /* }}} */
     573             : 
     574             : zend_class_entry *date_ce_date, *date_ce_timezone, *date_ce_interval, *date_ce_period;
     575             : zend_class_entry *date_ce_immutable, *date_ce_interface;
     576             : 
     577             : 
     578          60 : PHPAPI zend_class_entry *php_date_get_date_ce(void)
     579             : {
     580          60 :         return date_ce_date;
     581             : }
     582             : 
     583           0 : PHPAPI zend_class_entry *php_date_get_immutable_ce(void)
     584             : {
     585           0 :         return date_ce_immutable;
     586             : }
     587             : 
     588           0 : PHPAPI zend_class_entry *php_date_get_interface_ce(void)
     589             : {
     590           0 :         return date_ce_interface;
     591             : }
     592             : 
     593          25 : PHPAPI zend_class_entry *php_date_get_timezone_ce(void)
     594             : {
     595          25 :         return date_ce_timezone;
     596             : }
     597             : 
     598             : static zend_object_handlers date_object_handlers_date;
     599             : static zend_object_handlers date_object_handlers_immutable;
     600             : static zend_object_handlers date_object_handlers_timezone;
     601             : static zend_object_handlers date_object_handlers_interval;
     602             : static zend_object_handlers date_object_handlers_period;
     603             : 
     604             : #define DATE_SET_CONTEXT \
     605             :         zval *object; \
     606             :         object = getThis(); \
     607             : 
     608             : #define DATE_FETCH_OBJECT       \
     609             :         php_date_obj *obj;      \
     610             :         DATE_SET_CONTEXT; \
     611             :         if (object) {   \
     612             :                 if (zend_parse_parameters_none() == FAILURE) {  \
     613             :                         return; \
     614             :                 }       \
     615             :         } else {        \
     616             :                 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), NULL, "O", &object, date_ce_date) == FAILURE) { \
     617             :                         RETURN_FALSE;   \
     618             :                 }       \
     619             :         }       \
     620             :         obj = Z_PHPDATE_P(object);      \
     621             : 
     622             : #define DATE_CHECK_INITIALIZED(member, class_name) \
     623             :         if (!(member)) { \
     624             :                 php_error_docref(NULL, E_WARNING, "The " #class_name " object has not been correctly initialized by its constructor"); \
     625             :                 RETURN_FALSE; \
     626             :         }
     627             : 
     628             : static void date_object_free_storage_date(zend_object *object);
     629             : static void date_object_free_storage_timezone(zend_object *object);
     630             : static void date_object_free_storage_interval(zend_object *object);
     631             : static void date_object_free_storage_period(zend_object *object);
     632             : 
     633             : static zend_object *date_object_new_date(zend_class_entry *class_type);
     634             : static zend_object *date_object_new_timezone(zend_class_entry *class_type);
     635             : static zend_object *date_object_new_interval(zend_class_entry *class_type);
     636             : static zend_object *date_object_new_period(zend_class_entry *class_type);
     637             : 
     638             : static zend_object *date_object_clone_date(zval *this_ptr);
     639             : static zend_object *date_object_clone_timezone(zval *this_ptr);
     640             : static zend_object *date_object_clone_interval(zval *this_ptr);
     641             : static zend_object *date_object_clone_period(zval *this_ptr);
     642             : 
     643             : static int date_object_compare_date(zval *d1, zval *d2);
     644             : static HashTable *date_object_get_gc(zval *object, zval **table, int *n);
     645             : static HashTable *date_object_get_properties(zval *object);
     646             : static HashTable *date_object_get_gc_interval(zval *object, zval **table, int *n);
     647             : static HashTable *date_object_get_properties_interval(zval *object);
     648             : static HashTable *date_object_get_gc_period(zval *object, zval **table, int *n);
     649             : static HashTable *date_object_get_properties_period(zval *object);
     650             : static HashTable *date_object_get_properties_timezone(zval *object);
     651             : static HashTable *date_object_get_gc_timezone(zval *object, zval **table, int *n);
     652             : 
     653             : zval *date_interval_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv);
     654             : void date_interval_write_property(zval *object, zval *member, zval *value, void **cache_slot);
     655             : static zval *date_period_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv);
     656             : static void date_period_write_property(zval *object, zval *member, zval *value, void **cache_slot);
     657             : 
     658             : /* {{{ Module struct */
     659             : zend_module_entry date_module_entry = {
     660             :         STANDARD_MODULE_HEADER_EX,
     661             :         NULL,
     662             :         NULL,
     663             :         "date",                     /* extension name */
     664             :         date_functions,             /* function list */
     665             :         PHP_MINIT(date),            /* process startup */
     666             :         PHP_MSHUTDOWN(date),        /* process shutdown */
     667             :         PHP_RINIT(date),            /* request startup */
     668             :         PHP_RSHUTDOWN(date),        /* request shutdown */
     669             :         PHP_MINFO(date),            /* extension info */
     670             :         PHP_DATE_VERSION,                /* extension version */
     671             :         PHP_MODULE_GLOBALS(date),   /* globals descriptor */
     672             :         PHP_GINIT(date),            /* globals ctor */
     673             :         NULL,                       /* globals dtor */
     674             :         NULL,                       /* post deactivate */
     675             :         STANDARD_MODULE_PROPERTIES_EX
     676             : };
     677             : /* }}} */
     678             : 
     679             : 
     680             : /* {{{ PHP_GINIT_FUNCTION */
     681       23922 : static PHP_GINIT_FUNCTION(date)
     682             : {
     683       23922 :         date_globals->default_timezone = NULL;
     684       23922 :         date_globals->timezone = NULL;
     685       23922 :         date_globals->tzcache = NULL;
     686       23922 :         date_globals->timezone_valid = 0;
     687       23922 : }
     688             : /* }}} */
     689             : 
     690             : 
     691        1958 : static void _php_date_tzinfo_dtor(zval *zv) /* {{{ */
     692             : {
     693        1958 :         timelib_tzinfo *tzi = (timelib_tzinfo*)Z_PTR_P(zv);
     694             : 
     695        1958 :         timelib_tzinfo_dtor(tzi);
     696        1958 : } /* }}} */
     697             : 
     698             : /* {{{ PHP_RINIT_FUNCTION */
     699       23876 : PHP_RINIT_FUNCTION(date)
     700             : {
     701       23876 :         if (DATEG(timezone)) {
     702           0 :                 efree(DATEG(timezone));
     703             :         }
     704       23876 :         DATEG(timezone) = NULL;
     705       23876 :         DATEG(tzcache) = NULL;
     706       23876 :         DATEG(last_errors) = NULL;
     707             : 
     708       23876 :         return SUCCESS;
     709             : }
     710             : /* }}} */
     711             : 
     712             : /* {{{ PHP_RSHUTDOWN_FUNCTION */
     713       23919 : PHP_RSHUTDOWN_FUNCTION(date)
     714             : {
     715       23919 :         if (DATEG(timezone)) {
     716         528 :                 efree(DATEG(timezone));
     717             :         }
     718       23919 :         DATEG(timezone) = NULL;
     719       23919 :         if(DATEG(tzcache)) {
     720         667 :                 zend_hash_destroy(DATEG(tzcache));
     721         667 :                 FREE_HASHTABLE(DATEG(tzcache));
     722         667 :                 DATEG(tzcache) = NULL;
     723             :         }
     724       23919 :         if (DATEG(last_errors)) {
     725         262 :                 timelib_error_container_dtor(DATEG(last_errors));
     726         262 :                 DATEG(last_errors) = NULL;
     727             :         }
     728             : 
     729       23919 :         return SUCCESS;
     730             : }
     731             : /* }}} */
     732             : 
     733             : #define DATE_TIMEZONEDB      php_date_global_timezone_db ? php_date_global_timezone_db : timelib_builtin_db()
     734             : 
     735             : /*
     736             :  * RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
     737             :  *  date-time   =  [ day "," ] date time        ; dd mm yy hh:mm:ss zzz
     738             :  *  day         =  "Mon"  / "Tue" /  "Wed"  / "Thu"  /  "Fri"  / "Sat" /  "Sun"
     739             :  *  date        =  1*2DIGIT month 2DIGIT        ; day month year e.g. 20 Jun 82
     740             :  *  month       =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"  /  "May"  /  "Jun" /  "Jul"  /  "Aug"  /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
     741             :  *  time        =  hour zone                    ; ANSI and Military
     742             :  *  hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
     743             :  *  zone        =  "UT"  / "GMT"  /  "EST" / "EDT"  /  "CST" / "CDT"  /  "MST" / "MDT"  /  "PST" / "PDT"  /  1ALPHA  / ( ("+" / "-") 4DIGIT )
     744             :  */
     745             : #define DATE_FORMAT_RFC822   "D, d M y H:i:s O"
     746             : 
     747             : /*
     748             :  * RFC850, Section 2.1.4: http://www.ietf.org/rfc/rfc850.txt
     749             :  *  Format must be acceptable both to the ARPANET and to the getdate routine.
     750             :  *  One format that is acceptable to both is Weekday, DD-Mon-YY HH:MM:SS TIMEZONE
     751             :  *  TIMEZONE can be any timezone name (3 or more letters)
     752             :  */
     753             : #define DATE_FORMAT_RFC850   "l, d-M-y H:i:s T"
     754             : 
     755             : /*
     756             :  * RFC1036, Section 2.1.2: http://www.ietf.org/rfc/rfc1036.txt
     757             :  *  Its format must be acceptable both in RFC-822 and to the getdate(3)
     758             :  *  Wdy, DD Mon YY HH:MM:SS TIMEZONE
     759             :  *  There is no hope of having a complete list of timezones.  Universal
     760             :  *  Time (GMT), the North American timezones (PST, PDT, MST, MDT, CST,
     761             :  *  CDT, EST, EDT) and the +/-hhmm offset specifed in RFC-822 should be supported.
     762             :  */
     763             : #define DATE_FORMAT_RFC1036  "D, d M y H:i:s O"
     764             : 
     765             : /*
     766             :  * RFC1123, Section 5.2.14: http://www.ietf.org/rfc/rfc1123.txt
     767             :  *  RFC-822 Date and Time Specification: RFC-822 Section 5
     768             :  *  The syntax for the date is hereby changed to: date = 1*2DIGIT month 2*4DIGIT
     769             :  */
     770             : #define DATE_FORMAT_RFC1123  "D, d M Y H:i:s O"
     771             : 
     772             : /*
     773             :  * RFC2822, Section 3.3: http://www.ietf.org/rfc/rfc2822.txt
     774             :  *  FWS             =       ([*WSP CRLF] 1*WSP) /   ; Folding white space
     775             :  *  CFWS            =       *([FWS] comment) (([FWS] comment) / FWS)
     776             :  *
     777             :  *  date-time       =       [ day-of-week "," ] date FWS time [CFWS]
     778             :  *  day-of-week     =       ([FWS] day-name)
     779             :  *  day-name        =       "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
     780             :  *  date            =       day month year
     781             :  *  year            =       4*DIGIT
     782             :  *  month           =       (FWS month-name FWS)
     783             :  *  month-name      =       "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
     784             :  *  day             =       ([FWS] 1*2DIGIT)
     785             :  *  time            =       time-of-day FWS zone
     786             :  *  time-of-day     =       hour ":" minute [ ":" second ]
     787             :  *  hour            =       2DIGIT
     788             :  *  minute          =       2DIGIT
     789             :  *  second          =       2DIGIT
     790             :  *  zone            =       (( "+" / "-" ) 4DIGIT)
     791             :  */
     792             : #define DATE_FORMAT_RFC2822  "D, d M Y H:i:s O"
     793             : /*
     794             :  * RFC3339, Section 5.6: http://www.ietf.org/rfc/rfc3339.txt
     795             :  *  date-fullyear   = 4DIGIT
     796             :  *  date-month      = 2DIGIT  ; 01-12
     797             :  *  date-mday       = 2DIGIT  ; 01-28, 01-29, 01-30, 01-31 based on month/year
     798             :  *
     799             :  *  time-hour       = 2DIGIT  ; 00-23
     800             :  *  time-minute     = 2DIGIT  ; 00-59
     801             :  *  time-second     = 2DIGIT  ; 00-58, 00-59, 00-60 based on leap second rules
     802             :  *
     803             :  *  time-secfrac    = "." 1*DIGIT
     804             :  *  time-numoffset  = ("+" / "-") time-hour ":" time-minute
     805             :  *  time-offset     = "Z" / time-numoffset
     806             :  *
     807             :  *  partial-time    = time-hour ":" time-minute ":" time-second [time-secfrac]
     808             :  *  full-date       = date-fullyear "-" date-month "-" date-mday
     809             :  *  full-time       = partial-time time-offset
     810             :  *
     811             :  *  date-time       = full-date "T" full-time
     812             :  */
     813             : #define DATE_FORMAT_RFC3339  "Y-m-d\\TH:i:sP"
     814             : 
     815             : #define DATE_FORMAT_ISO8601  "Y-m-d\\TH:i:sO"
     816             : 
     817             : /*
     818             :  * RFC3339, Appendix A: http://www.ietf.org/rfc/rfc3339.txt
     819             :  *  ISO 8601 also requires (in section 5.3.1.3) that a decimal fraction
     820             :  *  be proceeded by a "0" if less than unity.  Annex B.2 of ISO 8601
     821             :  *  gives examples where the decimal fractions are not preceded by a "0".
     822             :  *  This grammar assumes section 5.3.1.3 is correct and that Annex B.2 is
     823             :  *  in error.
     824             :  */
     825             : #define DATE_FORMAT_RFC3339_EXTENDED  "Y-m-d\\TH:i:s.vP"
     826             : 
     827             : /*
     828             :  * This comes from various sources that like to contradict. I'm going with the
     829             :  * format here because of:
     830             :  * http://msdn.microsoft.com/en-us/library/windows/desktop/aa384321%28v=vs.85%29.aspx
     831             :  * and http://curl.haxx.se/rfc/cookie_spec.html
     832             :  */
     833             : #define DATE_FORMAT_COOKIE   "l, d-M-Y H:i:s T"
     834             : 
     835             : #define SUNFUNCS_RET_TIMESTAMP 0
     836             : #define SUNFUNCS_RET_STRING    1
     837             : #define SUNFUNCS_RET_DOUBLE    2
     838             : 
     839             : /* {{{ PHP_MINIT_FUNCTION */
     840       23922 : PHP_MINIT_FUNCTION(date)
     841             : {
     842       23922 :         REGISTER_INI_ENTRIES();
     843       23922 :         date_register_classes();
     844             : /*
     845             :  * RFC4287, Section 3.3: http://www.ietf.org/rfc/rfc4287.txt
     846             :  *   A Date construct is an element whose content MUST conform to the
     847             :  *   "date-time" production in [RFC3339].  In addition, an uppercase "T"
     848             :  *   character MUST be used to separate date and time, and an uppercase
     849             :  *   "Z" character MUST be present in the absence of a numeric time zone offset.
     850             :  */
     851       23922 :         REGISTER_STRING_CONSTANT("DATE_ATOM",    DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
     852             : /*
     853             :  * Preliminary specification: http://wp.netscape.com/newsref/std/cookie_spec.html
     854             :  *   "This is based on RFC 822, RFC 850,  RFC 1036, and  RFC 1123,
     855             :  *   with the variations that the only legal time zone is GMT
     856             :  *   and the separators between the elements of the date must be dashes."
     857             :  */
     858       23922 :         REGISTER_STRING_CONSTANT("DATE_COOKIE",  DATE_FORMAT_COOKIE,  CONST_CS | CONST_PERSISTENT);
     859       23922 :         REGISTER_STRING_CONSTANT("DATE_ISO8601", DATE_FORMAT_ISO8601, CONST_CS | CONST_PERSISTENT);
     860             : 
     861       23922 :         REGISTER_STRING_CONSTANT("DATE_RFC822",  DATE_FORMAT_RFC822,  CONST_CS | CONST_PERSISTENT);
     862       23922 :         REGISTER_STRING_CONSTANT("DATE_RFC850",  DATE_FORMAT_RFC850,  CONST_CS | CONST_PERSISTENT);
     863       23922 :         REGISTER_STRING_CONSTANT("DATE_RFC1036", DATE_FORMAT_RFC1036, CONST_CS | CONST_PERSISTENT);
     864       23922 :         REGISTER_STRING_CONSTANT("DATE_RFC1123", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
     865       23922 :         REGISTER_STRING_CONSTANT("DATE_RFC2822", DATE_FORMAT_RFC2822, CONST_CS | CONST_PERSISTENT);
     866       23922 :         REGISTER_STRING_CONSTANT("DATE_RFC3339", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
     867       23922 :         REGISTER_STRING_CONSTANT("DATE_RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED, CONST_CS | CONST_PERSISTENT);
     868             : 
     869             : /*
     870             :  * RSS 2.0 Specification: http://blogs.law.harvard.edu/tech/rss
     871             :  *   "All date-times in RSS conform to the Date and Time Specification of RFC 822,
     872             :  *   with the exception that the year may be expressed with two characters or four characters (four preferred)"
     873             :  */
     874       23922 :         REGISTER_STRING_CONSTANT("DATE_RSS",     DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
     875       23922 :         REGISTER_STRING_CONSTANT("DATE_W3C",     DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
     876             : 
     877       23922 :         REGISTER_LONG_CONSTANT("SUNFUNCS_RET_TIMESTAMP", SUNFUNCS_RET_TIMESTAMP, CONST_CS | CONST_PERSISTENT);
     878       23922 :         REGISTER_LONG_CONSTANT("SUNFUNCS_RET_STRING", SUNFUNCS_RET_STRING, CONST_CS | CONST_PERSISTENT);
     879       23922 :         REGISTER_LONG_CONSTANT("SUNFUNCS_RET_DOUBLE", SUNFUNCS_RET_DOUBLE, CONST_CS | CONST_PERSISTENT);
     880             : 
     881       23922 :         php_date_global_timezone_db = NULL;
     882       23922 :         php_date_global_timezone_db_enabled = 0;
     883       23922 :         DATEG(last_errors) = NULL;
     884       23922 :         return SUCCESS;
     885             : }
     886             : /* }}} */
     887             : 
     888             : /* {{{ PHP_MSHUTDOWN_FUNCTION */
     889       23963 : PHP_MSHUTDOWN_FUNCTION(date)
     890             : {
     891       23963 :         UNREGISTER_INI_ENTRIES();
     892             : 
     893       23963 :         if (DATEG(last_errors)) {
     894           0 :                 timelib_error_container_dtor(DATEG(last_errors));
     895             :         }
     896             : 
     897             : #ifndef ZTS
     898       23963 :         DATEG(default_timezone) = NULL;
     899             : #endif
     900             : 
     901       23963 :         return SUCCESS;
     902             : }
     903             : /* }}} */
     904             : 
     905             : /* {{{ PHP_MINFO_FUNCTION */
     906         151 : PHP_MINFO_FUNCTION(date)
     907             : {
     908         151 :         const timelib_tzdb *tzdb = DATE_TIMEZONEDB;
     909             : 
     910         151 :         php_info_print_table_start();
     911         151 :         php_info_print_table_row(2, "date/time support", "enabled");
     912         151 :         php_info_print_table_row(2, "\"Olson\" Timezone Database Version", tzdb->version);
     913         151 :         php_info_print_table_row(2, "Timezone Database", php_date_global_timezone_db_enabled ? "external" : "internal");
     914         151 :         php_info_print_table_row(2, "Default timezone", guess_timezone(tzdb));
     915         151 :         php_info_print_table_end();
     916             : 
     917         151 :         DISPLAY_INI_ENTRIES();
     918         151 : }
     919             : /* }}} */
     920             : 
     921             : /* {{{ Timezone Cache functions */
     922       15805 : static timelib_tzinfo *php_date_parse_tzfile(char *formal_tzname, const timelib_tzdb *tzdb)
     923             : {
     924             :         timelib_tzinfo *tzi;
     925             : 
     926       15805 :         if(!DATEG(tzcache)) {
     927         667 :                 ALLOC_HASHTABLE(DATEG(tzcache));
     928         667 :                 zend_hash_init(DATEG(tzcache), 4, NULL, _php_date_tzinfo_dtor, 0);
     929             :         }
     930             : 
     931       31610 :         if ((tzi = zend_hash_str_find_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname))) != NULL) {
     932       13752 :                 return tzi;
     933             :         }
     934             : 
     935        2053 :         tzi = timelib_parse_tzfile(formal_tzname, tzdb);
     936        2053 :         if (tzi) {
     937        1958 :                 zend_hash_str_add_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname), tzi);
     938             :         }
     939        2053 :         return tzi;
     940             : }
     941             : 
     942        2777 : timelib_tzinfo *php_date_parse_tzfile_wrapper(char *formal_tzname, const timelib_tzdb *tzdb)
     943             : {
     944        2777 :         return php_date_parse_tzfile(formal_tzname, tzdb);
     945             : }
     946             : /* }}} */
     947             : 
     948             : /* Callback to check the date.timezone only when changed increases performance */
     949             : /* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
     950       23988 : static PHP_INI_MH(OnUpdate_date_timezone)
     951             : {
     952       23988 :         if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
     953           0 :                 return FAILURE;
     954             :         }
     955             : 
     956       23988 :         DATEG(timezone_valid) = 0;
     957       23988 :         if (stage == PHP_INI_STAGE_RUNTIME) {
     958          33 :                 if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
     959           0 :                         if (DATEG(default_timezone) && *DATEG(default_timezone)) {
     960           0 :                                 php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
     961             :                         }
     962             :                 } else {
     963          33 :                         DATEG(timezone_valid) = 1;
     964             :                 }
     965             :         }
     966             : 
     967       23988 :         return SUCCESS;
     968             : }
     969             : /* }}} */
     970             : 
     971             : /* {{{ Helper functions */
     972       13169 : static char* guess_timezone(const timelib_tzdb *tzdb)
     973             : {
     974             :         /* Checking configure timezone */
     975       13169 :         if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
     976       10215 :                 return DATEG(timezone);
     977             :         }
     978             :         /* Check config setting for default timezone */
     979        2954 :         if (!DATEG(default_timezone)) {
     980             :                 /* Special case: ext/date wasn't initialized yet */
     981             :                 zval *ztz;
     982             : 
     983           0 :                 if (NULL != (ztz = cfg_get_entry("date.timezone", sizeof("date.timezone")))
     984           0 :                         && Z_TYPE_P(ztz) == IS_STRING && Z_STRLEN_P(ztz) > 0 && timelib_timezone_id_is_valid(Z_STRVAL_P(ztz), tzdb)) {
     985           0 :                         return Z_STRVAL_P(ztz);
     986             :                 }
     987        2954 :         } else if (*DATEG(default_timezone)) {
     988        2660 :                 if (DATEG(timezone_valid) == 1) {
     989        2527 :                         return DATEG(default_timezone);
     990             :                 }
     991             : 
     992         133 :                 if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
     993           2 :                         php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
     994           2 :                         return "UTC";
     995             :                 }
     996             : 
     997         131 :                 DATEG(timezone_valid) = 1;
     998         131 :                 return DATEG(default_timezone);
     999             :         }
    1000             :         /* Fallback to UTC */
    1001         294 :         return "UTC";
    1002             : }
    1003             : 
    1004       13018 : PHPAPI timelib_tzinfo *get_timezone_info(void)
    1005             : {
    1006             :         char *tz;
    1007             :         timelib_tzinfo *tzi;
    1008             : 
    1009       13018 :         tz = guess_timezone(DATE_TIMEZONEDB);
    1010       13018 :         tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
    1011       13018 :         if (! tzi) {
    1012           0 :                 php_error_docref(NULL, E_ERROR, "Timezone database is corrupt - this should *never* happen!");
    1013             :         }
    1014       13018 :         return tzi;
    1015             : }
    1016             : /* }}} */
    1017             : 
    1018             : 
    1019             : /* {{{ date() and gmdate() data */
    1020             : #include "zend_smart_str.h"
    1021             : 
    1022             : static char *mon_full_names[] = {
    1023             :         "January", "February", "March", "April",
    1024             :         "May", "June", "July", "August",
    1025             :         "September", "October", "November", "December"
    1026             : };
    1027             : 
    1028             : static char *mon_short_names[] = {
    1029             :         "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    1030             : };
    1031             : 
    1032             : static char *day_full_names[] = {
    1033             :         "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    1034             : };
    1035             : 
    1036             : static char *day_short_names[] = {
    1037             :         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    1038             : };
    1039             : 
    1040          37 : static char *english_suffix(timelib_sll number)
    1041             : {
    1042          37 :         if (number >= 10 && number <= 19) {
    1043          13 :                 return "th";
    1044             :         } else {
    1045          24 :                 switch (number % 10) {
    1046           4 :                         case 1: return "st";
    1047           2 :                         case 2: return "nd";
    1048           2 :                         case 3: return "rd";
    1049             :                 }
    1050             :         }
    1051          16 :         return "th";
    1052             : }
    1053             : /* }}} */
    1054             : 
    1055             : /* {{{ day of week helpers */
    1056         292 : char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
    1057             : {
    1058         292 :         timelib_sll day_of_week = timelib_day_of_week(y, m, d);
    1059         292 :         if (day_of_week < 0) {
    1060           0 :                 return "Unknown";
    1061             :         }
    1062         292 :         return day_full_names[day_of_week];
    1063             : }
    1064             : 
    1065         518 : char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
    1066             : {
    1067         518 :         timelib_sll day_of_week = timelib_day_of_week(y, m, d);
    1068         518 :         if (day_of_week < 0) {
    1069           0 :                 return "Unknown";
    1070             :         }
    1071         518 :         return day_short_names[day_of_week];
    1072             : }
    1073             : /* }}} */
    1074             : 
    1075             : /* {{{ date_format - (gm)date helper */
    1076        9277 : static zend_string *date_format(char *format, size_t format_len, timelib_time *t, int localtime)
    1077             : {
    1078        9277 :         smart_str            string = {0};
    1079             :         size_t               i;
    1080        9277 :         int                  length = 0;
    1081             :         char                 buffer[97];
    1082        9277 :         timelib_time_offset *offset = NULL;
    1083             :         timelib_sll          isoweek, isoyear;
    1084             :         int                  rfc_colon;
    1085        9277 :         int                  weekYearSet = 0;
    1086             : 
    1087        9277 :         if (!format_len) {
    1088          41 :                 return ZSTR_EMPTY_ALLOC();
    1089             :         }
    1090             : 
    1091        9236 :         if (localtime) {
    1092        8389 :                 if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
    1093        1259 :                         offset = timelib_time_offset_ctor();
    1094        1259 :                         offset->offset = (t->z - (t->dst * 60)) * -60;
    1095        1259 :                         offset->leap_secs = 0;
    1096        1259 :                         offset->is_dst = t->dst;
    1097        1259 :                         offset->abbr = timelib_strdup(t->tz_abbr);
    1098        7130 :                 } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
    1099         177 :                         offset = timelib_time_offset_ctor();
    1100         177 :                         offset->offset = (t->z) * -60;
    1101         177 :                         offset->leap_secs = 0;
    1102         177 :                         offset->is_dst = 0;
    1103         177 :                         offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
    1104         708 :                         snprintf(offset->abbr, 9, "GMT%c%02d%02d",
    1105         177 :                                                   localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
    1106         177 :                                                   localtime ? abs(offset->offset / 3600) : 0,
    1107         177 :                                                   localtime ? abs((offset->offset % 3600) / 60) : 0 );
    1108             :                 } else {
    1109        6953 :                         offset = timelib_get_time_zone_info(t->sse, t->tz_info);
    1110             :                 }
    1111             :         }
    1112             : 
    1113       86803 :         for (i = 0; i < format_len; i++) {
    1114       77567 :                 rfc_colon = 0;
    1115       77567 :                 switch (format[i]) {
    1116             :                         /* day */
    1117        5686 :                         case 'd': length = slprintf(buffer, 32, "%02d", (int) t->d); break;
    1118         459 :                         case 'D': length = slprintf(buffer, 32, "%s", php_date_short_day_name(t->y, t->m, t->d)); break;
    1119          86 :                         case 'j': length = slprintf(buffer, 32, "%d", (int) t->d); break;
    1120         257 :                         case 'l': length = slprintf(buffer, 32, "%s", php_date_full_day_name(t->y, t->m, t->d)); break;
    1121          37 :                         case 'S': length = slprintf(buffer, 32, "%s", english_suffix(t->d)); break;
    1122          12 :                         case 'w': length = slprintf(buffer, 32, "%d", (int) timelib_day_of_week(t->y, t->m, t->d)); break;
    1123           8 :                         case 'N': length = slprintf(buffer, 32, "%d", (int) timelib_iso_day_of_week(t->y, t->m, t->d)); break;
    1124           6 :                         case 'z': length = slprintf(buffer, 32, "%d", (int) timelib_day_of_year(t->y, t->m, t->d)); break;
    1125             : 
    1126             :                         /* week */
    1127             :                         case 'W':
    1128        1044 :                                 if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
    1129        1044 :                                 length = slprintf(buffer, 32, "%02d", (int) isoweek); break; /* iso weeknr */
    1130             :                         case 'o':
    1131        1030 :                                 if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
    1132        1030 :                                 length = slprintf(buffer, 32, "%d", (int) isoyear); break; /* iso year */
    1133             : 
    1134             :                         /* month */
    1135          65 :                         case 'F': length = slprintf(buffer, 32, "%s", mon_full_names[t->m - 1]); break;
    1136        5538 :                         case 'm': length = slprintf(buffer, 32, "%02d", (int) t->m); break;
    1137         223 :                         case 'M': length = slprintf(buffer, 32, "%s", mon_short_names[t->m - 1]); break;
    1138          18 :                         case 'n': length = slprintf(buffer, 32, "%d", (int) t->m); break;
    1139          20 :                         case 't': length = slprintf(buffer, 32, "%d", (int) timelib_days_in_month(t->y, t->m)); break;
    1140             : 
    1141             :                         /* year */
    1142          10 :                         case 'L': length = slprintf(buffer, 32, "%d", timelib_is_leap((int) t->y)); break;
    1143          49 :                         case 'y': length = slprintf(buffer, 32, "%02d", (int) t->y % 100); break;
    1144        5773 :                         case 'Y': length = slprintf(buffer, 32, "%s%04lld", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
    1145             : 
    1146             :                         /* time */
    1147          39 :                         case 'a': length = slprintf(buffer, 32, "%s", t->h >= 12 ? "pm" : "am"); break;
    1148          10 :                         case 'A': length = slprintf(buffer, 32, "%s", t->h >= 12 ? "PM" : "AM"); break;
    1149             :                         case 'B': {
    1150           7 :                                 int retval = (((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10) / 864);
    1151          14 :                                 while (retval < 0) {
    1152           0 :                                         retval += 1000;
    1153             :                                 }
    1154           7 :                                 retval = retval % 1000;
    1155           7 :                                 length = slprintf(buffer, 32, "%03d", retval);
    1156           7 :                                 break;
    1157             :                         }
    1158          38 :                         case 'g': length = slprintf(buffer, 32, "%d", (t->h % 12) ? (int) t->h % 12 : 12); break;
    1159          12 :                         case 'G': length = slprintf(buffer, 32, "%d", (int) t->h); break;
    1160           8 :                         case 'h': length = slprintf(buffer, 32, "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
    1161        5485 :                         case 'H': length = slprintf(buffer, 32, "%02d", (int) t->h); break;
    1162        5526 :                         case 'i': length = slprintf(buffer, 32, "%02d", (int) t->i); break;
    1163        5488 :                         case 's': length = slprintf(buffer, 32, "%02d", (int) t->s); break;
    1164        1428 :                         case 'u': length = slprintf(buffer, 32, "%06d", (int) floor(t->f * 1000000 + 0.5)); break;
    1165           2 :                         case 'v': length = slprintf(buffer, 32, "%03d", (int) floor(t->f * 1000 + 0.5)); break;
    1166             : 
    1167             :                         /* timezone */
    1168         123 :                         case 'I': length = slprintf(buffer, 32, "%d", localtime ? offset->is_dst : 0); break;
    1169          29 :                         case 'P': rfc_colon = 1; /* break intentionally missing */
    1170        2164 :                         case 'O': length = slprintf(buffer, 32, "%c%02d%s%02d",
    1171         366 :                                                                                         localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
    1172         366 :                                                                                         localtime ? abs(offset->offset / 3600) : 0,
    1173             :                                                                                         rfc_colon ? ":" : "",
    1174         366 :                                                                                         localtime ? abs((offset->offset % 3600) / 60) : 0
    1175             :                                                           );
    1176        1066 :                                           break;
    1177        3549 :                         case 'T': length = slprintf(buffer, 32, "%s", localtime ? offset->abbr : "GMT"); break;
    1178         376 :                         case 'e': if (!localtime) {
    1179           4 :                                               length = slprintf(buffer, 32, "%s", "UTC");
    1180             :                                           } else {
    1181         372 :                                                   switch (t->zone_type) {
    1182             :                                                           case TIMELIB_ZONETYPE_ID:
    1183         171 :                                                                   length = slprintf(buffer, 32, "%s", t->tz_info->name);
    1184         171 :                                                                   break;
    1185             :                                                           case TIMELIB_ZONETYPE_ABBR:
    1186          98 :                                                                   length = slprintf(buffer, 32, "%s", offset->abbr);
    1187          98 :                                                                   break;
    1188             :                                                           case TIMELIB_ZONETYPE_OFFSET:
    1189         309 :                                                                   length = slprintf(buffer, 32, "%c%02d:%02d",
    1190         103 :                                                                                                 ((offset->offset < 0) ? '-' : '+'),
    1191         103 :                                                                                                 abs(offset->offset / 3600),
    1192         103 :                                                                                                 abs((offset->offset % 3600) / 60)
    1193             :                                                                                    );
    1194             :                                                                   break;
    1195             :                                                   }
    1196             :                                           }
    1197         376 :                                           break;
    1198           5 :                         case 'Z': length = slprintf(buffer, 32, "%d", localtime ? offset->offset : 0); break;
    1199             : 
    1200             :                         /* full date/time */
    1201          40 :                         case 'c': length = slprintf(buffer, 96, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
    1202             :                                                                         (int) t->y, (int) t->m, (int) t->d,
    1203             :                                                                                         (int) t->h, (int) t->i, (int) t->s,
    1204           9 :                                                                                         localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
    1205           9 :                                                                                         localtime ? abs(offset->offset / 3600) : 0,
    1206           9 :                                                                                         localtime ? abs((offset->offset % 3600) / 60) : 0
    1207             :                                                           );
    1208          13 :                                           break;
    1209         265 :                         case 'r': length = slprintf(buffer, 96, "%3s, %02d %3s %04d %02d:%02d:%02d %c%02d%02d",
    1210             :                                                                         php_date_short_day_name(t->y, t->m, t->d),
    1211          59 :                                                                                         (int) t->d, mon_short_names[t->m - 1],
    1212             :                                                                                         (int) t->y, (int) t->h, (int) t->i, (int) t->s,
    1213          49 :                                                                                         localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
    1214          49 :                                                                                         localtime ? abs(offset->offset / 3600) : 0,
    1215          49 :                                                                                         localtime ? abs((offset->offset % 3600) / 60) : 0
    1216             :                                                           );
    1217          59 :                                           break;
    1218          35 :                         case 'U': length = slprintf(buffer, 32, "%lld", (timelib_sll) t->sse); break;
    1219             : 
    1220        1029 :                         case '\\': if (i < format_len) i++; /* break intentionally missing */
    1221             : 
    1222       33977 :                         default: buffer[0] = format[i]; buffer[1] = '\0'; length = 1; break;
    1223             :                 }
    1224       77567 :                 smart_str_appendl(&string, buffer, length);
    1225             :         }
    1226             : 
    1227             :         smart_str_0(&string);
    1228             : 
    1229        9236 :         if (localtime) {
    1230        8389 :                 timelib_time_offset_dtor(offset);
    1231             :         }
    1232             : 
    1233        9236 :         return string.s;
    1234             : }
    1235             : 
    1236        3121 : static void php_date(INTERNAL_FUNCTION_PARAMETERS, int localtime)
    1237             : {
    1238             :         char   *format;
    1239             :         size_t     format_len;
    1240             :         zend_long    ts;
    1241             : 
    1242        3121 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &format, &format_len, &ts) == FAILURE) {
    1243          44 :                 RETURN_FALSE;
    1244             :         }
    1245        3077 :         if (ZEND_NUM_ARGS() == 1) {
    1246         148 :                 ts = time(NULL);
    1247             :         }
    1248             : 
    1249        3077 :         RETURN_STR(php_format_date(format, format_len, ts, localtime));
    1250             : }
    1251             : /* }}} */
    1252             : 
    1253        3750 : PHPAPI zend_string *php_format_date(char *format, size_t format_len, time_t ts, int localtime) /* {{{ */
    1254             : {
    1255             :         timelib_time   *t;
    1256             :         timelib_tzinfo *tzi;
    1257             :         zend_string *string;
    1258             : 
    1259        3750 :         t = timelib_time_ctor();
    1260             : 
    1261        3750 :         if (localtime) {
    1262        2887 :                 tzi = get_timezone_info();
    1263        2887 :                 t->tz_info = tzi;
    1264        2887 :                 t->zone_type = TIMELIB_ZONETYPE_ID;
    1265        2887 :                 timelib_unixtime2local(t, ts);
    1266             :         } else {
    1267         863 :                 tzi = NULL;
    1268         863 :                 timelib_unixtime2gmt(t, ts);
    1269             :         }
    1270             : 
    1271        3750 :         string = date_format(format, format_len, t, localtime);
    1272             : 
    1273        3750 :         timelib_time_dtor(t);
    1274        3750 :         return string;
    1275             : }
    1276             : /* }}} */
    1277             : 
    1278             : /* {{{ php_idate
    1279             :  */
    1280          81 : PHPAPI int php_idate(char format, time_t ts, int localtime)
    1281             : {
    1282             :         timelib_time   *t;
    1283             :         timelib_tzinfo *tzi;
    1284          81 :         int retval = -1;
    1285          81 :         timelib_time_offset *offset = NULL;
    1286             :         timelib_sll isoweek, isoyear;
    1287             : 
    1288          81 :         t = timelib_time_ctor();
    1289             : 
    1290          81 :         if (!localtime) {
    1291          81 :                 tzi = get_timezone_info();
    1292          81 :                 t->tz_info = tzi;
    1293          81 :                 t->zone_type = TIMELIB_ZONETYPE_ID;
    1294          81 :                 timelib_unixtime2local(t, ts);
    1295             :         } else {
    1296           0 :                 tzi = NULL;
    1297           0 :                 timelib_unixtime2gmt(t, ts);
    1298             :         }
    1299             : 
    1300          81 :         if (!localtime) {
    1301          81 :                 if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
    1302           0 :                         offset = timelib_time_offset_ctor();
    1303           0 :                         offset->offset = (t->z - (t->dst * 60)) * -60;
    1304           0 :                         offset->leap_secs = 0;
    1305           0 :                         offset->is_dst = t->dst;
    1306           0 :                         offset->abbr = timelib_strdup(t->tz_abbr);
    1307          81 :                 } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
    1308           0 :                         offset = timelib_time_offset_ctor();
    1309           0 :                         offset->offset = (t->z - (t->dst * 60)) * -60;
    1310           0 :                         offset->leap_secs = 0;
    1311           0 :                         offset->is_dst = t->dst;
    1312           0 :                         offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
    1313           0 :                         snprintf(offset->abbr, 9, "GMT%c%02d%02d",
    1314           0 :                                                   !localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
    1315           0 :                                                   !localtime ? abs(offset->offset / 3600) : 0,
    1316           0 :                                                   !localtime ? abs((offset->offset % 3600) / 60) : 0 );
    1317             :                 } else {
    1318          81 :                         offset = timelib_get_time_zone_info(t->sse, t->tz_info);
    1319             :                 }
    1320             :         }
    1321             : 
    1322          81 :         timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear);
    1323             : 
    1324          81 :         switch (format) {
    1325             :                 /* day */
    1326           4 :                 case 'd': case 'j': retval = (int) t->d; break;
    1327             : 
    1328           3 :                 case 'w': retval = (int) timelib_day_of_week(t->y, t->m, t->d); break;
    1329           3 :                 case 'z': retval = (int) timelib_day_of_year(t->y, t->m, t->d); break;
    1330             : 
    1331             :                 /* week */
    1332           3 :                 case 'W': retval = (int) isoweek; break; /* iso weeknr */
    1333             : 
    1334             :                 /* month */
    1335           4 :                 case 'm': case 'n': retval = (int) t->m; break;
    1336           3 :                 case 't': retval = (int) timelib_days_in_month(t->y, t->m); break;
    1337             : 
    1338             :                 /* year */
    1339           3 :                 case 'L': retval = (int) timelib_is_leap((int) t->y); break;
    1340           5 :                 case 'y': retval = (int) (t->y % 100); break;
    1341          15 :                 case 'Y': retval = (int) t->y; break;
    1342             : 
    1343             :                 /* Swatch Beat a.k.a. Internet Time */
    1344             :                 case 'B':
    1345           4 :                         retval = (((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10) / 864);
    1346           8 :                         while (retval < 0) {
    1347           0 :                                 retval += 1000;
    1348             :                         }
    1349           4 :                         retval = retval % 1000;
    1350           4 :                         break;
    1351             : 
    1352             :                 /* time */
    1353           4 :                 case 'g': case 'h': retval = (int) ((t->h % 12) ? (int) t->h % 12 : 12); break;
    1354           4 :                 case 'H': case 'G': retval = (int) t->h; break;
    1355           3 :                 case 'i': retval = (int) t->i; break;
    1356           3 :                 case 's': retval = (int) t->s; break;
    1357             : 
    1358             :                 /* timezone */
    1359           3 :                 case 'I': retval = (int) (!localtime ? offset->is_dst : 0); break;
    1360           2 :                 case 'Z': retval = (int) (!localtime ? offset->offset : 0); break;
    1361             : 
    1362           3 :                 case 'U': retval = (int) t->sse; break;
    1363             :         }
    1364             : 
    1365          81 :         if (!localtime) {
    1366          81 :                 timelib_time_offset_dtor(offset);
    1367             :         }
    1368          81 :         timelib_time_dtor(t);
    1369             : 
    1370          81 :         return retval;
    1371             : }
    1372             : /* }}} */
    1373             : 
    1374             : /* {{{ proto string date(string format [, long timestamp])
    1375             :    Format a local date/time */
    1376        2904 : PHP_FUNCTION(date)
    1377             : {
    1378        2904 :         php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    1379        2904 : }
    1380             : /* }}} */
    1381             : 
    1382             : /* {{{ proto string gmdate(string format [, long timestamp])
    1383             :    Format a GMT date/time */
    1384         217 : PHP_FUNCTION(gmdate)
    1385             : {
    1386         217 :         php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    1387         217 : }
    1388             : /* }}} */
    1389             : 
    1390             : /* {{{ proto int idate(string format [, int timestamp])
    1391             :    Format a local time/date as integer */
    1392         140 : PHP_FUNCTION(idate)
    1393             : {
    1394             :         char   *format;
    1395             :         size_t     format_len;
    1396         140 :         zend_long    ts = 0;
    1397             :         int ret;
    1398             : 
    1399         140 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &format, &format_len, &ts) == FAILURE) {
    1400          26 :                 RETURN_FALSE;
    1401             :         }
    1402             : 
    1403         114 :         if (format_len != 1) {
    1404          33 :                 php_error_docref(NULL, E_WARNING, "idate format is one char");
    1405          33 :                 RETURN_FALSE;
    1406             :         }
    1407             : 
    1408          81 :         if (ZEND_NUM_ARGS() == 1) {
    1409          24 :                 ts = time(NULL);
    1410             :         }
    1411             : 
    1412          81 :         ret = php_idate(format[0], ts, 0);
    1413          81 :         if (ret == -1) {
    1414          12 :                 php_error_docref(NULL, E_WARNING, "Unrecognized date format token.");
    1415          12 :                 RETURN_FALSE;
    1416             :         }
    1417          69 :         RETURN_LONG(ret);
    1418             : }
    1419             : /* }}} */
    1420             : 
    1421             : /* {{{ php_date_set_tzdb - NOT THREADSAFE */
    1422           0 : PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb)
    1423             : {
    1424           0 :         const timelib_tzdb *builtin = timelib_builtin_db();
    1425             : 
    1426           0 :         if (php_version_compare(tzdb->version, builtin->version) > 0) {
    1427           0 :                 php_date_global_timezone_db = tzdb;
    1428           0 :                 php_date_global_timezone_db_enabled = 1;
    1429             :         }
    1430           0 : }
    1431             : /* }}} */
    1432             : 
    1433             : /* {{{ php_parse_date: Backwards compatibility function */
    1434          19 : PHPAPI zend_long php_parse_date(char *string, zend_long *now)
    1435             : {
    1436             :         timelib_time *parsed_time;
    1437          19 :         timelib_error_container *error = NULL;
    1438             :         int           error2;
    1439             :         zend_long   retval;
    1440             : 
    1441          19 :         parsed_time = timelib_strtotime(string, strlen(string), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    1442          19 :         if (error->error_count) {
    1443           3 :                 timelib_time_dtor(parsed_time);
    1444           3 :                 timelib_error_container_dtor(error);
    1445           3 :                 return -1;
    1446             :         }
    1447          16 :         timelib_error_container_dtor(error);
    1448          16 :         timelib_update_ts(parsed_time, NULL);
    1449          16 :         retval = timelib_date_to_int(parsed_time, &error2);
    1450          16 :         timelib_time_dtor(parsed_time);
    1451          16 :         if (error2) {
    1452           0 :                 return -1;
    1453             :         }
    1454          16 :         return retval;
    1455             : }
    1456             : /* }}} */
    1457             : 
    1458             : /* {{{ proto int strtotime(string time [, int now ])
    1459             :    Convert string representation of date and time to a timestamp */
    1460        4097 : PHP_FUNCTION(strtotime)
    1461             : {
    1462             :         char *times;
    1463             :         size_t   time_len;
    1464             :         int error1, error2;
    1465             :         struct timelib_error_container *error;
    1466        4097 :         zend_long preset_ts = 0, ts;
    1467             :         timelib_time *t, *now;
    1468             :         timelib_tzinfo *tzi;
    1469             : 
    1470        4097 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &times, &time_len, &preset_ts) == FAILURE || !time_len) {
    1471           2 :                 RETURN_FALSE;
    1472             :         }
    1473             : 
    1474        4095 :         tzi = get_timezone_info();
    1475             : 
    1476        4095 :         now = timelib_time_ctor();
    1477        4095 :         now->tz_info = tzi;
    1478        4095 :         now->zone_type = TIMELIB_ZONETYPE_ID;
    1479        8002 :         timelib_unixtime2local(now,
    1480        3907 :                 (ZEND_NUM_ARGS() == 2) ? (timelib_sll) preset_ts : (timelib_sll) time(NULL));
    1481             : 
    1482        4095 :         t = timelib_strtotime(times, time_len, &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    1483        4095 :         error1 = error->error_count;
    1484        4095 :         timelib_error_container_dtor(error);
    1485        4095 :         timelib_fill_holes(t, now, TIMELIB_NO_CLONE);
    1486        4095 :         timelib_update_ts(t, tzi);
    1487        4095 :         ts = timelib_date_to_int(t, &error2);
    1488             : 
    1489        4095 :         timelib_time_dtor(now);
    1490        4095 :         timelib_time_dtor(t);
    1491             : 
    1492        4095 :         if (error1 || error2) {
    1493          11 :                 RETURN_FALSE;
    1494             :         } else {
    1495        4084 :                 RETURN_LONG(ts);
    1496             :         }
    1497             : }
    1498             : /* }}} */
    1499             : 
    1500             : /* {{{ php_mktime - (gm)mktime helper */
    1501         922 : PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
    1502             : {
    1503         922 :         zend_long hou = 0, min = 0, sec = 0, mon = 0, day = 0, yea = 0;
    1504             :         timelib_time *now;
    1505         922 :         timelib_tzinfo *tzi = NULL;
    1506         922 :         zend_long ts, adjust_seconds = 0;
    1507             :         int error;
    1508             : 
    1509         922 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|llllll", &hou, &min, &sec, &mon, &day, &yea) == FAILURE) {
    1510         152 :                 RETURN_FALSE;
    1511             :         }
    1512             :         /* Initialize structure with current time */
    1513         770 :         now = timelib_time_ctor();
    1514         770 :         if (gmt) {
    1515         116 :                 timelib_unixtime2gmt(now, (timelib_sll) time(NULL));
    1516             :         } else {
    1517         654 :                 tzi = get_timezone_info();
    1518         654 :                 now->tz_info = tzi;
    1519         654 :                 now->zone_type = TIMELIB_ZONETYPE_ID;
    1520         654 :                 timelib_unixtime2local(now, (timelib_sll) time(NULL));
    1521             :         }
    1522             :         /* Fill in the new data */
    1523         770 :         switch (ZEND_NUM_ARGS()) {
    1524             :                 case 7:
    1525             :                         /* break intentionally missing */
    1526             :                 case 6:
    1527         713 :                         if (yea >= 0 && yea < 70) {
    1528          25 :                                 yea += 2000;
    1529         663 :                         } else if (yea >= 70 && yea <= 100) {
    1530           8 :                                 yea += 1900;
    1531             :                         }
    1532         688 :                         now->y = yea;
    1533             :                         /* break intentionally missing again */
    1534             :                 case 5:
    1535         704 :                         now->d = day;
    1536             :                         /* break missing intentionally here too */
    1537             :                 case 4:
    1538         720 :                         now->m = mon;
    1539             :                         /* and here */
    1540             :                 case 3:
    1541         736 :                         now->s = sec;
    1542             :                         /* yup, this break isn't here on purpose too */
    1543             :                 case 2:
    1544         752 :                         now->i = min;
    1545             :                         /* last intentionally missing break */
    1546             :                 case 1:
    1547         768 :                         now->h = hou;
    1548         768 :                         break;
    1549             :                 default:
    1550           2 :                         php_error_docref(NULL, E_DEPRECATED, "You should be using the time() function instead");
    1551             :         }
    1552             :         /* Update the timestamp */
    1553         770 :         if (gmt) {
    1554         116 :                 timelib_update_ts(now, NULL);
    1555             :         } else {
    1556         654 :                 timelib_update_ts(now, tzi);
    1557             :         }
    1558             : 
    1559             :         /* Clean up and return */
    1560         770 :         ts = timelib_date_to_int(now, &error);
    1561         770 :         ts += adjust_seconds;
    1562         770 :         timelib_time_dtor(now);
    1563             : 
    1564         770 :         if (error) {
    1565           0 :                 RETURN_FALSE;
    1566             :         } else {
    1567         770 :                 RETURN_LONG(ts);
    1568             :         }
    1569             : }
    1570             : /* }}} */
    1571             : 
    1572             : /* {{{ proto int mktime([int hour [, int min [, int sec [, int mon [, int day [, int year]]]]]])
    1573             :    Get UNIX timestamp for a date */
    1574         733 : PHP_FUNCTION(mktime)
    1575             : {
    1576         733 :         php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    1577         733 : }
    1578             : /* }}} */
    1579             : 
    1580             : /* {{{ proto int gmmktime([int hour [, int min [, int sec [, int mon [, int day [, int year]]]]]])
    1581             :    Get UNIX timestamp for a GMT date */
    1582         189 : PHP_FUNCTION(gmmktime)
    1583             : {
    1584         189 :         php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    1585         189 : }
    1586             : /* }}} */
    1587             : 
    1588             : /* {{{ proto bool checkdate(int month, int day, int year)
    1589             :    Returns true(1) if it is a valid date in gregorian calendar */
    1590         100 : PHP_FUNCTION(checkdate)
    1591             : {
    1592             :         zend_long m, d, y;
    1593             : 
    1594         100 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &m, &d, &y) == FAILURE) {
    1595          45 :                 RETURN_FALSE;
    1596             :         }
    1597             : 
    1598          55 :         if (y < 1 || y > 32767 || !timelib_valid_date(y, m, d)) {
    1599          36 :                 RETURN_FALSE;
    1600             :         }
    1601          19 :         RETURN_TRUE;    /* True : This month, day, year arguments are valid */
    1602             : }
    1603             : /* }}} */
    1604             : 
    1605             : #ifdef HAVE_STRFTIME
    1606             : /* {{{ php_strftime - (gm)strftime helper */
    1607         356 : PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
    1608             : {
    1609             :         char                *format;
    1610             :         size_t                  format_len;
    1611         356 :         zend_long                 timestamp = 0;
    1612             :         struct tm            ta;
    1613         356 :         int                  max_reallocs = 5;
    1614         356 :         size_t               buf_len = 256, real_len;
    1615             :         timelib_time        *ts;
    1616             :         timelib_tzinfo      *tzi;
    1617         356 :         timelib_time_offset *offset = NULL;
    1618             :         zend_string             *buf;
    1619             : 
    1620         356 :         timestamp = (zend_long) time(NULL);
    1621             : 
    1622         356 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &format, &format_len, &timestamp) == FAILURE) {
    1623          37 :                 RETURN_FALSE;
    1624             :         }
    1625             : 
    1626         319 :         if (format_len == 0) {
    1627          34 :                 RETURN_FALSE;
    1628             :         }
    1629             : 
    1630         285 :         ts = timelib_time_ctor();
    1631         285 :         if (gmt) {
    1632          97 :                 tzi = NULL;
    1633          97 :                 timelib_unixtime2gmt(ts, (timelib_sll) timestamp);
    1634             :         } else {
    1635         188 :                 tzi = get_timezone_info();
    1636         188 :                 ts->tz_info = tzi;
    1637         188 :                 ts->zone_type = TIMELIB_ZONETYPE_ID;
    1638         188 :                 timelib_unixtime2local(ts, (timelib_sll) timestamp);
    1639             :         }
    1640         285 :         ta.tm_sec   = ts->s;
    1641         285 :         ta.tm_min   = ts->i;
    1642         285 :         ta.tm_hour  = ts->h;
    1643         285 :         ta.tm_mday  = ts->d;
    1644         285 :         ta.tm_mon   = ts->m - 1;
    1645         285 :         ta.tm_year  = ts->y - 1900;
    1646         285 :         ta.tm_wday  = timelib_day_of_week(ts->y, ts->m, ts->d);
    1647         285 :         ta.tm_yday  = timelib_day_of_year(ts->y, ts->m, ts->d);
    1648         285 :         if (gmt) {
    1649          97 :                 ta.tm_isdst = 0;
    1650             : #if HAVE_TM_GMTOFF
    1651          97 :                 ta.tm_gmtoff = 0;
    1652             : #endif
    1653             : #if HAVE_TM_ZONE
    1654          97 :                 ta.tm_zone = "GMT";
    1655             : #endif
    1656             :         } else {
    1657         188 :                 offset = timelib_get_time_zone_info(timestamp, tzi);
    1658             : 
    1659         188 :                 ta.tm_isdst = offset->is_dst;
    1660             : #if HAVE_TM_GMTOFF
    1661         188 :                 ta.tm_gmtoff = offset->offset;
    1662             : #endif
    1663             : #if HAVE_TM_ZONE
    1664         188 :                 ta.tm_zone = offset->abbr;
    1665             : #endif
    1666             :         }
    1667             : 
    1668             :         /* VS2012 crt has a bug where strftime crash with %z and %Z format when the
    1669             :            initial buffer is too small. See
    1670             :            http://connect.microsoft.com/VisualStudio/feedback/details/759720/vs2012-strftime-crash-with-z-formatting-code */
    1671         285 :         buf = zend_string_alloc(buf_len, 0);
    1672         570 :         while ((real_len = strftime(ZSTR_VAL(buf), buf_len, format, &ta)) == buf_len || real_len == 0) {
    1673           0 :                 buf_len *= 2;
    1674           0 :                 buf = zend_string_extend(buf, buf_len, 0);
    1675           0 :                 if (!--max_reallocs) {
    1676           0 :                         break;
    1677             :                 }
    1678             :         }
    1679             : #ifdef PHP_WIN32
    1680             :         /* VS2012 strftime() returns number of characters, not bytes.
    1681             :                 See VC++11 bug id 766205. */
    1682             :         if (real_len > 0) {
    1683             :                 real_len = strlen(buf->val);
    1684             :         }
    1685             : #endif
    1686             : 
    1687         285 :         timelib_time_dtor(ts);
    1688         285 :         if (!gmt) {
    1689         188 :                 timelib_time_offset_dtor(offset);
    1690             :         }
    1691             : 
    1692         285 :         if (real_len && real_len != buf_len) {
    1693         285 :                 buf = zend_string_truncate(buf, real_len, 0);
    1694         285 :                 RETURN_NEW_STR(buf);
    1695             :         }
    1696             :         zend_string_free(buf);
    1697           0 :         RETURN_FALSE;
    1698             : }
    1699             : /* }}} */
    1700             : 
    1701             : /* {{{ proto string strftime(string format [, int timestamp])
    1702             :    Format a local time/date according to locale settings */
    1703         229 : PHP_FUNCTION(strftime)
    1704             : {
    1705         229 :         php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    1706         229 : }
    1707             : /* }}} */
    1708             : 
    1709             : /* {{{ proto string gmstrftime(string format [, int timestamp])
    1710             :    Format a GMT/UCT time/date according to locale settings */
    1711         127 : PHP_FUNCTION(gmstrftime)
    1712             : {
    1713         127 :         php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    1714         127 : }
    1715             : /* }}} */
    1716             : #endif
    1717             : 
    1718             : /* {{{ proto int time(void)
    1719             :    Return current UNIX timestamp */
    1720      239243 : PHP_FUNCTION(time)
    1721             : {
    1722      239243 :         RETURN_LONG((zend_long)time(NULL));
    1723             : }
    1724             : /* }}} */
    1725             : 
    1726             : /* {{{ proto array localtime([int timestamp [, bool associative_array]])
    1727             :    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 */
    1728          96 : PHP_FUNCTION(localtime)
    1729             : {
    1730          96 :         zend_long timestamp = (zend_long)time(NULL);
    1731          96 :         zend_bool associative = 0;
    1732             :         timelib_tzinfo *tzi;
    1733             :         timelib_time   *ts;
    1734             : 
    1735          96 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lb", &timestamp, &associative) == FAILURE) {
    1736          32 :                 RETURN_FALSE;
    1737             :         }
    1738             : 
    1739          64 :         tzi = get_timezone_info();
    1740          64 :         ts = timelib_time_ctor();
    1741          64 :         ts->tz_info = tzi;
    1742          64 :         ts->zone_type = TIMELIB_ZONETYPE_ID;
    1743          64 :         timelib_unixtime2local(ts, (timelib_sll) timestamp);
    1744             : 
    1745          64 :         array_init(return_value);
    1746             : 
    1747          64 :         if (associative) {
    1748          33 :                 add_assoc_long(return_value, "tm_sec",   ts->s);
    1749          33 :                 add_assoc_long(return_value, "tm_min",   ts->i);
    1750          33 :                 add_assoc_long(return_value, "tm_hour",  ts->h);
    1751          33 :                 add_assoc_long(return_value, "tm_mday",  ts->d);
    1752          33 :                 add_assoc_long(return_value, "tm_mon",   ts->m - 1);
    1753          33 :                 add_assoc_long(return_value, "tm_year",  ts->y - 1900);
    1754          33 :                 add_assoc_long(return_value, "tm_wday",  timelib_day_of_week(ts->y, ts->m, ts->d));
    1755          33 :                 add_assoc_long(return_value, "tm_yday",  timelib_day_of_year(ts->y, ts->m, ts->d));
    1756          33 :                 add_assoc_long(return_value, "tm_isdst", ts->dst);
    1757             :         } else {
    1758          31 :                 add_next_index_long(return_value, ts->s);
    1759          31 :                 add_next_index_long(return_value, ts->i);
    1760          31 :                 add_next_index_long(return_value, ts->h);
    1761          31 :                 add_next_index_long(return_value, ts->d);
    1762          31 :                 add_next_index_long(return_value, ts->m - 1);
    1763          31 :                 add_next_index_long(return_value, ts->y- 1900);
    1764          31 :                 add_next_index_long(return_value, timelib_day_of_week(ts->y, ts->m, ts->d));
    1765          31 :                 add_next_index_long(return_value, timelib_day_of_year(ts->y, ts->m, ts->d));
    1766          31 :                 add_next_index_long(return_value, ts->dst);
    1767             :         }
    1768             : 
    1769          64 :         timelib_time_dtor(ts);
    1770             : }
    1771             : /* }}} */
    1772             : 
    1773             : /* {{{ proto array getdate([int timestamp])
    1774             :    Get date/time information */
    1775          49 : PHP_FUNCTION(getdate)
    1776             : {
    1777          49 :         zend_long timestamp = (zend_long)time(NULL);
    1778             :         timelib_tzinfo *tzi;
    1779             :         timelib_time   *ts;
    1780             : 
    1781          49 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &timestamp) == FAILURE) {
    1782          14 :                 RETURN_FALSE;
    1783             :         }
    1784             : 
    1785          35 :         tzi = get_timezone_info();
    1786          35 :         ts = timelib_time_ctor();
    1787          35 :         ts->tz_info = tzi;
    1788          35 :         ts->zone_type = TIMELIB_ZONETYPE_ID;
    1789          35 :         timelib_unixtime2local(ts, (timelib_sll) timestamp);
    1790             : 
    1791          35 :         array_init(return_value);
    1792             : 
    1793          35 :         add_assoc_long(return_value, "seconds", ts->s);
    1794          35 :         add_assoc_long(return_value, "minutes", ts->i);
    1795          35 :         add_assoc_long(return_value, "hours", ts->h);
    1796          35 :         add_assoc_long(return_value, "mday", ts->d);
    1797          35 :         add_assoc_long(return_value, "wday", timelib_day_of_week(ts->y, ts->m, ts->d));
    1798          35 :         add_assoc_long(return_value, "mon", ts->m);
    1799          35 :         add_assoc_long(return_value, "year", ts->y);
    1800          35 :         add_assoc_long(return_value, "yday", timelib_day_of_year(ts->y, ts->m, ts->d));
    1801          35 :         add_assoc_string(return_value, "weekday", php_date_full_day_name(ts->y, ts->m, ts->d));
    1802          35 :         add_assoc_string(return_value, "month", mon_full_names[ts->m - 1]);
    1803          35 :         add_index_long(return_value, 0, timestamp);
    1804             : 
    1805          35 :         timelib_time_dtor(ts);
    1806             : }
    1807             : /* }}} */
    1808             : 
    1809             : #define PHP_DATE_TIMEZONE_GROUP_AFRICA     0x0001
    1810             : #define PHP_DATE_TIMEZONE_GROUP_AMERICA    0x0002
    1811             : #define PHP_DATE_TIMEZONE_GROUP_ANTARCTICA 0x0004
    1812             : #define PHP_DATE_TIMEZONE_GROUP_ARCTIC     0x0008
    1813             : #define PHP_DATE_TIMEZONE_GROUP_ASIA       0x0010
    1814             : #define PHP_DATE_TIMEZONE_GROUP_ATLANTIC   0x0020
    1815             : #define PHP_DATE_TIMEZONE_GROUP_AUSTRALIA  0x0040
    1816             : #define PHP_DATE_TIMEZONE_GROUP_EUROPE     0x0080
    1817             : #define PHP_DATE_TIMEZONE_GROUP_INDIAN     0x0100
    1818             : #define PHP_DATE_TIMEZONE_GROUP_PACIFIC    0x0200
    1819             : #define PHP_DATE_TIMEZONE_GROUP_UTC        0x0400
    1820             : #define PHP_DATE_TIMEZONE_GROUP_ALL        0x07FF
    1821             : #define PHP_DATE_TIMEZONE_GROUP_ALL_W_BC   0x0FFF
    1822             : #define PHP_DATE_TIMEZONE_PER_COUNTRY      0x1000
    1823             : 
    1824             : #define PHP_DATE_PERIOD_EXCLUDE_START_DATE 0x0001
    1825             : 
    1826             : 
    1827             : /* define an overloaded iterator structure */
    1828             : typedef struct {
    1829             :         zend_object_iterator  intern;
    1830             :         zval                  current;
    1831             :         php_period_obj       *object;
    1832             :         int                   current_index;
    1833             : } date_period_it;
    1834             : 
    1835             : /* {{{ date_period_it_invalidate_current */
    1836          97 : static void date_period_it_invalidate_current(zend_object_iterator *iter)
    1837             : {
    1838          97 :         date_period_it *iterator = (date_period_it *)iter;
    1839             : 
    1840         194 :         if (Z_TYPE(iterator->current) != IS_UNDEF) {
    1841          75 :                 zval_ptr_dtor(&iterator->current);
    1842          75 :                 ZVAL_UNDEF(&iterator->current);
    1843             :         }
    1844          97 : }
    1845             : /* }}} */
    1846             : 
    1847             : /* {{{ date_period_it_dtor */
    1848          11 : static void date_period_it_dtor(zend_object_iterator *iter)
    1849             : {
    1850          11 :         date_period_it *iterator = (date_period_it *)iter;
    1851             : 
    1852          11 :         date_period_it_invalidate_current(iter);
    1853             : 
    1854          11 :         zval_ptr_dtor(&iterator->intern.data);
    1855          11 : }
    1856             : /* }}} */
    1857             : 
    1858             : /* {{{ date_period_it_has_more */
    1859          86 : static int date_period_it_has_more(zend_object_iterator *iter)
    1860             : {
    1861          86 :         date_period_it *iterator = (date_period_it *)iter;
    1862          86 :         php_period_obj *object   = Z_PHPPERIOD_P(&iterator->intern.data);
    1863          86 :         timelib_time   *it_time = object->current;
    1864             : 
    1865             :         /* apply modification if it's not the first iteration */
    1866          86 :         if (!object->include_start_date || iterator->current_index > 0) {
    1867          76 :                 it_time->have_relative = 1;
    1868          76 :                 it_time->relative = *object->interval;
    1869          76 :                 it_time->sse_uptodate = 0;
    1870          76 :                 timelib_update_ts(it_time, NULL);
    1871          76 :                 timelib_update_from_sse(it_time);
    1872             :         }
    1873             : 
    1874          86 :         if (object->end) {
    1875          46 :                 return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
    1876             :         } else {
    1877          40 :                 return (iterator->current_index < object->recurrences) ? SUCCESS : FAILURE;
    1878             :         }
    1879             : }
    1880             : /* }}} */
    1881             : 
    1882             : /* {{{ date_period_it_current_data */
    1883          75 : static zval *date_period_it_current_data(zend_object_iterator *iter)
    1884             : {
    1885          75 :         date_period_it *iterator = (date_period_it *)iter;
    1886          75 :         php_period_obj *object   = Z_PHPPERIOD_P(&iterator->intern.data);
    1887          75 :         timelib_time   *it_time = object->current;
    1888             :         php_date_obj   *newdateobj;
    1889             : 
    1890             :         /* Create new object */
    1891          75 :         php_date_instantiate(object->start_ce, &iterator->current);
    1892          75 :         newdateobj = Z_PHPDATE_P(&iterator->current);
    1893          75 :         newdateobj->time = timelib_time_ctor();
    1894          75 :         *newdateobj->time = *it_time;
    1895          75 :         if (it_time->tz_abbr) {
    1896          63 :                 newdateobj->time->tz_abbr = timelib_strdup(it_time->tz_abbr);
    1897             :         }
    1898          75 :         if (it_time->tz_info) {
    1899          63 :                 newdateobj->time->tz_info = it_time->tz_info;
    1900             :         }
    1901             : 
    1902          75 :         return &iterator->current;
    1903             : }
    1904             : /* }}} */
    1905             : 
    1906             : /* {{{ date_period_it_current_key */
    1907           0 : static void date_period_it_current_key(zend_object_iterator *iter, zval *key)
    1908             : {
    1909           0 :         date_period_it *iterator = (date_period_it *)iter;
    1910           0 :         ZVAL_LONG(key, iterator->current_index);
    1911           0 : }
    1912             : /* }}} */
    1913             : 
    1914             : /* {{{ date_period_it_move_forward */
    1915          75 : static void date_period_it_move_forward(zend_object_iterator *iter)
    1916             : {
    1917          75 :         date_period_it   *iterator = (date_period_it *)iter;
    1918             : 
    1919          75 :         iterator->current_index++;
    1920          75 :         date_period_it_invalidate_current(iter);
    1921          75 : }
    1922             : /* }}} */
    1923             : 
    1924             : /* {{{ date_period_it_rewind */
    1925          11 : static void date_period_it_rewind(zend_object_iterator *iter)
    1926             : {
    1927          11 :         date_period_it *iterator = (date_period_it *)iter;
    1928             : 
    1929          11 :         iterator->current_index = 0;
    1930          11 :         if (iterator->object->current) {
    1931           3 :                 timelib_time_dtor(iterator->object->current);
    1932             :         }
    1933          11 :         iterator->object->current = timelib_time_clone(iterator->object->start);
    1934          11 :         date_period_it_invalidate_current(iter);
    1935          11 : }
    1936             : /* }}} */
    1937             : 
    1938             : /* iterator handler table */
    1939             : zend_object_iterator_funcs date_period_it_funcs = {
    1940             :         date_period_it_dtor,
    1941             :         date_period_it_has_more,
    1942             :         date_period_it_current_data,
    1943             :         date_period_it_current_key,
    1944             :         date_period_it_move_forward,
    1945             :         date_period_it_rewind,
    1946             :         date_period_it_invalidate_current
    1947             : };
    1948             : 
    1949          11 : zend_object_iterator *date_object_period_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
    1950             : {
    1951          11 :         date_period_it *iterator = emalloc(sizeof(date_period_it));
    1952             : 
    1953          11 :         if (by_ref) {
    1954           0 :                 zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
    1955             :         }
    1956             : 
    1957          11 :         zend_iterator_init((zend_object_iterator*)iterator);
    1958             : 
    1959          11 :         ZVAL_COPY(&iterator->intern.data, object);
    1960          11 :         iterator->intern.funcs = &date_period_it_funcs;
    1961          11 :         iterator->object = Z_PHPPERIOD_P(object);
    1962          11 :         ZVAL_UNDEF(&iterator->current);
    1963             : 
    1964          11 :         return (zend_object_iterator*)iterator;
    1965             : } /* }}} */
    1966             : 
    1967       47866 : static int implement_date_interface_handler(zend_class_entry *interface, zend_class_entry *implementor) /* {{{ */
    1968             : {
    1969       47890 :         if (implementor->type == ZEND_USER_CLASS &&
    1970          22 :                 !instanceof_function(implementor, date_ce_date) &&
    1971           2 :                 !instanceof_function(implementor, date_ce_immutable)
    1972             :         ) {
    1973           1 :                 zend_error(E_ERROR, "DateTimeInterface can't be implemented by user classes");
    1974             :         }
    1975             : 
    1976       47865 :         return SUCCESS;
    1977             : } /* }}} */
    1978             : 
    1979       23922 : static void date_register_classes(void) /* {{{ */
    1980             : {
    1981             :         zend_class_entry ce_date, ce_immutable, ce_timezone, ce_interval, ce_period, ce_interface;
    1982             : 
    1983       23922 :         INIT_CLASS_ENTRY(ce_interface, "DateTimeInterface", date_funcs_interface);
    1984       23922 :         date_ce_interface = zend_register_internal_interface(&ce_interface);
    1985       23922 :         date_ce_interface->interface_gets_implemented = implement_date_interface_handler;
    1986             : 
    1987       23922 :         INIT_CLASS_ENTRY(ce_date, "DateTime", date_funcs_date);
    1988       23922 :         ce_date.create_object = date_object_new_date;
    1989       23922 :         date_ce_date = zend_register_internal_class_ex(&ce_date, NULL);
    1990       23922 :         memcpy(&date_object_handlers_date, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    1991       23922 :         date_object_handlers_date.offset = XtOffsetOf(php_date_obj, std);
    1992       23922 :         date_object_handlers_date.free_obj = date_object_free_storage_date;
    1993       23922 :         date_object_handlers_date.clone_obj = date_object_clone_date;
    1994       23922 :         date_object_handlers_date.compare_objects = date_object_compare_date;
    1995       23922 :         date_object_handlers_date.get_properties = date_object_get_properties;
    1996       23922 :         date_object_handlers_date.get_gc = date_object_get_gc;
    1997       23922 :         zend_class_implements(date_ce_date, 1, date_ce_interface);
    1998             : 
    1999             : #define REGISTER_DATE_CLASS_CONST_STRING(const_name, value) \
    2000             :         zend_declare_class_constant_stringl(date_ce_date, const_name, sizeof(const_name)-1, value, sizeof(value)-1);
    2001             : 
    2002       23922 :         REGISTER_DATE_CLASS_CONST_STRING("ATOM",             DATE_FORMAT_RFC3339);
    2003       23922 :         REGISTER_DATE_CLASS_CONST_STRING("COOKIE",           DATE_FORMAT_COOKIE);
    2004       23922 :         REGISTER_DATE_CLASS_CONST_STRING("ISO8601",          DATE_FORMAT_ISO8601);
    2005       23922 :         REGISTER_DATE_CLASS_CONST_STRING("RFC822",           DATE_FORMAT_RFC822);
    2006       23922 :         REGISTER_DATE_CLASS_CONST_STRING("RFC850",           DATE_FORMAT_RFC850);
    2007       23922 :         REGISTER_DATE_CLASS_CONST_STRING("RFC1036",          DATE_FORMAT_RFC1036);
    2008       23922 :         REGISTER_DATE_CLASS_CONST_STRING("RFC1123",          DATE_FORMAT_RFC1123);
    2009       23922 :         REGISTER_DATE_CLASS_CONST_STRING("RFC2822",          DATE_FORMAT_RFC2822);
    2010       23922 :         REGISTER_DATE_CLASS_CONST_STRING("RFC3339",          DATE_FORMAT_RFC3339);
    2011       23922 :         REGISTER_DATE_CLASS_CONST_STRING("RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED);
    2012       23922 :         REGISTER_DATE_CLASS_CONST_STRING("RSS",              DATE_FORMAT_RFC1123);
    2013       23922 :         REGISTER_DATE_CLASS_CONST_STRING("W3C",              DATE_FORMAT_RFC3339);
    2014             : 
    2015       23922 :         INIT_CLASS_ENTRY(ce_immutable, "DateTimeImmutable", date_funcs_immutable);
    2016       23922 :         ce_immutable.create_object = date_object_new_date;
    2017       23922 :         date_ce_immutable = zend_register_internal_class_ex(&ce_immutable, NULL);
    2018       23922 :         memcpy(&date_object_handlers_immutable, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    2019       23922 :         date_object_handlers_immutable.clone_obj = date_object_clone_date;
    2020       23922 :         date_object_handlers_immutable.compare_objects = date_object_compare_date;
    2021       23922 :         date_object_handlers_immutable.get_properties = date_object_get_properties;
    2022       23922 :         date_object_handlers_immutable.get_gc = date_object_get_gc;
    2023       23922 :         zend_class_implements(date_ce_immutable, 1, date_ce_interface);
    2024             : 
    2025       23922 :         INIT_CLASS_ENTRY(ce_timezone, "DateTimeZone", date_funcs_timezone);
    2026       23922 :         ce_timezone.create_object = date_object_new_timezone;
    2027       23922 :         date_ce_timezone = zend_register_internal_class_ex(&ce_timezone, NULL);
    2028       23922 :         memcpy(&date_object_handlers_timezone, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    2029       23922 :         date_object_handlers_timezone.offset = XtOffsetOf(php_timezone_obj, std);
    2030       23922 :         date_object_handlers_timezone.free_obj = date_object_free_storage_timezone;
    2031       23922 :         date_object_handlers_timezone.clone_obj = date_object_clone_timezone;
    2032       23922 :         date_object_handlers_timezone.get_properties = date_object_get_properties_timezone;
    2033       23922 :         date_object_handlers_timezone.get_gc = date_object_get_gc_timezone;
    2034             : 
    2035             : #define REGISTER_TIMEZONE_CLASS_CONST_STRING(const_name, value) \
    2036             :         zend_declare_class_constant_long(date_ce_timezone, const_name, sizeof(const_name)-1, value);
    2037             : 
    2038       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("AFRICA",      PHP_DATE_TIMEZONE_GROUP_AFRICA);
    2039       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("AMERICA",     PHP_DATE_TIMEZONE_GROUP_AMERICA);
    2040       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("ANTARCTICA",  PHP_DATE_TIMEZONE_GROUP_ANTARCTICA);
    2041       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("ARCTIC",      PHP_DATE_TIMEZONE_GROUP_ARCTIC);
    2042       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("ASIA",        PHP_DATE_TIMEZONE_GROUP_ASIA);
    2043       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("ATLANTIC",    PHP_DATE_TIMEZONE_GROUP_ATLANTIC);
    2044       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("AUSTRALIA",   PHP_DATE_TIMEZONE_GROUP_AUSTRALIA);
    2045       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("EUROPE",      PHP_DATE_TIMEZONE_GROUP_EUROPE);
    2046       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("INDIAN",      PHP_DATE_TIMEZONE_GROUP_INDIAN);
    2047       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("PACIFIC",     PHP_DATE_TIMEZONE_GROUP_PACIFIC);
    2048       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("UTC",         PHP_DATE_TIMEZONE_GROUP_UTC);
    2049       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL",         PHP_DATE_TIMEZONE_GROUP_ALL);
    2050       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL_WITH_BC", PHP_DATE_TIMEZONE_GROUP_ALL_W_BC);
    2051       23922 :         REGISTER_TIMEZONE_CLASS_CONST_STRING("PER_COUNTRY", PHP_DATE_TIMEZONE_PER_COUNTRY);
    2052             : 
    2053       23922 :         INIT_CLASS_ENTRY(ce_interval, "DateInterval", date_funcs_interval);
    2054       23922 :         ce_interval.create_object = date_object_new_interval;
    2055       23922 :         date_ce_interval = zend_register_internal_class_ex(&ce_interval, NULL);
    2056       23922 :         memcpy(&date_object_handlers_interval, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    2057       23922 :         date_object_handlers_interval.offset = XtOffsetOf(php_interval_obj, std);
    2058       23922 :         date_object_handlers_interval.free_obj = date_object_free_storage_interval;
    2059       23922 :         date_object_handlers_interval.clone_obj = date_object_clone_interval;
    2060       23922 :         date_object_handlers_interval.read_property = date_interval_read_property;
    2061       23922 :         date_object_handlers_interval.write_property = date_interval_write_property;
    2062       23922 :         date_object_handlers_interval.get_properties = date_object_get_properties_interval;
    2063       23922 :         date_object_handlers_interval.get_property_ptr_ptr = NULL;
    2064       23922 :         date_object_handlers_interval.get_gc = date_object_get_gc_interval;
    2065             : 
    2066       23922 :         INIT_CLASS_ENTRY(ce_period, "DatePeriod", date_funcs_period);
    2067       23922 :         ce_period.create_object = date_object_new_period;
    2068       23922 :         date_ce_period = zend_register_internal_class_ex(&ce_period, NULL);
    2069       23922 :         date_ce_period->get_iterator = date_object_period_get_iterator;
    2070       23922 :         date_ce_period->iterator_funcs.funcs = &date_period_it_funcs;
    2071       23922 :         zend_class_implements(date_ce_period, 1, zend_ce_traversable);
    2072       23922 :         memcpy(&date_object_handlers_period, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    2073       23922 :         date_object_handlers_period.offset = XtOffsetOf(php_period_obj, std);
    2074       23922 :         date_object_handlers_period.free_obj = date_object_free_storage_period;
    2075       23922 :         date_object_handlers_period.clone_obj = date_object_clone_period;
    2076       23922 :         date_object_handlers_period.get_properties = date_object_get_properties_period;
    2077       23922 :         date_object_handlers_period.get_property_ptr_ptr = NULL;
    2078       23922 :         date_object_handlers_period.get_gc = date_object_get_gc_period;
    2079       23922 :         date_object_handlers_period.read_property = date_period_read_property;
    2080       23922 :         date_object_handlers_period.write_property = date_period_write_property;
    2081             : 
    2082             : #define REGISTER_PERIOD_CLASS_CONST_STRING(const_name, value) \
    2083             :         zend_declare_class_constant_long(date_ce_period, const_name, sizeof(const_name)-1, value);
    2084             : 
    2085       23922 :         REGISTER_PERIOD_CLASS_CONST_STRING("EXCLUDE_START_DATE", PHP_DATE_PERIOD_EXCLUDE_START_DATE);
    2086       23922 : } /* }}} */
    2087             : 
    2088      179316 : static inline zend_object *date_object_new_date_ex(zend_class_entry *class_type, int init_props) /* {{{ */
    2089             : {
    2090             :         php_date_obj *intern;
    2091             : 
    2092      179316 :         intern = ecalloc(1, sizeof(php_date_obj) + zend_object_properties_size(class_type));
    2093             : 
    2094      179316 :         zend_object_std_init(&intern->std, class_type);
    2095      179316 :         if (init_props) {
    2096        5044 :                 object_properties_init(&intern->std, class_type);
    2097             :         }
    2098      179316 :         intern->std.handlers = &date_object_handlers_date;
    2099             : 
    2100      179316 :         return &intern->std;
    2101             : } /* }}} */
    2102             : 
    2103        5044 : static zend_object *date_object_new_date(zend_class_entry *class_type) /* {{{ */
    2104             : {
    2105        5044 :         return date_object_new_date_ex(class_type, 1);
    2106             : } /* }}} */
    2107             : 
    2108      174272 : static zend_object *date_object_clone_date(zval *this_ptr) /* {{{ */
    2109             : {
    2110      174272 :         php_date_obj *old_obj = Z_PHPDATE_P(this_ptr);
    2111      174272 :         php_date_obj *new_obj = php_date_obj_from_obj(date_object_new_date_ex(old_obj->std.ce, 0));
    2112             : 
    2113      174272 :         zend_objects_clone_members(&new_obj->std, &old_obj->std);
    2114      174272 :         if (!old_obj->time) {
    2115           2 :                 return &new_obj->std;
    2116             :         }
    2117             : 
    2118             :         /* this should probably moved to a new `timelib_time *timelime_time_clone(timelib_time *)` */
    2119      174270 :         new_obj->time = timelib_time_ctor();
    2120      174270 :         *new_obj->time = *old_obj->time;
    2121      174270 :         if (old_obj->time->tz_abbr) {
    2122      174270 :                 new_obj->time->tz_abbr = timelib_strdup(old_obj->time->tz_abbr);
    2123             :         }
    2124      174270 :         if (old_obj->time->tz_info) {
    2125      174270 :                 new_obj->time->tz_info = old_obj->time->tz_info;
    2126             :         }
    2127             : 
    2128      174270 :         return &new_obj->std;
    2129             : } /* }}} */
    2130             : 
    2131          23 : static void date_clone_immutable(zval *object, zval *new_object) /* {{{ */
    2132             : {
    2133          23 :         ZVAL_OBJ(new_object, date_object_clone_date(object));
    2134          23 : } /* }}} */
    2135             : 
    2136      345627 : static int date_object_compare_date(zval *d1, zval *d2) /* {{{ */
    2137             : {
    2138      345627 :         php_date_obj *o1 = Z_PHPDATE_P(d1);
    2139      345627 :         php_date_obj *o2 = Z_PHPDATE_P(d2);
    2140             : 
    2141      345627 :         if (!o1->time || !o2->time) {
    2142           0 :                 php_error_docref(NULL, E_WARNING, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
    2143           0 :                 return 1;
    2144             :         }
    2145      345627 :         if (!o1->time->sse_uptodate) {
    2146           0 :                 timelib_update_ts(o1->time, o1->time->tz_info);
    2147             :         }
    2148      345627 :         if (!o2->time->sse_uptodate) {
    2149           0 :                 timelib_update_ts(o2->time, o2->time->tz_info);
    2150             :         }
    2151             : 
    2152      345627 :         return timelib_time_compare(o1->time, o2->time);
    2153             : } /* }}} */
    2154             : 
    2155           2 : static HashTable *date_object_get_gc(zval *object, zval **table, int *n) /* {{{ */
    2156             : {
    2157           2 :         *table = NULL;
    2158           2 :         *n = 0;
    2159           2 :         return zend_std_get_properties(object);
    2160             : } /* }}} */
    2161             : 
    2162           0 : static HashTable *date_object_get_gc_timezone(zval *object, zval **table, int *n) /* {{{ */
    2163             : {
    2164           0 :        *table = NULL;
    2165           0 :        *n = 0;
    2166           0 :        return zend_std_get_properties(object);
    2167             : } /* }}} */
    2168             : 
    2169         447 : static HashTable *date_object_get_properties(zval *object) /* {{{ */
    2170             : {
    2171             :         HashTable *props;
    2172             :         zval zv;
    2173             :         php_date_obj     *dateobj;
    2174             : 
    2175             : 
    2176         447 :         dateobj = Z_PHPDATE_P(object);
    2177             : 
    2178         447 :         props = zend_std_get_properties(object);
    2179             : 
    2180         447 :         if (!dateobj->time) {
    2181          27 :                 return props;
    2182             :         }
    2183             : 
    2184             :         /* first we add the date and time in ISO format */
    2185         420 :         ZVAL_STR(&zv, date_format("Y-m-d H:i:s.u", sizeof("Y-m-d H:i:s.u")-1, dateobj->time, 1));
    2186         420 :         zend_hash_str_update(props, "date", sizeof("date")-1, &zv);
    2187             : 
    2188             :         /* then we add the timezone name (or similar) */
    2189         420 :         if (dateobj->time->is_localtime) {
    2190         420 :                 ZVAL_LONG(&zv, dateobj->time->zone_type);
    2191         420 :                 zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
    2192             : 
    2193         420 :                 switch (dateobj->time->zone_type) {
    2194             :                         case TIMELIB_ZONETYPE_ID:
    2195         762 :                                 ZVAL_STRING(&zv, dateobj->time->tz_info->name);
    2196         381 :                                 break;
    2197             :                         case TIMELIB_ZONETYPE_OFFSET: {
    2198          12 :                                 zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
    2199          12 :                                 timelib_sll utc_offset = dateobj->time->z;
    2200             : 
    2201          24 :                                 ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
    2202             :                                         utc_offset > 0 ? '-' : '+',
    2203          12 :                                         abs(utc_offset / 60),
    2204          12 :                                         abs((utc_offset % 60)));
    2205             : 
    2206          12 :                                 ZVAL_NEW_STR(&zv, tmpstr);
    2207             :                                 }
    2208          12 :                                 break;
    2209             :                         case TIMELIB_ZONETYPE_ABBR:
    2210          54 :                                 ZVAL_STRING(&zv, dateobj->time->tz_abbr);
    2211             :                                 break;
    2212             :                 }
    2213         420 :                 zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
    2214             :         }
    2215             : 
    2216         420 :         return props;
    2217             : } /* }}} */
    2218             : 
    2219         837 : static inline zend_object *date_object_new_timezone_ex(zend_class_entry *class_type, int init_props) /* {{{ */
    2220             : {
    2221             :         php_timezone_obj *intern;
    2222             : 
    2223         837 :         intern = ecalloc(1, sizeof(php_timezone_obj) + zend_object_properties_size(class_type));
    2224             : 
    2225         837 :         zend_object_std_init(&intern->std, class_type);
    2226         837 :         if (init_props) {
    2227         829 :                 object_properties_init(&intern->std, class_type);
    2228             :         }
    2229         837 :         intern->std.handlers = &date_object_handlers_timezone;
    2230             : 
    2231         837 :         return &intern->std;
    2232             : } /* }}} */
    2233             : 
    2234         829 : static zend_object *date_object_new_timezone(zend_class_entry *class_type) /* {{{ */
    2235             : {
    2236         829 :         return date_object_new_timezone_ex(class_type, 1);
    2237             : } /* }}} */
    2238             : 
    2239           8 : static zend_object *date_object_clone_timezone(zval *this_ptr) /* {{{ */
    2240             : {
    2241           8 :         php_timezone_obj *old_obj = Z_PHPTIMEZONE_P(this_ptr);
    2242           8 :         php_timezone_obj *new_obj = php_timezone_obj_from_obj(date_object_new_timezone_ex(old_obj->std.ce, 0));
    2243             : 
    2244           8 :         zend_objects_clone_members(&new_obj->std, &old_obj->std);
    2245           8 :         if (!old_obj->initialized) {
    2246           1 :                 return &new_obj->std;
    2247             :         }
    2248             : 
    2249           7 :         new_obj->type = old_obj->type;
    2250           7 :         new_obj->initialized = 1;
    2251           7 :         switch (new_obj->type) {
    2252             :                 case TIMELIB_ZONETYPE_ID:
    2253           6 :                         new_obj->tzi.tz = old_obj->tzi.tz;
    2254           6 :                         break;
    2255             :                 case TIMELIB_ZONETYPE_OFFSET:
    2256           0 :                         new_obj->tzi.utc_offset = old_obj->tzi.utc_offset;
    2257           0 :                         break;
    2258             :                 case TIMELIB_ZONETYPE_ABBR:
    2259           1 :                         new_obj->tzi.z.utc_offset = old_obj->tzi.z.utc_offset;
    2260           1 :                         new_obj->tzi.z.dst        = old_obj->tzi.z.dst;
    2261           1 :                         new_obj->tzi.z.abbr       = timelib_strdup(old_obj->tzi.z.abbr);
    2262             :                         break;
    2263             :         }
    2264             : 
    2265           7 :         return &new_obj->std;
    2266             : } /* }}} */
    2267             : 
    2268          66 : static HashTable *date_object_get_properties_timezone(zval *object) /* {{{ */
    2269             : {
    2270             :         HashTable *props;
    2271             :         zval zv;
    2272             :         php_timezone_obj     *tzobj;
    2273             : 
    2274             : 
    2275          66 :         tzobj = Z_PHPTIMEZONE_P(object);
    2276             : 
    2277          66 :         props = zend_std_get_properties(object);
    2278             : 
    2279          66 :         if (!tzobj->initialized) {
    2280           8 :                 return props;
    2281             :         }
    2282             : 
    2283          58 :         ZVAL_LONG(&zv, tzobj->type);
    2284          58 :         zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
    2285             : 
    2286          58 :         switch (tzobj->type) {
    2287             :                 case TIMELIB_ZONETYPE_ID:
    2288          90 :                         ZVAL_STRING(&zv, tzobj->tzi.tz->name);
    2289          45 :                         break;
    2290             :                 case TIMELIB_ZONETYPE_OFFSET: {
    2291           5 :                         zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
    2292             : 
    2293          15 :                         ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
    2294           5 :                         tzobj->tzi.utc_offset > 0 ? '-' : '+',
    2295           5 :                         abs(tzobj->tzi.utc_offset / 60),
    2296           5 :                         abs((tzobj->tzi.utc_offset % 60)));
    2297             : 
    2298           5 :                         ZVAL_NEW_STR(&zv, tmpstr);
    2299             :                         }
    2300           5 :                         break;
    2301             :                 case TIMELIB_ZONETYPE_ABBR:
    2302          16 :                         ZVAL_STRING(&zv, tzobj->tzi.z.abbr);
    2303             :                         break;
    2304             :         }
    2305          58 :         zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
    2306             : 
    2307          58 :         return props;
    2308             : } /* }}} */
    2309             : 
    2310      349108 : static inline zend_object *date_object_new_interval_ex(zend_class_entry *class_type, int init_props) /* {{{ */
    2311             : {
    2312             :         php_interval_obj *intern;
    2313             : 
    2314      349108 :         intern = ecalloc(1, sizeof(php_interval_obj) + zend_object_properties_size(class_type));
    2315             : 
    2316      349108 :         zend_object_std_init(&intern->std, class_type);
    2317      349108 :         if (init_props) {
    2318      349108 :                 object_properties_init(&intern->std, class_type);
    2319             :         }
    2320      349108 :         intern->std.handlers = &date_object_handlers_interval;
    2321             : 
    2322      349108 :         return &intern->std;
    2323             : } /* }}} */
    2324             : 
    2325      349108 : static zend_object *date_object_new_interval(zend_class_entry *class_type) /* {{{ */
    2326             : {
    2327      349108 :         return date_object_new_interval_ex(class_type, 1);
    2328             : } /* }}} */
    2329             : 
    2330           0 : static zend_object *date_object_clone_interval(zval *this_ptr) /* {{{ */
    2331             : {
    2332           0 :         php_interval_obj *old_obj = Z_PHPINTERVAL_P(this_ptr);
    2333           0 :         php_interval_obj *new_obj = php_interval_obj_from_obj(date_object_new_interval_ex(old_obj->std.ce, 0));
    2334             : 
    2335           0 :         zend_objects_clone_members(&new_obj->std, &old_obj->std);
    2336             : 
    2337             :         /** FIX ME ADD CLONE STUFF **/
    2338           0 :         return &new_obj->std;
    2339             : } /* }}} */
    2340             : 
    2341           0 : static HashTable *date_object_get_gc_interval(zval *object, zval **table, int *n) /* {{{ */
    2342             : {
    2343             : 
    2344           0 :         *table = NULL;
    2345           0 :         *n = 0;
    2346           0 :         return zend_std_get_properties(object);
    2347             : } /* }}} */
    2348             : 
    2349          43 : static HashTable *date_object_get_properties_interval(zval *object) /* {{{ */
    2350             : {
    2351             :         HashTable *props;
    2352             :         zval zv;
    2353             :         php_interval_obj     *intervalobj;
    2354             : 
    2355          43 :         intervalobj = Z_PHPINTERVAL_P(object);
    2356             : 
    2357          43 :         props = zend_std_get_properties(object);
    2358             : 
    2359          43 :         if (!intervalobj->initialized) {
    2360          14 :                 return props;
    2361             :         }
    2362             : 
    2363             : #define PHP_DATE_INTERVAL_ADD_PROPERTY(n,f) \
    2364             :         ZVAL_LONG(&zv, (zend_long)intervalobj->diff->f); \
    2365             :         zend_hash_str_update(props, n, sizeof(n)-1, &zv);
    2366             : 
    2367          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("y", y);
    2368          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("m", m);
    2369          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("d", d);
    2370          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("h", h);
    2371          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("i", i);
    2372          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("s", s);
    2373          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("weekday", weekday);
    2374          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("weekday_behavior", weekday_behavior);
    2375          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("first_last_day_of", first_last_day_of);
    2376          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("invert", invert);
    2377          29 :         if (intervalobj->diff->days != -99999) {
    2378          20 :                 PHP_DATE_INTERVAL_ADD_PROPERTY("days", days);
    2379             :         } else {
    2380           9 :                 ZVAL_FALSE(&zv);
    2381           9 :                 zend_hash_str_update(props, "days", sizeof("days")-1, &zv);
    2382             :         }
    2383          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("special_type", special.type);
    2384          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("special_amount", special.amount);
    2385          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("have_weekday_relative", have_weekday_relative);
    2386          29 :         PHP_DATE_INTERVAL_ADD_PROPERTY("have_special_relative", have_special_relative);
    2387             : 
    2388          29 :         return props;
    2389             : } /* }}} */
    2390             : 
    2391          18 : static inline zend_object *date_object_new_period_ex(zend_class_entry *class_type, int init_props) /* {{{ */
    2392             : {
    2393             :         php_period_obj *intern;
    2394             : 
    2395          18 :         intern = ecalloc(1, sizeof(php_period_obj) + zend_object_properties_size(class_type));
    2396             : 
    2397          18 :         zend_object_std_init(&intern->std, class_type);
    2398          18 :         if (init_props) {
    2399          18 :                 object_properties_init(&intern->std, class_type);
    2400             :         }
    2401             : 
    2402          18 :         intern->std.handlers = &date_object_handlers_period;
    2403             : 
    2404          18 :         return &intern->std;
    2405             : } /* }}} */
    2406             : 
    2407          18 : static zend_object *date_object_new_period(zend_class_entry *class_type) /* {{{ */
    2408             : {
    2409          18 :         return date_object_new_period_ex(class_type, 1);
    2410             : } /* }}} */
    2411             : 
    2412           0 : static zend_object *date_object_clone_period(zval *this_ptr) /* {{{ */
    2413             : {
    2414           0 :         php_period_obj *old_obj = Z_PHPPERIOD_P(this_ptr);
    2415           0 :         php_period_obj *new_obj = php_period_obj_from_obj(date_object_new_period_ex(old_obj->std.ce, 0));
    2416             : 
    2417           0 :         zend_objects_clone_members(&new_obj->std, &old_obj->std);
    2418             : 
    2419             :         /** FIX ME ADD CLONE STUFF **/
    2420           0 :         return &new_obj->std;
    2421             : } /* }}} */
    2422             : 
    2423      179316 : static void date_object_free_storage_date(zend_object *object) /* {{{ */
    2424             : {
    2425      179316 :         php_date_obj *intern = php_date_obj_from_obj(object);
    2426             : 
    2427      179316 :         if (intern->time) {
    2428      179207 :                 timelib_time_dtor(intern->time);
    2429             :         }
    2430             : 
    2431      179316 :         zend_object_std_dtor(&intern->std);
    2432      179316 : } /* }}} */
    2433             : 
    2434         837 : static void date_object_free_storage_timezone(zend_object *object) /* {{{ */
    2435             : {
    2436         837 :         php_timezone_obj *intern = php_timezone_obj_from_obj(object);
    2437             : 
    2438         837 :         if (intern->type == TIMELIB_ZONETYPE_ABBR) {
    2439          22 :                 timelib_free(intern->tzi.z.abbr);
    2440             :         }
    2441         837 :         zend_object_std_dtor(&intern->std);
    2442         837 : } /* }}} */
    2443             : 
    2444      349108 : static void date_object_free_storage_interval(zend_object *object) /* {{{ */
    2445             : {
    2446      349108 :         php_interval_obj *intern = php_interval_obj_from_obj(object);
    2447             : 
    2448      349108 :         timelib_rel_time_dtor(intern->diff);
    2449      349108 :         zend_object_std_dtor(&intern->std);
    2450      349108 : } /* }}} */
    2451             : 
    2452          18 : static void date_object_free_storage_period(zend_object *object) /* {{{ */
    2453             : {
    2454          18 :         php_period_obj *intern = php_period_obj_from_obj(object);
    2455             : 
    2456          18 :         if (intern->start) {
    2457          14 :                 timelib_time_dtor(intern->start);
    2458             :         }
    2459             : 
    2460          18 :         if (intern->current) {
    2461           9 :                 timelib_time_dtor(intern->current);
    2462             :         }
    2463             : 
    2464          18 :         if (intern->end) {
    2465           5 :                 timelib_time_dtor(intern->end);
    2466             :         }
    2467             : 
    2468          18 :         timelib_rel_time_dtor(intern->interval);
    2469          18 :         zend_object_std_dtor(&intern->std);
    2470          18 : } /* }}} */
    2471             : 
    2472             : /* Advanced Interface */
    2473      174956 : PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object) /* {{{ */
    2474             : {
    2475      174956 :         object_init_ex(object, pce);
    2476      174956 :         return object;
    2477             : } /* }}} */
    2478             : 
    2479             : /* Helper function used to store the latest found warnings and errors while
    2480             :  * parsing, from either strtotime or parse_from_format. */
    2481        5221 : static void update_errors_warnings(timelib_error_container *last_errors) /* {{{ */
    2482             : {
    2483        5221 :         if (DATEG(last_errors)) {
    2484        4959 :                 timelib_error_container_dtor(DATEG(last_errors));
    2485        4959 :                 DATEG(last_errors) = NULL;
    2486             :         }
    2487        5221 :         DATEG(last_errors) = last_errors;
    2488        5221 : } /* }}} */
    2489             : 
    2490        4910 : PHPAPI int php_date_initialize(php_date_obj *dateobj, /*const*/ char *time_str, size_t time_str_len, char *format, zval *timezone_object, int ctor) /* {{{ */
    2491             : {
    2492             :         timelib_time   *now;
    2493        4910 :         timelib_tzinfo *tzi = NULL;
    2494        4910 :         timelib_error_container *err = NULL;
    2495        4910 :         int type = TIMELIB_ZONETYPE_ID, new_dst = 0;
    2496        4910 :         char *new_abbr = NULL;
    2497        4910 :         timelib_sll new_offset = 0;
    2498             : 
    2499        4910 :         if (dateobj->time) {
    2500           0 :                 timelib_time_dtor(dateobj->time);
    2501             :         }
    2502        4910 :         if (format) {
    2503        1065 :                 dateobj->time = timelib_parse_from_format(format, time_str_len ? time_str : "", time_str_len ? time_str_len : 0, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    2504             :         } else {
    2505        3845 :                 dateobj->time = timelib_strtotime(time_str_len ? time_str : "now", time_str_len ? time_str_len : sizeof("now") -1, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    2506             :         }
    2507             : 
    2508             :         /* update last errors and warnings */
    2509        4910 :         update_errors_warnings(err);
    2510             : 
    2511             : 
    2512        4910 :         if (ctor && err && err->error_count) {
    2513             :                 /* spit out the first library error message, at least */
    2514          84 :                 php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
    2515          84 :                         err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
    2516             :         }
    2517        4910 :         if (err && err->error_count) {
    2518          61 :                 timelib_time_dtor(dateobj->time);
    2519          61 :                 dateobj->time = 0;
    2520          61 :                 return 0;
    2521             :         }
    2522             : 
    2523        4849 :         if (timezone_object) {
    2524             :                 php_timezone_obj *tzobj;
    2525             : 
    2526          61 :                 tzobj = Z_PHPTIMEZONE_P(timezone_object);
    2527          61 :                 switch (tzobj->type) {
    2528             :                         case TIMELIB_ZONETYPE_ID:
    2529          58 :                                 tzi = tzobj->tzi.tz;
    2530          58 :                                 break;
    2531             :                         case TIMELIB_ZONETYPE_OFFSET:
    2532           0 :                                 new_offset = tzobj->tzi.utc_offset;
    2533           0 :                                 break;
    2534             :                         case TIMELIB_ZONETYPE_ABBR:
    2535           3 :                                 new_offset = tzobj->tzi.z.utc_offset;
    2536           3 :                                 new_dst    = tzobj->tzi.z.dst;
    2537           3 :                                 new_abbr   = timelib_strdup(tzobj->tzi.z.abbr);
    2538             :                                 break;
    2539             :                 }
    2540          61 :                 type = tzobj->type;
    2541        4788 :         } else if (dateobj->time->tz_info) {
    2542         546 :                 tzi = dateobj->time->tz_info;
    2543             :         } else {
    2544        4242 :                 tzi = get_timezone_info();
    2545             :         }
    2546             : 
    2547        4849 :         now = timelib_time_ctor();
    2548        4849 :         now->zone_type = type;
    2549        4849 :         switch (type) {
    2550             :                 case TIMELIB_ZONETYPE_ID:
    2551        4846 :                         now->tz_info = tzi;
    2552        4846 :                         break;
    2553             :                 case TIMELIB_ZONETYPE_OFFSET:
    2554           0 :                         now->z = new_offset;
    2555           0 :                         break;
    2556             :                 case TIMELIB_ZONETYPE_ABBR:
    2557           3 :                         now->z = new_offset;
    2558           3 :                         now->dst = new_dst;
    2559           3 :                         now->tz_abbr = new_abbr;
    2560             :                         break;
    2561             :         }
    2562        4849 :         timelib_unixtime2local(now, (timelib_sll) time(NULL));
    2563             : 
    2564        4849 :         timelib_fill_holes(dateobj->time, now, TIMELIB_NO_CLONE);
    2565        4849 :         timelib_update_ts(dateobj->time, tzi);
    2566        4849 :         timelib_update_from_sse(dateobj->time);
    2567             : 
    2568        4849 :         dateobj->time->have_relative = 0;
    2569             : 
    2570        4849 :         timelib_time_dtor(now);
    2571             : 
    2572        4849 :         return 1;
    2573             : } /* }}} */
    2574             : 
    2575             : /* {{{ proto DateTime date_create([string time[, DateTimeZone object]])
    2576             :    Returns new DateTime object
    2577             : */
    2578         247 : PHP_FUNCTION(date_create)
    2579             : {
    2580         247 :         zval           *timezone_object = NULL;
    2581         247 :         char           *time_str = NULL;
    2582         247 :         size_t          time_str_len = 0;
    2583             : 
    2584         247 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
    2585          38 :                 RETURN_FALSE;
    2586             :         }
    2587             : 
    2588         209 :         php_date_instantiate(date_ce_date, return_value);
    2589         209 :         if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
    2590          25 :                 zval_ptr_dtor(return_value);
    2591          25 :                 RETURN_FALSE;
    2592             :         }
    2593             : }
    2594             : /* }}} */
    2595             : 
    2596             : /* {{{ proto DateTime date_create_immutable([string time[, DateTimeZone object]])
    2597             :    Returns new DateTime object
    2598             : */
    2599          11 : PHP_FUNCTION(date_create_immutable)
    2600             : {
    2601          11 :         zval           *timezone_object = NULL;
    2602          11 :         char           *time_str = NULL;
    2603          11 :         size_t          time_str_len = 0;
    2604             : 
    2605          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
    2606           0 :                 RETURN_FALSE;
    2607             :         }
    2608             : 
    2609          11 :         php_date_instantiate(date_ce_immutable, return_value);
    2610          11 :         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             :         }
    2614             : }
    2615             : /* }}} */
    2616             : 
    2617             : /* {{{ proto DateTime date_create_from_format(string format, string time[, DateTimeZone object])
    2618             :    Returns new DateTime object formatted according to the specified format
    2619             : */
    2620        1064 : PHP_FUNCTION(date_create_from_format)
    2621             : {
    2622        1064 :         zval           *timezone_object = NULL;
    2623        1064 :         char           *time_str = NULL, *format_str = NULL;
    2624        1064 :         size_t             time_str_len = 0, format_str_len = 0;
    2625             : 
    2626        1064 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|O!", &format_str, &format_str_len, &time_str, &time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
    2627           1 :                 RETURN_FALSE;
    2628             :         }
    2629             : 
    2630        1063 :         php_date_instantiate(date_ce_date, return_value);
    2631        1063 :         if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
    2632           6 :                 zval_ptr_dtor(return_value);
    2633           6 :                 RETURN_FALSE;
    2634             :         }
    2635             : }
    2636             : /* }}} */
    2637             : 
    2638             : /* {{{ proto DateTime date_create_immutable_from_format(string format, string time[, DateTimeZone object])
    2639             :    Returns new DateTime object formatted according to the specified format
    2640             : */
    2641           2 : PHP_FUNCTION(date_create_immutable_from_format)
    2642             : {
    2643           2 :         zval           *timezone_object = NULL;
    2644           2 :         char           *time_str = NULL, *format_str = NULL;
    2645           2 :         size_t          time_str_len = 0, format_str_len = 0;
    2646             : 
    2647           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|O!", &format_str, &format_str_len, &time_str, &time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
    2648           0 :                 RETURN_FALSE;
    2649             :         }
    2650             : 
    2651           2 :         php_date_instantiate(date_ce_immutable, return_value);
    2652           2 :         if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
    2653           0 :                 zval_ptr_dtor(return_value);
    2654           0 :                 RETURN_FALSE;
    2655             :         }
    2656             : }
    2657             : /* }}} */
    2658             : 
    2659             : /* {{{ proto DateTime::__construct([string time[, DateTimeZone object]])
    2660             :    Creates new DateTime object
    2661             : */
    2662        3635 : PHP_METHOD(DateTime, __construct)
    2663             : {
    2664        3635 :         zval *timezone_object = NULL;
    2665        3635 :         char *time_str = NULL;
    2666        3635 :         size_t time_str_len = 0;
    2667             :         zend_error_handling error_handling;
    2668             : 
    2669        3635 :         if (FAILURE == zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone)) {
    2670          38 :                 return;
    2671             :         }
    2672             : 
    2673        3597 :         zend_replace_error_handling(EH_THROW, NULL, &error_handling);
    2674        7194 :         php_date_initialize(Z_PHPDATE_P(getThis()), time_str, time_str_len, NULL, timezone_object, 1);
    2675        3597 :         zend_restore_error_handling(&error_handling);
    2676             : }
    2677             : /* }}} */
    2678             : 
    2679             : /* {{{ proto DateTimeImmutable::__construct([string time[, DateTimeZone object]])
    2680             :    Creates new DateTimeImmutable object
    2681             : */
    2682          19 : PHP_METHOD(DateTimeImmutable, __construct)
    2683             : {
    2684          19 :         zval *timezone_object = NULL;
    2685          19 :         char *time_str = NULL;
    2686          19 :         size_t time_str_len = 0;
    2687             :         zend_error_handling error_handling;
    2688             : 
    2689          19 :         if (FAILURE == zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone)) {
    2690           0 :                 return;
    2691             :         }
    2692             : 
    2693          19 :         zend_replace_error_handling(EH_THROW, NULL, &error_handling);
    2694          38 :         php_date_initialize(Z_PHPDATE_P(getThis()), time_str, time_str_len, NULL, timezone_object, 1);
    2695          19 :         zend_restore_error_handling(&error_handling);
    2696             : }
    2697             : /* }}} */
    2698             : 
    2699             : /* {{{ proto DateTimeImmutable::createFromMutable(DateTimeZone object)
    2700             :    Creates new DateTimeImmutable object from an existing mutable DateTime object.
    2701             : */
    2702           2 : PHP_METHOD(DateTimeImmutable, createFromMutable)
    2703             : {
    2704           2 :         zval *datetime_object = NULL;
    2705           2 :         php_date_obj *new_obj = NULL;
    2706           2 :         php_date_obj *old_obj = NULL;
    2707             : 
    2708           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &datetime_object, date_ce_date) == FAILURE) {
    2709           1 :                 return;
    2710             :         }
    2711             : 
    2712           1 :         php_date_instantiate(date_ce_immutable, return_value);
    2713           1 :         old_obj = Z_PHPDATE_P(datetime_object);
    2714           1 :         new_obj = Z_PHPDATE_P(return_value);
    2715             : 
    2716           1 :         new_obj->time = timelib_time_ctor();
    2717           1 :         *new_obj->time = *old_obj->time;
    2718           1 :         if (old_obj->time->tz_abbr) {
    2719           1 :                 new_obj->time->tz_abbr = timelib_strdup(old_obj->time->tz_abbr);
    2720             :         }
    2721           1 :         if (old_obj->time->tz_info) {
    2722           1 :                 new_obj->time->tz_info = old_obj->time->tz_info;
    2723             :         }
    2724             : }
    2725             : /* }}} */
    2726             : 
    2727          13 : static int php_date_initialize_from_hash(php_date_obj **dateobj, HashTable *myht)
    2728             : {
    2729             :         zval             *z_date;
    2730             :         zval             *z_timezone;
    2731             :         zval             *z_timezone_type;
    2732             :         zval              tmp_obj;
    2733             :         timelib_tzinfo   *tzi;
    2734             :         php_timezone_obj *tzobj;
    2735             : 
    2736          13 :         z_date = zend_hash_str_find(myht, "date", sizeof("data")-1);
    2737          25 :         if (z_date && Z_TYPE_P(z_date) == IS_STRING) {
    2738          12 :                 z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type")-1);
    2739          24 :                 if (z_timezone_type && Z_TYPE_P(z_timezone_type) == IS_LONG) {
    2740          11 :                         z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone")-1);
    2741          22 :                         if (z_timezone && Z_TYPE_P(z_timezone) == IS_STRING) {
    2742          11 :                                 switch (Z_LVAL_P(z_timezone_type)) {
    2743             :                                         case TIMELIB_ZONETYPE_OFFSET:
    2744             :                                         case TIMELIB_ZONETYPE_ABBR: {
    2745           0 :                                                 char *tmp = emalloc(Z_STRLEN_P(z_date) + Z_STRLEN_P(z_timezone) + 2);
    2746             :                                                 int ret;
    2747           0 :                                                 snprintf(tmp, Z_STRLEN_P(z_date) + Z_STRLEN_P(z_timezone) + 2, "%s %s", Z_STRVAL_P(z_date), Z_STRVAL_P(z_timezone));
    2748           0 :                                                 ret = php_date_initialize(*dateobj, tmp, Z_STRLEN_P(z_date) + Z_STRLEN_P(z_timezone) + 1, NULL, NULL, 0);
    2749           0 :                                                 efree(tmp);
    2750           0 :                                                 return 1 == ret;
    2751             :                                         }
    2752             : 
    2753             :                                         case TIMELIB_ZONETYPE_ID: {
    2754             :                                                 int ret;
    2755             : 
    2756          10 :                                                 tzi = php_date_parse_tzfile(Z_STRVAL_P(z_timezone), DATE_TIMEZONEDB);
    2757             : 
    2758          10 :                                                 if (tzi == NULL) {
    2759           1 :                                                         return 0;
    2760             :                                                 }
    2761             : 
    2762           9 :                                                 tzobj = Z_PHPTIMEZONE_P(php_date_instantiate(date_ce_timezone, &tmp_obj));
    2763           9 :                                                 tzobj->type = TIMELIB_ZONETYPE_ID;
    2764           9 :                                                 tzobj->tzi.tz = tzi;
    2765           9 :                                                 tzobj->initialized = 1;
    2766             : 
    2767           9 :                                                 ret = php_date_initialize(*dateobj, Z_STRVAL_P(z_date), Z_STRLEN_P(z_date), NULL, &tmp_obj, 0);
    2768           9 :                                                 zval_ptr_dtor(&tmp_obj);
    2769           9 :                                                 return 1 == ret;
    2770             :                                         }
    2771             :                                 }
    2772             :                         }
    2773             :                 }
    2774             :         }
    2775           3 :         return 0;
    2776             : } /* }}} */
    2777             : 
    2778             : /* {{{ proto DateTime::__set_state()
    2779             : */
    2780           0 : PHP_METHOD(DateTime, __set_state)
    2781             : {
    2782             :         php_date_obj     *dateobj;
    2783             :         zval             *array;
    2784             :         HashTable        *myht;
    2785             : 
    2786           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
    2787           0 :                 RETURN_FALSE;
    2788             :         }
    2789             : 
    2790           0 :         myht = Z_ARRVAL_P(array);
    2791             : 
    2792           0 :         php_date_instantiate(date_ce_date, return_value);
    2793           0 :         dateobj = Z_PHPDATE_P(return_value);
    2794           0 :         if (!php_date_initialize_from_hash(&dateobj, myht)) {
    2795           0 :                 zend_throw_error(NULL, "Invalid serialization data for DateTime object");
    2796             :         }
    2797             : }
    2798             : /* }}} */
    2799             : 
    2800             : /* {{{ proto DateTimeImmutable::__set_state()
    2801             : */
    2802           0 : PHP_METHOD(DateTimeImmutable, __set_state)
    2803             : {
    2804             :         php_date_obj     *dateobj;
    2805             :         zval             *array;
    2806             :         HashTable        *myht;
    2807             : 
    2808           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
    2809           0 :                 RETURN_FALSE;
    2810             :         }
    2811             : 
    2812           0 :         myht = Z_ARRVAL_P(array);
    2813             : 
    2814           0 :         php_date_instantiate(date_ce_immutable, return_value);
    2815           0 :         dateobj = Z_PHPDATE_P(return_value);
    2816           0 :         if (!php_date_initialize_from_hash(&dateobj, myht)) {
    2817           0 :                 zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
    2818             :         }
    2819             : }
    2820             : /* }}} */
    2821             : 
    2822             : /* {{{ proto DateTime::__wakeup()
    2823             : */
    2824          13 : PHP_METHOD(DateTime, __wakeup)
    2825             : {
    2826          26 :         zval             *object = getThis();
    2827             :         php_date_obj     *dateobj;
    2828             :         HashTable        *myht;
    2829             : 
    2830          13 :         dateobj = Z_PHPDATE_P(object);
    2831             : 
    2832          13 :         myht = Z_OBJPROP_P(object);
    2833             : 
    2834          13 :         if (!php_date_initialize_from_hash(&dateobj, myht)) {
    2835           6 :                 zend_throw_error(NULL, "Invalid serialization data for DateTime object");
    2836             :         }
    2837          13 : }
    2838             : /* }}} */
    2839             : 
    2840             : /* Helper function used to add an associative array of warnings and errors to a zval */
    2841          88 : static void zval_from_error_container(zval *z, timelib_error_container *error) /* {{{ */
    2842             : {
    2843             :         int   i;
    2844             :         zval element;
    2845             : 
    2846          88 :         add_assoc_long(z, "warning_count", error->warning_count);
    2847          88 :         array_init(&element);
    2848         108 :         for (i = 0; i < error->warning_count; i++) {
    2849          20 :                 add_index_string(&element, error->warning_messages[i].position, error->warning_messages[i].message);
    2850             :         }
    2851          88 :         add_assoc_zval(z, "warnings", &element);
    2852             : 
    2853          88 :         add_assoc_long(z, "error_count", error->error_count);
    2854          88 :         array_init(&element);
    2855         144 :         for (i = 0; i < error->error_count; i++) {
    2856          56 :                 add_index_string(&element, error->error_messages[i].position, error->error_messages[i].message);
    2857             :         }
    2858          88 :         add_assoc_zval(z, "errors", &element);
    2859          88 : } /* }}} */
    2860             : 
    2861             : /* {{{ proto array date_get_last_errors()
    2862             :    Returns the warnings and errors found while parsing a date/time string.
    2863             : */
    2864          16 : PHP_FUNCTION(date_get_last_errors)
    2865             : {
    2866          16 :         if (DATEG(last_errors)) {
    2867          15 :                 array_init(return_value);
    2868          15 :                 zval_from_error_container(return_value, DATEG(last_errors));
    2869             :         } else {
    2870           1 :                 RETURN_FALSE;
    2871             :         }
    2872             : }
    2873             : /* }}} */
    2874             : 
    2875          73 : void php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAMETERS, timelib_time *parsed_time, struct timelib_error_container *error) /* {{{ */
    2876             : {
    2877             :         zval element;
    2878             : 
    2879          73 :         array_init(return_value);
    2880             : #define PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(name, elem) \
    2881             :         if (parsed_time->elem == -99999) {               \
    2882             :                 add_assoc_bool(return_value, #name, 0); \
    2883             :         } else {                                       \
    2884             :                 add_assoc_long(return_value, #name, parsed_time->elem); \
    2885             :         }
    2886          73 :         PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(year,      y);
    2887          73 :         PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(month,     m);
    2888          73 :         PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(day,       d);
    2889          73 :         PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(hour,      h);
    2890          73 :         PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(minute,    i);
    2891          73 :         PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(second,    s);
    2892             : 
    2893          73 :         if (parsed_time->f == -99999) {
    2894          55 :                 add_assoc_bool(return_value, "fraction", 0);
    2895             :         } else {
    2896          18 :                 add_assoc_double(return_value, "fraction", parsed_time->f);
    2897             :         }
    2898             : 
    2899          73 :         zval_from_error_container(return_value, error);
    2900             : 
    2901          73 :         timelib_error_container_dtor(error);
    2902             : 
    2903          73 :         add_assoc_bool(return_value, "is_localtime", parsed_time->is_localtime);
    2904             : 
    2905          73 :         if (parsed_time->is_localtime) {
    2906          19 :                 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone_type, zone_type);
    2907          19 :                 switch (parsed_time->zone_type) {
    2908             :                         case TIMELIB_ZONETYPE_OFFSET:
    2909           5 :                                 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone, z);
    2910           5 :                                 add_assoc_bool(return_value, "is_dst", parsed_time->dst);
    2911           5 :                                 break;
    2912             :                         case TIMELIB_ZONETYPE_ID:
    2913           0 :                                 if (parsed_time->tz_abbr) {
    2914           0 :                                         add_assoc_string(return_value, "tz_abbr", parsed_time->tz_abbr);
    2915             :                                 }
    2916           0 :                                 if (parsed_time->tz_info) {
    2917           0 :                                         add_assoc_string(return_value, "tz_id", parsed_time->tz_info->name);
    2918             :                                 }
    2919           0 :                                 break;
    2920             :                         case TIMELIB_ZONETYPE_ABBR:
    2921           7 :                                 PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone, z);
    2922           7 :                                 add_assoc_bool(return_value, "is_dst", parsed_time->dst);
    2923           7 :                                 add_assoc_string(return_value, "tz_abbr", parsed_time->tz_abbr);
    2924             :                                 break;
    2925             :                 }
    2926             :         }
    2927          73 :         if (parsed_time->have_relative) {
    2928           7 :                 array_init(&element);
    2929           7 :                 add_assoc_long(&element, "year",   parsed_time->relative.y);
    2930           7 :                 add_assoc_long(&element, "month",  parsed_time->relative.m);
    2931           7 :                 add_assoc_long(&element, "day",    parsed_time->relative.d);
    2932           7 :                 add_assoc_long(&element, "hour",   parsed_time->relative.h);
    2933           7 :                 add_assoc_long(&element, "minute", parsed_time->relative.i);
    2934           7 :                 add_assoc_long(&element, "second", parsed_time->relative.s);
    2935           7 :                 if (parsed_time->relative.have_weekday_relative) {
    2936           0 :                         add_assoc_long(&element, "weekday", parsed_time->relative.weekday);
    2937             :                 }
    2938           7 :                 if (parsed_time->relative.have_special_relative && (parsed_time->relative.special.type == TIMELIB_SPECIAL_WEEKDAY)) {
    2939           0 :                         add_assoc_long(&element, "weekdays", parsed_time->relative.special.amount);
    2940             :                 }
    2941           7 :                 if (parsed_time->relative.first_last_day_of) {
    2942           2 :                         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);
    2943             :                 }
    2944           7 :                 add_assoc_zval(return_value, "relative", &element);
    2945             :         }
    2946          73 :         timelib_time_dtor(parsed_time);
    2947          73 : } /* }}} */
    2948             : 
    2949             : /* {{{ proto array date_parse(string date)
    2950             :    Returns associative array with detailed info about given date
    2951             : */
    2952          63 : PHP_FUNCTION(date_parse)
    2953             : {
    2954             :         char                           *date;
    2955             :         size_t                          date_len;
    2956             :         struct timelib_error_container *error;
    2957             :         timelib_time                   *parsed_time;
    2958             : 
    2959          63 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &date, &date_len) == FAILURE) {
    2960           9 :                 RETURN_FALSE;
    2961             :         }
    2962             : 
    2963          54 :         parsed_time = timelib_strtotime(date, date_len, &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    2964          54 :         php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAM_PASSTHRU, parsed_time, error);
    2965             : }
    2966             : /* }}} */
    2967             : 
    2968             : /* {{{ proto array date_parse_from_format(string format, string date)
    2969             :    Returns associative array with detailed info about given date
    2970             : */
    2971          20 : PHP_FUNCTION(date_parse_from_format)
    2972             : {
    2973             :         char                           *date, *format;
    2974             :         size_t                          date_len, format_len;
    2975             :         struct timelib_error_container *error;
    2976             :         timelib_time                   *parsed_time;
    2977             : 
    2978          20 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &format, &format_len, &date, &date_len) == FAILURE) {
    2979           1 :                 RETURN_FALSE;
    2980             :         }
    2981             : 
    2982          19 :         parsed_time = timelib_parse_from_format(format, date, date_len, &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    2983          19 :         php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAM_PASSTHRU, parsed_time, error);
    2984             : }
    2985             : /* }}} */
    2986             : 
    2987             : /* {{{ proto string date_format(DateTimeInterface object, string format)
    2988             :    Returns date formatted according to given format
    2989             : */
    2990        5159 : PHP_FUNCTION(date_format)
    2991             : {
    2992             :         zval         *object;
    2993             :         php_date_obj *dateobj;
    2994             :         char         *format;
    2995             :         size_t       format_len;
    2996             : 
    2997       10318 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_interface, &format, &format_len) == FAILURE) {
    2998          48 :                 RETURN_FALSE;
    2999             :         }
    3000        5111 :         dateobj = Z_PHPDATE_P(object);
    3001        5111 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3002        5107 :         RETURN_STR(date_format(format, format_len, dateobj->time, dateobj->time->is_localtime));
    3003             : }
    3004             : /* }}} */
    3005             : 
    3006         311 : static int php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{ */
    3007             : {
    3008             :         php_date_obj *dateobj;
    3009             :         timelib_time *tmp_time;
    3010         311 :         timelib_error_container *err = NULL;
    3011             : 
    3012         311 :         dateobj = Z_PHPDATE_P(object);
    3013             : 
    3014         311 :         if (!(dateobj->time)) {
    3015           0 :                 php_error_docref(NULL, E_WARNING, "The DateTime object has not been correctly initialized by its constructor");
    3016           0 :                 return 0;
    3017             :         }
    3018             : 
    3019         311 :         tmp_time = timelib_strtotime(modify, modify_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    3020             : 
    3021             :         /* update last errors and warnings */
    3022         311 :         update_errors_warnings(err);
    3023         311 :         if (err && err->error_count) {
    3024             :                 /* spit out the first library error message, at least */
    3025         123 :                 php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
    3026         123 :                         err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
    3027          41 :                 timelib_time_dtor(tmp_time);
    3028          41 :                 return 0;
    3029             :         }
    3030             : 
    3031         270 :         memcpy(&dateobj->time->relative, &tmp_time->relative, sizeof(struct timelib_rel_time));
    3032         270 :         dateobj->time->have_relative = tmp_time->have_relative;
    3033         270 :         dateobj->time->sse_uptodate = 0;
    3034             : 
    3035         270 :         if (tmp_time->y != -99999) {
    3036           2 :                 dateobj->time->y = tmp_time->y;
    3037             :         }
    3038         270 :         if (tmp_time->m != -99999) {
    3039           7 :                 dateobj->time->m = tmp_time->m;
    3040             :         }
    3041         270 :         if (tmp_time->d != -99999) {
    3042           7 :                 dateobj->time->d = tmp_time->d;
    3043             :         }
    3044             : 
    3045         270 :         if (tmp_time->h != -99999) {
    3046         222 :                 dateobj->time->h = tmp_time->h;
    3047         222 :                 if (tmp_time->i != -99999) {
    3048         220 :                         dateobj->time->i = tmp_time->i;
    3049         220 :                         if (tmp_time->s != -99999) {
    3050         220 :                                 dateobj->time->s = tmp_time->s;
    3051             :                         } else {
    3052           0 :                                 dateobj->time->s = 0;
    3053             :                         }
    3054             :                 } else {
    3055           2 :                         dateobj->time->i = 0;
    3056           2 :                         dateobj->time->s = 0;
    3057             :                 }
    3058             :         }
    3059         270 :         timelib_time_dtor(tmp_time);
    3060             : 
    3061         270 :         timelib_update_ts(dateobj->time, NULL);
    3062         270 :         timelib_update_from_sse(dateobj->time);
    3063         270 :         dateobj->time->have_relative = 0;
    3064         270 :         memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
    3065             : 
    3066         270 :         return 1;
    3067             : } /* }}} */
    3068             : 
    3069             : /* {{{ proto DateTime date_modify(DateTime object, string modify)
    3070             :    Alters the timestamp.
    3071             : */
    3072         344 : PHP_FUNCTION(date_modify)
    3073             : {
    3074             :         zval         *object;
    3075             :         char         *modify;
    3076             :         size_t        modify_len;
    3077             : 
    3078         688 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_date, &modify, &modify_len) == FAILURE) {
    3079          48 :                 RETURN_FALSE;
    3080             :         }
    3081             : 
    3082         296 :         if (!php_date_modify(object, modify, modify_len)) {
    3083          41 :                 RETURN_FALSE;
    3084             :         }
    3085             : 
    3086         255 :         Z_ADDREF_P(object);
    3087         255 :         ZVAL_COPY_VALUE(return_value, object);
    3088             : }
    3089             : /* }}} */
    3090             : 
    3091             : /* {{{ proto DateTimeImmutable::modify()
    3092             : */
    3093          15 : PHP_METHOD(DateTimeImmutable, modify)
    3094             : {
    3095             :         zval *object, new_object;
    3096             :         char *modify;
    3097             :         size_t   modify_len;
    3098             : 
    3099          30 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_immutable, &modify, &modify_len) == FAILURE) {
    3100           0 :                 RETURN_FALSE;
    3101             :         }
    3102             : 
    3103          15 :         date_clone_immutable(object, &new_object);
    3104          15 :         if (!php_date_modify(&new_object, modify, modify_len)) {
    3105           0 :                 RETURN_FALSE;
    3106             :         }
    3107             : 
    3108          15 :         ZVAL_OBJ(return_value, Z_OBJ(new_object));
    3109             : }
    3110             : /* }}} */
    3111             : 
    3112      167470 : static void php_date_add(zval *object, zval *interval, zval *return_value) /* {{{ */
    3113             : {
    3114             :         php_date_obj     *dateobj;
    3115             :         php_interval_obj *intobj;
    3116             :         timelib_time     *new_time;
    3117             : 
    3118      167470 :         dateobj = Z_PHPDATE_P(object);
    3119      167470 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3120      167470 :         intobj = Z_PHPINTERVAL_P(interval);
    3121      167470 :         DATE_CHECK_INITIALIZED(intobj->initialized, DateInterval);
    3122             : 
    3123      167470 :         new_time = timelib_add(dateobj->time, intobj->diff);
    3124      167470 :         timelib_time_dtor(dateobj->time);
    3125      167470 :         dateobj->time = new_time;
    3126             : } /* }}} */
    3127             : 
    3128             : /* {{{ proto DateTime date_add(DateTime object, DateInterval interval)
    3129             :    Adds an interval to the current date in object.
    3130             : */
    3131      167470 : PHP_FUNCTION(date_add)
    3132             : {
    3133             :         zval *object, *interval;
    3134             : 
    3135      334940 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
    3136           1 :                 RETURN_FALSE;
    3137             :         }
    3138             : 
    3139      167469 :         php_date_add(object, interval, return_value);
    3140             : 
    3141      167469 :         Z_ADDREF_P(object);
    3142      167469 :         ZVAL_COPY_VALUE(return_value, object);
    3143             : }
    3144             : /* }}} */
    3145             : 
    3146             : /* {{{ proto DateTimeImmutable::add()
    3147             : */
    3148           1 : PHP_METHOD(DateTimeImmutable, add)
    3149             : {
    3150             :         zval *object, *interval, new_object;
    3151             : 
    3152           2 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_immutable, &interval, date_ce_interval) == FAILURE) {
    3153           0 :                 RETURN_FALSE;
    3154             :         }
    3155             : 
    3156           1 :         date_clone_immutable(object, &new_object);
    3157           1 :         php_date_add(&new_object, interval, return_value);
    3158             : 
    3159           1 :         ZVAL_OBJ(return_value, Z_OBJ(new_object));
    3160             : }
    3161             : /* }}} */
    3162             : 
    3163        7509 : static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{{ */
    3164             : {
    3165             :         php_date_obj     *dateobj;
    3166             :         php_interval_obj *intobj;
    3167             :         timelib_time     *new_time;
    3168             : 
    3169        7509 :         dateobj = Z_PHPDATE_P(object);
    3170        7509 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3171        7509 :         intobj = Z_PHPINTERVAL_P(interval);
    3172        7509 :         DATE_CHECK_INITIALIZED(intobj->initialized, DateInterval);
    3173             : 
    3174        7509 :         if (intobj->diff->have_special_relative) {
    3175           2 :                 php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
    3176           2 :                 return;
    3177             :         }
    3178             : 
    3179        7507 :         new_time = timelib_sub(dateobj->time, intobj->diff);
    3180        7507 :         timelib_time_dtor(dateobj->time);
    3181        7507 :         dateobj->time = new_time;
    3182             : } /* }}} */
    3183             : 
    3184             : /* {{{ proto DateTime date_sub(DateTime object, DateInterval interval)
    3185             :    Subtracts an interval to the current date in object.
    3186             : */
    3187        7508 : PHP_FUNCTION(date_sub)
    3188             : {
    3189             :         zval *object, *interval;
    3190             : 
    3191       15016 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
    3192           0 :                 RETURN_FALSE;
    3193             :         }
    3194             : 
    3195        7508 :         php_date_sub(object, interval, return_value);
    3196             : 
    3197        7508 :         Z_ADDREF_P(object);
    3198        7508 :         ZVAL_COPY_VALUE(return_value, object);
    3199             : }
    3200             : /* }}} */
    3201             : 
    3202             : /* {{{ proto DateTimeImmutable::sub()
    3203             : */
    3204           1 : PHP_METHOD(DateTimeImmutable, sub)
    3205             : {
    3206             :         zval *object, *interval, new_object;
    3207             : 
    3208           2 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_immutable, &interval, date_ce_interval) == FAILURE) {
    3209           0 :                 RETURN_FALSE;
    3210             :         }
    3211             : 
    3212           1 :         date_clone_immutable(object, &new_object);
    3213           1 :         php_date_sub(&new_object, interval, return_value);
    3214             : 
    3215           1 :         ZVAL_OBJ(return_value, Z_OBJ(new_object));
    3216             : }
    3217             : /* }}} */
    3218             : 
    3219         764 : static void set_timezone_from_timelib_time(php_timezone_obj *tzobj, timelib_time *t)
    3220             : {
    3221         764 :        tzobj->initialized = 1;
    3222         764 :        tzobj->type = t->zone_type;
    3223         764 :        switch (t->zone_type) {
    3224             :                case TIMELIB_ZONETYPE_ID:
    3225         725 :                        tzobj->tzi.tz = t->tz_info;
    3226         725 :                        break;
    3227             :                case TIMELIB_ZONETYPE_OFFSET:
    3228          18 :                        tzobj->tzi.utc_offset = t->z;
    3229          18 :                        break;
    3230             :                case TIMELIB_ZONETYPE_ABBR:
    3231          21 :                        tzobj->tzi.z.utc_offset = t->z;
    3232          21 :                        tzobj->tzi.z.dst = t->dst;
    3233          21 :                        tzobj->tzi.z.abbr = timelib_strdup(t->tz_abbr);
    3234             :                        break;
    3235             :        }
    3236         764 : }
    3237             : 
    3238             : 
    3239             : /* {{{ proto DateTimeZone date_timezone_get(DateTimeInterface object)
    3240             :    Return new DateTimeZone object relative to give DateTime
    3241             : */
    3242          82 : PHP_FUNCTION(date_timezone_get)
    3243             : {
    3244             :         zval             *object;
    3245             :         php_date_obj     *dateobj;
    3246             :         php_timezone_obj *tzobj;
    3247             : 
    3248         164 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
    3249          33 :                 RETURN_FALSE;
    3250             :         }
    3251          49 :         dateobj = Z_PHPDATE_P(object);
    3252          49 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3253          49 :         if (dateobj->time->is_localtime/* && dateobj->time->tz_info*/) {
    3254          49 :                 php_date_instantiate(date_ce_timezone, return_value);
    3255          49 :                 tzobj = Z_PHPTIMEZONE_P(return_value);
    3256          49 :                 set_timezone_from_timelib_time(tzobj, dateobj->time);
    3257             :         } else {
    3258           0 :                 RETURN_FALSE;
    3259             :         }
    3260             : }
    3261             : /* }}} */
    3262             : 
    3263         262 : static void php_date_timezone_set(zval *object, zval *timezone_object, zval *return_value) /* {{{ */
    3264             : {
    3265             :         php_date_obj     *dateobj;
    3266             :         php_timezone_obj *tzobj;
    3267             : 
    3268         262 :         dateobj = Z_PHPDATE_P(object);
    3269         262 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3270         262 :         tzobj = Z_PHPTIMEZONE_P(timezone_object);
    3271             : 
    3272         262 :         switch (tzobj->type) {
    3273             :                 case TIMELIB_ZONETYPE_OFFSET:
    3274           1 :                         timelib_set_timezone_from_offset(dateobj->time, tzobj->tzi.utc_offset);
    3275           1 :                         break;
    3276             :                 case TIMELIB_ZONETYPE_ABBR:
    3277           3 :                         timelib_set_timezone_from_abbr(dateobj->time, tzobj->tzi.z);
    3278           3 :                         break;
    3279             :                 case TIMELIB_ZONETYPE_ID:
    3280         258 :                         timelib_set_timezone(dateobj->time, tzobj->tzi.tz);
    3281             :                         break;
    3282             :         }
    3283         262 :         timelib_unixtime2local(dateobj->time, dateobj->time->sse);
    3284             : } /* }}} */
    3285             : 
    3286             : /* {{{ proto DateTime date_timezone_set(DateTime object, DateTimeZone object)
    3287             :    Sets the timezone for the DateTime object.
    3288             : */
    3289         352 : PHP_FUNCTION(date_timezone_set)
    3290             : {
    3291             :         zval *object;
    3292             :         zval *timezone_object;
    3293             : 
    3294         704 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &timezone_object, date_ce_timezone) == FAILURE) {
    3295          92 :                 RETURN_FALSE;
    3296             :         }
    3297             : 
    3298         260 :         php_date_timezone_set(object, timezone_object, return_value);
    3299             : 
    3300         260 :         Z_ADDREF_P(object);
    3301         260 :         ZVAL_COPY_VALUE(return_value, object);
    3302             : }
    3303             : /* }}} */
    3304             : 
    3305             : /* {{{ proto DateTimeImmutable::setTimezone()
    3306             : */
    3307           2 : PHP_METHOD(DateTimeImmutable, setTimezone)
    3308             : {
    3309             :         zval *object, new_object;
    3310             :         zval *timezone_object;
    3311             : 
    3312           4 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_immutable, &timezone_object, date_ce_timezone) == FAILURE) {
    3313           0 :                 RETURN_FALSE;
    3314             :         }
    3315             : 
    3316           2 :         date_clone_immutable(object, &new_object);
    3317           2 :         php_date_timezone_set(&new_object, timezone_object, return_value);
    3318             : 
    3319           2 :         ZVAL_OBJ(return_value, Z_OBJ(new_object));
    3320             : }
    3321             : /* }}} */
    3322             : 
    3323             : /* {{{ proto long date_offset_get(DateTimeInterface object)
    3324             :    Returns the DST offset.
    3325             : */
    3326          60 : PHP_FUNCTION(date_offset_get)
    3327             : {
    3328             :         zval                *object;
    3329             :         php_date_obj        *dateobj;
    3330             :         timelib_time_offset *offset;
    3331             : 
    3332         120 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
    3333          34 :                 RETURN_FALSE;
    3334             :         }
    3335          26 :         dateobj = Z_PHPDATE_P(object);
    3336          26 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3337          26 :         if (dateobj->time->is_localtime/* && dateobj->time->tz_info*/) {
    3338          26 :                 switch (dateobj->time->zone_type) {
    3339             :                         case TIMELIB_ZONETYPE_ID:
    3340           6 :                                 offset = timelib_get_time_zone_info(dateobj->time->sse, dateobj->time->tz_info);
    3341           6 :                                 RETVAL_LONG(offset->offset);
    3342           6 :                                 timelib_time_offset_dtor(offset);
    3343           6 :                                 break;
    3344             :                         case TIMELIB_ZONETYPE_OFFSET:
    3345          18 :                                 RETVAL_LONG(dateobj->time->z * -60);
    3346          18 :                                 break;
    3347             :                         case TIMELIB_ZONETYPE_ABBR:
    3348           2 :                                 RETVAL_LONG((dateobj->time->z - (60 * dateobj->time->dst)) * -60);
    3349             :                                 break;
    3350             :                 }
    3351          26 :                 return;
    3352             :         } else {
    3353           0 :                 RETURN_LONG(0);
    3354             :         }
    3355             : }
    3356             : /* }}} */
    3357             : 
    3358         133 : static void php_date_time_set(zval *object, zend_long h, zend_long i, zend_long s, zval *return_value) /* {{{ */
    3359             : {
    3360             :         php_date_obj *dateobj;
    3361             : 
    3362         133 :         dateobj = Z_PHPDATE_P(object);
    3363         133 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3364         133 :         dateobj->time->h = h;
    3365         133 :         dateobj->time->i = i;
    3366         133 :         dateobj->time->s = s;
    3367         133 :         timelib_update_ts(dateobj->time, NULL);
    3368             : } /* }}} */
    3369             : 
    3370             : /* {{{ proto DateTime date_time_set(DateTime object, long hour, long minute[, long second])
    3371             :    Sets the time.
    3372             : */
    3373         248 : PHP_FUNCTION(date_time_set)
    3374             : {
    3375             :         zval *object;
    3376         248 :         zend_long  h, i, s = 0;
    3377             : 
    3378         496 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|l", &object, date_ce_date, &h, &i, &s) == FAILURE) {
    3379         116 :                 RETURN_FALSE;
    3380             :         }
    3381             : 
    3382         132 :         php_date_time_set(object, h, i, s, return_value);
    3383             : 
    3384         132 :         Z_ADDREF_P(object);
    3385         132 :         ZVAL_COPY_VALUE(return_value, object);
    3386             : }
    3387             : /* }}} */
    3388             : 
    3389             : /* {{{ proto DateTimeImmutable::setTime()
    3390             : */
    3391           1 : PHP_METHOD(DateTimeImmutable, setTime)
    3392             : {
    3393             :         zval *object, new_object;
    3394           1 :         zend_long  h, i, s = 0;
    3395             : 
    3396           2 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|l", &object, date_ce_immutable, &h, &i, &s) == FAILURE) {
    3397           0 :                 RETURN_FALSE;
    3398             :         }
    3399             : 
    3400           1 :         date_clone_immutable(object, &new_object);
    3401           1 :         php_date_time_set(&new_object, h, i, s, return_value);
    3402             : 
    3403           1 :         ZVAL_OBJ(return_value, Z_OBJ(new_object));
    3404             : }
    3405             : /* }}} */
    3406             : 
    3407         118 : static void php_date_date_set(zval *object, zend_long y, zend_long m, zend_long d, zval *return_value) /* {{{ */
    3408             : {
    3409             :         php_date_obj *dateobj;
    3410             : 
    3411         118 :         dateobj = Z_PHPDATE_P(object);
    3412         118 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3413         118 :         dateobj->time->y = y;
    3414         118 :         dateobj->time->m = m;
    3415         118 :         dateobj->time->d = d;
    3416         118 :         timelib_update_ts(dateobj->time, NULL);
    3417             : } /* }}} */
    3418             : 
    3419             : /* {{{ proto DateTime date_date_set(DateTime object, long year, long month, long day)
    3420             :    Sets the date.
    3421             : */
    3422         234 : PHP_FUNCTION(date_date_set)
    3423             : {
    3424             :         zval *object;
    3425             :         zend_long  y, m, d;
    3426             : 
    3427         468 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olll", &object, date_ce_date, &y, &m, &d) == FAILURE) {
    3428         117 :                 RETURN_FALSE;
    3429             :         }
    3430             : 
    3431         117 :         php_date_date_set(object, y, m, d, return_value);
    3432             : 
    3433         117 :         Z_ADDREF_P(object);
    3434         117 :         ZVAL_COPY_VALUE(return_value, object);
    3435             : }
    3436             : /* }}} */
    3437             : 
    3438             : /* {{{ proto DateTimeImmutable::setDate()
    3439             : */
    3440           1 : PHP_METHOD(DateTimeImmutable, setDate)
    3441             : {
    3442             :         zval *object, new_object;
    3443             :         zend_long  y, m, d;
    3444             : 
    3445           2 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olll", &object, date_ce_immutable, &y, &m, &d) == FAILURE) {
    3446           0 :                 RETURN_FALSE;
    3447             :         }
    3448             : 
    3449           1 :         date_clone_immutable(object, &new_object);
    3450           1 :         php_date_date_set(&new_object, y, m, d, return_value);
    3451             : 
    3452           1 :         ZVAL_OBJ(return_value, Z_OBJ(new_object));
    3453             : }
    3454             : /* }}} */
    3455             : 
    3456         104 : static void php_date_isodate_set(zval *object, zend_long y, zend_long w, zend_long d, zval *return_value) /* {{{ */
    3457             : {
    3458             :         php_date_obj *dateobj;
    3459             : 
    3460         104 :         dateobj = Z_PHPDATE_P(object);
    3461         104 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3462         104 :         dateobj->time->y = y;
    3463         104 :         dateobj->time->m = 1;
    3464         104 :         dateobj->time->d = 1;
    3465         104 :         memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
    3466         104 :         dateobj->time->relative.d = timelib_daynr_from_weeknr(y, w, d);
    3467         104 :         dateobj->time->have_relative = 1;
    3468             : 
    3469         104 :         timelib_update_ts(dateobj->time, NULL);
    3470             : } /* }}} */
    3471             : 
    3472             : /* {{{ proto DateTime date_isodate_set(DateTime object, long year, long week[, long day])
    3473             :    Sets the ISO date.
    3474             : */
    3475         220 : PHP_FUNCTION(date_isodate_set)
    3476             : {
    3477             :         zval *object;
    3478         220 :         zend_long  y, w, d = 1;
    3479             : 
    3480         440 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|l", &object, date_ce_date, &y, &w, &d) == FAILURE) {
    3481         117 :                 RETURN_FALSE;
    3482             :         }
    3483             : 
    3484         103 :         php_date_isodate_set(object, y, w, d, return_value);
    3485             : 
    3486         103 :         Z_ADDREF_P(object);
    3487         103 :         ZVAL_COPY_VALUE(return_value, object);
    3488             : }
    3489             : /* }}} */
    3490             : 
    3491             : /* {{{ proto DateTimeImmutable::setISODate()
    3492             : */
    3493           1 : PHP_METHOD(DateTimeImmutable, setISODate)
    3494             : {
    3495             :         zval *object, new_object;
    3496           1 :         zend_long  y, w, d = 1;
    3497             : 
    3498           2 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|l", &object, date_ce_immutable, &y, &w, &d) == FAILURE) {
    3499           0 :                 RETURN_FALSE;
    3500             :         }
    3501             : 
    3502           1 :         date_clone_immutable(object, &new_object);
    3503           1 :         php_date_isodate_set(&new_object, y, w, d, return_value);
    3504             : 
    3505           1 :         ZVAL_OBJ(return_value, Z_OBJ(new_object));
    3506             : }
    3507             : /* }}} */
    3508             : 
    3509           7 : static void php_date_timestamp_set(zval *object, zend_long timestamp, zval *return_value) /* {{{ */
    3510             : {
    3511             :         php_date_obj *dateobj;
    3512             : 
    3513           7 :         dateobj = Z_PHPDATE_P(object);
    3514           7 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3515           7 :         timelib_unixtime2local(dateobj->time, (timelib_sll)timestamp);
    3516           7 :         timelib_update_ts(dateobj->time, NULL);
    3517             : } /* }}} */
    3518             : 
    3519             : /* {{{ proto DateTime date_timestamp_set(DateTime object, long unixTimestamp)
    3520             :    Sets the date and time based on an Unix timestamp.
    3521             : */
    3522          11 : PHP_FUNCTION(date_timestamp_set)
    3523             : {
    3524             :         zval *object;
    3525             :         zend_long  timestamp;
    3526             : 
    3527          22 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &object, date_ce_date, &timestamp) == FAILURE) {
    3528           5 :                 RETURN_FALSE;
    3529             :         }
    3530             : 
    3531           6 :         php_date_timestamp_set(object, timestamp, return_value);
    3532             : 
    3533           6 :         Z_ADDREF_P(object);
    3534           6 :         ZVAL_COPY_VALUE(return_value, object);
    3535             : }
    3536             : /* }}} */
    3537             : 
    3538             : /* {{{ proto DateTimeImmutable::setTimestamp()
    3539             : */
    3540           1 : PHP_METHOD(DateTimeImmutable, setTimestamp)
    3541             : {
    3542             :         zval *object, new_object;
    3543             :         zend_long  timestamp;
    3544             : 
    3545           2 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &object, date_ce_immutable, &timestamp) == FAILURE) {
    3546           0 :                 RETURN_FALSE;
    3547             :         }
    3548             : 
    3549           1 :         date_clone_immutable(object, &new_object);
    3550           1 :         php_date_timestamp_set(&new_object, timestamp, return_value);
    3551             : 
    3552           1 :         ZVAL_OBJ(return_value, Z_OBJ(new_object));
    3553             : }
    3554             : /* }}} */
    3555             : 
    3556             : /* {{{ proto long date_timestamp_get(DateTimeInterface object)
    3557             :    Gets the Unix timestamp.
    3558             : */
    3559          45 : PHP_FUNCTION(date_timestamp_get)
    3560             : {
    3561             :         zval         *object;
    3562             :         php_date_obj *dateobj;
    3563             :         zend_long          timestamp;
    3564             :         int           error;
    3565             : 
    3566          90 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
    3567           1 :                 RETURN_FALSE;
    3568             :         }
    3569          44 :         dateobj = Z_PHPDATE_P(object);
    3570          44 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
    3571          43 :         timelib_update_ts(dateobj->time, NULL);
    3572             : 
    3573          43 :         timestamp = timelib_date_to_int(dateobj->time, &error);
    3574          43 :         if (error) {
    3575           0 :                 RETURN_FALSE;
    3576             :         } else {
    3577          43 :                 RETVAL_LONG(timestamp);
    3578             :         }
    3579             : }
    3580             : /* }}} */
    3581             : 
    3582             : /* {{{ proto DateInterval date_diff(DateTime object [, bool absolute])
    3583             :    Returns the difference between two DateTime objects.
    3584             : */
    3585      173476 : PHP_FUNCTION(date_diff)
    3586             : {
    3587             :         zval         *object1, *object2;
    3588             :         php_date_obj *dateobj1, *dateobj2;
    3589             :         php_interval_obj *interval;
    3590      173476 :         zend_bool      absolute = 0;
    3591             : 
    3592      346952 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO|b", &object1, date_ce_interface, &object2, date_ce_interface, &absolute) == FAILURE) {
    3593           0 :                 RETURN_FALSE;
    3594             :         }
    3595      173476 :         dateobj1 = Z_PHPDATE_P(object1);
    3596      173476 :         dateobj2 = Z_PHPDATE_P(object2);
    3597      173476 :         DATE_CHECK_INITIALIZED(dateobj1->time, DateTimeInterface);
    3598      173476 :         DATE_CHECK_INITIALIZED(dateobj2->time, DateTimeInterface);
    3599      173476 :         timelib_update_ts(dateobj1->time, NULL);
    3600      173476 :         timelib_update_ts(dateobj2->time, NULL);
    3601             : 
    3602      173476 :         php_date_instantiate(date_ce_interval, return_value);
    3603      173476 :         interval = Z_PHPINTERVAL_P(return_value);
    3604      173476 :         interval->diff = timelib_diff(dateobj1->time, dateobj2->time);
    3605      173476 :         if (absolute) {
    3606           4 :                 interval->diff->invert = 0;
    3607             :         }
    3608      173476 :         interval->initialized = 1;
    3609             : }
    3610             : /* }}} */
    3611             : 
    3612         760 : static int timezone_initialize(php_timezone_obj *tzobj, /*const*/ char *tz, size_t tz_len) /* {{{ */
    3613             : {
    3614         760 :         timelib_time *dummy_t = ecalloc(1, sizeof(timelib_time));
    3615             :         int           dst, not_found;
    3616         760 :         char         *orig_tz = tz;
    3617             : 
    3618         760 :         if (strlen(tz) != tz_len) {
    3619           2 :                 php_error_docref(NULL, E_WARNING, "Timezone must not contain null bytes");
    3620           2 :                 efree(dummy_t);
    3621           2 :                 return FAILURE;
    3622             :         }
    3623             : 
    3624         758 :         dummy_t->z = timelib_parse_zone(&tz, &dst, dummy_t, &not_found, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    3625         758 :         if (not_found) {
    3626          43 :                 php_error_docref(NULL, E_WARNING, "Unknown or bad timezone (%s)", orig_tz);
    3627          43 :                 efree(dummy_t);
    3628          43 :                 return FAILURE;
    3629             :         } else {
    3630         715 :                 set_timezone_from_timelib_time(tzobj, dummy_t);
    3631         715 :                 timelib_free(dummy_t->tz_abbr);
    3632         715 :                 efree(dummy_t);
    3633         715 :                 return SUCCESS;
    3634             :         }
    3635             : } /* }}} */
    3636             : 
    3637             : /* {{{ proto DateTimeZone timezone_open(string timezone)
    3638             :    Returns new DateTimeZone object
    3639             : */
    3640          49 : PHP_FUNCTION(timezone_open)
    3641             : {
    3642             :         char *tz;
    3643             :         size_t tz_len;
    3644             :         php_timezone_obj *tzobj;
    3645             : 
    3646          49 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &tz, &tz_len) == FAILURE) {
    3647           8 :                 RETURN_FALSE;
    3648             :         }
    3649          41 :         tzobj = Z_PHPTIMEZONE_P(php_date_instantiate(date_ce_timezone, return_value));
    3650          41 :         if (SUCCESS != timezone_initialize(tzobj, tz, tz_len)) {
    3651          21 :                 zval_ptr_dtor(return_value);
    3652          21 :                 RETURN_FALSE;
    3653             :         }
    3654             : }
    3655             : /* }}} */
    3656             : 
    3657             : /* {{{ proto DateTimeZone::__construct(string timezone)
    3658             :    Creates new DateTimeZone object.
    3659             : */
    3660         723 : PHP_METHOD(DateTimeZone, __construct)
    3661             : {
    3662             :         char *tz;
    3663             :         size_t tz_len;
    3664             :         php_timezone_obj *tzobj;
    3665             :         zend_error_handling error_handling;
    3666             : 
    3667         723 :         if (FAILURE == zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s", &tz, &tz_len)) {
    3668           7 :                 return;
    3669             :         }
    3670             : 
    3671         716 :         zend_replace_error_handling(EH_THROW, NULL, &error_handling);
    3672        1432 :         tzobj = Z_PHPTIMEZONE_P(getThis());
    3673         716 :         timezone_initialize(tzobj, tz, tz_len);
    3674         716 :         zend_restore_error_handling(&error_handling);
    3675             : }
    3676             : /* }}} */
    3677             : 
    3678           4 : static int php_date_timezone_initialize_from_hash(zval **return_value, php_timezone_obj **tzobj, HashTable *myht) /* {{{ */
    3679             : {
    3680             :         zval            *z_timezone;
    3681             :         zval            *z_timezone_type;
    3682             : 
    3683           4 :         if ((z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type") - 1)) != NULL) {
    3684           4 :                 if ((z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone") - 1)) != NULL) {
    3685           4 :                         if (Z_TYPE_P(z_timezone_type) != IS_LONG) {
    3686           1 :                                 return FAILURE;
    3687             :                         }
    3688           3 :                         if (Z_TYPE_P(z_timezone) != IS_STRING) {
    3689           0 :                                 return FAILURE;
    3690             :                         }
    3691           3 :                         if (SUCCESS == timezone_initialize(*tzobj, Z_STRVAL_P(z_timezone), Z_STRLEN_P(z_timezone))) {
    3692           3 :                                 return SUCCESS;
    3693             :                         }
    3694             :                 }
    3695             :         }
    3696           0 :         return FAILURE;
    3697             : } /* }}} */
    3698             : 
    3699             : /* {{{ proto DateTimeZone::__set_state()
    3700             :  *  */
    3701           0 : PHP_METHOD(DateTimeZone, __set_state)
    3702             : {
    3703             :         php_timezone_obj *tzobj;
    3704             :         zval             *array;
    3705             :         HashTable        *myht;
    3706             : 
    3707           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
    3708           0 :                 return;
    3709             :         }
    3710             : 
    3711           0 :         myht = Z_ARRVAL_P(array);
    3712             : 
    3713           0 :         php_date_instantiate(date_ce_timezone, return_value);
    3714           0 :         tzobj = Z_PHPTIMEZONE_P(return_value);
    3715           0 :         if(php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht) != SUCCESS) {
    3716           0 :                 zend_throw_error(NULL, "Timezone initialization failed");
    3717           0 :                 zval_dtor(return_value);
    3718             :         }
    3719             : }
    3720             : /* }}} */
    3721             : 
    3722             : /* {{{ proto DateTimeZone::__wakeup()
    3723             :  *  */
    3724           4 : PHP_METHOD(DateTimeZone, __wakeup)
    3725             : {
    3726           8 :         zval             *object = getThis();
    3727             :         php_timezone_obj *tzobj;
    3728             :         HashTable        *myht;
    3729             : 
    3730           4 :         tzobj = Z_PHPTIMEZONE_P(object);
    3731             : 
    3732           4 :         myht = Z_OBJPROP_P(object);
    3733             : 
    3734           4 :         if(php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht) != SUCCESS) {
    3735           1 :                 zend_throw_error(NULL, "Timezone initialization failed");
    3736             :         }
    3737           4 : }
    3738             : /* }}} */
    3739             : 
    3740             : /* {{{ proto string timezone_name_get(DateTimeZone object)
    3741             :    Returns the name of the timezone.
    3742             : */
    3743          53 : PHP_FUNCTION(timezone_name_get)
    3744             : {
    3745             :         zval             *object;
    3746             :         php_timezone_obj *tzobj;
    3747             : 
    3748         106 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_timezone) == FAILURE) {
    3749           6 :                 RETURN_FALSE;
    3750             :         }
    3751          47 :         tzobj = Z_PHPTIMEZONE_P(object);
    3752          47 :         DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
    3753             : 
    3754          46 :         switch (tzobj->type) {
    3755             :                 case TIMELIB_ZONETYPE_ID:
    3756          70 :                         RETURN_STRING(tzobj->tzi.tz->name);
    3757             :                         break;
    3758             :                 case TIMELIB_ZONETYPE_OFFSET: {
    3759           8 :                         zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
    3760           8 :                         timelib_sll utc_offset = tzobj->tzi.utc_offset;
    3761             : 
    3762          16 :                         ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
    3763             :                                 utc_offset > 0 ? '-' : '+',
    3764           8 :                                 abs(utc_offset / 60),
    3765           8 :                                 abs((utc_offset % 60)));
    3766             : 
    3767           8 :                         RETURN_NEW_STR(tmpstr);
    3768             :                         }
    3769             :                         break;
    3770             :                 case TIMELIB_ZONETYPE_ABBR:
    3771           6 :                         RETURN_STRING(tzobj->tzi.z.abbr);
    3772             :                         break;
    3773             :         }
    3774             : }
    3775             : /* }}} */
    3776             : 
    3777             : /* {{{ proto string timezone_name_from_abbr(string abbr[, long gmtOffset[, long isdst]])
    3778             :    Returns the timezone name from abbrevation
    3779             : */
    3780         107 : PHP_FUNCTION(timezone_name_from_abbr)
    3781             : {
    3782             :         char    *abbr;
    3783             :         char    *tzid;
    3784             :         size_t      abbr_len;
    3785         107 :         zend_long     gmtoffset = -1;
    3786         107 :         zend_long     isdst = -1;
    3787             : 
    3788         107 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &abbr, &abbr_len, &gmtoffset, &isdst) == FAILURE) {
    3789          35 :                 RETURN_FALSE;
    3790             :         }
    3791          72 :         tzid = timelib_timezone_id_from_abbr(abbr, gmtoffset, isdst);
    3792             : 
    3793          72 :         if (tzid) {
    3794         136 :                 RETURN_STRING(tzid);
    3795             :         } else {
    3796           4 :                 RETURN_FALSE;
    3797             :         }
    3798             : }
    3799             : /* }}} */
    3800             : 
    3801             : /* {{{ proto long timezone_offset_get(DateTimeZone object, DateTimeInterface datetime)
    3802             :    Returns the timezone offset.
    3803             : */
    3804          57 : PHP_FUNCTION(timezone_offset_get)
    3805             : {
    3806             :         zval                *object, *dateobject;
    3807             :         php_timezone_obj    *tzobj;
    3808             :         php_date_obj        *dateobj;
    3809             :         timelib_time_offset *offset;
    3810             : 
    3811         114 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_timezone, &dateobject, date_ce_interface) == FAILURE) {
    3812          35 :                 RETURN_FALSE;
    3813             :         }
    3814          22 :         tzobj = Z_PHPTIMEZONE_P(object);
    3815          22 :         DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
    3816          22 :         dateobj = Z_PHPDATE_P(dateobject);
    3817          22 :         DATE_CHECK_INITIALIZED(dateobj->time, DateTimeInterface);
    3818             : 
    3819          22 :         switch (tzobj->type) {
    3820             :                 case TIMELIB_ZONETYPE_ID:
    3821          13 :                         offset = timelib_get_time_zone_info(dateobj->time->sse, tzobj->tzi.tz);
    3822          13 :                         RETVAL_LONG(offset->offset);
    3823          13 :                         timelib_time_offset_dtor(offset);
    3824          13 :                         break;
    3825             :                 case TIMELIB_ZONETYPE_OFFSET:
    3826           6 :                         RETURN_LONG(tzobj->tzi.utc_offset * -60);
    3827             :                         break;
    3828             :                 case TIMELIB_ZONETYPE_ABBR:
    3829           3 :                         RETURN_LONG((tzobj->tzi.z.utc_offset - (tzobj->tzi.z.dst*60)) * -60);
    3830             :                         break;
    3831             :         }
    3832             : }
    3833             : /* }}} */
    3834             : 
    3835             : /* {{{ proto array timezone_transitions_get(DateTimeZone object [, long timestamp_begin [, long timestamp_end ]])
    3836             :    Returns numerically indexed array containing associative array for all transitions in the specified range for the timezone.
    3837             : */
    3838          94 : PHP_FUNCTION(timezone_transitions_get)
    3839             : {
    3840             :         zval                *object, element;
    3841             :         php_timezone_obj    *tzobj;
    3842          94 :         unsigned int         i, begin = 0, found;
    3843          94 :         zend_long            timestamp_begin = ZEND_LONG_MIN, timestamp_end = ZEND_LONG_MAX;
    3844             : 
    3845         188 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ll", &object, date_ce_timezone, &timestamp_begin, &timestamp_end) == FAILURE) {
    3846          60 :                 RETURN_FALSE;
    3847             :         }
    3848          34 :         tzobj = Z_PHPTIMEZONE_P(object);
    3849          34 :         DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
    3850          34 :         if (tzobj->type != TIMELIB_ZONETYPE_ID) {
    3851           0 :                 RETURN_FALSE;
    3852             :         }
    3853             : 
    3854             : #define add_nominal() \
    3855             :                 array_init(&element); \
    3856             :                 add_assoc_long(&element, "ts",     timestamp_begin); \
    3857             :                 add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601, 13, timestamp_begin, 0)); \
    3858             :                 add_assoc_long(&element, "offset", tzobj->tzi.tz->type[0].offset); \
    3859             :                 add_assoc_bool(&element, "isdst",  tzobj->tzi.tz->type[0].isdst); \
    3860             :                 add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[0].abbr_idx]); \
    3861             :                 add_next_index_zval(return_value, &element);
    3862             : 
    3863             : #define add(i,ts) \
    3864             :                 array_init(&element); \
    3865             :                 add_assoc_long(&element, "ts",     ts); \
    3866             :                 add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601, 13, ts, 0)); \
    3867             :                 add_assoc_long(&element, "offset", tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].offset); \
    3868             :                 add_assoc_bool(&element, "isdst",  tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].isdst); \
    3869             :                 add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].abbr_idx]); \
    3870             :                 add_next_index_zval(return_value, &element);
    3871             : 
    3872             : #define add_last() add(tzobj->tzi.tz->bit32.timecnt - 1, timestamp_begin)
    3873             : 
    3874          34 :         array_init(return_value);
    3875             : 
    3876          34 :         if (timestamp_begin == ZEND_LONG_MIN) {
    3877           2 :                 add_nominal();
    3878           2 :                 begin = 0;
    3879           2 :                 found = 1;
    3880             :         } else {
    3881          32 :                 begin = 0;
    3882          32 :                 found = 0;
    3883          32 :                 if (tzobj->tzi.tz->bit32.timecnt > 0) {
    3884             :                         do {
    3885        3591 :                                 if (tzobj->tzi.tz->trans[begin] > timestamp_begin) {
    3886          32 :                                         if (begin > 0) {
    3887          32 :                                                 add(begin - 1, timestamp_begin);
    3888             :                                         } else {
    3889           0 :                                                 add_nominal();
    3890             :                                         }
    3891          32 :                                         found = 1;
    3892          32 :                                         break;
    3893             :                                 }
    3894        3559 :                                 begin++;
    3895        3559 :                         } while (begin < tzobj->tzi.tz->bit32.timecnt);
    3896             :                 }
    3897             :         }
    3898             : 
    3899          34 :         if (!found) {
    3900           0 :                 if (tzobj->tzi.tz->bit32.timecnt > 0) {
    3901           0 :                         add_last();
    3902             :                 } else {
    3903           0 :                         add_nominal();
    3904             :                 }
    3905             :         } else {
    3906        4737 :                 for (i = begin; i < tzobj->tzi.tz->bit32.timecnt; ++i) {
    3907        4703 :                         if (tzobj->tzi.tz->trans[i] < timestamp_end) {
    3908         625 :                                 add(i, tzobj->tzi.tz->trans[i]);
    3909             :                         }
    3910             :                 }
    3911             :         }
    3912             : }
    3913             : /* }}} */
    3914             : 
    3915             : /* {{{ proto array timezone_location_get()
    3916             :    Returns location information for a timezone, including country code, latitude/longitude and comments
    3917             : */
    3918         351 : PHP_FUNCTION(timezone_location_get)
    3919             : {
    3920             :         zval                *object;
    3921             :         php_timezone_obj    *tzobj;
    3922             : 
    3923         702 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_timezone) == FAILURE) {
    3924           0 :                 RETURN_FALSE;
    3925             :         }
    3926         351 :         tzobj = Z_PHPTIMEZONE_P(object);
    3927         351 :         DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
    3928         350 :         if (tzobj->type != TIMELIB_ZONETYPE_ID) {
    3929           0 :                 RETURN_FALSE;
    3930             :         }
    3931             : 
    3932         350 :         array_init(return_value);
    3933         350 :         add_assoc_string(return_value, "country_code", tzobj->tzi.tz->location.country_code);
    3934         350 :         add_assoc_double(return_value, "latitude", tzobj->tzi.tz->location.latitude);
    3935         350 :         add_assoc_double(return_value, "longitude", tzobj->tzi.tz->location.longitude);
    3936         350 :         add_assoc_string(return_value, "comments", tzobj->tzi.tz->location.comments);
    3937             : }
    3938             : /* }}} */
    3939             : 
    3940      175604 : static int date_interval_initialize(timelib_rel_time **rt, /*const*/ char *format, size_t format_length) /* {{{ */
    3941             : {
    3942      175604 :         timelib_time     *b = NULL, *e = NULL;
    3943      175604 :         timelib_rel_time *p = NULL;
    3944      175604 :         int               r = 0;
    3945      175604 :         int               retval = 0;
    3946             :         struct timelib_error_container *errors;
    3947             : 
    3948      175604 :         timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
    3949             : 
    3950      175604 :         if (errors->error_count > 0) {
    3951           2 :                 php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
    3952           2 :                 retval = FAILURE;
    3953             :         } else {
    3954      175602 :                 if(p) {
    3955      175597 :                         *rt = p;
    3956      175597 :                         retval = SUCCESS;
    3957             :                 } else {
    3958           8 :                         if(b && e) {
    3959           3 :                                 timelib_update_ts(b, NULL);
    3960           3 :                                 timelib_update_ts(e, NULL);
    3961           3 :                                 *rt = timelib_diff(b, e);
    3962           3 :                                 retval = SUCCESS;
    3963             :                         } else {
    3964           2 :                                 php_error_docref(NULL, E_WARNING, "Failed to parse interval (%s)", format);
    3965           2 :                                 retval = FAILURE;
    3966             :                         }
    3967             :                 }
    3968             :         }
    3969      175604 :         timelib_error_container_dtor(errors);
    3970      175604 :         timelib_free(b);
    3971      175604 :         timelib_free(e);
    3972      175604 :         return retval;
    3973             : } /* }}} */
    3974             : 
    3975             : /* {{{ date_interval_read_property */
    3976          44 : zval *date_interval_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv)
    3977             : {
    3978             :         php_interval_obj *obj;
    3979             :         zval *retval;
    3980             :         zval tmp_member;
    3981          44 :         timelib_sll value = -1;
    3982             : 
    3983          44 :         if (Z_TYPE_P(member) != IS_STRING) {
    3984           0 :                 tmp_member = *member;
    3985             :                 zval_copy_ctor(&tmp_member);
    3986           0 :                 convert_to_string(&tmp_member);
    3987           0 :                 member = &tmp_member;
    3988           0 :                 cache_slot = NULL;
    3989             :         }
    3990             : 
    3991          44 :         obj = Z_PHPINTERVAL_P(object);
    3992             : 
    3993          44 :         if (!obj->initialized) {
    3994           3 :                 retval = (zend_get_std_object_handlers())->read_property(object, member, type, cache_slot, rv);
    3995           3 :                 if (member == &tmp_member) {
    3996             :                         zval_dtor(member);
    3997             :                 }
    3998           3 :                 return retval;
    3999             :         }
    4000             : 
    4001             : #define GET_VALUE_FROM_STRUCT(n,m)            \
    4002             :         if (strcmp(Z_STRVAL_P(member), m) == 0) { \
    4003             :                 value = obj->diff->n;                 \
    4004             :                 break;                                                            \
    4005             :         }
    4006             :         do {
    4007          41 :                 GET_VALUE_FROM_STRUCT(y, "y");
    4008          35 :                 GET_VALUE_FROM_STRUCT(m, "m");
    4009          30 :                 GET_VALUE_FROM_STRUCT(d, "d");
    4010          22 :                 GET_VALUE_FROM_STRUCT(h, "h");
    4011          18 :                 GET_VALUE_FROM_STRUCT(i, "i");
    4012          14 :                 GET_VALUE_FROM_STRUCT(s, "s");
    4013          10 :                 GET_VALUE_FROM_STRUCT(invert, "invert");
    4014           9 :                 GET_VALUE_FROM_STRUCT(days, "days");
    4015             :                 /* didn't find any */
    4016           4 :                 retval = (zend_get_std_object_handlers())->read_property(object, member, type, cache_slot, rv);
    4017             : 
    4018           4 :                 if (member == &tmp_member) {
    4019             :                         zval_dtor(member);
    4020             :                 }
    4021             : 
    4022           4 :                 return retval;
    4023             :         } while(0);
    4024             : 
    4025          37 :         retval = rv;
    4026             : 
    4027          37 :         if (value != -99999) {
    4028          36 :                 ZVAL_LONG(retval, value);
    4029             :         } else {
    4030           1 :                 ZVAL_FALSE(retval);
    4031             :         }
    4032             : 
    4033          37 :         if (member == &tmp_member) {
    4034             :                 zval_dtor(member);
    4035             :         }
    4036             : 
    4037          37 :         return retval;
    4038             : }
    4039             : /* }}} */
    4040             : 
    4041             : /* {{{ date_interval_write_property */
    4042         536 : void date_interval_write_property(zval *object, zval *member, zval *value, void **cache_slot)
    4043             : {
    4044             :         php_interval_obj *obj;
    4045             :         zval tmp_member;
    4046             : 
    4047         536 :         if (Z_TYPE_P(member) != IS_STRING) {
    4048           0 :                 tmp_member = *member;
    4049             :                 zval_copy_ctor(&tmp_member);
    4050           0 :                 convert_to_string(&tmp_member);
    4051           0 :                 member = &tmp_member;
    4052           0 :                 cache_slot = NULL;
    4053             :         }
    4054             : 
    4055         536 :         obj = Z_PHPINTERVAL_P(object);
    4056             : 
    4057         536 :         if (!obj->initialized) {
    4058           1 :                 (zend_get_std_object_handlers())->write_property(object, member, value, cache_slot);
    4059           1 :                 if (member == &tmp_member) {
    4060             :                         zval_dtor(member);
    4061             :                 }
    4062           1 :                 return;
    4063             :         }
    4064             : 
    4065             : #define SET_VALUE_FROM_STRUCT(n,m)            \
    4066             :         if (strcmp(Z_STRVAL_P(member), m) == 0) { \
    4067             :                 obj->diff->n = zval_get_long(value); \
    4068             :                 break;                                                            \
    4069             :         }
    4070             : 
    4071             :         do {
    4072         535 :                 SET_VALUE_FROM_STRUCT(y, "y");
    4073         535 :                 SET_VALUE_FROM_STRUCT(m, "m");
    4074         536 :                 SET_VALUE_FROM_STRUCT(d, "d");
    4075         534 :                 SET_VALUE_FROM_STRUCT(h, "h");
    4076         534 :                 SET_VALUE_FROM_STRUCT(i, "i");
    4077         534 :                 SET_VALUE_FROM_STRUCT(s, "s");
    4078        1066 :                 SET_VALUE_FROM_STRUCT(invert, "invert");
    4079             :                 /* didn't find any */
    4080           2 :                 (zend_get_std_object_handlers())->write_property(object, member, value, cache_slot);
    4081             :         } while(0);
    4082             : 
    4083         535 :         if (member == &tmp_member) {
    4084             :                 zval_dtor(member);
    4085             :         }
    4086             : }
    4087             : /* }}} */
    4088             : 
    4089             : 
    4090             : /* {{{ proto DateInterval::__construct([string interval_spec])
    4091             :    Creates new DateInterval object.
    4092             : */
    4093      175604 : PHP_METHOD(DateInterval, __construct)
    4094             : {
    4095      175604 :         char *interval_string = NULL;
    4096             :         size_t   interval_string_length;
    4097             :         php_interval_obj *diobj;
    4098             :         timelib_rel_time *reltime;
    4099             :         zend_error_handling error_handling;
    4100             : 
    4101      175604 :         if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s", &interval_string, &interval_string_length) == FAILURE) {
    4102           0 :                 return;
    4103             :         }
    4104             : 
    4105      175604 :         zend_replace_error_handling(EH_THROW, NULL, &error_handling);
    4106      175604 :         if (date_interval_initialize(&reltime, interval_string, interval_string_length) == SUCCESS) {
    4107      351200 :                 diobj = Z_PHPINTERVAL_P(getThis());
    4108      175600 :                 diobj->diff = reltime;
    4109      175600 :                 diobj->initialized = 1;
    4110             :         }
    4111      175604 :         zend_restore_error_handling(&error_handling);
    4112             : }
    4113             : /* }}} */
    4114             : 
    4115             : 
    4116           8 : static int php_date_interval_initialize_from_hash(zval **return_value, php_interval_obj **intobj, HashTable *myht) /* {{{ */
    4117             : {
    4118           8 :         (*intobj)->diff = timelib_rel_time_ctor();
    4119             : 
    4120             : #define PHP_DATE_INTERVAL_READ_PROPERTY(element, member, itype, def) \
    4121             :         do { \
    4122             :                 zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
    4123             :                 if (z_arg) { \
    4124             :                         (*intobj)->diff->member = (itype)zval_get_long(z_arg); \
    4125             :                 } else { \
    4126             :                         (*intobj)->diff->member = (itype)def; \
    4127             :                 } \
    4128             :         } while (0);
    4129             : 
    4130             : #define PHP_DATE_INTERVAL_READ_PROPERTY_I64(element, member) \
    4131             :         do { \
    4132             :                 zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
    4133             :                 if (z_arg) { \
    4134             :                         zend_string *str = zval_get_string(z_arg); \
    4135             :                         DATE_A64I((*intobj)->diff->member, ZSTR_VAL(str)); \
    4136             :                         zend_string_release(str); \
    4137             :                 } else { \
    4138             :                         (*intobj)->diff->member = -1LL; \
    4139             :                 } \
    4140             :         } while (0);
    4141             : 
    4142          15 :         PHP_DATE_INTERVAL_READ_PROPERTY("y", y, timelib_sll, -1)
    4143          14 :         PHP_DATE_INTERVAL_READ_PROPERTY("m", m, timelib_sll, -1)
    4144          14 :         PHP_DATE_INTERVAL_READ_PROPERTY("d", d, timelib_sll, -1)
    4145          14 :         PHP_DATE_INTERVAL_READ_PROPERTY("h", h, timelib_sll, -1)
    4146          14 :         PHP_DATE_INTERVAL_READ_PROPERTY("i", i, timelib_sll, -1)
    4147          14 :         PHP_DATE_INTERVAL_READ_PROPERTY("s", s, timelib_sll, -1)
    4148          13 :         PHP_DATE_INTERVAL_READ_PROPERTY("weekday", weekday, int, -1)
    4149          13 :         PHP_DATE_INTERVAL_READ_PROPERTY("weekday_behavior", weekday_behavior, int, -1)
    4150          13 :         PHP_DATE_INTERVAL_READ_PROPERTY("first_last_day_of", first_last_day_of, int, -1)
    4151          14 :         PHP_DATE_INTERVAL_READ_PROPERTY("invert", invert, int, 0);
    4152          16 :         PHP_DATE_INTERVAL_READ_PROPERTY_I64("days", days);
    4153          13 :         PHP_DATE_INTERVAL_READ_PROPERTY("special_type", special.type, unsigned int, 0);
    4154          16 :         PHP_DATE_INTERVAL_READ_PROPERTY_I64("special_amount", special.amount);
    4155          13 :         PHP_DATE_INTERVAL_READ_PROPERTY("have_weekday_relative", have_weekday_relative, unsigned int, 0);
    4156          13 :         PHP_DATE_INTERVAL_READ_PROPERTY("have_special_relative", have_special_relative, unsigned int, 0);
    4157           8 :         (*intobj)->initialized = 1;
    4158             : 
    4159           8 :         return 0;
    4160             : } /* }}} */
    4161             : 
    4162             : /* {{{ proto DateInterval::__set_state()
    4163             : */
    4164           1 : PHP_METHOD(DateInterval, __set_state)
    4165             : {
    4166             :         php_interval_obj *intobj;
    4167             :         zval             *array;
    4168             :         HashTable        *myht;
    4169             : 
    4170           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
    4171           0 :                 RETURN_FALSE;
    4172             :         }
    4173             : 
    4174           1 :         myht = Z_ARRVAL_P(array);
    4175             : 
    4176           1 :         php_date_instantiate(date_ce_interval, return_value);
    4177           1 :         intobj = Z_PHPINTERVAL_P(return_value);
    4178           1 :         php_date_interval_initialize_from_hash(&return_value, &intobj, myht);
    4179             : }
    4180             : /* }}} */
    4181             : 
    4182             : /* {{{ proto DateInterval::__wakeup()
    4183             : */
    4184           7 : PHP_METHOD(DateInterval, __wakeup)
    4185             : {
    4186          14 :         zval             *object = getThis();
    4187             :         php_interval_obj *intobj;
    4188             :         HashTable        *myht;
    4189             : 
    4190           7 :         intobj = Z_PHPINTERVAL_P(object);
    4191             : 
    4192           7 :         myht = Z_OBJPROP_P(object);
    4193             : 
    4194           7 :         php_date_interval_initialize_from_hash(&return_value, &intobj, myht);
    4195           7 : }
    4196             : /* }}} */
    4197             : 
    4198             : /* {{{ proto DateInterval date_interval_create_from_date_string(string time)
    4199             :    Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string
    4200             : */
    4201          16 : PHP_FUNCTION(date_interval_create_from_date_string)
    4202             : {
    4203          16 :         char           *time_str = NULL;
    4204          16 :         size_t          time_str_len = 0;
    4205             :         timelib_time   *time;
    4206          16 :         timelib_error_container *err = NULL;
    4207             :         php_interval_obj *diobj;
    4208             : 
    4209          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &time_str, &time_str_len) == FAILURE) {
    4210           2 :                 RETURN_FALSE;
    4211             :         }
    4212             : 
    4213          14 :         php_date_instantiate(date_ce_interval, return_value);
    4214             : 
    4215          14 :         time = timelib_strtotime(time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    4216          14 :         diobj = Z_PHPINTERVAL_P(return_value);
    4217          14 :         diobj->diff = timelib_rel_time_clone(&time->relative);
    4218          14 :         diobj->initialized = 1;
    4219          14 :         timelib_time_dtor(time);
    4220          14 :         timelib_error_container_dtor(err);
    4221             : }
    4222             : /* }}} */
    4223             : 
    4224             : /* {{{ date_interval_format -  */
    4225      173490 : static zend_string *date_interval_format(char *format, size_t format_len, timelib_rel_time *t)
    4226             : {
    4227      173490 :         smart_str            string = {0};
    4228             :         size_t               i;
    4229      173490 :         int                  length, have_format_spec = 0;
    4230             :         char                 buffer[33];
    4231             : 
    4232      173490 :         if (!format_len) {
    4233           0 :                 return ZSTR_EMPTY_ALLOC();
    4234             :         }
    4235             : 
    4236     1909460 :         for (i = 0; i < format_len; i++) {
    4237     1735970 :                 if (have_format_spec) {
    4238      520999 :                         switch (format[i]) {
    4239           1 :                                 case 'Y': length = slprintf(buffer, 32, "%02d", (int) t->y); break;
    4240      173110 :                                 case 'y': length = slprintf(buffer, 32, "%d", (int) t->y); break;
    4241             : 
    4242           1 :                                 case 'M': length = slprintf(buffer, 32, "%02d", (int) t->m); break;
    4243      173110 :                                 case 'm': length = slprintf(buffer, 32, "%d", (int) t->m); break;
    4244             : 
    4245           1 :                                 case 'D': length = slprintf(buffer, 32, "%02d", (int) t->d); break;
    4246      173153 :                                 case 'd': length = slprintf(buffer, 32, "%d", (int) t->d); break;
    4247             : 
    4248           1 :                                 case 'H': length = slprintf(buffer, 32, "%02d", (int) t->h); break;
    4249         358 :                                 case 'h': length = slprintf(buffer, 32, "%d", (int) t->h); break;
    4250             : 
    4251           1 :                                 case 'I': length = slprintf(buffer, 32, "%02d", (int) t->i); break;
    4252         318 :                                 case 'i': length = slprintf(buffer, 32, "%d", (int) t->i); break;
    4253             : 
    4254           1 :                                 case 'S': length = slprintf(buffer, 32, "%02" ZEND_LONG_FMT_SPEC, (zend_long) t->s); break;
    4255         317 :                                 case 's': length = slprintf(buffer, 32, ZEND_LONG_FMT, (zend_long) t->s); break;
    4256             : 
    4257             :                                 case 'a': {
    4258         311 :                                         if ((int) t->days != -99999) {
    4259         309 :                                                 length = slprintf(buffer, 32, "%d", (int) t->days);
    4260             :                                         } else {
    4261           2 :                                                 length = slprintf(buffer, 32, "(unknown)");
    4262             :                                         }
    4263         311 :                                 } break;
    4264           2 :                                 case 'r': length = slprintf(buffer, 32, "%s", t->invert ? "-" : ""); break;
    4265         311 :                                 case 'R': length = slprintf(buffer, 32, "%c", t->invert ? '-' : '+'); break;
    4266             : 
    4267           1 :                                 case '%': length = slprintf(buffer, 32, "%%"); break;
    4268           2 :                                 default: buffer[0] = '%'; buffer[1] = format[i]; buffer[2] = '\0'; length = 2; break;
    4269             :                         }
    4270      520999 :                         smart_str_appendl(&string, buffer, length);
    4271      520999 :                         have_format_spec = 0;
    4272             :                 } else {
    4273     1214971 :                         if (format[i] == '%') {
    4274      521000 :                                 have_format_spec = 1;
    4275             :                         } else {
    4276      693971 :                                 smart_str_appendc(&string, format[i]);
    4277             :                         }
    4278             :                 }
    4279             :         }
    4280             : 
    4281             :         smart_str_0(&string);
    4282             : 
    4283      173490 :         if (string.s == NULL) {
    4284           1 :                 return ZSTR_EMPTY_ALLOC();
    4285             :         }
    4286             : 
    4287      173489 :         return string.s;
    4288             : }
    4289             : /* }}} */
    4290             : 
    4291             : /* {{{ proto string date_interval_format(DateInterval object, string format)
    4292             :    Formats the interval.
    4293             : */
    4294      173490 : PHP_FUNCTION(date_interval_format)
    4295             : {
    4296             :         zval             *object;
    4297             :         php_interval_obj *diobj;
    4298             :         char             *format;
    4299             :         size_t            format_len;
    4300             : 
    4301      346980 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_interval, &format, &format_len) == FAILURE) {
    4302           0 :                 RETURN_FALSE;
    4303             :         }
    4304      173490 :         diobj = Z_PHPINTERVAL_P(object);
    4305      173490 :         DATE_CHECK_INITIALIZED(diobj->initialized, DateInterval);
    4306             : 
    4307      173490 :         RETURN_STR(date_interval_format(format, format_len, diobj->diff));
    4308             : }
    4309             : /* }}} */
    4310             : 
    4311           3 : static int date_period_initialize(timelib_time **st, timelib_time **et, timelib_rel_time **d, zend_long *recurrences, /*const*/ char *format, size_t format_length) /* {{{ */
    4312             : {
    4313           3 :         timelib_time     *b = NULL, *e = NULL;
    4314           3 :         timelib_rel_time *p = NULL;
    4315           3 :         int               r = 0;
    4316           3 :         int               retval = 0;
    4317             :         struct timelib_error_container *errors;
    4318             : 
    4319           3 :         timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
    4320             : 
    4321           3 :         if (errors->error_count > 0) {
    4322           2 :                 php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
    4323           2 :                 retval = FAILURE;
    4324             :         } else {
    4325           1 :                 *st = b;
    4326           1 :                 *et = e;
    4327           1 :                 *d  = p;
    4328           1 :                 *recurrences = r;
    4329           1 :                 retval = SUCCESS;
    4330             :         }
    4331           3 :         timelib_error_container_dtor(errors);
    4332           3 :         return retval;
    4333             : } /* }}} */
    4334             : 
    4335             : /* {{{ proto DatePeriod::__construct(DateTime $start, DateInterval $interval, int recurrences|DateTime $end)
    4336             :    Creates new DatePeriod object.
    4337             : */
    4338          16 : PHP_METHOD(DatePeriod, __construct)
    4339             : {
    4340             :         php_period_obj   *dpobj;
    4341             :         php_date_obj     *dateobj;
    4342             :         php_interval_obj *intobj;
    4343          16 :         zval *start, *end = NULL, *interval;
    4344          16 :         zend_long  recurrences = 0, options = 0;
    4345          16 :         char *isostr = NULL;
    4346          16 :         size_t   isostr_len = 0;
    4347             :         timelib_time *clone;
    4348             :         zend_error_handling error_handling;
    4349             : 
    4350          16 :         zend_replace_error_handling(EH_THROW, NULL, &error_handling);
    4351          16 :         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) {
    4352           9 :                 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) {
    4353           4 :                         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "s|l", &isostr, &isostr_len, &options) == FAILURE) {
    4354           1 :                                 php_error_docref(NULL, E_WARNING, "This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments.");
    4355           1 :                                 zend_restore_error_handling(&error_handling);
    4356           1 :                                 return;
    4357             :                         }
    4358             :                 }
    4359             :         }
    4360             : 
    4361          30 :         dpobj = Z_PHPPERIOD_P(getThis());
    4362          15 :         dpobj->current = NULL;
    4363             : 
    4364          15 :         if (isostr) {
    4365           3 :                 date_period_initialize(&(dpobj->start), &(dpobj->end), &(dpobj->interval), &recurrences, isostr, isostr_len);
    4366           3 :                 if (dpobj->start == NULL) {
    4367           2 :                         php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain a start date.", isostr);
    4368             :                 }
    4369           3 :                 if (dpobj->interval == NULL) {
    4370           2 :                         php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain an interval.", isostr);
    4371             :                 }
    4372           3 :                 if (dpobj->end == NULL && recurrences == 0) {
    4373           2 :                         php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain an end date or a recurrence count.", isostr);
    4374             :                 }
    4375             : 
    4376           3 :                 if (dpobj->start) {
    4377           1 :                         timelib_update_ts(dpobj->start, NULL);
    4378             :                 }
    4379           3 :                 if (dpobj->end) {
    4380           0 :                         timelib_update_ts(dpobj->end, NULL);
    4381             :                 }
    4382           3 :                 dpobj->start_ce = date_ce_date;
    4383             :         } else {
    4384             :                 /* init */
    4385          12 :                 intobj  = Z_PHPINTERVAL_P(interval);
    4386             : 
    4387             :                 /* start date */
    4388          12 :                 dateobj = Z_PHPDATE_P(start);
    4389          12 :                 clone = timelib_time_ctor();
    4390          12 :                 memcpy(clone, dateobj->time, sizeof(timelib_time));
    4391          12 :                 if (dateobj->time->tz_abbr) {
    4392          12 :                         clone->tz_abbr = timelib_strdup(dateobj->time->tz_abbr);
    4393             :                 }
    4394          12 :                 if (dateobj->time->tz_info) {
    4395          12 :                         clone->tz_info = dateobj->time->tz_info;
    4396             :                 }
    4397          12 :                 dpobj->start = clone;
    4398          12 :                 dpobj->start_ce = Z_OBJCE_P(start);
    4399             : 
    4400             :                 /* interval */
    4401          12 :                 dpobj->interval = timelib_rel_time_clone(intobj->diff);
    4402             : 
    4403             :                 /* end date */
    4404          12 :                 if (end) {
    4405           5 :                         dateobj = Z_PHPDATE_P(end);
    4406           5 :                         clone = timelib_time_clone(dateobj->time);
    4407           5 :                         dpobj->end = clone;
    4408             :                 }
    4409             :         }
    4410             : 
    4411             :         /* options */
    4412          15 :         dpobj->include_start_date = !(options & PHP_DATE_PERIOD_EXCLUDE_START_DATE);
    4413             : 
    4414             :         /* recurrrences */
    4415          15 :         dpobj->recurrences = recurrences + dpobj->include_start_date;
    4416             : 
    4417          15 :         dpobj->initialized = 1;
    4418             : 
    4419          15 :         zend_restore_error_handling(&error_handling);
    4420             : }
    4421             : /* }}} */
    4422             : 
    4423             : /* {{{ proto DatePeriod::getStartDate()
    4424             :    Get start date.
    4425             : */
    4426           2 : PHP_METHOD(DatePeriod, getStartDate)
    4427             : {
    4428             :         php_period_obj   *dpobj;
    4429             :         php_date_obj     *dateobj;
    4430             : 
    4431           2 :         if (zend_parse_parameters_none() == FAILURE) {
    4432           0 :                 return;
    4433             :         }
    4434             : 
    4435           4 :         dpobj = Z_PHPPERIOD_P(getThis());
    4436             : 
    4437           2 :         php_date_instantiate(dpobj->start_ce, return_value);
    4438           2 :         dateobj = Z_PHPDATE_P(return_value);
    4439           2 :         dateobj->time = timelib_time_ctor();
    4440           2 :         *dateobj->time = *dpobj->start;
    4441           2 :         if (dpobj->start->tz_abbr) {
    4442           2 :                 dateobj->time->tz_abbr = timelib_strdup(dpobj->start->tz_abbr);
    4443             :         }
    4444           2 :         if (dpobj->start->tz_info) {
    4445           2 :                 dateobj->time->tz_info = dpobj->start->tz_info;
    4446             :         }
    4447             : }
    4448             : /* }}} */
    4449             : 
    4450             : /* {{{ proto DatePeriod::getEndDate()
    4451             :    Get end date.
    4452             : */
    4453           3 : PHP_METHOD(DatePeriod, getEndDate)
    4454             : {
    4455             :         php_period_obj   *dpobj;
    4456             :         php_date_obj     *dateobj;
    4457             : 
    4458           3 :         if (zend_parse_parameters_none() == FAILURE) {
    4459           0 :                 return;
    4460             :         }
    4461             : 
    4462           6 :         dpobj = Z_PHPPERIOD_P(getThis());
    4463             : 
    4464           3 :         if (!dpobj->end) {
    4465           1 :                 return;
    4466             :         }
    4467             : 
    4468           2 :         php_date_instantiate(dpobj->start_ce, return_value);
    4469           2 :         dateobj = Z_PHPDATE_P(return_value);
    4470           2 :         dateobj->time = timelib_time_ctor();
    4471           2 :         *dateobj->time = *dpobj->end;
    4472           2 :         if (dpobj->end->tz_abbr) {
    4473           2 :                 dateobj->time->tz_abbr = timelib_strdup(dpobj->end->tz_abbr);
    4474             :         }
    4475           2 :         if (dpobj->end->tz_info) {
    4476           2 :                 dateobj->time->tz_info = dpobj->end->tz_info;
    4477             :         }
    4478             : }
    4479             : /* }}} */
    4480             : 
    4481             : /* {{{ proto DatePeriod::getDateInterval()
    4482             :    Get date interval.
    4483             : */
    4484           1 : PHP_METHOD(DatePeriod, getDateInterval)
    4485             : {
    4486             :         php_period_obj   *dpobj;
    4487             :         php_interval_obj *diobj;
    4488             : 
    4489           1 :         if (zend_parse_parameters_none() == FAILURE) {
    4490           0 :                 return;
    4491             :         }
    4492             : 
    4493           2 :         dpobj = Z_PHPPERIOD_P(getThis());
    4494             : 
    4495           1 :         php_date_instantiate(date_ce_interval, return_value);
    4496           1 :         diobj = Z_PHPINTERVAL_P(return_value);
    4497           1 :         diobj->diff = timelib_rel_time_clone(dpobj->interval);
    4498           1 :         diobj->initialized = 1;
    4499             : }
    4500             : /* }}} */
    4501             : 
    4502        3534 : static int check_id_allowed(char *id, zend_long what) /* {{{ */
    4503             : {
    4504        3534 :         if (what & PHP_DATE_TIMEZONE_GROUP_AFRICA     && strncasecmp(id, "Africa/",      7) == 0) return 1;
    4505        3318 :         if (what & PHP_DATE_TIMEZONE_GROUP_AMERICA    && strncasecmp(id, "America/",     8) == 0) return 1;
    4506        2493 :         if (what & PHP_DATE_TIMEZONE_GROUP_ANTARCTICA && strncasecmp(id, "Antarctica/", 11) == 0) return 1;
    4507        2445 :         if (what & PHP_DATE_TIMEZONE_GROUP_ARCTIC     && strncasecmp(id, "Arctic/",      7) == 0) return 1;
    4508        2441 :         if (what & PHP_DATE_TIMEZONE_GROUP_ASIA       && strncasecmp(id, "Asia/",        5) == 0) return 1;
    4509        2061 :         if (what & PHP_DATE_TIMEZONE_GROUP_ATLANTIC   && strncasecmp(id, "Atlantic/",    9) == 0) return 1;
    4510        2013 :         if (what & PHP_DATE_TIMEZONE_GROUP_AUSTRALIA  && strncasecmp(id, "Australia/",  10) == 0) return 1;
    4511        1921 :         if (what & PHP_DATE_TIMEZONE_GROUP_EUROPE     && strncasecmp(id, "Europe/",      7) == 0) return 1;
    4512        1611 :         if (what & PHP_DATE_TIMEZONE_GROUP_INDIAN     && strncasecmp(id, "Indian/",      7) == 0) return 1;
    4513        1567 :         if (what & PHP_DATE_TIMEZONE_GROUP_PACIFIC    && strncasecmp(id, "Pacific/",     8) == 0) return 1;
    4514        1395 :         if (what & PHP_DATE_TIMEZONE_GROUP_UTC        && strncasecmp(id, "UTC",          3) == 0) return 1;
    4515        1390 :         return 0;
    4516             : } /* }}} */
    4517             : 
    4518             : /* {{{ proto array timezone_identifiers_list([long what[, string country]])
    4519             :    Returns numerically index array with all timezone identifiers.
    4520             : */
    4521           8 : PHP_FUNCTION(timezone_identifiers_list)
    4522             : {
    4523             :         const timelib_tzdb             *tzdb;
    4524             :         const timelib_tzdb_index_entry *table;
    4525             :         int                             i, item_count;
    4526           8 :         zend_long                            what = PHP_DATE_TIMEZONE_GROUP_ALL;
    4527           8 :         char                           *option = NULL;
    4528           8 :         size_t                             option_len = 0;
    4529             : 
    4530           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ls", &what, &option, &option_len) == FAILURE) {
    4531           0 :                 RETURN_FALSE;
    4532             :         }
    4533             : 
    4534             :         /* Extra validation */
    4535           8 :         if (what == PHP_DATE_TIMEZONE_PER_COUNTRY && option_len != 2) {
    4536           1 :                 php_error_docref(NULL, E_NOTICE, "A two-letter ISO 3166-1 compatible country code is expected");
    4537           1 :                 RETURN_FALSE;
    4538             :         }
    4539             : 
    4540           7 :         tzdb = DATE_TIMEZONEDB;
    4541           7 :         item_count = tzdb->index_size;
    4542           7 :         table = tzdb->index;
    4543             : 
    4544           7 :         array_init(return_value);
    4545             : 
    4546        4130 :         for (i = 0; i < item_count; ++i) {
    4547        4123 :                 if (what == PHP_DATE_TIMEZONE_PER_COUNTRY) {
    4548           0 :                         if (tzdb->data[table[i].pos + 5] == option[0] && tzdb->data[table[i].pos + 6] == option[1]) {
    4549           0 :                                 add_next_index_string(return_value, table[i].id);
    4550             :                         }
    4551        4123 :                 } else if (what == PHP_DATE_TIMEZONE_GROUP_ALL_W_BC || (check_id_allowed(table[i].id, what) && (tzdb->data[table[i].pos + 4] == '\1'))) {
    4552        2483 :                         add_next_index_string(return_value, table[i].id);
    4553             :                 }
    4554             :         };
    4555             : }
    4556             : /* }}} */
    4557             : 
    4558             : /* {{{ proto array timezone_version_get()
    4559             :    Returns the Olson database version number.
    4560             : */
    4561           2 : PHP_FUNCTION(timezone_version_get)
    4562             : {
    4563             :         const timelib_tzdb *tzdb;
    4564             : 
    4565           2 :         tzdb = DATE_TIMEZONEDB;
    4566           4 :         RETURN_STRING(tzdb->version);
    4567             : }
    4568             : /* }}} */
    4569             : 
    4570             : /* {{{ proto array timezone_abbreviations_list()
    4571             :    Returns associative array containing dst, offset and the timezone name
    4572             : */
    4573           5 : PHP_FUNCTION(timezone_abbreviations_list)
    4574             : {
    4575             :         const timelib_tz_lookup_table *table, *entry;
    4576             :         zval                          element, *abbr_array_p, abbr_array;
    4577             : 
    4578           5 :         table = timelib_timezone_abbreviations_list();
    4579           5 :         array_init(return_value);
    4580           5 :         entry = table;
    4581             : 
    4582             :         do {
    4583        9150 :                 array_init(&element);
    4584        9150 :                 add_assoc_bool_ex(&element, "dst", sizeof("dst") -1, entry->type);
    4585        9150 :                 add_assoc_long_ex(&element, "offset", sizeof("offset") - 1, entry->gmtoffset);
    4586        9150 :                 if (entry->full_tz_name) {
    4587        9025 :                         add_assoc_string_ex(&element, "timezone_id", sizeof("timezone_id") - 1, entry->full_tz_name);
    4588             :                 } else {
    4589         125 :                         add_assoc_null_ex(&element, "timezone_id", sizeof("timezone_id") - 1);
    4590             :                 }
    4591             : 
    4592        9150 :                 abbr_array_p = zend_hash_str_find(Z_ARRVAL_P(return_value), entry->name, strlen(entry->name));
    4593        9150 :                 if (!abbr_array_p) {
    4594        1870 :                         array_init(&abbr_array);
    4595        1870 :                         add_assoc_zval(return_value, entry->name, &abbr_array);
    4596             :                 } else {
    4597        7280 :                         ZVAL_COPY_VALUE(&abbr_array, abbr_array_p);
    4598             :                 }
    4599        9150 :                 add_next_index_zval(&abbr_array, &element);
    4600        9150 :                 entry++;
    4601        9150 :         } while (entry->name);
    4602           5 : }
    4603             : /* }}} */
    4604             : 
    4605             : /* {{{ proto bool date_default_timezone_set(string timezone_identifier)
    4606             :    Sets the default timezone used by all date/time functions in a script */
    4607         676 : PHP_FUNCTION(date_default_timezone_set)
    4608             : {
    4609             :         char *zone;
    4610             :         size_t   zone_len;
    4611             : 
    4612         676 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &zone, &zone_len) == FAILURE) {
    4613           4 :                 RETURN_FALSE;
    4614             :         }
    4615         672 :         if (!timelib_timezone_id_is_valid(zone, DATE_TIMEZONEDB)) {
    4616          25 :                 php_error_docref(NULL, E_NOTICE, "Timezone ID '%s' is invalid", zone);
    4617          25 :                 RETURN_FALSE;
    4618             :         }
    4619         647 :         if (DATEG(timezone)) {
    4620         119 :                 efree(DATEG(timezone));
    4621         119 :                 DATEG(timezone) = NULL;
    4622             :         }
    4623         647 :         DATEG(timezone) = estrndup(zone, zone_len);
    4624         647 :         RETURN_TRUE;
    4625             : }
    4626             : /* }}} */
    4627             : 
    4628             : /* {{{ proto string date_default_timezone_get()
    4629             :    Gets the default timezone used by all date/time functions in a script */
    4630          19 : PHP_FUNCTION(date_default_timezone_get)
    4631             : {
    4632             :         timelib_tzinfo *default_tz;
    4633             : 
    4634          19 :         default_tz = get_timezone_info();
    4635          38 :         RETVAL_STRING(default_tz->name);
    4636          19 : }
    4637             : /* }}} */
    4638             : 
    4639             : /* {{{ php_do_date_sunrise_sunset
    4640             :  *  Common for date_sunrise() and date_sunset() functions
    4641             :  */
    4642         902 : static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, int calc_sunset)
    4643             : {
    4644         902 :         double latitude = 0.0, longitude = 0.0, zenith = 0.0, gmt_offset = 0, altitude;
    4645             :         double h_rise, h_set, N;
    4646             :         timelib_sll rise, set, transit;
    4647         902 :         zend_long time, retformat = 0;
    4648             :         int             rs;
    4649             :         timelib_time   *t;
    4650             :         timelib_tzinfo *tzi;
    4651             :         zend_string    *retstr;
    4652             : 
    4653         902 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|ldddd", &time, &retformat, &latitude, &longitude, &zenith, &gmt_offset) == FAILURE) {
    4654         366 :                 RETURN_FALSE;
    4655             :         }
    4656             : 
    4657         536 :         switch (ZEND_NUM_ARGS()) {
    4658             :                 case 1:
    4659           0 :                         retformat = SUNFUNCS_RET_STRING;
    4660             :                 case 2:
    4661           0 :                         latitude = INI_FLT("date.default_latitude");
    4662             :                 case 3:
    4663           0 :                         longitude = INI_FLT("date.default_longitude");
    4664             :                 case 4:
    4665          48 :                         if (calc_sunset) {
    4666          24 :                                 zenith = INI_FLT("date.sunset_zenith");
    4667             :                         } else {
    4668          24 :                                 zenith = INI_FLT("date.sunrise_zenith");
    4669             :                         }
    4670             :                 case 5:
    4671             :                 case 6:
    4672         536 :                         break;
    4673             :                 default:
    4674           0 :                         php_error_docref(NULL, E_WARNING, "invalid format");
    4675           0 :                         RETURN_FALSE;
    4676             :                         break;
    4677             :         }
    4678        1072 :         if (retformat != SUNFUNCS_RET_TIMESTAMP &&
    4679         370 :                 retformat != SUNFUNCS_RET_STRING &&
    4680         166 :                 retformat != SUNFUNCS_RET_DOUBLE)
    4681             :         {
    4682           0 :                 php_error_docref(NULL, E_WARNING, "Wrong return format given, pick one of SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING or SUNFUNCS_RET_DOUBLE");
    4683           0 :                 RETURN_FALSE;
    4684             :         }
    4685         536 :         altitude = 90 - zenith;
    4686             : 
    4687             :         /* Initialize time struct */
    4688         536 :         t = timelib_time_ctor();
    4689         536 :         tzi = get_timezone_info();
    4690         536 :         t->tz_info = tzi;
    4691         536 :         t->zone_type = TIMELIB_ZONETYPE_ID;
    4692             : 
    4693         536 :         if (ZEND_NUM_ARGS() <= 5) {
    4694          48 :                 gmt_offset = timelib_get_current_offset(t) / 3600;
    4695             :         }
    4696             : 
    4697         536 :         timelib_unixtime2local(t, time);
    4698         536 :         rs = timelib_astro_rise_set_altitude(t, longitude, latitude, altitude, 1, &h_rise, &h_set, &rise, &set, &transit);
    4699         536 :         timelib_time_dtor(t);
    4700             : 
    4701         536 :         if (rs != 0) {
    4702         120 :                 RETURN_FALSE;
    4703             :         }
    4704             : 
    4705         416 :         if (retformat == SUNFUNCS_RET_TIMESTAMP) {
    4706         126 :                 RETURN_LONG(calc_sunset ? set : rise);
    4707             :         }
    4708         290 :         N = (calc_sunset ? h_set : h_rise) + gmt_offset;
    4709             : 
    4710         290 :         if (N > 24 || N < 0) {
    4711          28 :                 N -= floor(N / 24) * 24;
    4712             :         }
    4713             : 
    4714         290 :         switch (retformat) {
    4715             :                 case SUNFUNCS_RET_STRING:
    4716         164 :                         retstr = strpprintf(0, "%02d:%02d", (int) N, (int) (60 * (N - (int) N)));
    4717         164 :                         RETURN_NEW_STR(retstr);
    4718             :                         break;
    4719             :                 case SUNFUNCS_RET_DOUBLE:
    4720         126 :                         RETURN_DOUBLE(N);
    4721             :                         break;
    4722             :         }
    4723             : }
    4724             : /* }}} */
    4725             : 
    4726             : /* {{{ proto mixed date_sunrise(mixed time [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]])
    4727             :    Returns time of sunrise for a given day and location */
    4728         450 : PHP_FUNCTION(date_sunrise)
    4729             : {
    4730         450 :         php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    4731         450 : }
    4732             : /* }}} */
    4733             : 
    4734             : /* {{{ proto mixed date_sunset(mixed time [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]])
    4735             :    Returns time of sunset for a given day and location */
    4736         452 : PHP_FUNCTION(date_sunset)
    4737             : {
    4738         452 :         php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    4739         452 : }
    4740             : /* }}} */
    4741             : 
    4742             : /* {{{ proto array date_sun_info(long time, float latitude, float longitude)
    4743             :    Returns an array with information about sun set/rise and twilight begin/end */
    4744          79 : PHP_FUNCTION(date_sun_info)
    4745             : {
    4746             :         zend_long       time;
    4747             :         double          latitude, longitude;
    4748             :         timelib_time   *t, *t2;
    4749             :         timelib_tzinfo *tzi;
    4750             :         int             rs;
    4751             :         timelib_sll     rise, set, transit;
    4752             :         int             dummy;
    4753             :         double          ddummy;
    4754             : 
    4755          79 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ldd", &time, &latitude, &longitude) == FAILURE) {
    4756          24 :                 RETURN_FALSE;
    4757             :         }
    4758             :         /* Initialize time struct */
    4759          55 :         t = timelib_time_ctor();
    4760          55 :         tzi = get_timezone_info();
    4761          55 :         t->tz_info = tzi;
    4762          55 :         t->zone_type = TIMELIB_ZONETYPE_ID;
    4763          55 :         timelib_unixtime2local(t, time);
    4764             : 
    4765             :         /* Setup */
    4766          55 :         t2 = timelib_time_ctor();
    4767          55 :         array_init(return_value);
    4768             : 
    4769             :         /* Get sun up/down and transit */
    4770          55 :         rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -35.0/60, 1, &ddummy, &ddummy, &rise, &set, &transit);
    4771          55 :         switch (rs) {
    4772             :                 case -1: /* always below */
    4773           1 :                         add_assoc_bool(return_value, "sunrise", 0);
    4774           1 :                         add_assoc_bool(return_value, "sunset", 0);
    4775           1 :                         break;
    4776             :                 case 1: /* always above */
    4777           2 :                         add_assoc_bool(return_value, "sunrise", 1);
    4778           2 :                         add_assoc_bool(return_value, "sunset", 1);
    4779           2 :                         break;
    4780             :                 default:
    4781          52 :                         t2->sse = rise;
    4782          52 :                         add_assoc_long(return_value, "sunrise", timelib_date_to_int(t2, &dummy));
    4783          52 :                         t2->sse = set;
    4784          52 :                         add_assoc_long(return_value, "sunset", timelib_date_to_int(t2, &dummy));
    4785             :         }
    4786          55 :         t2->sse = transit;
    4787          55 :         add_assoc_long(return_value, "transit", timelib_date_to_int(t2, &dummy));
    4788             : 
    4789             :         /* Get civil twilight */
    4790          55 :         rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -6.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
    4791          55 :         switch (rs) {
    4792             :                 case -1: /* always below */
    4793           1 :                         add_assoc_bool(return_value, "civil_twilight_begin", 0);
    4794           1 :                         add_assoc_bool(return_value, "civil_twilight_end", 0);
    4795           1 :                         break;
    4796             :                 case 1: /* always above */
    4797           2 :                         add_assoc_bool(return_value, "civil_twilight_begin", 1);
    4798           2 :                         add_assoc_bool(return_value, "civil_twilight_end", 1);
    4799           2 :                         break;
    4800             :                 default:
    4801          52 :                         t2->sse = rise;
    4802          52 :                         add_assoc_long(return_value, "civil_twilight_begin", timelib_date_to_int(t2, &dummy));
    4803          52 :                         t2->sse = set;
    4804          52 :                         add_assoc_long(return_value, "civil_twilight_end", timelib_date_to_int(t2, &dummy));
    4805             :         }
    4806             : 
    4807             :         /* Get nautical twilight */
    4808          55 :         rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -12.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
    4809          55 :         switch (rs) {
    4810             :                 case -1: /* always below */
    4811           1 :                         add_assoc_bool(return_value, "nautical_twilight_begin", 0);
    4812           1 :                         add_assoc_bool(return_value, "nautical_twilight_end", 0);
    4813           1 :                         break;
    4814             :                 case 1: /* always above */
    4815           1 :                         add_assoc_bool(return_value, "nautical_twilight_begin", 1);
    4816           1 :                         add_assoc_bool(return_value, "nautical_twilight_end", 1);
    4817           1 :                         break;
    4818             :                 default:
    4819          53 :                         t2->sse = rise;
    4820          53 :                         add_assoc_long(return_value, "nautical_twilight_begin", timelib_date_to_int(t2, &dummy));
    4821          53 :                         t2->sse = set;
    4822          53 :                         add_assoc_long(return_value, "nautical_twilight_end", timelib_date_to_int(t2, &dummy));
    4823             :         }
    4824             : 
    4825             :         /* Get astronomical twilight */
    4826          55 :         rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -18.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
    4827          55 :         switch (rs) {
    4828             :                 case -1: /* always below */
    4829           1 :                         add_assoc_bool(return_value, "astronomical_twilight_begin", 0);
    4830           1 :                         add_assoc_bool(return_value, "astronomical_twilight_end", 0);
    4831           1 :                         break;
    4832             :                 case 1: /* always above */
    4833           1 :                         add_assoc_bool(return_value, "astronomical_twilight_begin", 1);
    4834           1 :                         add_assoc_bool(return_value, "astronomical_twilight_end", 1);
    4835           1 :                         break;
    4836             :                 default:
    4837          53 :                         t2->sse = rise;
    4838          53 :                         add_assoc_long(return_value, "astronomical_twilight_begin", timelib_date_to_int(t2, &dummy));
    4839          53 :                         t2->sse = set;
    4840          53 :                         add_assoc_long(return_value, "astronomical_twilight_end", timelib_date_to_int(t2, &dummy));
    4841             :         }
    4842          55 :         timelib_time_dtor(t);
    4843          55 :         timelib_time_dtor(t2);
    4844             : }
    4845             : /* }}} */
    4846             : 
    4847           0 : static HashTable *date_object_get_gc_period(zval *object, zval **table, int *n) /* {{{ */
    4848             : {
    4849           0 :         *table = NULL;
    4850           0 :         *n = 0;
    4851           0 :         return zend_std_get_properties(object);
    4852             : } /* }}} */
    4853             : 
    4854           9 : static HashTable *date_object_get_properties_period(zval *object) /* {{{ */
    4855             : {
    4856             :         HashTable               *props;
    4857             :         zval                     zv;
    4858             :         php_period_obj  *period_obj;
    4859             : 
    4860           9 :         period_obj = Z_PHPPERIOD_P(object);
    4861             : 
    4862           9 :         props = zend_std_get_properties(object);
    4863             : 
    4864           9 :         if (!period_obj->start) {
    4865           4 :                 return props;
    4866             :         }
    4867             : 
    4868           5 :         if (period_obj->start) {
    4869             :                 php_date_obj *date_obj;
    4870           5 :                 object_init_ex(&zv, date_ce_date);
    4871           5 :                 date_obj = Z_PHPDATE_P(&zv);
    4872           5 :                 date_obj->time = timelib_time_clone(period_obj->start);
    4873             :         } else {
    4874           0 :                 ZVAL_NULL(&zv);
    4875             :         }
    4876           5 :         zend_hash_str_update(props, "start", sizeof("start")-1, &zv);
    4877             : 
    4878           5 :         if (period_obj->current) {
    4879             :                 php_date_obj *date_obj;
    4880           3 :                 object_init_ex(&zv, date_ce_date);
    4881           3 :                 date_obj = Z_PHPDATE_P(&zv);
    4882           3 :                 date_obj->time = timelib_time_clone(period_obj->current);
    4883             :         } else {
    4884           2 :                 ZVAL_NULL(&zv);
    4885             :         }
    4886           5 :         zend_hash_str_update(props, "current", sizeof("current")-1, &zv);
    4887             : 
    4888           5 :         if (period_obj->end) {
    4889             :                 php_date_obj *date_obj;
    4890           0 :                 object_init_ex(&zv, date_ce_date);
    4891           0 :                 date_obj = Z_PHPDATE_P(&zv);
    4892           0 :                 date_obj->time = timelib_time_clone(period_obj->end);
    4893             :         } else {
    4894           5 :                 ZVAL_NULL(&zv);
    4895             :         }
    4896           5 :         zend_hash_str_update(props, "end", sizeof("end")-1, &zv);
    4897             : 
    4898           5 :         if (period_obj->interval) {
    4899             :                 php_interval_obj *interval_obj;
    4900           5 :                 object_init_ex(&zv, date_ce_interval);
    4901           5 :                 interval_obj = Z_PHPINTERVAL_P(&zv);
    4902           5 :                 interval_obj->diff = timelib_rel_time_clone(period_obj->interval);
    4903           5 :                 interval_obj->initialized = 1;
    4904             :         } else {
    4905           0 :                 ZVAL_NULL(&zv);
    4906             :         }
    4907           5 :         zend_hash_str_update(props, "interval", sizeof("interval")-1, &zv);
    4908             : 
    4909             :         /* converted to larger type (int->long); must check when unserializing */
    4910           5 :         ZVAL_LONG(&zv, (zend_long) period_obj->recurrences);
    4911           5 :         zend_hash_str_update(props, "recurrences", sizeof("recurrences")-1, &zv);
    4912             : 
    4913           5 :         ZVAL_BOOL(&zv, period_obj->include_start_date);
    4914           5 :         zend_hash_str_update(props, "include_start_date", sizeof("include_start_date")-1, &zv);
    4915             : 
    4916           5 :         return props;
    4917             : } /* }}} */
    4918             : 
    4919           2 : static int php_date_period_initialize_from_hash(php_period_obj *period_obj, HashTable *myht) /* {{{ */
    4920             : {
    4921             :         zval *ht_entry;
    4922             : 
    4923             :         /* this function does no rollback on error */
    4924             : 
    4925           2 :         ht_entry = zend_hash_str_find(myht, "start", sizeof("start")-1);
    4926           2 :         if (ht_entry) {
    4927           2 :                 if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_date) {
    4928             :                         php_date_obj *date_obj;
    4929           1 :                         date_obj = Z_PHPDATE_P(ht_entry);
    4930           1 :                         period_obj->start = timelib_time_clone(date_obj->time);
    4931           1 :                         period_obj->start_ce = Z_OBJCE_P(ht_entry);
    4932           0 :                 } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
    4933           0 :                         return 0;
    4934             :                 }
    4935             :         } else {
    4936           1 :                 return 0;
    4937             :         }
    4938             : 
    4939           1 :         ht_entry = zend_hash_str_find(myht, "end", sizeof("end")-1);
    4940           1 :         if (ht_entry) {
    4941           1 :                 if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_date) {
    4942             :                         php_date_obj *date_obj;
    4943           0 :                         date_obj = Z_PHPDATE_P(ht_entry);
    4944           0 :                         period_obj->end = timelib_time_clone(date_obj->time);
    4945           1 :                 } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
    4946           0 :                         return 0;
    4947             :                 }
    4948             :         } else {
    4949           0 :                 return 0;
    4950             :         }
    4951             : 
    4952           1 :         ht_entry = zend_hash_str_find(myht, "current", sizeof("current")-1);
    4953           1 :         if (ht_entry) {
    4954           2 :                 if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_date) {
    4955             :                         php_date_obj *date_obj;
    4956           1 :                         date_obj = Z_PHPDATE_P(ht_entry);
    4957           1 :                         period_obj->current = timelib_time_clone(date_obj->time);
    4958           0 :                 } else if (Z_TYPE_P(ht_entry) != IS_NULL)  {
    4959           0 :                         return 0;
    4960             :                 }
    4961             :         } else {
    4962           0 :                 return 0;
    4963             :         }
    4964             : 
    4965           1 :         ht_entry = zend_hash_str_find(myht, "interval", sizeof("interval")-1);
    4966           1 :         if (ht_entry) {
    4967           2 :                 if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_interval) {
    4968             :                         php_interval_obj *interval_obj;
    4969           1 :                         interval_obj = Z_PHPINTERVAL_P(ht_entry);
    4970           1 :                         period_obj->interval = timelib_rel_time_clone(interval_obj->diff);
    4971             :                 } else { /* interval is required */
    4972           0 :                         return 0;
    4973             :                 }
    4974             :         } else {
    4975           0 :                 return 0;
    4976             :         }
    4977             : 
    4978           1 :         ht_entry = zend_hash_str_find(myht, "recurrences", sizeof("recurrences")-1);
    4979           5 :         if (ht_entry &&
    4980           2 :                         Z_TYPE_P(ht_entry) == IS_LONG && Z_LVAL_P(ht_entry) >= 0 && Z_LVAL_P(ht_entry) <= INT_MAX) {
    4981           1 :                 period_obj->recurrences = Z_LVAL_P(ht_entry);
    4982             :         } else {
    4983           0 :                 return 0;
    4984             :         }
    4985             : 
    4986           1 :         ht_entry = zend_hash_str_find(myht, "include_start_date", sizeof("include_start_date")-1);
    4987           4 :         if (ht_entry &&
    4988             :                         (Z_TYPE_P(ht_entry) == IS_FALSE || Z_TYPE_P(ht_entry) == IS_TRUE)) {
    4989           1 :                 period_obj->include_start_date = (Z_TYPE_P(ht_entry) == IS_TRUE);
    4990             :         } else {
    4991           0 :                 return 0;
    4992             :         }
    4993             : 
    4994           1 :         period_obj->initialized = 1;
    4995             : 
    4996           1 :         return 1;
    4997             : } /* }}} */
    4998             : 
    4999             : /* {{{ proto DatePeriod::__set_state()
    5000             : */
    5001           0 : PHP_METHOD(DatePeriod, __set_state)
    5002             : {
    5003             :         php_period_obj   *period_obj;
    5004             :         zval             *array;
    5005             :         HashTable        *myht;
    5006             : 
    5007           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
    5008           0 :                 RETURN_FALSE;
    5009             :         }
    5010             : 
    5011           0 :         myht = Z_ARRVAL_P(array);
    5012             : 
    5013           0 :         object_init_ex(return_value, date_ce_period);
    5014           0 :         period_obj = Z_PHPPERIOD_P(return_value);
    5015           0 :         if (!php_date_period_initialize_from_hash(period_obj, myht)) {
    5016           0 :                 zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
    5017             :         }
    5018             : }
    5019             : /* }}} */
    5020             : 
    5021             : /* {{{ proto DatePeriod::__wakeup()
    5022             : */
    5023           2 : PHP_METHOD(DatePeriod, __wakeup)
    5024             : {
    5025           4 :         zval             *object = getThis();
    5026             :         php_period_obj   *period_obj;
    5027             :         HashTable        *myht;
    5028             : 
    5029           2 :         period_obj = Z_PHPPERIOD_P(object);
    5030             : 
    5031           2 :         myht = Z_OBJPROP_P(object);
    5032             : 
    5033           2 :         if (!php_date_period_initialize_from_hash(period_obj, myht)) {
    5034           1 :                 zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
    5035             :         }
    5036           2 : }
    5037             : /* }}} */
    5038             : 
    5039             : /* {{{ date_period_read_property */
    5040           0 : static zval *date_period_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv)
    5041             : {
    5042             :         zval *zv;
    5043           0 :         if (type != BP_VAR_IS && type != BP_VAR_R) {
    5044           0 :                 zend_throw_error(NULL, "Retrieval of DatePeriod properties for modification is unsupported");
    5045           0 :                 return &EG(uninitialized_zval);
    5046             :         }
    5047             : 
    5048           0 :         Z_OBJPROP_P(object); /* build properties hash table */
    5049             : 
    5050           0 :         zv = std_object_handlers.read_property(object, member, type, cache_slot, rv);
    5051           0 :         if (Z_TYPE_P(zv) == IS_OBJECT && Z_OBJ_HANDLER_P(zv, clone_obj)) {
    5052             :                 /* defensive copy */
    5053           0 :                 ZVAL_OBJ(zv, Z_OBJ_HANDLER_P(zv, clone_obj)(zv));
    5054             :         }
    5055             : 
    5056           0 :         return zv;
    5057             : }
    5058             : /* }}} */
    5059             : 
    5060             : /* {{{ date_period_write_property */
    5061           0 : static void date_period_write_property(zval *object, zval *member, zval *value, void **cache_slot)
    5062             : {
    5063           0 :         zend_throw_error(NULL, "Writing to DatePeriod properties is unsupported");
    5064           0 : }
    5065             : /* }}} */
    5066             : 
    5067             : /*
    5068             :  * Local variables:
    5069             :  * tab-width: 4
    5070             :  * c-basic-offset: 4
    5071             :  * End:
    5072             :  * vim600: fdm=marker
    5073             :  * vim: noet sw=4 ts=4
    5074             :  */

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:04 +0000 (7 days ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.