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

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:11 +0000 (7 days ago)

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