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: 1700 1902 89.4 %
Date: 2014-07-29 Functions: 120 132 90.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 29 Jul 2014 09:53:02 +0000 (45 hours ago)

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