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

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:02 +0000 (45 days ago)

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