PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - date - php_date.c
Test: PHP Code Coverage
Date: 2009-11-21 Instrumented lines: 1652
Code covered: 86.5 % Executed lines: 1429
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5

Generated at Sat, 21 Nov 2009 12:26:58 +0000 (3 days ago)

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