File indexing completed on 2024-05-12 06:02:23

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Date
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @version    $Id$
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 /**
0023  * @category   Zend
0024  * @package    Zend_Date
0025  * @subpackage Zend_Date_DateObject
0026  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0027  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0028  */
0029 abstract class Zend_Date_DateObject {
0030 
0031     /**
0032      * UNIX Timestamp
0033      */
0034     private   $_unixTimestamp;
0035     protected static $_cache         = null;
0036     protected static $_cacheTags     = false;
0037     protected static $_defaultOffset = 0;
0038 
0039     /**
0040      * active timezone
0041      */
0042     private   $_timezone    = 'UTC';
0043     private   $_offset      = 0;
0044     private   $_syncronised = 0;
0045 
0046     // turn off DST correction if UTC or GMT
0047     protected $_dst         = true;
0048 
0049     /**
0050      * Table of Monthdays
0051      */
0052     private static $_monthTable = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
0053 
0054     /**
0055      * Table of Years
0056      */
0057     private static $_yearTable = array(
0058         1970 => 0,            1960 => -315619200,   1950 => -631152000,
0059         1940 => -946771200,   1930 => -1262304000,  1920 => -1577923200,
0060         1910 => -1893456000,  1900 => -2208988800,  1890 => -2524521600,
0061         1880 => -2840140800,  1870 => -3155673600,  1860 => -3471292800,
0062         1850 => -3786825600,  1840 => -4102444800,  1830 => -4417977600,
0063         1820 => -4733596800,  1810 => -5049129600,  1800 => -5364662400,
0064         1790 => -5680195200,  1780 => -5995814400,  1770 => -6311347200,
0065         1760 => -6626966400,  1750 => -6942499200,  1740 => -7258118400,
0066         1730 => -7573651200,  1720 => -7889270400,  1710 => -8204803200,
0067         1700 => -8520336000,  1690 => -8835868800,  1680 => -9151488000,
0068         1670 => -9467020800,  1660 => -9782640000,  1650 => -10098172800,
0069         1640 => -10413792000, 1630 => -10729324800, 1620 => -11044944000,
0070         1610 => -11360476800, 1600 => -11676096000);
0071 
0072     /**
0073      * Set this object to have a new UNIX timestamp.
0074      *
0075      * @param  string|integer  $timestamp  OPTIONAL timestamp; defaults to local time using time()
0076      * @return string|integer  old timestamp
0077      * @throws Zend_Date_Exception
0078      */
0079     protected function setUnixTimestamp($timestamp = null)
0080     {
0081         $old = $this->_unixTimestamp;
0082 
0083         if (is_numeric($timestamp)) {
0084             $this->_unixTimestamp = $timestamp;
0085         } else if ($timestamp === null) {
0086             $this->_unixTimestamp = time();
0087         } else {
0088             // require_once 'Zend/Date/Exception.php';
0089             throw new Zend_Date_Exception('\'' . $timestamp . '\' is not a valid UNIX timestamp', 0, null, $timestamp);
0090         }
0091 
0092         return $old;
0093     }
0094 
0095     /**
0096      * Returns this object's UNIX timestamp
0097      * A timestamp greater then the integer range will be returned as string
0098      * This function does not return the timestamp as object. Use copy() instead.
0099      *
0100      * @return  integer|string  timestamp
0101      */
0102     protected function getUnixTimestamp()
0103     {
0104         if ($this->_unixTimestamp === intval($this->_unixTimestamp)) {
0105             return (int) $this->_unixTimestamp;
0106         } else {
0107             return (string) $this->_unixTimestamp;
0108         }
0109     }
0110 
0111     /**
0112      * Internal function.
0113      * Returns time().  This method exists to allow unit tests to work-around methods that might otherwise
0114      * be hard-coded to use time().  For example, this makes it possible to test isYesterday() in Date.php.
0115      *
0116      * @param   integer  $sync      OPTIONAL time syncronisation value
0117      * @return  integer  timestamp
0118      */
0119     protected function _getTime($sync = null)
0120     {
0121         if ($sync !== null) {
0122             $this->_syncronised = round($sync);
0123         }
0124         return (time() + $this->_syncronised);
0125     }
0126 
0127     /**
0128      * Internal mktime function used by Zend_Date.
0129      * The timestamp returned by mktime() can exceed the precision of traditional UNIX timestamps,
0130      * by allowing PHP to auto-convert to using a float value.
0131      *
0132      * Returns a timestamp relative to 1970/01/01 00:00:00 GMT/UTC.
0133      * DST (Summer/Winter) is depriciated since php 5.1.0.
0134      * Year has to be 4 digits otherwise it would be recognised as
0135      * year 70 AD instead of 1970 AD as expected !!
0136      *
0137      * @param  integer  $hour
0138      * @param  integer  $minute
0139      * @param  integer  $second
0140      * @param  integer  $month
0141      * @param  integer  $day
0142      * @param  integer  $year
0143      * @param  boolean  $gmt     OPTIONAL true = other arguments are for UTC time, false = arguments are for local time/date
0144      * @return  integer|float  timestamp (number of seconds elapsed relative to 1970/01/01 00:00:00 GMT/UTC)
0145      */
0146     protected function mktime($hour, $minute, $second, $month, $day, $year, $gmt = false)
0147     {
0148         // complete date but in 32bit timestamp - use PHP internal
0149         if ((1901 < $year) and ($year < 2038)) {
0150 
0151             $oldzone = @date_default_timezone_get();
0152             // Timezone also includes DST settings, therefor substracting the GMT offset is not enough
0153             // We have to set the correct timezone to get the right value
0154             if (($this->_timezone != $oldzone) and ($gmt === false)) {
0155                 date_default_timezone_set($this->_timezone);
0156             }
0157             $result = ($gmt) ? @gmmktime($hour, $minute, $second, $month, $day, $year)
0158                              :   @mktime($hour, $minute, $second, $month, $day, $year);
0159             date_default_timezone_set($oldzone);
0160 
0161             return $result;
0162         }
0163 
0164         if ($gmt !== true) {
0165             $second += $this->_offset;
0166         }
0167 
0168         if (isset(self::$_cache)) {
0169             $id = strtr('Zend_DateObject_mkTime_' . $this->_offset . '_' . $year.$month.$day.'_'.$hour.$minute.$second . '_'.(int)$gmt, '-','_');
0170             if ($result = self::$_cache->load($id)) {
0171                 return unserialize($result);
0172             }
0173         }
0174 
0175         // date to integer
0176         $day   = intval($day);
0177         $month = intval($month);
0178         $year  = intval($year);
0179 
0180         // correct months > 12 and months < 1
0181         if ($month > 12) {
0182             $overlap = floor($month / 12);
0183             $year   += $overlap;
0184             $month  -= $overlap * 12;
0185         } else {
0186             $overlap = ceil((1 - $month) / 12);
0187             $year   -= $overlap;
0188             $month  += $overlap * 12;
0189         }
0190 
0191         $date = 0;
0192         if ($year >= 1970) {
0193 
0194             // Date is after UNIX epoch
0195             // go through leapyears
0196             // add months from latest given year
0197             for ($count = 1970; $count <= $year; $count++) {
0198 
0199                 $leapyear = self::isYearLeapYear($count);
0200                 if ($count < $year) {
0201 
0202                     $date += 365;
0203                     if ($leapyear === true) {
0204                         $date++;
0205                     }
0206 
0207                 } else {
0208 
0209                     for ($mcount = 0; $mcount < ($month - 1); $mcount++) {
0210                         $date += self::$_monthTable[$mcount];
0211                         if (($leapyear === true) and ($mcount == 1)) {
0212                             $date++;
0213                         }
0214 
0215                     }
0216                 }
0217             }
0218 
0219             $date += $day - 1;
0220             $date = (($date * 86400) + ($hour * 3600) + ($minute * 60) + $second);
0221         } else {
0222 
0223             // Date is before UNIX epoch
0224             // go through leapyears
0225             // add months from latest given year
0226             for ($count = 1969; $count >= $year; $count--) {
0227 
0228                 $leapyear = self::isYearLeapYear($count);
0229                 if ($count > $year)
0230                 {
0231                     $date += 365;
0232                     if ($leapyear === true)
0233                         $date++;
0234                 } else {
0235 
0236                     for ($mcount = 11; $mcount > ($month - 1); $mcount--) {
0237                         $date += self::$_monthTable[$mcount];
0238                         if (($leapyear === true) and ($mcount == 2)) {
0239                             $date++;
0240                         }
0241 
0242                     }
0243                 }
0244             }
0245 
0246             $date += (self::$_monthTable[$month - 1] - $day);
0247             $date = -(($date * 86400) + (86400 - (($hour * 3600) + ($minute * 60) + $second)));
0248 
0249             // gregorian correction for 5.Oct.1582
0250             if ($date < -12220185600) {
0251                 $date += 864000;
0252             } else if ($date < -12219321600) {
0253                 $date  = -12219321600;
0254             }
0255         }
0256 
0257         if (isset(self::$_cache)) {
0258             if (self::$_cacheTags) {
0259                 self::$_cache->save( serialize($date), $id, array('Zend_Date'));
0260             } else {
0261                 self::$_cache->save( serialize($date), $id);
0262             }
0263         }
0264 
0265         return $date;
0266     }
0267 
0268     /**
0269      * Returns true, if given $year is a leap year.
0270      *
0271      * @param  integer  $year
0272      * @return boolean  true, if year is leap year
0273      */
0274     protected static function isYearLeapYear($year)
0275     {
0276         // all leapyears can be divided through 4
0277         if (($year % 4) != 0) {
0278             return false;
0279         }
0280 
0281         // all leapyears can be divided through 400
0282         if ($year % 400 == 0) {
0283             return true;
0284         } else if (($year > 1582) and ($year % 100 == 0)) {
0285             return false;
0286         }
0287 
0288         return true;
0289     }
0290 
0291     /**
0292      * Internal mktime function used by Zend_Date for handling 64bit timestamps.
0293      *
0294      * Returns a formatted date for a given timestamp.
0295      *
0296      * @param  string   $format     format for output
0297      * @param  mixed    $timestamp
0298      * @param  boolean  $gmt        OPTIONAL true = other arguments are for UTC time, false = arguments are for local time/date
0299      * @return string
0300      */
0301     protected function date($format, $timestamp = null, $gmt = false)
0302     {
0303         $oldzone = @date_default_timezone_get();
0304         if ($this->_timezone != $oldzone) {
0305             date_default_timezone_set($this->_timezone);
0306         }
0307 
0308         if ($timestamp === null) {
0309             $result = ($gmt) ? @gmdate($format) : @date($format);
0310             date_default_timezone_set($oldzone);
0311             return $result;
0312         }
0313 
0314         if (abs($timestamp) <= 0x7FFFFFFF) {
0315             // See ZF-11992
0316             // "o" will sometimes resolve to the previous year (see 
0317             // http://php.net/date ; it's part of the ISO 8601 
0318             // standard). However, this is not desired, so replacing 
0319             // all occurrences of "o" not preceded by a backslash 
0320             // with "Y"
0321             $format = preg_replace('/(?<!\\\\)o/', 'Y', $format);
0322             $result = ($gmt) ? @gmdate($format, $timestamp) : @date($format, $timestamp);
0323             date_default_timezone_set($oldzone);
0324             return $result;
0325         }
0326 
0327         $jump      = false;
0328         $origstamp = $timestamp;
0329         if (isset(self::$_cache)) {
0330             $idstamp = strtr('Zend_DateObject_date_' . $this->_offset . '_'. $timestamp . '_'.(int)$gmt, '-','_');
0331             if ($result2 = self::$_cache->load($idstamp)) {
0332                 $timestamp = unserialize($result2);
0333                 $jump = true;
0334             }
0335         }
0336 
0337         // check on false or null alone fails
0338         if (empty($gmt) and empty($jump)) {
0339             $tempstamp = $timestamp;
0340             if ($tempstamp > 0) {
0341                 while (abs($tempstamp) > 0x7FFFFFFF) {
0342                     $tempstamp -= (86400 * 23376);
0343                 }
0344 
0345                 $dst = date("I", $tempstamp);
0346                 if ($dst === 1) {
0347                     $timestamp += 3600;
0348                 }
0349 
0350                 $temp       = date('Z', $tempstamp);
0351                 $timestamp += $temp;
0352             }
0353 
0354             if (isset(self::$_cache)) {
0355                 if (self::$_cacheTags) {
0356                     self::$_cache->save( serialize($timestamp), $idstamp, array('Zend_Date'));
0357                 } else {
0358                     self::$_cache->save( serialize($timestamp), $idstamp);
0359                 }
0360             }
0361         }
0362 
0363         if (($timestamp < 0) and ($gmt !== true)) {
0364             $timestamp -= $this->_offset;
0365         }
0366 
0367         date_default_timezone_set($oldzone);
0368         $date   = $this->getDateParts($timestamp, true);
0369         $length = strlen($format);
0370         $output = '';
0371 
0372         for ($i = 0; $i < $length; $i++) {
0373             switch($format[$i]) {
0374                 // day formats
0375                 case 'd':  // day of month, 2 digits, with leading zero, 01 - 31
0376                     $output .= (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']);
0377                     break;
0378 
0379                 case 'D':  // day of week, 3 letters, Mon - Sun
0380                     $output .= date('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
0381                     break;
0382 
0383                 case 'j':  // day of month, without leading zero, 1 - 31
0384                     $output .= $date['mday'];
0385                     break;
0386 
0387                 case 'l':  // day of week, full string name, Sunday - Saturday
0388                     $output .= date('l', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
0389                     break;
0390 
0391                 case 'N':  // ISO 8601 numeric day of week, 1 - 7
0392                     $day = self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
0393                     if ($day == 0) {
0394                         $day = 7;
0395                     }
0396                     $output .= $day;
0397                     break;
0398 
0399                 case 'S':  // english suffix for day of month, st nd rd th
0400                     if (($date['mday'] % 10) == 1) {
0401                         $output .= 'st';
0402                     } else if ((($date['mday'] % 10) == 2) and ($date['mday'] != 12)) {
0403                         $output .= 'nd';
0404                     } else if (($date['mday'] % 10) == 3) {
0405                         $output .= 'rd';
0406                     } else {
0407                         $output .= 'th';
0408                     }
0409                     break;
0410 
0411                 case 'w':  // numeric day of week, 0 - 6
0412                     $output .= self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
0413                     break;
0414 
0415                 case 'z':  // day of year, 0 - 365
0416                     $output .= $date['yday'];
0417                     break;
0418 
0419 
0420                 // week formats
0421                 case 'W':  // ISO 8601, week number of year
0422                     $output .= $this->weekNumber($date['year'], $date['mon'], $date['mday']);
0423                     break;
0424 
0425 
0426                 // month formats
0427                 case 'F':  // string month name, january - december
0428                     $output .= date('F', mktime(0, 0, 0, $date['mon'], 2, 1971));
0429                     break;
0430 
0431                 case 'm':  // number of month, with leading zeros, 01 - 12
0432                     $output .= (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']);
0433                     break;
0434 
0435                 case 'M':  // 3 letter month name, Jan - Dec
0436                     $output .= date('M',mktime(0, 0, 0, $date['mon'], 2, 1971));
0437                     break;
0438 
0439                 case 'n':  // number of month, without leading zeros, 1 - 12
0440                     $output .= $date['mon'];
0441                     break;
0442 
0443                 case 't':  // number of day in month
0444                     $output .= self::$_monthTable[$date['mon'] - 1];
0445                     break;
0446 
0447 
0448                 // year formats
0449                 case 'L':  // is leap year ?
0450                     $output .= (self::isYearLeapYear($date['year'])) ? '1' : '0';
0451                     break;
0452 
0453                 case 'o':  // ISO 8601 year number
0454                     $week = $this->weekNumber($date['year'], $date['mon'], $date['mday']);
0455                     if (($week > 50) and ($date['mon'] == 1)) {
0456                         $output .= ($date['year'] - 1);
0457                     } else {
0458                         $output .= $date['year'];
0459                     }
0460                     break;
0461 
0462                 case 'Y':  // year number, 4 digits
0463                     $output .= $date['year'];
0464                     break;
0465 
0466                 case 'y':  // year number, 2 digits
0467                     $output .= substr($date['year'], strlen($date['year']) - 2, 2);
0468                     break;
0469 
0470 
0471                 // time formats
0472                 case 'a':  // lower case am/pm
0473                     $output .= (($date['hours'] >= 12) ? 'pm' : 'am');
0474                     break;
0475 
0476                 case 'A':  // upper case am/pm
0477                     $output .= (($date['hours'] >= 12) ? 'PM' : 'AM');
0478                     break;
0479 
0480                 case 'B':  // swatch internet time
0481                     $dayseconds = ($date['hours'] * 3600) + ($date['minutes'] * 60) + $date['seconds'];
0482                     if ($gmt === true) {
0483                         $dayseconds += 3600;
0484                     }
0485                     $output .= (int) (($dayseconds % 86400) / 86.4);
0486                     break;
0487 
0488                 case 'g':  // hours without leading zeros, 12h format
0489                     if ($date['hours'] > 12) {
0490                         $hour = $date['hours'] - 12;
0491                     } else {
0492                         if ($date['hours'] == 0) {
0493                             $hour = '12';
0494                         } else {
0495                             $hour = $date['hours'];
0496                         }
0497                     }
0498                     $output .= $hour;
0499                     break;
0500 
0501                 case 'G':  // hours without leading zeros, 24h format
0502                     $output .= $date['hours'];
0503                     break;
0504 
0505                 case 'h':  // hours with leading zeros, 12h format
0506                     if ($date['hours'] > 12) {
0507                         $hour = $date['hours'] - 12;
0508                     } else {
0509                         if ($date['hours'] == 0) {
0510                             $hour = '12';
0511                         } else {
0512                             $hour = $date['hours'];
0513                         }
0514                     }
0515                     $output .= (($hour < 10) ? '0'.$hour : $hour);
0516                     break;
0517 
0518                 case 'H':  // hours with leading zeros, 24h format
0519                     $output .= (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']);
0520                     break;
0521 
0522                 case 'i':  // minutes with leading zeros
0523                     $output .= (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']);
0524                     break;
0525 
0526                 case 's':  // seconds with leading zeros
0527                     $output .= (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']);
0528                     break;
0529 
0530 
0531                 // timezone formats
0532                 case 'e':  // timezone identifier
0533                     if ($gmt === true) {
0534                         $output .= gmdate('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
0535                                                       $date['mon'], $date['mday'], 2000));
0536                     } else {
0537                         $output .=   date('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
0538                                                       $date['mon'], $date['mday'], 2000));
0539                     }
0540                     break;
0541 
0542                 case 'I':  // daylight saving time or not
0543                     if ($gmt === true) {
0544                         $output .= gmdate('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
0545                                                       $date['mon'], $date['mday'], 2000));
0546                     } else {
0547                         $output .=   date('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
0548                                                       $date['mon'], $date['mday'], 2000));
0549                     }
0550                     break;
0551 
0552                 case 'O':  // difference to GMT in hours
0553                     $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
0554                     $output .= sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
0555                     break;
0556 
0557                 case 'P':  // difference to GMT with colon
0558                     $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
0559                     $gmtstr = sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
0560                     $output = $output . substr($gmtstr, 0, 3) . ':' . substr($gmtstr, 3);
0561                     break;
0562 
0563                 case 'T':  // timezone settings
0564                     if ($gmt === true) {
0565                         $output .= gmdate('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
0566                                                       $date['mon'], $date['mday'], 2000));
0567                     } else {
0568                         $output .=   date('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
0569                                                       $date['mon'], $date['mday'], 2000));
0570                     }
0571                     break;
0572 
0573                 case 'Z':  // timezone offset in seconds
0574                     $output .= ($gmt === true) ? 0 : -$this->getGmtOffset();
0575                     break;
0576 
0577 
0578                 // complete time formats
0579                 case 'c':  // ISO 8601 date format
0580                     $difference = $this->getGmtOffset();
0581                     $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
0582                     $difference = substr($difference, 0, 3) . ':' . substr($difference, 3);
0583                     $output .= $date['year'] . '-'
0584                              . (($date['mon']     < 10) ? '0' . $date['mon']     : $date['mon'])     . '-'
0585                              . (($date['mday']    < 10) ? '0' . $date['mday']    : $date['mday'])    . 'T'
0586                              . (($date['hours']   < 10) ? '0' . $date['hours']   : $date['hours'])   . ':'
0587                              . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
0588                              . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds'])
0589                              . $difference;
0590                     break;
0591 
0592                 case 'r':  // RFC 2822 date format
0593                     $difference = $this->getGmtOffset();
0594                     $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
0595                     $output .= gmdate('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday']))) . ', '
0596                              . (($date['mday']    < 10) ? '0' . $date['mday']    : $date['mday'])    . ' '
0597                              . date('M', mktime(0, 0, 0, $date['mon'], 2, 1971)) . ' '
0598                              . $date['year'] . ' '
0599                              . (($date['hours']   < 10) ? '0' . $date['hours']   : $date['hours'])   . ':'
0600                              . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
0601                              . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']) . ' '
0602                              . $difference;
0603                     break;
0604 
0605                 case 'U':  // Unix timestamp
0606                     $output .= $origstamp;
0607                     break;
0608 
0609 
0610                 // special formats
0611                 case "\\":  // next letter to print with no format
0612                     $i++;
0613                     if ($i < $length) {
0614                         $output .= $format[$i];
0615                     }
0616                     break;
0617 
0618                 default:  // letter is no format so add it direct
0619                     $output .= $format[$i];
0620                     break;
0621             }
0622         }
0623 
0624         return (string) $output;
0625     }
0626 
0627     /**
0628      * Returns the day of week for a Gregorian calendar date.
0629      * 0 = sunday, 6 = saturday
0630      *
0631      * @param  integer  $year
0632      * @param  integer  $month
0633      * @param  integer  $day
0634      * @return integer  dayOfWeek
0635      */
0636     protected static function dayOfWeek($year, $month, $day)
0637     {
0638         if ((1901 < $year) and ($year < 2038)) {
0639             return (int) date('w', mktime(0, 0, 0, $month, $day, $year));
0640         }
0641 
0642         // gregorian correction
0643         $correction = 0;
0644         if (($year < 1582) or (($year == 1582) and (($month < 10) or (($month == 10) && ($day < 15))))) {
0645             $correction = 3;
0646         }
0647 
0648         if ($month > 2) {
0649             $month -= 2;
0650         } else {
0651             $month += 10;
0652             $year--;
0653         }
0654 
0655         $day  = floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4);
0656         $day += floor(($year / 100) / 4) - 2 * floor($year / 100) + 77 + $correction;
0657 
0658         return (int) ($day - 7 * floor($day / 7));
0659     }
0660 
0661     /**
0662      * Internal getDateParts function for handling 64bit timestamps, similar to:
0663      * http://www.php.net/getdate
0664      *
0665      * Returns an array of date parts for $timestamp, relative to 1970/01/01 00:00:00 GMT/UTC.
0666      *
0667      * $fast specifies ALL date parts should be returned (slower)
0668      * Default is false, and excludes $dayofweek, weekday, month and timestamp from parts returned.
0669      *
0670      * @param   mixed    $timestamp
0671      * @param   boolean  $fast   OPTIONAL defaults to fast (false), resulting in fewer date parts
0672      * @return  array
0673      */
0674     protected function getDateParts($timestamp = null, $fast = null)
0675     {
0676 
0677         // actual timestamp
0678         if (!is_numeric($timestamp)) {
0679             return getdate();
0680         }
0681 
0682         // 32bit timestamp
0683         if (abs($timestamp) <= 0x7FFFFFFF) {
0684             return @getdate((int) $timestamp);
0685         }
0686 
0687         if (isset(self::$_cache)) {
0688             $id = strtr('Zend_DateObject_getDateParts_' . $timestamp.'_'.(int)$fast, '-','_');
0689             if ($result = self::$_cache->load($id)) {
0690                 return unserialize($result);
0691             }
0692         }
0693 
0694         $otimestamp = $timestamp;
0695         $numday = 0;
0696         $month = 0;
0697         // gregorian correction
0698         if ($timestamp < -12219321600) {
0699             $timestamp -= 864000;
0700         }
0701 
0702         // timestamp lower 0
0703         if ($timestamp < 0) {
0704             $sec = 0;
0705             $act = 1970;
0706 
0707             // iterate through 10 years table, increasing speed
0708             foreach(self::$_yearTable as $year => $seconds) {
0709                 if ($timestamp >= $seconds) {
0710                     $i = $act;
0711                     break;
0712                 }
0713                 $sec = $seconds;
0714                 $act = $year;
0715             }
0716 
0717             $timestamp -= $sec;
0718             if (!isset($i)) {
0719                 $i = $act;
0720             }
0721 
0722             // iterate the max last 10 years
0723             do {
0724                 --$i;
0725                 $day = $timestamp;
0726 
0727                 $timestamp += 31536000;
0728                 $leapyear = self::isYearLeapYear($i);
0729                 if ($leapyear === true) {
0730                     $timestamp += 86400;
0731                 }
0732 
0733                 if ($timestamp >= 0) {
0734                     $year = $i;
0735                     break;
0736                 }
0737             } while ($timestamp < 0);
0738 
0739             $secondsPerYear = 86400 * ($leapyear ? 366 : 365) + $day;
0740 
0741             $timestamp = $day;
0742             // iterate through months
0743             for ($i = 12; --$i >= 0;) {
0744                 $day = $timestamp;
0745 
0746                 $timestamp += self::$_monthTable[$i] * 86400;
0747                 if (($leapyear === true) and ($i == 1)) {
0748                     $timestamp += 86400;
0749                 }
0750 
0751                 if ($timestamp >= 0) {
0752                     $month  = $i;
0753                     $numday = self::$_monthTable[$i];
0754                     if (($leapyear === true) and ($i == 1)) {
0755                         ++$numday;
0756                     }
0757                     break;
0758                 }
0759             }
0760 
0761             $timestamp  = $day;
0762             $numberdays = $numday + ceil(($timestamp + 1) / 86400);
0763 
0764             $timestamp += ($numday - $numberdays + 1) * 86400;
0765             $hours      = floor($timestamp / 3600);
0766         } else {
0767 
0768             // iterate through years
0769             for ($i = 1970;;$i++) {
0770                 $day = $timestamp;
0771 
0772                 $timestamp -= 31536000;
0773                 $leapyear = self::isYearLeapYear($i);
0774                 if ($leapyear === true) {
0775                     $timestamp -= 86400;
0776                 }
0777 
0778                 if ($timestamp < 0) {
0779                     $year = $i;
0780                     break;
0781                 }
0782             }
0783 
0784             $secondsPerYear = $day;
0785 
0786             $timestamp = $day;
0787             // iterate through months
0788             for ($i = 0; $i <= 11; $i++) {
0789                 $day = $timestamp;
0790                 $timestamp -= self::$_monthTable[$i] * 86400;
0791 
0792                 if (($leapyear === true) and ($i == 1)) {
0793                     $timestamp -= 86400;
0794                 }
0795 
0796                 if ($timestamp < 0) {
0797                     $month  = $i;
0798                     $numday = self::$_monthTable[$i];
0799                     if (($leapyear === true) and ($i == 1)) {
0800                         ++$numday;
0801                     }
0802                     break;
0803                 }
0804             }
0805 
0806             $timestamp  = $day;
0807             $numberdays = ceil(($timestamp + 1) / 86400);
0808             $timestamp  = $timestamp - ($numberdays - 1) * 86400;
0809             $hours = floor($timestamp / 3600);
0810         }
0811 
0812         $timestamp -= $hours * 3600;
0813 
0814         $month  += 1;
0815         $minutes = floor($timestamp / 60);
0816         $seconds = $timestamp - $minutes * 60;
0817 
0818         if ($fast === true) {
0819             $array = array(
0820                 'seconds' => $seconds,
0821                 'minutes' => $minutes,
0822                 'hours'   => $hours,
0823                 'mday'    => $numberdays,
0824                 'mon'     => $month,
0825                 'year'    => $year,
0826                 'yday'    => floor($secondsPerYear / 86400),
0827             );
0828         } else {
0829 
0830             $dayofweek = self::dayOfWeek($year, $month, $numberdays);
0831             $array = array(
0832                     'seconds' => $seconds,
0833                     'minutes' => $minutes,
0834                     'hours'   => $hours,
0835                     'mday'    => $numberdays,
0836                     'wday'    => $dayofweek,
0837                     'mon'     => $month,
0838                     'year'    => $year,
0839                     'yday'    => floor($secondsPerYear / 86400),
0840                     'weekday' => gmdate('l', 86400 * (3 + $dayofweek)),
0841                     'month'   => gmdate('F', mktime(0, 0, 0, $month, 1, 1971)),
0842                     0         => $otimestamp
0843             );
0844         }
0845 
0846         if (isset(self::$_cache)) {
0847             if (self::$_cacheTags) {
0848                 self::$_cache->save( serialize($array), $id, array('Zend_Date'));
0849             } else {
0850                 self::$_cache->save( serialize($array), $id);
0851             }
0852         }
0853 
0854         return $array;
0855     }
0856 
0857     /**
0858      * Internal getWeekNumber function for handling 64bit timestamps
0859      *
0860      * Returns the ISO 8601 week number of a given date
0861      *
0862      * @param  integer  $year
0863      * @param  integer  $month
0864      * @param  integer  $day
0865      * @return integer
0866      */
0867     protected function weekNumber($year, $month, $day)
0868     {
0869         if ((1901 < $year) and ($year < 2038)) {
0870             return (int) date('W', mktime(0, 0, 0, $month, $day, $year));
0871         }
0872 
0873         $dayofweek = self::dayOfWeek($year, $month, $day);
0874         $firstday  = self::dayOfWeek($year, 1, 1);
0875         if (($month == 1) and (($firstday < 1) or ($firstday > 4)) and ($day < 4)) {
0876             $firstday  = self::dayOfWeek($year - 1, 1, 1);
0877             $month     = 12;
0878             $day       = 31;
0879 
0880         } else if (($month == 12) and ((self::dayOfWeek($year + 1, 1, 1) < 5) and
0881                    (self::dayOfWeek($year + 1, 1, 1) > 0))) {
0882             return 1;
0883         }
0884 
0885         return intval (((self::dayOfWeek($year, 1, 1) < 5) and (self::dayOfWeek($year, 1, 1) > 0)) +
0886                4 * ($month - 1) + (2 * ($month - 1) + ($day - 1) + $firstday - $dayofweek + 6) * 36 / 256);
0887     }
0888 
0889     /**
0890      * Internal _range function
0891      * Sets the value $a to be in the range of [0, $b]
0892      *
0893      * @param float $a - value to correct
0894      * @param float $b - maximum range to set
0895      */
0896     private function _range($a, $b) {
0897         while ($a < 0) {
0898             $a += $b;
0899         }
0900         while ($a >= $b) {
0901             $a -= $b;
0902         }
0903         return $a;
0904     }
0905 
0906     /**
0907      * Calculates the sunrise or sunset based on a location
0908      *
0909      * @param  array  $location  Location for calculation MUST include 'latitude', 'longitude', 'horizon'
0910      * @param  bool   $horizon   true: sunrise; false: sunset
0911      * @return mixed  - false: midnight sun, integer:
0912      */
0913     protected function calcSun($location, $horizon, $rise = false)
0914     {
0915         // timestamp within 32bit
0916         if (abs($this->_unixTimestamp) <= 0x7FFFFFFF) {
0917             if ($rise === false) {
0918                 return date_sunset($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
0919                                    $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
0920             }
0921             return date_sunrise($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
0922                                 $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
0923         }
0924 
0925         // self calculation - timestamp bigger than 32bit
0926         // fix circle values
0927         $quarterCircle      = 0.5 * M_PI;
0928         $halfCircle         =       M_PI;
0929         $threeQuarterCircle = 1.5 * M_PI;
0930         $fullCircle         = 2   * M_PI;
0931 
0932         // radiant conversion for coordinates
0933         $radLatitude  = $location['latitude']   * $halfCircle / 180;
0934         $radLongitude = $location['longitude']  * $halfCircle / 180;
0935 
0936         // get solar coordinates
0937         $tmpRise       = $rise ? $quarterCircle : $threeQuarterCircle;
0938         $radDay        = $this->date('z',$this->_unixTimestamp) + ($tmpRise - $radLongitude) / $fullCircle;
0939 
0940         // solar anomoly and longitude
0941         $solAnomoly    = $radDay * 0.017202 - 0.0574039;
0942         $solLongitude  = $solAnomoly + 0.0334405 * sin($solAnomoly);
0943         $solLongitude += 4.93289 + 3.49066E-4 * sin(2 * $solAnomoly);
0944 
0945         // get quadrant
0946         $solLongitude = $this->_range($solLongitude, $fullCircle);
0947 
0948         if (($solLongitude / $quarterCircle) - intval($solLongitude / $quarterCircle) == 0) {
0949             $solLongitude += 4.84814E-6;
0950         }
0951 
0952         // solar ascension
0953         $solAscension = sin($solLongitude) / cos($solLongitude);
0954         $solAscension = atan2(0.91746 * $solAscension, 1);
0955 
0956         // adjust quadrant
0957         if ($solLongitude > $threeQuarterCircle) {
0958             $solAscension += $fullCircle;
0959         } else if ($solLongitude > $quarterCircle) {
0960             $solAscension += $halfCircle;
0961         }
0962 
0963         // solar declination
0964         $solDeclination  = 0.39782 * sin($solLongitude);
0965         $solDeclination /=  sqrt(-$solDeclination * $solDeclination + 1);
0966         $solDeclination  = atan2($solDeclination, 1);
0967 
0968         $solHorizon = $horizon - sin($solDeclination) * sin($radLatitude);
0969         $solHorizon /= cos($solDeclination) * cos($radLatitude);
0970 
0971         // midnight sun, always night
0972         if (abs($solHorizon) > 1) {
0973             return false;
0974         }
0975 
0976         $solHorizon /= sqrt(-$solHorizon * $solHorizon + 1);
0977         $solHorizon  = $quarterCircle - atan2($solHorizon, 1);
0978 
0979         if ($rise) {
0980             $solHorizon = $fullCircle - $solHorizon;
0981         }
0982 
0983         // time calculation
0984         $localTime     = $solHorizon + $solAscension - 0.0172028 * $radDay - 1.73364;
0985         $universalTime = $localTime - $radLongitude;
0986 
0987         // determinate quadrant
0988         $universalTime = $this->_range($universalTime, $fullCircle);
0989 
0990         // radiant to hours
0991         $universalTime *= 24 / $fullCircle;
0992 
0993         // convert to time
0994         $hour = intval($universalTime);
0995         $universalTime    = ($universalTime - $hour) * 60;
0996         $min  = intval($universalTime);
0997         $universalTime    = ($universalTime - $min) * 60;
0998         $sec  = intval($universalTime);
0999 
1000         return $this->mktime($hour, $min, $sec, $this->date('m', $this->_unixTimestamp),
1001                              $this->date('j', $this->_unixTimestamp), $this->date('Y', $this->_unixTimestamp),
1002                              -1, true);
1003     }
1004 
1005     /**
1006      * Sets a new timezone for calculation of $this object's gmt offset.
1007      * For a list of supported timezones look here: http://php.net/timezones
1008      * If no timezone can be detected or the given timezone is wrong UTC will be set.
1009      *
1010      * @param  string  $zone      OPTIONAL timezone for date calculation; defaults to date_default_timezone_get()
1011      * @return Zend_Date_DateObject Provides fluent interface
1012      * @throws Zend_Date_Exception
1013      */
1014     public function setTimezone($zone = null)
1015     {
1016         $oldzone = @date_default_timezone_get();
1017         if ($zone === null) {
1018             $zone = $oldzone;
1019         }
1020 
1021         // throw an error on false input, but only if the new date extension is available
1022         if (function_exists('timezone_open')) {
1023             if (!@timezone_open($zone)) {
1024                 // require_once 'Zend/Date/Exception.php';
1025                 throw new Zend_Date_Exception("timezone ($zone) is not a known timezone", 0, null, $zone);
1026             }
1027         }
1028         // this can generate an error if the date extension is not available and a false timezone is given
1029         $result = @date_default_timezone_set($zone);
1030         if ($result === true) {
1031             $this->_offset   = mktime(0, 0, 0, 1, 2, 1970) - gmmktime(0, 0, 0, 1, 2, 1970);
1032             $this->_timezone = $zone;
1033         }
1034         date_default_timezone_set($oldzone);
1035 
1036         if (($zone == 'UTC') or ($zone == 'GMT')) {
1037             $this->_dst = false;
1038         } else {
1039             $this->_dst = true;
1040         }
1041 
1042         return $this;
1043     }
1044 
1045     /**
1046      * Return the timezone of $this object.
1047      * The timezone is initially set when the object is instantiated.
1048      *
1049      * @return  string  actual set timezone string
1050      */
1051     public function getTimezone()
1052     {
1053         return $this->_timezone;
1054     }
1055 
1056     /**
1057      * Return the offset to GMT of $this object's timezone.
1058      * The offset to GMT is initially set when the object is instantiated using the currently,
1059      * in effect, default timezone for PHP functions.
1060      *
1061      * @return  integer  seconds difference between GMT timezone and timezone when object was instantiated
1062      */
1063     public function getGmtOffset()
1064     {
1065         $date   = $this->getDateParts($this->getUnixTimestamp(), true);
1066         $zone   = @date_default_timezone_get();
1067         $result = @date_default_timezone_set($this->_timezone);
1068         if ($result === true) {
1069             $offset = $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
1070                                     $date['mon'], $date['mday'], $date['year'], false)
1071                     - $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
1072                                     $date['mon'], $date['mday'], $date['year'], true);
1073         }
1074         date_default_timezone_set($zone);
1075 
1076         return $offset;
1077     }
1078 
1079     /**
1080      * Internal method to check if the given cache supports tags
1081      *
1082      * @param Zend_Cache $cache
1083      */
1084     protected static function _getTagSupportForCache()
1085     {
1086         $backend = self::$_cache->getBackend();
1087         if ($backend instanceof Zend_Cache_Backend_ExtendedInterface) {
1088             $cacheOptions = $backend->getCapabilities();
1089             self::$_cacheTags = $cacheOptions['tags'];
1090         } else {
1091             self::$_cacheTags = false;
1092         }
1093 
1094         return self::$_cacheTags;
1095     }
1096 }