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: 1847 2079 88.8 %
Date: 2015-07-05 Functions: 151 165 91.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 05 Jul 2015 15:44:45 +0000 (24 hours ago)

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