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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:58:53 +0000 (4 days ago)

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