web/lib/Zend/Date.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category  Zend
       
    16  * @package   Zend_Date
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license   http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version   $Id: Date.php 22713 2010-07-29 11:41:56Z thomas $
       
    20  */
       
    21 
       
    22 /**
       
    23  * Include needed Date classes
       
    24  */
       
    25 require_once 'Zend/Date/DateObject.php';
       
    26 require_once 'Zend/Locale.php';
       
    27 require_once 'Zend/Locale/Format.php';
       
    28 require_once 'Zend/Locale/Math.php';
       
    29 
       
    30 /**
       
    31  * @category  Zend
       
    32  * @package   Zend_Date
       
    33  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    34  * @license   http://framework.zend.com/license/new-bsd     New BSD License
       
    35  */
       
    36 class Zend_Date extends Zend_Date_DateObject
       
    37 {
       
    38     private $_locale  = null;
       
    39 
       
    40     // Fractional second variables
       
    41     private $_fractional = 0;
       
    42     private $_precision  = 3;
       
    43 
       
    44     private static $_options = array(
       
    45         'format_type'  => 'iso',      // format for date strings 'iso' or 'php'
       
    46         'fix_dst'      => true,       // fix dst on summer/winter time change
       
    47         'extend_month' => false,      // false - addMonth like SQL, true like excel
       
    48         'cache'        => null,       // cache to set
       
    49         'timesync'     => null        // timesync server to set
       
    50     );
       
    51 
       
    52     // Class wide Date Constants
       
    53     const DAY               = 'dd';
       
    54     const DAY_SHORT         = 'd';
       
    55     const DAY_SUFFIX        = 'SS';
       
    56     const DAY_OF_YEAR       = 'D';
       
    57     const WEEKDAY           = 'EEEE';
       
    58     const WEEKDAY_SHORT     = 'EEE';
       
    59     const WEEKDAY_NARROW    = 'E';
       
    60     const WEEKDAY_NAME      = 'EE';
       
    61     const WEEKDAY_8601      = 'eee';
       
    62     const WEEKDAY_DIGIT     = 'e';
       
    63     const WEEK              = 'ww';
       
    64     const MONTH             = 'MM';
       
    65     const MONTH_SHORT       = 'M';
       
    66     const MONTH_DAYS        = 'ddd';
       
    67     const MONTH_NAME        = 'MMMM';
       
    68     const MONTH_NAME_SHORT  = 'MMM';
       
    69     const MONTH_NAME_NARROW = 'MMMMM';
       
    70     const YEAR              = 'y';
       
    71     const YEAR_SHORT        = 'yy';
       
    72     const YEAR_8601         = 'Y';
       
    73     const YEAR_SHORT_8601   = 'YY';
       
    74     const LEAPYEAR          = 'l';
       
    75     const MERIDIEM          = 'a';
       
    76     const SWATCH            = 'B';
       
    77     const HOUR              = 'HH';
       
    78     const HOUR_SHORT        = 'H';
       
    79     const HOUR_AM           = 'hh';
       
    80     const HOUR_SHORT_AM     = 'h';
       
    81     const MINUTE            = 'mm';
       
    82     const MINUTE_SHORT      = 'm';
       
    83     const SECOND            = 'ss';
       
    84     const SECOND_SHORT      = 's';
       
    85     const MILLISECOND       = 'S';
       
    86     const TIMEZONE_NAME     = 'zzzz';
       
    87     const DAYLIGHT          = 'I';
       
    88     const GMT_DIFF          = 'Z';
       
    89     const GMT_DIFF_SEP      = 'ZZZZ';
       
    90     const TIMEZONE          = 'z';
       
    91     const TIMEZONE_SECS     = 'X';
       
    92     const ISO_8601          = 'c';
       
    93     const RFC_2822          = 'r';
       
    94     const TIMESTAMP         = 'U';
       
    95     const ERA               = 'G';
       
    96     const ERA_NAME          = 'GGGG';
       
    97     const ERA_NARROW        = 'GGGGG';
       
    98     const DATES             = 'F';
       
    99     const DATE_FULL         = 'FFFFF';
       
   100     const DATE_LONG         = 'FFFF';
       
   101     const DATE_MEDIUM       = 'FFF';
       
   102     const DATE_SHORT        = 'FF';
       
   103     const TIMES             = 'WW';
       
   104     const TIME_FULL         = 'TTTTT';
       
   105     const TIME_LONG         = 'TTTT';
       
   106     const TIME_MEDIUM       = 'TTT';
       
   107     const TIME_SHORT        = 'TT';
       
   108     const DATETIME          = 'K';
       
   109     const DATETIME_FULL     = 'KKKKK';
       
   110     const DATETIME_LONG     = 'KKKK';
       
   111     const DATETIME_MEDIUM   = 'KKK';
       
   112     const DATETIME_SHORT    = 'KK';
       
   113     const ATOM              = 'OOO';
       
   114     const COOKIE            = 'CCC';
       
   115     const RFC_822           = 'R';
       
   116     const RFC_850           = 'RR';
       
   117     const RFC_1036          = 'RRR';
       
   118     const RFC_1123          = 'RRRR';
       
   119     const RFC_3339          = 'RRRRR';
       
   120     const RSS               = 'SSS';
       
   121     const W3C               = 'WWW';
       
   122 
       
   123     /**
       
   124      * Generates the standard date object, could be a unix timestamp, localized date,
       
   125      * string, integer, array and so on. Also parts of dates or time are supported
       
   126      * Always set the default timezone: http://php.net/date_default_timezone_set
       
   127      * For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
       
   128      * For detailed instructions please look in the docu.
       
   129      *
       
   130      * @param  string|integer|Zend_Date|array  $date    OPTIONAL Date value or value of date part to set
       
   131      *                                                 ,depending on $part. If null the actual time is set
       
   132      * @param  string                          $part    OPTIONAL Defines the input format of $date
       
   133      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
   134      * @return Zend_Date
       
   135      * @throws Zend_Date_Exception
       
   136      */
       
   137     public function __construct($date = null, $part = null, $locale = null)
       
   138     {
       
   139         if (is_object($date) and !($date instanceof Zend_TimeSync_Protocol) and
       
   140             !($date instanceof Zend_Date)) {
       
   141             if ($locale instanceof Zend_Locale) {
       
   142                 $locale = $date;
       
   143                 $date   = null;
       
   144                 $part   = null;
       
   145             } else {
       
   146                 $date = (string) $date;
       
   147             }
       
   148         }
       
   149 
       
   150         if (($date !== null) and !is_array($date) and !($date instanceof Zend_TimeSync_Protocol) and
       
   151             !($date instanceof Zend_Date) and !defined($date) and Zend_Locale::isLocale($date, true, false)) {
       
   152             $locale = $date;
       
   153             $date   = null;
       
   154             $part   = null;
       
   155         } else if (($part !== null) and !defined($part) and Zend_Locale::isLocale($part, true, false)) {
       
   156             $locale = $part;
       
   157             $part   = null;
       
   158         }
       
   159 
       
   160         $this->setLocale($locale);
       
   161         if (is_string($date) && ($part === null) && (strlen($date) <= 5)) {
       
   162             $part = $date;
       
   163             $date = null;
       
   164         }
       
   165 
       
   166         if ($date === null) {
       
   167             if ($part === null) {
       
   168                 $date = time();
       
   169             } else if ($part !== self::TIMESTAMP) {
       
   170                 $date = self::now($locale);
       
   171                 $date = $date->get($part);
       
   172             }
       
   173         }
       
   174 
       
   175         if ($date instanceof Zend_TimeSync_Protocol) {
       
   176             $date = $date->getInfo();
       
   177             $date = $this->_getTime($date['offset']);
       
   178             $part = null;
       
   179         } else if (parent::$_defaultOffset != 0) {
       
   180             $date = $this->_getTime(parent::$_defaultOffset);
       
   181         }
       
   182 
       
   183         // set the timezone and offset for $this
       
   184         $zone = @date_default_timezone_get();
       
   185         $this->setTimezone($zone);
       
   186 
       
   187         // try to get timezone from date-string
       
   188         if (!is_int($date)) {
       
   189             $zone = $this->getTimezoneFromString($date);
       
   190             $this->setTimezone($zone);
       
   191         }
       
   192 
       
   193         // set datepart
       
   194         if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
       
   195             // switch off dst handling for value setting
       
   196             $this->setUnixTimestamp($this->getGmtOffset());
       
   197             $this->set($date, $part, $this->_locale);
       
   198 
       
   199             // DST fix
       
   200             if (is_array($date) === true) {
       
   201                 if (!isset($date['hour'])) {
       
   202                     $date['hour'] = 0;
       
   203                 }
       
   204 
       
   205                 $hour = $this->toString('H', 'iso', true);
       
   206                 $hour = $date['hour'] - $hour;
       
   207                 switch ($hour) {
       
   208                     case 1 :
       
   209                     case -23 :
       
   210                         $this->addTimestamp(3600);
       
   211                         break;
       
   212                     case -1 :
       
   213                     case 23 :
       
   214                         $this->subTimestamp(3600);
       
   215                         break;
       
   216                     case 2 :
       
   217                     case -22 :
       
   218                         $this->addTimestamp(7200);
       
   219                         break;
       
   220                     case -2 :
       
   221                     case 22 :
       
   222                         $this->subTimestamp(7200);
       
   223                         break;
       
   224                 }
       
   225             }
       
   226         } else {
       
   227             $this->setUnixTimestamp($date);
       
   228         }
       
   229     }
       
   230 
       
   231     /**
       
   232      * Sets class wide options, if no option was given, the actual set options will be returned
       
   233      *
       
   234      * @param  array  $options  Options to set
       
   235      * @throws Zend_Date_Exception
       
   236      * @return Options array if no option was given
       
   237      */
       
   238     public static function setOptions(array $options = array())
       
   239     {
       
   240         if (empty($options)) {
       
   241             return self::$_options;
       
   242         }
       
   243 
       
   244         foreach ($options as $name => $value) {
       
   245             $name  = strtolower($name);
       
   246 
       
   247             if (array_key_exists($name, self::$_options)) {
       
   248                 switch($name) {
       
   249                     case 'format_type' :
       
   250                         if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
       
   251                             require_once 'Zend/Date/Exception.php';
       
   252                             throw new Zend_Date_Exception("Unknown format type ($value) for dates, only 'iso' and 'php' supported", 0, null, $value);
       
   253                         }
       
   254                         break;
       
   255                     case 'fix_dst' :
       
   256                         if (!is_bool($value)) {
       
   257                             require_once 'Zend/Date/Exception.php';
       
   258                             throw new Zend_Date_Exception("'fix_dst' has to be boolean", 0, null, $value);
       
   259                         }
       
   260                         break;
       
   261                     case 'extend_month' :
       
   262                         if (!is_bool($value)) {
       
   263                             require_once 'Zend/Date/Exception.php';
       
   264                             throw new Zend_Date_Exception("'extend_month' has to be boolean", 0, null, $value);
       
   265                         }
       
   266                         break;
       
   267                     case 'cache' :
       
   268                         if ($value === null) {
       
   269                             parent::$_cache = null;
       
   270                         } else {
       
   271                             if (!$value instanceof Zend_Cache_Core) {
       
   272                                 require_once 'Zend/Date/Exception.php';
       
   273                                 throw new Zend_Date_Exception("Instance of Zend_Cache expected");
       
   274                             }
       
   275 
       
   276                             parent::$_cache = $value;
       
   277                             parent::$_cacheTags = Zend_Date_DateObject::_getTagSupportForCache();
       
   278                             Zend_Locale_Data::setCache($value);
       
   279                         }
       
   280                         break;
       
   281                     case 'timesync' :
       
   282                         if ($value === null) {
       
   283                             parent::$_defaultOffset = 0;
       
   284                         } else {
       
   285                             if (!$value instanceof Zend_TimeSync_Protocol) {
       
   286                                 require_once 'Zend/Date/Exception.php';
       
   287                                 throw new Zend_Date_Exception("Instance of Zend_TimeSync expected");
       
   288                             }
       
   289 
       
   290                             $date = $value->getInfo();
       
   291                             parent::$_defaultOffset = $date['offset'];
       
   292                         }
       
   293                         break;
       
   294                 }
       
   295                 self::$_options[$name] = $value;
       
   296             }
       
   297             else {
       
   298                 require_once 'Zend/Date/Exception.php';
       
   299                 throw new Zend_Date_Exception("Unknown option: $name = $value");
       
   300             }
       
   301         }
       
   302     }
       
   303 
       
   304     /**
       
   305      * Returns this object's internal UNIX timestamp (equivalent to Zend_Date::TIMESTAMP).
       
   306      * If the timestamp is too large for integers, then the return value will be a string.
       
   307      * This function does not return the timestamp as an object.
       
   308      * Use clone() or copyPart() instead.
       
   309      *
       
   310      * @return integer|string  UNIX timestamp
       
   311      */
       
   312     public function getTimestamp()
       
   313     {
       
   314         return $this->getUnixTimestamp();
       
   315     }
       
   316 
       
   317     /**
       
   318      * Returns the calculated timestamp
       
   319      * HINT: timestamps are always GMT
       
   320      *
       
   321      * @param  string                          $calc    Type of calculation to make
       
   322      * @param  string|integer|array|Zend_Date  $stamp   Timestamp to calculate, when null the actual timestamp is calculated
       
   323      * @return Zend_Date|integer
       
   324      * @throws Zend_Date_Exception
       
   325      */
       
   326     private function _timestamp($calc, $stamp)
       
   327     {
       
   328         if ($stamp instanceof Zend_Date) {
       
   329             // extract timestamp from object
       
   330             $stamp = $stamp->getTimestamp();
       
   331         }
       
   332 
       
   333         if (is_array($stamp)) {
       
   334             if (isset($stamp['timestamp']) === true) {
       
   335                 $stamp = $stamp['timestamp'];
       
   336             } else {
       
   337                 require_once 'Zend/Date/Exception.php';
       
   338                 throw new Zend_Date_Exception('no timestamp given in array');
       
   339             }
       
   340         }
       
   341 
       
   342         if ($calc === 'set') {
       
   343             $return = $this->setUnixTimestamp($stamp);
       
   344         } else {
       
   345             $return = $this->_calcdetail($calc, $stamp, self::TIMESTAMP, null);
       
   346         }
       
   347         if ($calc != 'cmp') {
       
   348             return $this;
       
   349         }
       
   350         return $return;
       
   351     }
       
   352 
       
   353     /**
       
   354      * Sets a new timestamp
       
   355      *
       
   356      * @param  integer|string|array|Zend_Date  $timestamp  Timestamp to set
       
   357      * @return Zend_Date Provides fluid interface
       
   358      * @throws Zend_Date_Exception
       
   359      */
       
   360     public function setTimestamp($timestamp)
       
   361     {
       
   362         return $this->_timestamp('set', $timestamp);
       
   363     }
       
   364 
       
   365     /**
       
   366      * Adds a timestamp
       
   367      *
       
   368      * @param  integer|string|array|Zend_Date  $timestamp  Timestamp to add
       
   369      * @return Zend_Date Provides fluid interface
       
   370      * @throws Zend_Date_Exception
       
   371      */
       
   372     public function addTimestamp($timestamp)
       
   373     {
       
   374         return $this->_timestamp('add', $timestamp);
       
   375     }
       
   376 
       
   377     /**
       
   378      * Subtracts a timestamp
       
   379      *
       
   380      * @param  integer|string|array|Zend_Date  $timestamp  Timestamp to sub
       
   381      * @return Zend_Date Provides fluid interface
       
   382      * @throws Zend_Date_Exception
       
   383      */
       
   384     public function subTimestamp($timestamp)
       
   385     {
       
   386         return $this->_timestamp('sub', $timestamp);
       
   387     }
       
   388 
       
   389     /**
       
   390      * Compares two timestamps, returning the difference as integer
       
   391      *
       
   392      * @param  integer|string|array|Zend_Date  $timestamp  Timestamp to compare
       
   393      * @return integer  0 = equal, 1 = later, -1 = earlier
       
   394      * @throws Zend_Date_Exception
       
   395      */
       
   396     public function compareTimestamp($timestamp)
       
   397     {
       
   398         return $this->_timestamp('cmp', $timestamp);
       
   399     }
       
   400 
       
   401     /**
       
   402      * Returns a string representation of the object
       
   403      * Supported format tokens are:
       
   404      * G - era, y - year, Y - ISO year, M - month, w - week of year, D - day of year, d - day of month
       
   405      * E - day of week, e - number of weekday (1-7), h - hour 1-12, H - hour 0-23, m - minute, s - second
       
   406      * A - milliseconds of day, z - timezone, Z - timezone offset, S - fractional second, a - period of day
       
   407      *
       
   408      * Additionally format tokens but non ISO conform are:
       
   409      * SS - day suffix, eee - php number of weekday(0-6), ddd - number of days per month
       
   410      * l - Leap year, B - swatch internet time, I - daylight saving time, X - timezone offset in seconds
       
   411      * r - RFC2822 format, U - unix timestamp
       
   412      *
       
   413      * Not supported ISO tokens are
       
   414      * u - extended year, Q - quarter, q - quarter, L - stand alone month, W - week of month
       
   415      * F - day of week of month, g - modified julian, c - stand alone weekday, k - hour 0-11, K - hour 1-24
       
   416      * v - wall zone
       
   417      *
       
   418      * @param  string              $format  OPTIONAL Rule for formatting output. If null the default date format is used
       
   419      * @param  string              $type    OPTIONAL Type for the format string which overrides the standard setting
       
   420      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
   421      * @return string
       
   422      */
       
   423     public function toString($format = null, $type = null, $locale = null)
       
   424     {
       
   425         if (is_object($format)) {
       
   426             if ($format instanceof Zend_Locale) {
       
   427                 $locale = $format;
       
   428                 $format = null;
       
   429             } else {
       
   430                 $format = (string) $format;
       
   431             }
       
   432         }
       
   433 
       
   434         if (is_object($type)) {
       
   435             if ($type instanceof Zend_Locale) {
       
   436                 $locale = $type;
       
   437                 $type   = null;
       
   438             } else {
       
   439                 $type = (string) $type;
       
   440             }
       
   441         }
       
   442 
       
   443         if (($format !== null) && !defined($format)
       
   444             && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
       
   445             && Zend_Locale::isLocale($format, null, false)) {
       
   446             $locale = $format;
       
   447             $format = null;
       
   448         }
       
   449 
       
   450         if (($type !== null) and ($type != 'php') and ($type != 'iso') and
       
   451             Zend_Locale::isLocale($type, null, false)) {
       
   452             $locale = $type;
       
   453             $type = null;
       
   454         }
       
   455 
       
   456         if ($locale === null) {
       
   457             $locale = $this->getLocale();
       
   458         }
       
   459 
       
   460         if ($format === null) {
       
   461             $format = Zend_Locale_Format::getDateFormat($locale) . ' ' . Zend_Locale_Format::getTimeFormat($locale);
       
   462         } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
       
   463             $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
       
   464         }
       
   465 
       
   466         return $this->date($this->_toToken($format, $locale), $this->getUnixTimestamp(), false);
       
   467     }
       
   468 
       
   469     /**
       
   470      * Returns a string representation of the date which is equal with the timestamp
       
   471      *
       
   472      * @return string
       
   473      */
       
   474     public function __toString()
       
   475     {
       
   476         return $this->toString(null, $this->_locale);
       
   477     }
       
   478 
       
   479     /**
       
   480      * Returns a integer representation of the object
       
   481      * But returns false when the given part is no value f.e. Month-Name
       
   482      *
       
   483      * @param  string|integer|Zend_Date  $part  OPTIONAL Defines the date or datepart to return as integer
       
   484      * @return integer|false
       
   485      */
       
   486     public function toValue($part = null)
       
   487     {
       
   488         $result = $this->get($part);
       
   489         if (is_numeric($result)) {
       
   490           return intval("$result");
       
   491         } else {
       
   492           return false;
       
   493         }
       
   494     }
       
   495 
       
   496     /**
       
   497      * Returns an array representation of the object
       
   498      *
       
   499      * @return array
       
   500      */
       
   501     public function toArray()
       
   502     {
       
   503         return array('day'       => $this->toString(self::DAY_SHORT, 'iso'),
       
   504                      'month'     => $this->toString(self::MONTH_SHORT, 'iso'),
       
   505                      'year'      => $this->toString(self::YEAR, 'iso'),
       
   506                      'hour'      => $this->toString(self::HOUR_SHORT, 'iso'),
       
   507                      'minute'    => $this->toString(self::MINUTE_SHORT, 'iso'),
       
   508                      'second'    => $this->toString(self::SECOND_SHORT, 'iso'),
       
   509                      'timezone'  => $this->toString(self::TIMEZONE, 'iso'),
       
   510                      'timestamp' => $this->toString(self::TIMESTAMP, 'iso'),
       
   511                      'weekday'   => $this->toString(self::WEEKDAY_8601, 'iso'),
       
   512                      'dayofyear' => $this->toString(self::DAY_OF_YEAR, 'iso'),
       
   513                      'week'      => $this->toString(self::WEEK, 'iso'),
       
   514                      'gmtsecs'   => $this->toString(self::TIMEZONE_SECS, 'iso'));
       
   515     }
       
   516 
       
   517     /**
       
   518      * Returns a representation of a date or datepart
       
   519      * This could be for example a localized monthname, the time without date,
       
   520      * the era or only the fractional seconds. There are about 50 different supported date parts.
       
   521      * For a complete list of supported datepart values look into the docu
       
   522      *
       
   523      * @param  string              $part    OPTIONAL Part of the date to return, if null the timestamp is returned
       
   524      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
   525      * @return string  date or datepart
       
   526      */
       
   527     public function get($part = null, $locale = null)
       
   528     {
       
   529         if ($locale === null) {
       
   530             $locale = $this->getLocale();
       
   531         }
       
   532 
       
   533         if (($part !== null) && !defined($part)
       
   534             && ($part != 'ee') && ($part != 'ss') && ($part != 'GG') && ($part != 'MM') && ($part != 'EE') && ($part != 'TT')
       
   535             && Zend_Locale::isLocale($part, null, false)) {
       
   536             $locale = $part;
       
   537             $part = null;
       
   538         }
       
   539 
       
   540         if ($part === null) {
       
   541             $part = self::TIMESTAMP;
       
   542         } else if (self::$_options['format_type'] == 'php') {
       
   543             $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
       
   544         }
       
   545 
       
   546         return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);
       
   547     }
       
   548 
       
   549     /**
       
   550      * Internal method to apply tokens
       
   551      *
       
   552      * @param string $part
       
   553      * @param string $locale
       
   554      * @return string
       
   555      */
       
   556     private function _toToken($part, $locale) {
       
   557         // get format tokens
       
   558         $comment = false;
       
   559         $format  = '';
       
   560         $orig    = '';
       
   561         for ($i = 0; isset($part[$i]); ++$i) {
       
   562             if ($part[$i] == "'") {
       
   563                 $comment = $comment ? false : true;
       
   564                 if (isset($part[$i+1]) && ($part[$i+1] == "'")) {
       
   565                     $comment = $comment ? false : true;
       
   566                     $format .= "\\'";
       
   567                     ++$i;
       
   568                 }
       
   569 
       
   570                 $orig = '';
       
   571                 continue;
       
   572             }
       
   573 
       
   574             if ($comment) {
       
   575                 $format .= '\\' . $part[$i];
       
   576                 $orig = '';
       
   577             } else {
       
   578                 $orig .= $part[$i];
       
   579                 if (!isset($part[$i+1]) || (isset($orig[0]) && ($orig[0] != $part[$i+1]))) {
       
   580                     $format .= $this->_parseIsoToDate($orig, $locale);
       
   581                     $orig  = '';
       
   582                 }
       
   583             }
       
   584         }
       
   585 
       
   586         return $format;
       
   587     }
       
   588 
       
   589     /**
       
   590      * Internal parsing method
       
   591      *
       
   592      * @param string $token
       
   593      * @param string $locale
       
   594      * @return string
       
   595      */
       
   596     private function _parseIsoToDate($token, $locale) {
       
   597         switch($token) {
       
   598             case self::DAY :
       
   599                 return 'd';
       
   600                 break;
       
   601 
       
   602             case self::WEEKDAY_SHORT :
       
   603                 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
       
   604                 $day     = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
       
   605                 return $this->_toComment(iconv_substr($day, 0, 3, 'UTF-8'));
       
   606                 break;
       
   607 
       
   608             case self::DAY_SHORT :
       
   609                 return 'j';
       
   610                 break;
       
   611 
       
   612             case self::WEEKDAY :
       
   613                 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
       
   614                 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)));
       
   615                 break;
       
   616 
       
   617             case self::WEEKDAY_8601 :
       
   618                 return 'N';
       
   619                 break;
       
   620 
       
   621             case 'ee' :
       
   622                 return $this->_toComment(str_pad($this->date('N', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
       
   623                 break;
       
   624 
       
   625             case self::DAY_SUFFIX :
       
   626                 return 'S';
       
   627                 break;
       
   628 
       
   629             case self::WEEKDAY_DIGIT :
       
   630                 return 'w';
       
   631                 break;
       
   632 
       
   633             case self::DAY_OF_YEAR :
       
   634                 return 'z';
       
   635                 break;
       
   636 
       
   637             case 'DDD' :
       
   638                 return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 3, '0', STR_PAD_LEFT));
       
   639                 break;
       
   640 
       
   641             case 'DD' :
       
   642                 return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
       
   643                 break;
       
   644 
       
   645             case self::WEEKDAY_NARROW :
       
   646             case 'EEEEE' :
       
   647                 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
       
   648                 $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
       
   649                 return $this->_toComment(iconv_substr($day, 0, 1, 'UTF-8'));
       
   650                 break;
       
   651 
       
   652             case self::WEEKDAY_NAME :
       
   653                 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
       
   654                 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)));
       
   655                 break;
       
   656 
       
   657             case 'w' :
       
   658                 $week = $this->date('W', $this->getUnixTimestamp(), false);
       
   659                 return $this->_toComment(($week[0] == '0') ? $week[1] : $week);
       
   660                 break;
       
   661 
       
   662             case self::WEEK :
       
   663                 return 'W';
       
   664                 break;
       
   665 
       
   666             case self::MONTH_NAME :
       
   667                 $month = $this->date('n', $this->getUnixTimestamp(), false);
       
   668                 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)));
       
   669                 break;
       
   670 
       
   671             case self::MONTH :
       
   672                 return 'm';
       
   673                 break;
       
   674 
       
   675             case self::MONTH_NAME_SHORT :
       
   676                 $month = $this->date('n', $this->getUnixTimestamp(), false);
       
   677                 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)));
       
   678                 break;
       
   679 
       
   680             case self::MONTH_SHORT :
       
   681                 return 'n';
       
   682                 break;
       
   683 
       
   684             case self::MONTH_DAYS :
       
   685                 return 't';
       
   686                 break;
       
   687 
       
   688             case self::MONTH_NAME_NARROW :
       
   689                 $month = $this->date('n', $this->getUnixTimestamp(), false);
       
   690                 $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
       
   691                 return $this->_toComment(iconv_substr($mon, 0, 1, 'UTF-8'));
       
   692                 break;
       
   693 
       
   694             case self::LEAPYEAR :
       
   695                 return 'L';
       
   696                 break;
       
   697 
       
   698             case self::YEAR_8601 :
       
   699                 return 'o';
       
   700                 break;
       
   701 
       
   702             case self::YEAR :
       
   703                 return 'Y';
       
   704                 break;
       
   705 
       
   706             case self::YEAR_SHORT :
       
   707                 return 'y';
       
   708                 break;
       
   709 
       
   710             case self::YEAR_SHORT_8601 :
       
   711                 return $this->_toComment(substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2));
       
   712                 break;
       
   713 
       
   714             case self::MERIDIEM :
       
   715                 $am = $this->date('a', $this->getUnixTimestamp(), false);
       
   716                 if ($am == 'am') {
       
   717                     return $this->_toComment(Zend_Locale_Data::getContent($locale, 'am'));
       
   718                 }
       
   719 
       
   720                 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'pm'));
       
   721                 break;
       
   722 
       
   723             case self::SWATCH :
       
   724                 return 'B';
       
   725                 break;
       
   726 
       
   727             case self::HOUR_SHORT_AM :
       
   728                 return 'g';
       
   729                 break;
       
   730 
       
   731             case self::HOUR_SHORT :
       
   732                 return 'G';
       
   733                 break;
       
   734 
       
   735             case self::HOUR_AM :
       
   736                 return 'h';
       
   737                 break;
       
   738 
       
   739             case self::HOUR :
       
   740                 return 'H';
       
   741                 break;
       
   742 
       
   743             case self::MINUTE :
       
   744                 return $this->_toComment(str_pad($this->date('i', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
       
   745                 break;
       
   746 
       
   747             case self::SECOND :
       
   748                 return $this->_toComment(str_pad($this->date('s', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
       
   749                 break;
       
   750 
       
   751             case self::MINUTE_SHORT :
       
   752                 return 'i';
       
   753                 break;
       
   754 
       
   755             case self::SECOND_SHORT :
       
   756                 return 's';
       
   757                 break;
       
   758 
       
   759             case self::MILLISECOND :
       
   760                 return $this->_toComment($this->getMilliSecond());
       
   761                 break;
       
   762 
       
   763             case self::TIMEZONE_NAME :
       
   764             case 'vvvv' :
       
   765                 return 'e';
       
   766                 break;
       
   767 
       
   768             case self::DAYLIGHT :
       
   769                 return 'I';
       
   770                 break;
       
   771 
       
   772             case self::GMT_DIFF :
       
   773             case 'ZZ' :
       
   774             case 'ZZZ' :
       
   775                 return 'O';
       
   776                 break;
       
   777 
       
   778             case self::GMT_DIFF_SEP :
       
   779                 return 'P';
       
   780                 break;
       
   781 
       
   782             case self::TIMEZONE :
       
   783             case 'v' :
       
   784             case 'zz' :
       
   785             case 'zzz' :
       
   786                 return 'T';
       
   787                 break;
       
   788 
       
   789             case self::TIMEZONE_SECS :
       
   790                 return 'Z';
       
   791                 break;
       
   792 
       
   793             case self::ISO_8601 :
       
   794                 return 'c';
       
   795                 break;
       
   796 
       
   797             case self::RFC_2822 :
       
   798                 return 'r';
       
   799                 break;
       
   800 
       
   801             case self::TIMESTAMP :
       
   802                 return 'U';
       
   803                 break;
       
   804 
       
   805             case self::ERA :
       
   806             case 'GG' :
       
   807             case 'GGG' :
       
   808                 $year = $this->date('Y', $this->getUnixTimestamp(), false);
       
   809                 if ($year < 0) {
       
   810                     return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')));
       
   811                 }
       
   812 
       
   813                 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')));
       
   814                 break;
       
   815 
       
   816             case self::ERA_NARROW :
       
   817                 $year = $this->date('Y', $this->getUnixTimestamp(), false);
       
   818                 if ($year < 0) {
       
   819                     return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')), 0, 1, 'UTF-8')) . '.';
       
   820                 }
       
   821 
       
   822                 return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')), 0, 1, 'UTF-8')) . '.';
       
   823                 break;
       
   824 
       
   825             case self::ERA_NAME :
       
   826                 $year = $this->date('Y', $this->getUnixTimestamp(), false);
       
   827                 if ($year < 0) {
       
   828                     return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0')));
       
   829                 }
       
   830 
       
   831                 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1')));
       
   832                 break;
       
   833 
       
   834             case self::DATES :
       
   835                 return $this->_toToken(Zend_Locale_Format::getDateFormat($locale), $locale);
       
   836                 break;
       
   837 
       
   838             case self::DATE_FULL :
       
   839                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full')), $locale);
       
   840                 break;
       
   841 
       
   842             case self::DATE_LONG :
       
   843                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long')), $locale);
       
   844                 break;
       
   845 
       
   846             case self::DATE_MEDIUM :
       
   847                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium')), $locale);
       
   848                 break;
       
   849 
       
   850             case self::DATE_SHORT :
       
   851                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short')), $locale);
       
   852                 break;
       
   853 
       
   854             case self::TIMES :
       
   855                 return $this->_toToken(Zend_Locale_Format::getTimeFormat($locale), $locale);
       
   856                 break;
       
   857 
       
   858             case self::TIME_FULL :
       
   859                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'full'), $locale);
       
   860                 break;
       
   861 
       
   862             case self::TIME_LONG :
       
   863                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'long'), $locale);
       
   864                 break;
       
   865 
       
   866             case self::TIME_MEDIUM :
       
   867                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'medium'), $locale);
       
   868                 break;
       
   869 
       
   870             case self::TIME_SHORT :
       
   871                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'short'), $locale);
       
   872                 break;
       
   873 
       
   874             case self::DATETIME :
       
   875                 return $this->_toToken(Zend_Locale_Format::getDateTimeFormat($locale), $locale);
       
   876                 break;
       
   877 
       
   878             case self::DATETIME_FULL :
       
   879                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full')), $locale);
       
   880                 break;
       
   881 
       
   882             case self::DATETIME_LONG :
       
   883                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long')), $locale);
       
   884                 break;
       
   885 
       
   886             case self::DATETIME_MEDIUM :
       
   887                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium')), $locale);
       
   888                 break;
       
   889 
       
   890             case self::DATETIME_SHORT :
       
   891                 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short')), $locale);
       
   892                 break;
       
   893 
       
   894             case self::ATOM :
       
   895                 return 'Y\-m\-d\TH\:i\:sP';
       
   896                 break;
       
   897 
       
   898             case self::COOKIE :
       
   899                 return 'l\, d\-M\-y H\:i\:s e';
       
   900                 break;
       
   901 
       
   902             case self::RFC_822 :
       
   903                 return 'D\, d M y H\:i\:s O';
       
   904                 break;
       
   905 
       
   906             case self::RFC_850 :
       
   907                 return 'l\, d\-M\-y H\:i\:s e';
       
   908                 break;
       
   909 
       
   910             case self::RFC_1036 :
       
   911                 return 'D\, d M y H\:i\:s O';
       
   912                 break;
       
   913 
       
   914             case self::RFC_1123 :
       
   915                 return 'D\, d M Y H\:i\:s O';
       
   916                 break;
       
   917 
       
   918             case self::RFC_3339 :
       
   919                 return 'Y\-m\-d\TH\:i\:sP';
       
   920                 break;
       
   921 
       
   922             case self::RSS :
       
   923                 return 'D\, d M Y H\:i\:s O';
       
   924                 break;
       
   925 
       
   926             case self::W3C :
       
   927                 return 'Y\-m\-d\TH\:i\:sP';
       
   928                 break;
       
   929         }
       
   930 
       
   931         if ($token == '') {
       
   932             return '';
       
   933         }
       
   934 
       
   935         switch ($token[0]) {
       
   936             case 'y' :
       
   937                 if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
       
   938                     return 'Y';
       
   939                 }
       
   940 
       
   941                 $length = iconv_strlen($token, 'UTF-8');
       
   942                 return $this->_toComment(str_pad($this->date('Y', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
       
   943                 break;
       
   944 
       
   945             case 'Y' :
       
   946                 if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
       
   947                     return 'o';
       
   948                 }
       
   949 
       
   950                 $length = iconv_strlen($token, 'UTF-8');
       
   951                 return $this->_toComment(str_pad($this->date('o', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
       
   952                 break;
       
   953 
       
   954             case 'A' :
       
   955                 $length  = iconv_strlen($token, 'UTF-8');
       
   956                 $result  = substr($this->getMilliSecond(), 0, 3);
       
   957                 $result += $this->date('s', $this->getUnixTimestamp(), false) * 1000;
       
   958                 $result += $this->date('i', $this->getUnixTimestamp(), false) * 60000;
       
   959                 $result += $this->date('H', $this->getUnixTimestamp(), false) * 3600000;
       
   960 
       
   961                 return $this->_toComment(str_pad($result, $length, '0', STR_PAD_LEFT));
       
   962                 break;
       
   963         }
       
   964 
       
   965         return $this->_toComment($token);
       
   966     }
       
   967 
       
   968     /**
       
   969      * Private function to make a comment of a token
       
   970      *
       
   971      * @param string $token
       
   972      * @return string
       
   973      */
       
   974     private function _toComment($token)
       
   975     {
       
   976         $token = str_split($token);
       
   977         $result = '';
       
   978         foreach ($token as $tok) {
       
   979             $result .= '\\' . $tok;
       
   980         }
       
   981 
       
   982         return $result;
       
   983     }
       
   984 
       
   985     /**
       
   986      * Return digit from standard names (english)
       
   987      * Faster implementation than locale aware searching
       
   988      *
       
   989      * @param  string  $name
       
   990      * @return integer  Number of this month
       
   991      * @throws Zend_Date_Exception
       
   992      */
       
   993     private function _getDigitFromName($name)
       
   994     {
       
   995         switch($name) {
       
   996             case "Jan":
       
   997                 return 1;
       
   998 
       
   999             case "Feb":
       
  1000                 return 2;
       
  1001 
       
  1002             case "Mar":
       
  1003                 return 3;
       
  1004 
       
  1005             case "Apr":
       
  1006                 return 4;
       
  1007 
       
  1008             case "May":
       
  1009                 return 5;
       
  1010 
       
  1011             case "Jun":
       
  1012                 return 6;
       
  1013 
       
  1014             case "Jul":
       
  1015                 return 7;
       
  1016 
       
  1017             case "Aug":
       
  1018                 return 8;
       
  1019 
       
  1020             case "Sep":
       
  1021                 return 9;
       
  1022 
       
  1023             case "Oct":
       
  1024                 return 10;
       
  1025 
       
  1026             case "Nov":
       
  1027                 return 11;
       
  1028 
       
  1029             case "Dec":
       
  1030                 return 12;
       
  1031 
       
  1032             default:
       
  1033                 require_once 'Zend/Date/Exception.php';
       
  1034                 throw new Zend_Date_Exception('Month ($name) is not a known month');
       
  1035         }
       
  1036     }
       
  1037 
       
  1038     /**
       
  1039      * Counts the exact year number
       
  1040      * < 70 - 2000 added, >70 < 100 - 1900, others just returned
       
  1041      *
       
  1042      * @param  integer  $value year number
       
  1043      * @return integer  Number of year
       
  1044      */
       
  1045     public static function getFullYear($value)
       
  1046     {
       
  1047         if ($value >= 0) {
       
  1048             if ($value < 70) {
       
  1049                 $value += 2000;
       
  1050             } else if ($value < 100) {
       
  1051                 $value += 1900;
       
  1052             }
       
  1053         }
       
  1054         return $value;
       
  1055     }
       
  1056 
       
  1057     /**
       
  1058      * Sets the given date as new date or a given datepart as new datepart returning the new datepart
       
  1059      * This could be for example a localized dayname, the date without time,
       
  1060      * the month or only the seconds. There are about 50 different supported date parts.
       
  1061      * For a complete list of supported datepart values look into the docu
       
  1062      *
       
  1063      * @param  string|integer|array|Zend_Date  $date    Date or datepart to set
       
  1064      * @param  string                          $part    OPTIONAL Part of the date to set, if null the timestamp is set
       
  1065      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  1066      * @return Zend_Date Provides fluid interface
       
  1067      * @throws Zend_Date_Exception
       
  1068      */
       
  1069     public function set($date, $part = null, $locale = null)
       
  1070     {
       
  1071         if (self::$_options['format_type'] == 'php') {
       
  1072             $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
       
  1073         }
       
  1074 
       
  1075         $zone = $this->getTimezoneFromString($date);
       
  1076         $this->setTimezone($zone);
       
  1077 
       
  1078         $this->_calculate('set', $date, $part, $locale);
       
  1079         return $this;
       
  1080     }
       
  1081 
       
  1082     /**
       
  1083      * Adds a date or datepart to the existing date, by extracting $part from $date,
       
  1084      * and modifying this object by adding that part.  The $part is then extracted from
       
  1085      * this object and returned as an integer or numeric string (for large values, or $part's
       
  1086      * corresponding to pre-defined formatted date strings).
       
  1087      * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
       
  1088      * There are about 50 different supported date parts.
       
  1089      * For a complete list of supported datepart values look into the docu.
       
  1090      *
       
  1091      * @param  string|integer|array|Zend_Date  $date    Date or datepart to add
       
  1092      * @param  string                          $part    OPTIONAL Part of the date to add, if null the timestamp is added
       
  1093      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  1094      * @return Zend_Date Provides fluid interface
       
  1095      * @throws Zend_Date_Exception
       
  1096      */
       
  1097     public function add($date, $part = self::TIMESTAMP, $locale = null)
       
  1098     {
       
  1099         if (self::$_options['format_type'] == 'php') {
       
  1100             $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
       
  1101         }
       
  1102 
       
  1103         $this->_calculate('add', $date, $part, $locale);
       
  1104         return $this;
       
  1105     }
       
  1106 
       
  1107     /**
       
  1108      * Subtracts a date from another date.
       
  1109      * This could be for example a RFC2822 date, the time,
       
  1110      * the year or only the timestamp. There are about 50 different supported date parts.
       
  1111      * For a complete list of supported datepart values look into the docu
       
  1112      * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
       
  1113      *
       
  1114      * @param  string|integer|array|Zend_Date  $date    Date or datepart to subtract
       
  1115      * @param  string                          $part    OPTIONAL Part of the date to sub, if null the timestamp is subtracted
       
  1116      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  1117      * @return Zend_Date Provides fluid interface
       
  1118      * @throws Zend_Date_Exception
       
  1119      */
       
  1120     public function sub($date, $part = self::TIMESTAMP, $locale = null)
       
  1121     {
       
  1122         if (self::$_options['format_type'] == 'php') {
       
  1123             $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
       
  1124         }
       
  1125 
       
  1126         $this->_calculate('sub', $date, $part, $locale);
       
  1127         return $this;
       
  1128     }
       
  1129 
       
  1130     /**
       
  1131      * Compares a date or datepart with the existing one.
       
  1132      * Returns -1 if earlier, 0 if equal and 1 if later.
       
  1133      *
       
  1134      * @param  string|integer|array|Zend_Date  $date    Date or datepart to compare with the date object
       
  1135      * @param  string                          $part    OPTIONAL Part of the date to compare, if null the timestamp is subtracted
       
  1136      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  1137      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  1138      * @throws Zend_Date_Exception
       
  1139      */
       
  1140     public function compare($date, $part = self::TIMESTAMP, $locale = null)
       
  1141     {
       
  1142         if (self::$_options['format_type'] == 'php') {
       
  1143             $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
       
  1144         }
       
  1145 
       
  1146         $compare = $this->_calculate('cmp', $date, $part, $locale);
       
  1147 
       
  1148         if ($compare > 0) {
       
  1149             return 1;
       
  1150         } else if ($compare < 0) {
       
  1151             return -1;
       
  1152         }
       
  1153         return 0;
       
  1154     }
       
  1155 
       
  1156     /**
       
  1157      * Returns a new instance of Zend_Date with the selected part copied.
       
  1158      * To make an exact copy, use PHP's clone keyword.
       
  1159      * For a complete list of supported date part values look into the docu.
       
  1160      * If a date part is copied, all other date parts are set to standard values.
       
  1161      * For example: If only YEAR is copied, the returned date object is equal to
       
  1162      * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
       
  1163      * If only HOUR is copied, the returned date object is equal to
       
  1164      * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
       
  1165      *
       
  1166      * @param  string              $part    Part of the date to compare, if null the timestamp is subtracted
       
  1167      * @param  string|Zend_Locale  $locale  OPTIONAL New object's locale.  No adjustments to timezone are made.
       
  1168      * @return Zend_Date New clone with requested part
       
  1169      */
       
  1170     public function copyPart($part, $locale = null)
       
  1171     {
       
  1172         $clone = clone $this;           // copy all instance variables
       
  1173         $clone->setUnixTimestamp(0);    // except the timestamp
       
  1174         if ($locale != null) {
       
  1175             $clone->setLocale($locale); // set an other locale if selected
       
  1176         }
       
  1177         $clone->set($this, $part);
       
  1178         return $clone;
       
  1179     }
       
  1180 
       
  1181     /**
       
  1182      * Internal function, returns the offset of a given timezone
       
  1183      *
       
  1184      * @param string $zone
       
  1185      * @return integer
       
  1186      */
       
  1187     public function getTimezoneFromString($zone)
       
  1188     {
       
  1189         if (is_array($zone)) {
       
  1190             return $this->getTimezone();
       
  1191         }
       
  1192 
       
  1193         if ($zone instanceof Zend_Date) {
       
  1194             return $zone->getTimezone();
       
  1195         }
       
  1196 
       
  1197         $match = array();
       
  1198         preg_match('/\dZ$/', $zone, $match);
       
  1199         if (!empty($match)) {
       
  1200             return "Etc/UTC";
       
  1201         }
       
  1202 
       
  1203         preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
       
  1204         if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
       
  1205             $zone = "Etc/GMT";
       
  1206             $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
       
  1207             $zone .= (int) abs($match[count($match) - 1]);
       
  1208             return $zone;
       
  1209         }
       
  1210 
       
  1211         preg_match('/([[:alpha:]\/]{3,30})(?!.*([[:alpha:]\/]{3,30}))/', $zone, $match);
       
  1212         try {
       
  1213             if (!empty($match) and (!is_int($match[count($match) - 1]))) {
       
  1214                 $oldzone = $this->getTimezone();
       
  1215                 $this->setTimezone($match[count($match) - 1]);
       
  1216                 $result = $this->getTimezone();
       
  1217                 $this->setTimezone($oldzone);
       
  1218                 if ($result !== $oldzone) {
       
  1219                     return $match[count($match) - 1];
       
  1220                 }
       
  1221             }
       
  1222         } catch (Exception $e) {
       
  1223             // fall through
       
  1224         }
       
  1225 
       
  1226         return $this->getTimezone();
       
  1227     }
       
  1228 
       
  1229     /**
       
  1230      * Calculates the date or object
       
  1231      *
       
  1232      * @param  string                    $calc  Calculation to make
       
  1233      * @param  string|integer            $date  Date for calculation
       
  1234      * @param  string|integer            $comp  Second date for calculation
       
  1235      * @param  boolean|integer           $dst   Use dst correction if option is set
       
  1236      * @return integer|string|Zend_Date  new timestamp or Zend_Date depending on calculation
       
  1237      */
       
  1238     private function _assign($calc, $date, $comp = 0, $dst = false)
       
  1239     {
       
  1240         switch ($calc) {
       
  1241             case 'set' :
       
  1242                 if (!empty($comp)) {
       
  1243                     $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $comp));
       
  1244                 }
       
  1245                 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
       
  1246                 $value = $this->getUnixTimestamp();
       
  1247                 break;
       
  1248             case 'add' :
       
  1249                 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
       
  1250                 $value = $this->getUnixTimestamp();
       
  1251                 break;
       
  1252             case 'sub' :
       
  1253                 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $date));
       
  1254                 $value = $this->getUnixTimestamp();
       
  1255                 break;
       
  1256             default :
       
  1257                 // cmp - compare
       
  1258                 return call_user_func(Zend_Locale_Math::$comp, $comp, $date);
       
  1259                 break;
       
  1260         }
       
  1261 
       
  1262         // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
       
  1263         if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
       
  1264             $hour = $this->toString(self::HOUR, 'iso');
       
  1265             if ($hour != $dst) {
       
  1266                 if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
       
  1267                     $value += 3600;
       
  1268                 } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
       
  1269                     $value -= 3600;
       
  1270                 }
       
  1271                 $this->setUnixTimestamp($value);
       
  1272             }
       
  1273         }
       
  1274         return $this->getUnixTimestamp();
       
  1275     }
       
  1276 
       
  1277 
       
  1278     /**
       
  1279      * Calculates the date or object
       
  1280      *
       
  1281      * @param  string                          $calc    Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set'
       
  1282      * @param  string|integer|array|Zend_Date  $date    Date or datepart to calculate with
       
  1283      * @param  string                          $part    Part of the date to calculate, if null the timestamp is used
       
  1284      * @param  string|Zend_Locale              $locale  Locale for parsing input
       
  1285      * @return integer|string|Zend_Date        new timestamp
       
  1286      * @throws Zend_Date_Exception
       
  1287      */
       
  1288     private function _calculate($calc, $date, $part, $locale)
       
  1289     {
       
  1290         if ($date === null) {
       
  1291             require_once 'Zend/Date/Exception.php';
       
  1292             throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
       
  1293         }
       
  1294 
       
  1295         if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale::isLocale($part, null, false))) {
       
  1296             $locale = $part;
       
  1297             $part   = null;
       
  1298         }
       
  1299 
       
  1300         if ($locale === null) {
       
  1301             $locale = $this->getLocale();
       
  1302         }
       
  1303 
       
  1304         $locale = (string) $locale;
       
  1305 
       
  1306         // Create date parts
       
  1307         $year   = $this->toString(self::YEAR, 'iso');
       
  1308         $month  = $this->toString(self::MONTH_SHORT, 'iso');
       
  1309         $day    = $this->toString(self::DAY_SHORT, 'iso');
       
  1310         $hour   = $this->toString(self::HOUR_SHORT, 'iso');
       
  1311         $minute = $this->toString(self::MINUTE_SHORT, 'iso');
       
  1312         $second = $this->toString(self::SECOND_SHORT, 'iso');
       
  1313         // If object extract value
       
  1314         if ($date instanceof Zend_Date) {
       
  1315             $date = $date->toString($part, 'iso', $locale);
       
  1316         }
       
  1317 
       
  1318         if (is_array($date) === true) {
       
  1319             if (empty($part) === false) {
       
  1320                 switch($part) {
       
  1321                     // Fall through
       
  1322                     case self::DAY:
       
  1323                     case self::DAY_SHORT:
       
  1324                         if (isset($date['day']) === true) {
       
  1325                             $date = $date['day'];
       
  1326                         }
       
  1327                         break;
       
  1328                     // Fall through
       
  1329                     case self::WEEKDAY_SHORT:
       
  1330                     case self::WEEKDAY:
       
  1331                     case self::WEEKDAY_8601:
       
  1332                     case self::WEEKDAY_DIGIT:
       
  1333                     case self::WEEKDAY_NARROW:
       
  1334                     case self::WEEKDAY_NAME:
       
  1335                         if (isset($date['weekday']) === true) {
       
  1336                             $date = $date['weekday'];
       
  1337                             $part = self::WEEKDAY_DIGIT;
       
  1338                         }
       
  1339                         break;
       
  1340                     case self::DAY_OF_YEAR:
       
  1341                         if (isset($date['day_of_year']) === true) {
       
  1342                             $date = $date['day_of_year'];
       
  1343                         }
       
  1344                         break;
       
  1345                     // Fall through
       
  1346                     case self::MONTH:
       
  1347                     case self::MONTH_SHORT:
       
  1348                     case self::MONTH_NAME:
       
  1349                     case self::MONTH_NAME_SHORT:
       
  1350                     case self::MONTH_NAME_NARROW:
       
  1351                         if (isset($date['month']) === true) {
       
  1352                             $date = $date['month'];
       
  1353                         }
       
  1354                         break;
       
  1355                     // Fall through
       
  1356                     case self::YEAR:
       
  1357                     case self::YEAR_SHORT:
       
  1358                     case self::YEAR_8601:
       
  1359                     case self::YEAR_SHORT_8601:
       
  1360                         if (isset($date['year']) === true) {
       
  1361                             $date = $date['year'];
       
  1362                         }
       
  1363                         break;
       
  1364                     // Fall through
       
  1365                     case self::HOUR:
       
  1366                     case self::HOUR_AM:
       
  1367                     case self::HOUR_SHORT:
       
  1368                     case self::HOUR_SHORT_AM:
       
  1369                         if (isset($date['hour']) === true) {
       
  1370                             $date = $date['hour'];
       
  1371                         }
       
  1372                         break;
       
  1373                     // Fall through
       
  1374                     case self::MINUTE:
       
  1375                     case self::MINUTE_SHORT:
       
  1376                         if (isset($date['minute']) === true) {
       
  1377                             $date = $date['minute'];
       
  1378                         }
       
  1379                         break;
       
  1380                     // Fall through
       
  1381                     case self::SECOND:
       
  1382                     case self::SECOND_SHORT:
       
  1383                         if (isset($date['second']) === true) {
       
  1384                             $date = $date['second'];
       
  1385                         }
       
  1386                         break;
       
  1387                     // Fall through
       
  1388                     case self::TIMEZONE:
       
  1389                     case self::TIMEZONE_NAME:
       
  1390                         if (isset($date['timezone']) === true) {
       
  1391                             $date = $date['timezone'];
       
  1392                         }
       
  1393                         break;
       
  1394                     case self::TIMESTAMP:
       
  1395                         if (isset($date['timestamp']) === true) {
       
  1396                             $date = $date['timestamp'];
       
  1397                         }
       
  1398                         break;
       
  1399                     case self::WEEK:
       
  1400                         if (isset($date['week']) === true) {
       
  1401                             $date = $date['week'];
       
  1402                         }
       
  1403                         break;
       
  1404                     case self::TIMEZONE_SECS:
       
  1405                         if (isset($date['gmtsecs']) === true) {
       
  1406                             $date = $date['gmtsecs'];
       
  1407                         }
       
  1408                         break;
       
  1409                     default:
       
  1410                         require_once 'Zend/Date/Exception.php';
       
  1411                         throw new Zend_Date_Exception("datepart for part ($part) not found in array");
       
  1412                         break;
       
  1413                 }
       
  1414             } else {
       
  1415                 $hours = 0;
       
  1416                 if (isset($date['hour']) === true) {
       
  1417                     $hours = $date['hour'];
       
  1418                 }
       
  1419                 $minutes = 0;
       
  1420                 if (isset($date['minute']) === true) {
       
  1421                     $minutes = $date['minute'];
       
  1422                 }
       
  1423                 $seconds = 0;
       
  1424                 if (isset($date['second']) === true) {
       
  1425                     $seconds = $date['second'];
       
  1426                 }
       
  1427                 $months = 0;
       
  1428                 if (isset($date['month']) === true) {
       
  1429                     $months = $date['month'];
       
  1430                 }
       
  1431                 $days = 0;
       
  1432                 if (isset($date['day']) === true) {
       
  1433                     $days = $date['day'];
       
  1434                 }
       
  1435                 $years = 0;
       
  1436                 if (isset($date['year']) === true) {
       
  1437                     $years = $date['year'];
       
  1438                 }
       
  1439                 return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true),
       
  1440                                              $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour);
       
  1441             }
       
  1442         }
       
  1443 
       
  1444         // $date as object, part of foreign date as own date
       
  1445         switch($part) {
       
  1446 
       
  1447             // day formats
       
  1448             case self::DAY:
       
  1449                 if (is_numeric($date)) {
       
  1450                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
       
  1451                                                  $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
       
  1452                 }
       
  1453 
       
  1454                 require_once 'Zend/Date/Exception.php';
       
  1455                 throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
       
  1456                 break;
       
  1457 
       
  1458             case self::WEEKDAY_SHORT:
       
  1459                 $daylist = Zend_Locale_Data::getList($locale, 'day');
       
  1460                 $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
       
  1461                 $cnt = 0;
       
  1462 
       
  1463                 foreach ($daylist as $key => $value) {
       
  1464                     if (strtoupper(iconv_substr($value, 0, 3, 'UTF-8')) == strtoupper($date)) {
       
  1465                          $found = $cnt;
       
  1466                         break;
       
  1467                     }
       
  1468                     ++$cnt;
       
  1469                 }
       
  1470 
       
  1471                 // Weekday found
       
  1472                 if ($cnt < 7) {
       
  1473                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
       
  1474                                                  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
       
  1475                 }
       
  1476 
       
  1477                 // Weekday not found
       
  1478                 require_once 'Zend/Date/Exception.php';
       
  1479                 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
       
  1480                 break;
       
  1481 
       
  1482             case self::DAY_SHORT:
       
  1483                 if (is_numeric($date)) {
       
  1484                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
       
  1485                                                  $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
       
  1486                 }
       
  1487 
       
  1488                 require_once 'Zend/Date/Exception.php';
       
  1489                 throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
       
  1490                 break;
       
  1491 
       
  1492             case self::WEEKDAY:
       
  1493                 $daylist = Zend_Locale_Data::getList($locale, 'day');
       
  1494                 $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
       
  1495                 $cnt = 0;
       
  1496 
       
  1497                 foreach ($daylist as $key => $value) {
       
  1498                     if (strtoupper($value) == strtoupper($date)) {
       
  1499                         $found = $cnt;
       
  1500                         break;
       
  1501                     }
       
  1502                     ++$cnt;
       
  1503                 }
       
  1504 
       
  1505                 // Weekday found
       
  1506                 if ($cnt < 7) {
       
  1507                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
       
  1508                                                  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
       
  1509                 }
       
  1510 
       
  1511                 // Weekday not found
       
  1512                 require_once 'Zend/Date/Exception.php';
       
  1513                 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
       
  1514                 break;
       
  1515 
       
  1516             case self::WEEKDAY_8601:
       
  1517                 $weekday = (int) $this->toString(self::WEEKDAY_8601, 'iso', $locale);
       
  1518                 if ((intval($date) > 0) and (intval($date) < 8)) {
       
  1519                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
       
  1520                                                  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
       
  1521                 }
       
  1522 
       
  1523                 // Weekday not found
       
  1524                 require_once 'Zend/Date/Exception.php';
       
  1525                 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
       
  1526                 break;
       
  1527 
       
  1528             case self::DAY_SUFFIX:
       
  1529                 require_once 'Zend/Date/Exception.php';
       
  1530                 throw new Zend_Date_Exception('day suffix not supported', 0, null, $date);
       
  1531                 break;
       
  1532 
       
  1533             case self::WEEKDAY_DIGIT:
       
  1534                 $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
       
  1535                 if (is_numeric($date) and (intval($date) >= 0) and (intval($date) < 7)) {
       
  1536                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
       
  1537                                                  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
       
  1538                 }
       
  1539 
       
  1540                 // Weekday not found
       
  1541                 require_once 'Zend/Date/Exception.php';
       
  1542                 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
       
  1543                 break;
       
  1544 
       
  1545             case self::DAY_OF_YEAR:
       
  1546                 if (is_numeric($date)) {
       
  1547                     if (($calc == 'add') || ($calc == 'sub')) {
       
  1548                         $year = 1970;
       
  1549                         ++$date;
       
  1550                         ++$day;
       
  1551                     }
       
  1552 
       
  1553                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1, $date, $year, true),
       
  1554                                                  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
       
  1555                 }
       
  1556 
       
  1557                 require_once 'Zend/Date/Exception.php';
       
  1558                 throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
       
  1559                 break;
       
  1560 
       
  1561             case self::WEEKDAY_NARROW:
       
  1562                 $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
       
  1563                 $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
       
  1564                 $cnt = 0;
       
  1565                 foreach ($daylist as $key => $value) {
       
  1566                     if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($date)) {
       
  1567                         $found = $cnt;
       
  1568                         break;
       
  1569                     }
       
  1570                     ++$cnt;
       
  1571                 }
       
  1572 
       
  1573                 // Weekday found
       
  1574                 if ($cnt < 7) {
       
  1575                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
       
  1576                                                  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
       
  1577                 }
       
  1578 
       
  1579                 // Weekday not found
       
  1580                 require_once 'Zend/Date/Exception.php';
       
  1581                 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
       
  1582                 break;
       
  1583 
       
  1584             case self::WEEKDAY_NAME:
       
  1585                 $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
       
  1586                 $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
       
  1587                 $cnt = 0;
       
  1588                 foreach ($daylist as $key => $value) {
       
  1589                     if (strtoupper($value) == strtoupper($date)) {
       
  1590                         $found = $cnt;
       
  1591                         break;
       
  1592                     }
       
  1593                     ++$cnt;
       
  1594                 }
       
  1595 
       
  1596                 // Weekday found
       
  1597                 if ($cnt < 7) {
       
  1598                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
       
  1599                                                  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
       
  1600                 }
       
  1601 
       
  1602                 // Weekday not found
       
  1603                 require_once 'Zend/Date/Exception.php';
       
  1604                 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
       
  1605                 break;
       
  1606 
       
  1607             // week formats
       
  1608             case self::WEEK:
       
  1609                 if (is_numeric($date)) {
       
  1610                     $week = (int) $this->toString(self::WEEK, 'iso', $locale);
       
  1611                     return $this->_assign($calc, parent::mktime(0, 0, 0, 1, 1 + ($date * 7), 1970, true),
       
  1612                                                  parent::mktime(0, 0, 0, 1, 1 + ($week * 7), 1970, true), $hour);
       
  1613                 }
       
  1614 
       
  1615                 require_once 'Zend/Date/Exception.php';
       
  1616                 throw new Zend_Date_Exception("invalid date ($date) operand, week expected", 0, null, $date);
       
  1617                 break;
       
  1618 
       
  1619             // month formats
       
  1620             case self::MONTH_NAME:
       
  1621                 $monthlist = Zend_Locale_Data::getList($locale, 'month');
       
  1622                 $cnt = 0;
       
  1623                 foreach ($monthlist as $key => $value) {
       
  1624                     if (strtoupper($value) == strtoupper($date)) {
       
  1625                         $found = $key;
       
  1626                         break;
       
  1627                     }
       
  1628                     ++$cnt;
       
  1629                 }
       
  1630                 $date = array_search($date, $monthlist);
       
  1631 
       
  1632                 // Monthname found
       
  1633                 if ($cnt < 12) {
       
  1634                     $fixday = 0;
       
  1635                     if ($calc == 'add') {
       
  1636                         $date += $found;
       
  1637                         $calc = 'set';
       
  1638                         if (self::$_options['extend_month'] == false) {
       
  1639                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1640                             if ($parts['mday'] != $day) {
       
  1641                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1642                             }
       
  1643                         }
       
  1644                     } else if ($calc == 'sub') {
       
  1645                         $date = $month - $found;
       
  1646                         $calc = 'set';
       
  1647                         if (self::$_options['extend_month'] == false) {
       
  1648                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1649                             if ($parts['mday'] != $day) {
       
  1650                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1651                             }
       
  1652                         }
       
  1653                     }
       
  1654                     return $this->_assign($calc, $this->mktime(0, 0, 0, $date,  $day + $fixday, $year, true),
       
  1655                                                  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
       
  1656                 }
       
  1657 
       
  1658                 // Monthname not found
       
  1659                 require_once 'Zend/Date/Exception.php';
       
  1660                 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
       
  1661                 break;
       
  1662 
       
  1663             case self::MONTH:
       
  1664                 if (is_numeric($date)) {
       
  1665                     $fixday = 0;
       
  1666                     if ($calc == 'add') {
       
  1667                         $date += $month;
       
  1668                         $calc = 'set';
       
  1669                         if (self::$_options['extend_month'] == false) {
       
  1670                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1671                             if ($parts['mday'] != $day) {
       
  1672                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1673                             }
       
  1674                         }
       
  1675                     } else if ($calc == 'sub') {
       
  1676                         $date = $month - $date;
       
  1677                         $calc = 'set';
       
  1678                         if (self::$_options['extend_month'] == false) {
       
  1679                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1680                             if ($parts['mday'] != $day) {
       
  1681                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1682                             }
       
  1683                         }
       
  1684                     }
       
  1685                     return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
       
  1686                                                  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
       
  1687                 }
       
  1688 
       
  1689                 require_once 'Zend/Date/Exception.php';
       
  1690                 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
       
  1691                 break;
       
  1692 
       
  1693             case self::MONTH_NAME_SHORT:
       
  1694                 $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
       
  1695                 $cnt = 0;
       
  1696                 foreach ($monthlist as $key => $value) {
       
  1697                     if (strtoupper($value) == strtoupper($date)) {
       
  1698                         $found = $key;
       
  1699                         break;
       
  1700                     }
       
  1701                     ++$cnt;
       
  1702                 }
       
  1703                 $date = array_search($date, $monthlist);
       
  1704 
       
  1705                 // Monthname found
       
  1706                 if ($cnt < 12) {
       
  1707                     $fixday = 0;
       
  1708                     if ($calc == 'add') {
       
  1709                         $date += $found;
       
  1710                         $calc = 'set';
       
  1711                         if (self::$_options['extend_month'] === false) {
       
  1712                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1713                             if ($parts['mday'] != $day) {
       
  1714                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1715                             }
       
  1716                         }
       
  1717                     } else if ($calc == 'sub') {
       
  1718                         $date = $month - $found;
       
  1719                         $calc = 'set';
       
  1720                         if (self::$_options['extend_month'] === false) {
       
  1721                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1722                             if ($parts['mday'] != $day) {
       
  1723                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1724                             }
       
  1725                         }
       
  1726                     }
       
  1727                     return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
       
  1728                                                  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
       
  1729                 }
       
  1730 
       
  1731                 // Monthname not found
       
  1732                 require_once 'Zend/Date/Exception.php';
       
  1733                 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
       
  1734                 break;
       
  1735 
       
  1736             case self::MONTH_SHORT:
       
  1737                 if (is_numeric($date) === true) {
       
  1738                     $fixday = 0;
       
  1739                     if ($calc === 'add') {
       
  1740                         $date += $month;
       
  1741                         $calc  = 'set';
       
  1742                         if (self::$_options['extend_month'] === false) {
       
  1743                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1744                             if ($parts['mday'] != $day) {
       
  1745                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1746                             }
       
  1747                         }
       
  1748                     } else if ($calc === 'sub') {
       
  1749                         $date = $month - $date;
       
  1750                         $calc = 'set';
       
  1751                         if (self::$_options['extend_month'] === false) {
       
  1752                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1753                             if ($parts['mday'] != $day) {
       
  1754                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1755                             }
       
  1756                         }
       
  1757                     }
       
  1758 
       
  1759                     return $this->_assign($calc, $this->mktime(0, 0, 0, $date,  $day + $fixday, $year, true),
       
  1760                                                  $this->mktime(0, 0, 0, $month, $day,           $year, true), $hour);
       
  1761                 }
       
  1762 
       
  1763                 require_once 'Zend/Date/Exception.php';
       
  1764                 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
       
  1765                 break;
       
  1766 
       
  1767             case self::MONTH_DAYS:
       
  1768                 require_once 'Zend/Date/Exception.php';
       
  1769                 throw new Zend_Date_Exception('month days not supported', 0, null, $date);
       
  1770                 break;
       
  1771 
       
  1772             case self::MONTH_NAME_NARROW:
       
  1773                 $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow'));
       
  1774                 $cnt       = 0;
       
  1775                 foreach ($monthlist as $key => $value) {
       
  1776                     if (strtoupper($value) === strtoupper($date)) {
       
  1777                         $found = $key;
       
  1778                         break;
       
  1779                     }
       
  1780                     ++$cnt;
       
  1781                 }
       
  1782                 $date = array_search($date, $monthlist);
       
  1783 
       
  1784                 // Monthname found
       
  1785                 if ($cnt < 12) {
       
  1786                     $fixday = 0;
       
  1787                     if ($calc === 'add') {
       
  1788                         $date += $found;
       
  1789                         $calc  = 'set';
       
  1790                         if (self::$_options['extend_month'] === false) {
       
  1791                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1792                             if ($parts['mday'] != $day) {
       
  1793                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1794                             }
       
  1795                         }
       
  1796                     } else if ($calc === 'sub') {
       
  1797                         $date = $month - $found;
       
  1798                         $calc = 'set';
       
  1799                         if (self::$_options['extend_month'] === false) {
       
  1800                             $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
       
  1801                             if ($parts['mday'] != $day) {
       
  1802                                 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
       
  1803                             }
       
  1804                         }
       
  1805                     }
       
  1806                     return $this->_assign($calc, $this->mktime(0, 0, 0, $date,  $day + $fixday, $year, true),
       
  1807                                                  $this->mktime(0, 0, 0, $month, $day,           $year, true), $hour);
       
  1808                 }
       
  1809 
       
  1810                 // Monthname not found
       
  1811                 require_once 'Zend/Date/Exception.php';
       
  1812                 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
       
  1813                 break;
       
  1814 
       
  1815             // year formats
       
  1816             case self::LEAPYEAR:
       
  1817                 require_once 'Zend/Date/Exception.php';
       
  1818                 throw new Zend_Date_Exception('leap year not supported', 0, null, $date);
       
  1819                 break;
       
  1820 
       
  1821             case self::YEAR_8601:
       
  1822                 if (is_numeric($date)) {
       
  1823                     if ($calc === 'add') {
       
  1824                         $date += $year;
       
  1825                         $calc  = 'set';
       
  1826                     } else if ($calc === 'sub') {
       
  1827                         $date = $year - $date;
       
  1828                         $calc = 'set';
       
  1829                     }
       
  1830 
       
  1831                     return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
       
  1832                                                  $this->mktime(0, 0, 0, $month, $day, $year,         true), false);
       
  1833                 }
       
  1834 
       
  1835                 require_once 'Zend/Date/Exception.php';
       
  1836                 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
       
  1837                 break;
       
  1838 
       
  1839             case self::YEAR:
       
  1840                 if (is_numeric($date)) {
       
  1841                     if ($calc === 'add') {
       
  1842                         $date += $year;
       
  1843                         $calc  = 'set';
       
  1844                     } else if ($calc === 'sub') {
       
  1845                         $date = $year - $date;
       
  1846                         $calc = 'set';
       
  1847                     }
       
  1848 
       
  1849                     return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
       
  1850                                                  $this->mktime(0, 0, 0, $month, $day, $year,         true), false);
       
  1851                 }
       
  1852 
       
  1853                 require_once 'Zend/Date/Exception.php';
       
  1854                 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
       
  1855                 break;
       
  1856 
       
  1857             case self::YEAR_SHORT:
       
  1858                 if (is_numeric($date)) {
       
  1859                     $date = intval($date);
       
  1860                     if (($calc == 'set') || ($calc == 'cmp')) {
       
  1861                         $date = self::getFullYear($date);
       
  1862                     }
       
  1863                     if ($calc === 'add') {
       
  1864                         $date += $year;
       
  1865                         $calc  = 'set';
       
  1866                     } else if ($calc === 'sub') {
       
  1867                         $date = $year - $date;
       
  1868                         $calc = 'set';
       
  1869                     }
       
  1870 
       
  1871                     return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
       
  1872                                                  $this->mktime(0, 0, 0, $month, $day, $year, true), false);
       
  1873                 }
       
  1874 
       
  1875                 require_once 'Zend/Date/Exception.php';
       
  1876                 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
       
  1877                 break;
       
  1878 
       
  1879             case self::YEAR_SHORT_8601:
       
  1880                 if (is_numeric($date)) {
       
  1881                     $date = intval($date);
       
  1882                     if (($calc === 'set') || ($calc === 'cmp')) {
       
  1883                         $date = self::getFullYear($date);
       
  1884                     }
       
  1885                     if ($calc === 'add') {
       
  1886                         $date += $year;
       
  1887                         $calc  = 'set';
       
  1888                     } else if ($calc === 'sub') {
       
  1889                         $date = $year - $date;
       
  1890                         $calc = 'set';
       
  1891                     }
       
  1892 
       
  1893                     return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
       
  1894                                                  $this->mktime(0, 0, 0, $month, $day, $year, true), false);
       
  1895                 }
       
  1896 
       
  1897                 require_once 'Zend/Date/Exception.php';
       
  1898                 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
       
  1899                 break;
       
  1900 
       
  1901             // time formats
       
  1902             case self::MERIDIEM:
       
  1903                 require_once 'Zend/Date/Exception.php';
       
  1904                 throw new Zend_Date_Exception('meridiem not supported', 0, null, $date);
       
  1905                 break;
       
  1906 
       
  1907             case self::SWATCH:
       
  1908                 if (is_numeric($date)) {
       
  1909                     $rest    = intval($date);
       
  1910                     $hours   = floor($rest * 24 / 1000);
       
  1911                     $rest    = $rest - ($hours * 1000 / 24);
       
  1912                     $minutes = floor($rest * 1440 / 1000);
       
  1913                     $rest    = $rest - ($minutes * 1000 / 1440);
       
  1914                     $seconds = floor($rest * 86400 / 1000);
       
  1915                     return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true),
       
  1916                                                  $this->mktime($hour,  $minute,  $second,  1, 1, 1970, true), false);
       
  1917                 }
       
  1918 
       
  1919                 require_once 'Zend/Date/Exception.php';
       
  1920                 throw new Zend_Date_Exception("invalid date ($date) operand, swatchstamp expected", 0, null, $date);
       
  1921                 break;
       
  1922 
       
  1923             case self::HOUR_SHORT_AM:
       
  1924                 if (is_numeric($date)) {
       
  1925                     return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
       
  1926                                                  $this->mktime($hour,         0, 0, 1, 1, 1970, true), false);
       
  1927                 }
       
  1928 
       
  1929                 require_once 'Zend/Date/Exception.php';
       
  1930                 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
       
  1931                 break;
       
  1932 
       
  1933             case self::HOUR_SHORT:
       
  1934                 if (is_numeric($date)) {
       
  1935                     return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
       
  1936                                                  $this->mktime($hour,         0, 0, 1, 1, 1970, true), false);
       
  1937                 }
       
  1938 
       
  1939                 require_once 'Zend/Date/Exception.php';
       
  1940                 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
       
  1941                 break;
       
  1942 
       
  1943             case self::HOUR_AM:
       
  1944                 if (is_numeric($date)) {
       
  1945                     return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
       
  1946                                                  $this->mktime($hour,         0, 0, 1, 1, 1970, true), false);
       
  1947                 }
       
  1948 
       
  1949                 require_once 'Zend/Date/Exception.php';
       
  1950                 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
       
  1951                 break;
       
  1952 
       
  1953             case self::HOUR:
       
  1954                 if (is_numeric($date)) {
       
  1955                     return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
       
  1956                                                  $this->mktime($hour,         0, 0, 1, 1, 1970, true), false);
       
  1957                 }
       
  1958 
       
  1959                 require_once 'Zend/Date/Exception.php';
       
  1960                 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
       
  1961                 break;
       
  1962 
       
  1963             case self::MINUTE:
       
  1964                 if (is_numeric($date)) {
       
  1965                     return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
       
  1966                                                  $this->mktime(0, $minute,       0, 1, 1, 1970, true), false);
       
  1967                 }
       
  1968 
       
  1969                 require_once 'Zend/Date/Exception.php';
       
  1970                 throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
       
  1971                 break;
       
  1972 
       
  1973             case self::SECOND:
       
  1974                 if (is_numeric($date)) {
       
  1975                     return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
       
  1976                                                  $this->mktime(0, 0, $second,       1, 1, 1970, true), false);
       
  1977                 }
       
  1978 
       
  1979                 require_once 'Zend/Date/Exception.php';
       
  1980                 throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
       
  1981                 break;
       
  1982 
       
  1983             case self::MILLISECOND:
       
  1984                 if (is_numeric($date)) {
       
  1985                     switch($calc) {
       
  1986                         case 'set' :
       
  1987                             return $this->setMillisecond($date);
       
  1988                             break;
       
  1989                         case 'add' :
       
  1990                             return $this->addMillisecond($date);
       
  1991                             break;
       
  1992                         case 'sub' :
       
  1993                             return $this->subMillisecond($date);
       
  1994                             break;
       
  1995                     }
       
  1996 
       
  1997                     return $this->compareMillisecond($date);
       
  1998                 }
       
  1999 
       
  2000                 require_once 'Zend/Date/Exception.php';
       
  2001                 throw new Zend_Date_Exception("invalid date ($date) operand, milliseconds expected", 0, null, $date);
       
  2002                 break;
       
  2003 
       
  2004             case self::MINUTE_SHORT:
       
  2005                 if (is_numeric($date)) {
       
  2006                     return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
       
  2007                                                  $this->mktime(0, $minute,       0, 1, 1, 1970, true), false);
       
  2008                 }
       
  2009 
       
  2010                 require_once 'Zend/Date/Exception.php';
       
  2011                 throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
       
  2012                 break;
       
  2013 
       
  2014             case self::SECOND_SHORT:
       
  2015                 if (is_numeric($date)) {
       
  2016                     return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
       
  2017                                                  $this->mktime(0, 0, $second,       1, 1, 1970, true), false);
       
  2018                 }
       
  2019 
       
  2020                 require_once 'Zend/Date/Exception.php';
       
  2021                 throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
       
  2022                 break;
       
  2023 
       
  2024             // timezone formats
       
  2025             // break intentionally omitted
       
  2026             case self::TIMEZONE_NAME:
       
  2027             case self::TIMEZONE:
       
  2028             case self::TIMEZONE_SECS:
       
  2029                 require_once 'Zend/Date/Exception.php';
       
  2030                 throw new Zend_Date_Exception('timezone not supported', 0, null, $date);
       
  2031                 break;
       
  2032 
       
  2033             case self::DAYLIGHT:
       
  2034                 require_once 'Zend/Date/Exception.php';
       
  2035                 throw new Zend_Date_Exception('daylight not supported', 0, null, $date);
       
  2036                 break;
       
  2037 
       
  2038             case self::GMT_DIFF:
       
  2039             case self::GMT_DIFF_SEP:
       
  2040                 require_once 'Zend/Date/Exception.php';
       
  2041                 throw new Zend_Date_Exception('gmtdiff not supported', 0, null, $date);
       
  2042                 break;
       
  2043 
       
  2044             // date strings
       
  2045             case self::ISO_8601:
       
  2046                 // (-)YYYY-MM-dd
       
  2047                 preg_match('/^(-{0,1}\d{4})-(\d{2})-(\d{2})/', $date, $datematch);
       
  2048                 // (-)YY-MM-dd
       
  2049                 if (empty($datematch)) {
       
  2050                     preg_match('/^(-{0,1}\d{2})-(\d{2})-(\d{2})/', $date, $datematch);
       
  2051                 }
       
  2052                 // (-)YYYYMMdd
       
  2053                 if (empty($datematch)) {
       
  2054                     preg_match('/^(-{0,1}\d{4})(\d{2})(\d{2})/', $date, $datematch);
       
  2055                 }
       
  2056                 // (-)YYMMdd
       
  2057                 if (empty($datematch)) {
       
  2058                     preg_match('/^(-{0,1}\d{2})(\d{2})(\d{2})/', $date, $datematch);
       
  2059                 }
       
  2060                 $tmpdate = $date;
       
  2061                 if (!empty($datematch)) {
       
  2062                     $dateMatchCharCount = iconv_strlen($datematch[0], 'UTF-8');
       
  2063                     $tmpdate = iconv_substr($date,
       
  2064                                             $dateMatchCharCount,
       
  2065                                             iconv_strlen($date, 'UTF-8') - $dateMatchCharCount,
       
  2066                                             'UTF-8');
       
  2067                 }
       
  2068                 // (T)hh:mm:ss
       
  2069                 preg_match('/[T,\s]{0,1}(\d{2}):(\d{2}):(\d{2})/', $tmpdate, $timematch);
       
  2070                 if (empty($timematch)) {
       
  2071                     preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
       
  2072                 }
       
  2073                 if (empty($datematch) and empty($timematch)) {
       
  2074                     require_once 'Zend/Date/Exception.php';
       
  2075                     throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", 0, null, $date);
       
  2076                 }
       
  2077                 if (!empty($timematch)) {
       
  2078                     $timeMatchCharCount = iconv_strlen($timematch[0], 'UTF-8');
       
  2079                     $tmpdate = iconv_substr($tmpdate,
       
  2080                                             $timeMatchCharCount,
       
  2081                                             iconv_strlen($tmpdate, 'UTF-8') - $timeMatchCharCount,
       
  2082                                             'UTF-8');
       
  2083                 }
       
  2084                 if (empty($datematch)) {
       
  2085                     $datematch[1] = 1970;
       
  2086                     $datematch[2] = 1;
       
  2087                     $datematch[3] = 1;
       
  2088                 } else if (iconv_strlen($datematch[1], 'UTF-8') == 2) {
       
  2089                     $datematch[1] = self::getFullYear($datematch[1]);
       
  2090                 }
       
  2091                 if (empty($timematch)) {
       
  2092                     $timematch[1] = 0;
       
  2093                     $timematch[2] = 0;
       
  2094                     $timematch[3] = 0;
       
  2095                 }
       
  2096 
       
  2097                 if (($calc == 'set') || ($calc == 'cmp')) {
       
  2098                     --$datematch[2];
       
  2099                     --$month;
       
  2100                     --$datematch[3];
       
  2101                     --$day;
       
  2102                     $datematch[1] -= 1970;
       
  2103                     $year         -= 1970;
       
  2104                 }
       
  2105                 return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false),
       
  2106                                              $this->mktime($hour,         $minute,       $second,       1 + $month,        1 + $day,          1970 + $year,         false), false);
       
  2107                 break;
       
  2108 
       
  2109             case self::RFC_2822:
       
  2110                 $result = preg_match('/^\w{3},\s(\d{1,2})\s(\w{3})\s(\d{4})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4})$/', $date, $match);
       
  2111                 if (!$result) {
       
  2112                     require_once 'Zend/Date/Exception.php';
       
  2113                     throw new Zend_Date_Exception("no RFC 2822 format ($date)", 0, null, $date);
       
  2114                 }
       
  2115 
       
  2116                 $months  = $this->_getDigitFromName($match[2]);
       
  2117 
       
  2118                 if (($calc == 'set') || ($calc == 'cmp')) {
       
  2119                     --$months;
       
  2120                     --$month;
       
  2121                     --$match[1];
       
  2122                     --$day;
       
  2123                     $match[3] -= 1970;
       
  2124                     $year     -= 1970;
       
  2125                 }
       
  2126                 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
       
  2127                                              $this->mktime($hour,     $minute,   $second,   1 + $month,  1 + $day,      1970 + $year,     false), false);
       
  2128                 break;
       
  2129 
       
  2130             case self::TIMESTAMP:
       
  2131                 if (is_numeric($date)) {
       
  2132                     return $this->_assign($calc, $date, $this->getUnixTimestamp());
       
  2133                 }
       
  2134 
       
  2135                 require_once 'Zend/Date/Exception.php';
       
  2136                 throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", 0, null, $date);
       
  2137                 break;
       
  2138 
       
  2139             // additional formats
       
  2140             // break intentionally omitted
       
  2141             case self::ERA:
       
  2142             case self::ERA_NAME:
       
  2143                 require_once 'Zend/Date/Exception.php';
       
  2144                 throw new Zend_Date_Exception('era not supported', 0, null, $date);
       
  2145                 break;
       
  2146 
       
  2147             case self::DATES:
       
  2148                 try {
       
  2149                     $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
       
  2150 
       
  2151                     if (($calc == 'set') || ($calc == 'cmp')) {
       
  2152                         --$parsed['month'];
       
  2153                         --$month;
       
  2154                         --$parsed['day'];
       
  2155                         --$day;
       
  2156                         $parsed['year'] -= 1970;
       
  2157                         $year  -= 1970;
       
  2158                     }
       
  2159 
       
  2160                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2161                                                  $this->mktime(0, 0, 0, 1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2162                 } catch (Zend_Locale_Exception $e) {
       
  2163                     require_once 'Zend/Date/Exception.php';
       
  2164                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2165                 }
       
  2166                 break;
       
  2167 
       
  2168             case self::DATE_FULL:
       
  2169                 try {
       
  2170                     $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
       
  2171                     $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2172 
       
  2173                     if (($calc == 'set') || ($calc == 'cmp')) {
       
  2174                         --$parsed['month'];
       
  2175                         --$month;
       
  2176                         --$parsed['day'];
       
  2177                         --$day;
       
  2178                         $parsed['year'] -= 1970;
       
  2179                         $year  -= 1970;
       
  2180                     }
       
  2181                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2182                                                  $this->mktime(0, 0, 0, 1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2183                 } catch (Zend_Locale_Exception $e) {
       
  2184                     require_once 'Zend/Date/Exception.php';
       
  2185                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2186                 }
       
  2187                 break;
       
  2188 
       
  2189             case self::DATE_LONG:
       
  2190                 try {
       
  2191                     $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
       
  2192                     $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2193 
       
  2194                     if (($calc == 'set') || ($calc == 'cmp')){
       
  2195                         --$parsed['month'];
       
  2196                         --$month;
       
  2197                         --$parsed['day'];
       
  2198                         --$day;
       
  2199                         $parsed['year'] -= 1970;
       
  2200                         $year  -= 1970;
       
  2201                     }
       
  2202                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2203                                                  $this->mktime(0, 0, 0, 1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2204                 } catch (Zend_Locale_Exception $e) {
       
  2205                     require_once 'Zend/Date/Exception.php';
       
  2206                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2207                 }
       
  2208                 break;
       
  2209 
       
  2210             case self::DATE_MEDIUM:
       
  2211                 try {
       
  2212                     $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
       
  2213                     $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2214 
       
  2215                     if (($calc == 'set') || ($calc == 'cmp')) {
       
  2216                         --$parsed['month'];
       
  2217                         --$month;
       
  2218                         --$parsed['day'];
       
  2219                         --$day;
       
  2220                         $parsed['year'] -= 1970;
       
  2221                         $year  -= 1970;
       
  2222                     }
       
  2223                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2224                                                  $this->mktime(0, 0, 0, 1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2225                 } catch (Zend_Locale_Exception $e) {
       
  2226                     require_once 'Zend/Date/Exception.php';
       
  2227                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2228                 }
       
  2229                 break;
       
  2230 
       
  2231             case self::DATE_SHORT:
       
  2232                 try {
       
  2233                     $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
       
  2234                     $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2235 
       
  2236                     $parsed['year'] = self::getFullYear($parsed['year']);
       
  2237 
       
  2238                     if (($calc == 'set') || ($calc == 'cmp')) {
       
  2239                         --$parsed['month'];
       
  2240                         --$month;
       
  2241                         --$parsed['day'];
       
  2242                         --$day;
       
  2243                         $parsed['year'] -= 1970;
       
  2244                         $year  -= 1970;
       
  2245                     }
       
  2246                     return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2247                                                  $this->mktime(0, 0, 0, 1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2248                 } catch (Zend_Locale_Exception $e) {
       
  2249                     require_once 'Zend/Date/Exception.php';
       
  2250                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2251                 }
       
  2252                 break;
       
  2253 
       
  2254             case self::TIMES:
       
  2255                 try {
       
  2256                     if ($calc != 'set') {
       
  2257                         $month = 1;
       
  2258                         $day   = 1;
       
  2259                         $year  = 1970;
       
  2260                     }
       
  2261                     $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
       
  2262                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
       
  2263                                                  $this->mktime($hour,           $minute,           $second,           $month, $day, $year, true), false);
       
  2264                 } catch (Zend_Locale_Exception $e) {
       
  2265                     require_once 'Zend/Date/Exception.php';
       
  2266                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2267                 }
       
  2268                 break;
       
  2269 
       
  2270             case self::TIME_FULL:
       
  2271                 try {
       
  2272                     $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
       
  2273                     $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2274                     if ($calc != 'set') {
       
  2275                         $month = 1;
       
  2276                         $day   = 1;
       
  2277                         $year  = 1970;
       
  2278                     }
       
  2279 
       
  2280                     if (!isset($parsed['second'])) {
       
  2281                         $parsed['second'] = 0;
       
  2282                     }
       
  2283 
       
  2284                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
       
  2285                                                  $this->mktime($hour,           $minute,           $second,           $month, $day, $year, true), false);
       
  2286                 } catch (Zend_Locale_Exception $e) {
       
  2287                     require_once 'Zend/Date/Exception.php';
       
  2288                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2289                 }
       
  2290                 break;
       
  2291 
       
  2292             case self::TIME_LONG:
       
  2293                 try {
       
  2294                     $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
       
  2295                     $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2296                     if ($calc != 'set') {
       
  2297                         $month = 1;
       
  2298                         $day   = 1;
       
  2299                         $year  = 1970;
       
  2300                     }
       
  2301                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
       
  2302                                                  $this->mktime($hour,           $minute,           $second,           $month, $day, $year, true), false);
       
  2303                 } catch (Zend_Locale_Exception $e) {
       
  2304                     require_once 'Zend/Date/Exception.php';
       
  2305                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2306                 }
       
  2307                 break;
       
  2308 
       
  2309             case self::TIME_MEDIUM:
       
  2310                 try {
       
  2311                     $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
       
  2312                     $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2313                     if ($calc != 'set') {
       
  2314                         $month = 1;
       
  2315                         $day   = 1;
       
  2316                         $year  = 1970;
       
  2317                     }
       
  2318                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
       
  2319                                                  $this->mktime($hour,           $minute,           $second,           $month, $day, $year, true), false);
       
  2320                 } catch (Zend_Locale_Exception $e) {
       
  2321                     require_once 'Zend/Date/Exception.php';
       
  2322                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2323                 }
       
  2324                 break;
       
  2325 
       
  2326             case self::TIME_SHORT:
       
  2327                 try {
       
  2328                     $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
       
  2329                     $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2330                     if ($calc != 'set') {
       
  2331                         $month = 1;
       
  2332                         $day   = 1;
       
  2333                         $year  = 1970;
       
  2334                     }
       
  2335 
       
  2336                     if (!isset($parsed['second'])) {
       
  2337                         $parsed['second'] = 0;
       
  2338                     }
       
  2339 
       
  2340                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
       
  2341                                                  $this->mktime($hour,           $minute,           $second,           $month, $day, $year, true), false);
       
  2342                 } catch (Zend_Locale_Exception $e) {
       
  2343                     require_once 'Zend/Date/Exception.php';
       
  2344                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2345                 }
       
  2346                 break;
       
  2347 
       
  2348             case self::DATETIME:
       
  2349                 try {
       
  2350                     $parsed = Zend_Locale_Format::getDateTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
       
  2351                     if (($calc == 'set') || ($calc == 'cmp')) {
       
  2352                         --$parsed['month'];
       
  2353                         --$month;
       
  2354                         --$parsed['day'];
       
  2355                         --$day;
       
  2356                         $parsed['year'] -= 1970;
       
  2357                         $year  -= 1970;
       
  2358                     }
       
  2359                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2360                                                  $this->mktime($hour,           $minute,           $second,           1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2361                 } catch (Zend_Locale_Exception $e) {
       
  2362                     require_once 'Zend/Date/Exception.php';
       
  2363                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2364                 }
       
  2365                 break;
       
  2366 
       
  2367             case self::DATETIME_FULL:
       
  2368                 try {
       
  2369                     $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
       
  2370                     $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2371 
       
  2372                     if (($calc == 'set') || ($calc == 'cmp')) {
       
  2373                         --$parsed['month'];
       
  2374                         --$month;
       
  2375                         --$parsed['day'];
       
  2376                         --$day;
       
  2377                         $parsed['year'] -= 1970;
       
  2378                         $year  -= 1970;
       
  2379                     }
       
  2380 
       
  2381                     if (!isset($parsed['second'])) {
       
  2382                         $parsed['second'] = 0;
       
  2383                     }
       
  2384 
       
  2385                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2386                                                  $this->mktime($hour,           $minute,           $second,           1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2387                 } catch (Zend_Locale_Exception $e) {
       
  2388                     require_once 'Zend/Date/Exception.php';
       
  2389                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2390                 }
       
  2391                 break;
       
  2392 
       
  2393             case self::DATETIME_LONG:
       
  2394                 try {
       
  2395                     $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
       
  2396                     $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2397 
       
  2398                     if (($calc == 'set') || ($calc == 'cmp')){
       
  2399                         --$parsed['month'];
       
  2400                         --$month;
       
  2401                         --$parsed['day'];
       
  2402                         --$day;
       
  2403                         $parsed['year'] -= 1970;
       
  2404                         $year  -= 1970;
       
  2405                     }
       
  2406                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2407                                                  $this->mktime($hour,           $minute,           $second,           1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2408                 } catch (Zend_Locale_Exception $e) {
       
  2409                     require_once 'Zend/Date/Exception.php';
       
  2410                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2411                 }
       
  2412                 break;
       
  2413 
       
  2414             case self::DATETIME_MEDIUM:
       
  2415                 try {
       
  2416                     $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
       
  2417                     $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2418                     if (($calc == 'set') || ($calc == 'cmp')) {
       
  2419                         --$parsed['month'];
       
  2420                         --$month;
       
  2421                         --$parsed['day'];
       
  2422                         --$day;
       
  2423                         $parsed['year'] -= 1970;
       
  2424                         $year  -= 1970;
       
  2425                     }
       
  2426                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2427                                                  $this->mktime($hour,           $minute,           $second,           1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2428                 } catch (Zend_Locale_Exception $e) {
       
  2429                     require_once 'Zend/Date/Exception.php';
       
  2430                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2431                 }
       
  2432                 break;
       
  2433 
       
  2434             case self::DATETIME_SHORT:
       
  2435                 try {
       
  2436                     $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
       
  2437                     $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
       
  2438 
       
  2439                     $parsed['year'] = self::getFullYear($parsed['year']);
       
  2440 
       
  2441                     if (($calc == 'set') || ($calc == 'cmp')) {
       
  2442                         --$parsed['month'];
       
  2443                         --$month;
       
  2444                         --$parsed['day'];
       
  2445                         --$day;
       
  2446                         $parsed['year'] -= 1970;
       
  2447                         $year  -= 1970;
       
  2448                     }
       
  2449 
       
  2450                     if (!isset($parsed['second'])) {
       
  2451                         $parsed['second'] = 0;
       
  2452                     }
       
  2453 
       
  2454                     return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
       
  2455                                                  $this->mktime($hour,           $minute,           $second,           1 + $month,           1 + $day,           1970 + $year,           true), $hour);
       
  2456                 } catch (Zend_Locale_Exception $e) {
       
  2457                     require_once 'Zend/Date/Exception.php';
       
  2458                     throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2459                 }
       
  2460                 break;
       
  2461 
       
  2462             // ATOM and RFC_3339 are identical
       
  2463             case self::ATOM:
       
  2464             case self::RFC_3339:
       
  2465                 $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\d{0,4}([+-]{1}\d{2}:\d{2}|Z)$/', $date, $match);
       
  2466                 if (!$result) {
       
  2467                     require_once 'Zend/Date/Exception.php';
       
  2468                     throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", 0, null, $date);
       
  2469                 }
       
  2470 
       
  2471                 if (($calc == 'set') || ($calc == 'cmp')) {
       
  2472                     --$match[2];
       
  2473                     --$month;
       
  2474                     --$match[3];
       
  2475                     --$day;
       
  2476                     $match[1] -= 1970;
       
  2477                     $year     -= 1970;
       
  2478                 }
       
  2479                 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
       
  2480                                              $this->mktime($hour,     $minute,   $second,   1 + $month,    1 + $day,      1970 + $year,     true), false);
       
  2481                 break;
       
  2482 
       
  2483             case self::COOKIE:
       
  2484                 $result = preg_match("/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,20}$/", $date, $match);
       
  2485                 if (!$result) {
       
  2486                     require_once 'Zend/Date/Exception.php';
       
  2487                     throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", 0, null, $date);
       
  2488                 }
       
  2489                 $matchStartPos = iconv_strpos($match[0], ' ', 0, 'UTF-8') + 1;
       
  2490                 $match[0] = iconv_substr($match[0],
       
  2491                                          $matchStartPos,
       
  2492                                          iconv_strlen($match[0], 'UTF-8') - $matchStartPos,
       
  2493                                          'UTF-8');
       
  2494 
       
  2495                 $months    = $this->_getDigitFromName($match[2]);
       
  2496                 $match[3] = self::getFullYear($match[3]);
       
  2497 
       
  2498                 if (($calc == 'set') || ($calc == 'cmp')) {
       
  2499                     --$months;
       
  2500                     --$month;
       
  2501                     --$match[1];
       
  2502                     --$day;
       
  2503                     $match[3] -= 1970;
       
  2504                     $year     -= 1970;
       
  2505                 }
       
  2506                 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
       
  2507                                              $this->mktime($hour,     $minute,   $second,   1 + $month,  1 + $day,      1970 + $year,     true), false);
       
  2508                 break;
       
  2509 
       
  2510             case self::RFC_822:
       
  2511             case self::RFC_1036:
       
  2512                 // new RFC 822 format, identical to RFC 1036 standard
       
  2513                 $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
       
  2514                 if (!$result) {
       
  2515                     require_once 'Zend/Date/Exception.php';
       
  2516                     throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", 0, null, $date);
       
  2517                 }
       
  2518 
       
  2519                 $months    = $this->_getDigitFromName($match[2]);
       
  2520                 $match[3] = self::getFullYear($match[3]);
       
  2521 
       
  2522                 if (($calc == 'set') || ($calc == 'cmp')) {
       
  2523                     --$months;
       
  2524                     --$month;
       
  2525                     --$match[1];
       
  2526                     --$day;
       
  2527                     $match[3] -= 1970;
       
  2528                     $year     -= 1970;
       
  2529                 }
       
  2530                 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
       
  2531                                              $this->mktime($hour,     $minute,   $second,   1 + $month,  1 + $day,      1970 + $year,     false), false);
       
  2532                 break;
       
  2533 
       
  2534             case self::RFC_850:
       
  2535                 $result = preg_match('/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,21}$/', $date, $match);
       
  2536                 if (!$result) {
       
  2537                     require_once 'Zend/Date/Exception.php';
       
  2538                     throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", 0, null, $date);
       
  2539                 }
       
  2540 
       
  2541                 $months    = $this->_getDigitFromName($match[2]);
       
  2542                 $match[3] = self::getFullYear($match[3]);
       
  2543 
       
  2544                 if (($calc == 'set') || ($calc == 'cmp')) {
       
  2545                     --$months;
       
  2546                     --$month;
       
  2547                     --$match[1];
       
  2548                     --$day;
       
  2549                     $match[3] -= 1970;
       
  2550                     $year     -= 1970;
       
  2551                 }
       
  2552                 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
       
  2553                                              $this->mktime($hour,     $minute,   $second,   1 + $month,  1 + $day,      1970 + $year,     true), false);
       
  2554                 break;
       
  2555 
       
  2556             case self::RFC_1123:
       
  2557                 $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2,4})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
       
  2558                 if (!$result) {
       
  2559                     require_once 'Zend/Date/Exception.php';
       
  2560                     throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", 0, null, $date);
       
  2561                 }
       
  2562 
       
  2563                 $months  = $this->_getDigitFromName($match[2]);
       
  2564 
       
  2565                 if (($calc == 'set') || ($calc == 'cmp')) {
       
  2566                     --$months;
       
  2567                     --$month;
       
  2568                     --$match[1];
       
  2569                     --$day;
       
  2570                     $match[3] -= 1970;
       
  2571                     $year     -= 1970;
       
  2572                 }
       
  2573                 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
       
  2574                                              $this->mktime($hour,     $minute,   $second,   1 + $month,  1 + $day,      1970 + $year,     true), false);
       
  2575                 break;
       
  2576 
       
  2577             case self::RSS:
       
  2578                 $result = preg_match('/^\w{3},\s(\d{2})\s(\w{3})\s(\d{2,4})\s(\d{1,2}):(\d{2}):(\d{2})\s.{1,21}$/', $date, $match);
       
  2579                 if (!$result) {
       
  2580                     require_once 'Zend/Date/Exception.php';
       
  2581                     throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", 0, null, $date);
       
  2582                 }
       
  2583 
       
  2584                 $months  = $this->_getDigitFromName($match[2]);
       
  2585                 $match[3] = self::getFullYear($match[3]);
       
  2586 
       
  2587                 if (($calc == 'set') || ($calc == 'cmp')) {
       
  2588                     --$months;
       
  2589                     --$month;
       
  2590                     --$match[1];
       
  2591                     --$day;
       
  2592                     $match[3] -= 1970;
       
  2593                     $year  -= 1970;
       
  2594                 }
       
  2595                 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
       
  2596                                              $this->mktime($hour,     $minute,   $second,   1 + $month,  1 + $day,      1970 + $year,     true), false);
       
  2597                 break;
       
  2598 
       
  2599             case self::W3C:
       
  2600                 $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
       
  2601                 if (!$result) {
       
  2602                     require_once 'Zend/Date/Exception.php';
       
  2603                     throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", 0, null, $date);
       
  2604                 }
       
  2605 
       
  2606                 if (($calc == 'set') || ($calc == 'cmp')) {
       
  2607                     --$match[2];
       
  2608                     --$month;
       
  2609                     --$match[3];
       
  2610                     --$day;
       
  2611                     $match[1] -= 1970;
       
  2612                     $year     -= 1970;
       
  2613                 }
       
  2614                 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
       
  2615                                              $this->mktime($hour,     $minute,   $second,   1 + $month,    1 + $day,      1970 + $year,     true), false);
       
  2616                 break;
       
  2617 
       
  2618             default:
       
  2619                 if (!is_numeric($date) || !empty($part)) {
       
  2620                     try {
       
  2621                         if (empty($part)) {
       
  2622                             $part  = Zend_Locale_Format::getDateFormat($locale) . " ";
       
  2623                             $part .= Zend_Locale_Format::getTimeFormat($locale);
       
  2624                         }
       
  2625 
       
  2626                         $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
       
  2627                         if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
       
  2628                             $parsed['year'] = self::getFullYear($parsed['year']);
       
  2629                         }
       
  2630 
       
  2631                         if (($calc == 'set') || ($calc == 'cmp')) {
       
  2632                             if (isset($parsed['month'])) {
       
  2633                                 --$parsed['month'];
       
  2634                             } else {
       
  2635                                 $parsed['month'] = 0;
       
  2636                             }
       
  2637 
       
  2638                             if (isset($parsed['day'])) {
       
  2639                                 --$parsed['day'];
       
  2640                             } else {
       
  2641                                 $parsed['day'] = 0;
       
  2642                             }
       
  2643 
       
  2644                             if (isset($parsed['year'])) {
       
  2645                                 $parsed['year'] -= 1970;
       
  2646                             } else {
       
  2647                                 $parsed['year'] = 0;
       
  2648                             }
       
  2649                         }
       
  2650 
       
  2651                         return $this->_assign($calc, $this->mktime(
       
  2652                             isset($parsed['hour']) ? $parsed['hour'] : 0,
       
  2653                             isset($parsed['minute']) ? $parsed['minute'] : 0,
       
  2654                             isset($parsed['second']) ? $parsed['second'] : 0,
       
  2655                             isset($parsed['month']) ? (1 + $parsed['month']) : 1,
       
  2656                             isset($parsed['day']) ? (1 + $parsed['day']) : 1,
       
  2657                             isset($parsed['year']) ? (1970 + $parsed['year']) : 1970,
       
  2658                             false), $this->getUnixTimestamp(), false);
       
  2659                     } catch (Zend_Locale_Exception $e) {
       
  2660                         if (!is_numeric($date)) {
       
  2661                             require_once 'Zend/Date/Exception.php';
       
  2662                             throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
       
  2663                         }
       
  2664                     }
       
  2665                 }
       
  2666 
       
  2667                 return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
       
  2668                 break;
       
  2669         }
       
  2670     }
       
  2671 
       
  2672     /**
       
  2673      * Returns true when both date objects or date parts are equal.
       
  2674      * For example:
       
  2675      * 15.May.2000 <-> 15.June.2000 Equals only for Day or Year... all other will return false
       
  2676      *
       
  2677      * @param  string|integer|array|Zend_Date  $date    Date or datepart to equal with
       
  2678      * @param  string                          $part    OPTIONAL Part of the date to compare, if null the timestamp is used
       
  2679      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  2680      * @return boolean
       
  2681      * @throws Zend_Date_Exception
       
  2682      */
       
  2683     public function equals($date, $part = self::TIMESTAMP, $locale = null)
       
  2684     {
       
  2685         $result = $this->compare($date, $part, $locale);
       
  2686 
       
  2687         if ($result == 0) {
       
  2688             return true;
       
  2689         }
       
  2690 
       
  2691         return false;
       
  2692     }
       
  2693 
       
  2694     /**
       
  2695      * Returns if the given date or datepart is earlier
       
  2696      * For example:
       
  2697      * 15.May.2000 <-> 13.June.1999 will return true for day, year and date, but not for month
       
  2698      *
       
  2699      * @param  string|integer|array|Zend_Date  $date    Date or datepart to compare with
       
  2700      * @param  string                          $part    OPTIONAL Part of the date to compare, if null the timestamp is used
       
  2701      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  2702      * @return boolean
       
  2703      * @throws Zend_Date_Exception
       
  2704      */
       
  2705     public function isEarlier($date, $part = null, $locale = null)
       
  2706     {
       
  2707         $result = $this->compare($date, $part, $locale);
       
  2708 
       
  2709         if ($result == -1) {
       
  2710             return true;
       
  2711         }
       
  2712 
       
  2713         return false;
       
  2714     }
       
  2715 
       
  2716     /**
       
  2717      * Returns if the given date or datepart is later
       
  2718      * For example:
       
  2719      * 15.May.2000 <-> 13.June.1999 will return true for month but false for day, year and date
       
  2720      * Returns if the given date is later
       
  2721      *
       
  2722      * @param  string|integer|array|Zend_Date  $date    Date or datepart to compare with
       
  2723      * @param  string                          $part    OPTIONAL Part of the date to compare, if null the timestamp is used
       
  2724      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  2725      * @return boolean
       
  2726      * @throws Zend_Date_Exception
       
  2727      */
       
  2728     public function isLater($date, $part = null, $locale = null)
       
  2729     {
       
  2730         $result = $this->compare($date, $part, $locale);
       
  2731 
       
  2732         if ($result == 1) {
       
  2733             return true;
       
  2734         }
       
  2735 
       
  2736         return false;
       
  2737     }
       
  2738 
       
  2739     /**
       
  2740      * Returns only the time of the date as new Zend_Date object
       
  2741      * For example:
       
  2742      * 15.May.2000 10:11:23 will return a dateobject equal to 01.Jan.1970 10:11:23
       
  2743      *
       
  2744      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
  2745      * @return Zend_Date
       
  2746      */
       
  2747     public function getTime($locale = null)
       
  2748     {
       
  2749         if (self::$_options['format_type'] == 'php') {
       
  2750             $format = 'H:i:s';
       
  2751         } else {
       
  2752             $format = self::TIME_MEDIUM;
       
  2753         }
       
  2754 
       
  2755         return $this->copyPart($format, $locale);
       
  2756     }
       
  2757 
       
  2758     /**
       
  2759      * Returns the calculated time
       
  2760      *
       
  2761      * @param  string                    $calc    Calculation to make
       
  2762      * @param  string|integer|array|Zend_Date  $time    Time to calculate with, if null the actual time is taken
       
  2763      * @param  string                          $format  Timeformat for parsing input
       
  2764      * @param  string|Zend_Locale              $locale  Locale for parsing input
       
  2765      * @return integer|Zend_Date  new time
       
  2766      * @throws Zend_Date_Exception
       
  2767      */
       
  2768     private function _time($calc, $time, $format, $locale)
       
  2769     {
       
  2770         if ($time === null) {
       
  2771             require_once 'Zend/Date/Exception.php';
       
  2772             throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
       
  2773         }
       
  2774 
       
  2775         if ($time instanceof Zend_Date) {
       
  2776             // extract time from object
       
  2777             $time = $time->toString('HH:mm:ss', 'iso');
       
  2778         } else {
       
  2779             if (is_array($time)) {
       
  2780                 if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
       
  2781                     (isset($time['second']) === true)) {
       
  2782                     $parsed = $time;
       
  2783                 } else {
       
  2784                     require_once 'Zend/Date/Exception.php';
       
  2785                     throw new Zend_Date_Exception("no hour, minute or second given in array");
       
  2786                 }
       
  2787             } else {
       
  2788                 if (self::$_options['format_type'] == 'php') {
       
  2789                     $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
       
  2790                 }
       
  2791                 try {
       
  2792                     if ($locale === null) {
       
  2793                         $locale = $this->getLocale();
       
  2794                     }
       
  2795 
       
  2796                     $parsed = Zend_Locale_Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
       
  2797                 } catch (Zend_Locale_Exception $e) {
       
  2798                     require_once 'Zend/Date/Exception.php';
       
  2799                     throw new Zend_Date_Exception($e->getMessage(), 0, $e);
       
  2800                 }
       
  2801             }
       
  2802 
       
  2803             if (!array_key_exists('hour', $parsed)) {
       
  2804                 $parsed['hour'] = 0;
       
  2805             }
       
  2806 
       
  2807             if (!array_key_exists('minute', $parsed)) {
       
  2808                 $parsed['minute'] = 0;
       
  2809             }
       
  2810 
       
  2811             if (!array_key_exists('second', $parsed)) {
       
  2812                 $parsed['second'] = 0;
       
  2813             }
       
  2814 
       
  2815             $time  = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT) . ":";
       
  2816             $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT) . ":";
       
  2817             $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT);
       
  2818         }
       
  2819 
       
  2820         $return = $this->_calcdetail($calc, $time, self::TIMES, 'de');
       
  2821         if ($calc != 'cmp') {
       
  2822             return $this;
       
  2823         }
       
  2824 
       
  2825         return $return;
       
  2826     }
       
  2827 
       
  2828 
       
  2829     /**
       
  2830      * Sets a new time for the date object. Format defines how to parse the time string.
       
  2831      * Also a complete date can be given, but only the time is used for setting.
       
  2832      * For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
       
  2833      * Returned is the new date object and the existing date is left as it was before
       
  2834      *
       
  2835      * @param  string|integer|array|Zend_Date  $time    Time to set
       
  2836      * @param  string                          $format  OPTIONAL Timeformat for parsing input
       
  2837      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  2838      * @return Zend_Date Provides fluid interface
       
  2839      * @throws Zend_Date_Exception
       
  2840      */
       
  2841     public function setTime($time, $format = null, $locale = null)
       
  2842     {
       
  2843         return $this->_time('set', $time, $format, $locale);
       
  2844     }
       
  2845 
       
  2846 
       
  2847     /**
       
  2848      * Adds a time to the existing date. Format defines how to parse the time string.
       
  2849      * If only parts are given the other parts are set to 0.
       
  2850      * If no format is given, the standardformat of this locale is used.
       
  2851      * For example: HH:mm:ss -> 10 -> +10 hours
       
  2852      *
       
  2853      * @param  string|integer|array|Zend_Date  $time    Time to add
       
  2854      * @param  string                          $format  OPTIONAL Timeformat for parsing input
       
  2855      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  2856      * @return Zend_Date Provides fluid interface
       
  2857      * @throws Zend_Date_Exception
       
  2858      */
       
  2859     public function addTime($time, $format = null, $locale = null)
       
  2860     {
       
  2861         return $this->_time('add', $time, $format, $locale);
       
  2862     }
       
  2863 
       
  2864 
       
  2865     /**
       
  2866      * Subtracts a time from the existing date. Format defines how to parse the time string.
       
  2867      * If only parts are given the other parts are set to 0.
       
  2868      * If no format is given, the standardformat of this locale is used.
       
  2869      * For example: HH:mm:ss -> 10 -> -10 hours
       
  2870      *
       
  2871      * @param  string|integer|array|Zend_Date  $time    Time to sub
       
  2872      * @param  string                          $format  OPTIONAL Timeformat for parsing input
       
  2873      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  2874      * @return Zend_Date Provides fluid inteface
       
  2875      * @throws Zend_Date_Exception
       
  2876      */
       
  2877     public function subTime($time, $format = null, $locale = null)
       
  2878     {
       
  2879         return $this->_time('sub', $time, $format, $locale);
       
  2880     }
       
  2881 
       
  2882 
       
  2883     /**
       
  2884      * Compares the time from the existing date. Format defines how to parse the time string.
       
  2885      * If only parts are given the other parts are set to default.
       
  2886      * If no format us given, the standardformat of this locale is used.
       
  2887      * For example: HH:mm:ss -> 10 -> 10 hours
       
  2888      *
       
  2889      * @param  string|integer|array|Zend_Date  $time    Time to compare
       
  2890      * @param  string                          $format  OPTIONAL Timeformat for parsing input
       
  2891      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  2892      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  2893      * @throws Zend_Date_Exception
       
  2894      */
       
  2895     public function compareTime($time, $format = null, $locale = null)
       
  2896     {
       
  2897         return $this->_time('cmp', $time, $format, $locale);
       
  2898     }
       
  2899 
       
  2900     /**
       
  2901      * Returns a clone of $this, with the time part set to 00:00:00.
       
  2902      *
       
  2903      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
  2904      * @return Zend_Date
       
  2905      */
       
  2906     public function getDate($locale = null)
       
  2907     {
       
  2908         $orig = self::$_options['format_type'];
       
  2909         if (self::$_options['format_type'] == 'php') {
       
  2910             self::$_options['format_type'] = 'iso';
       
  2911         }
       
  2912 
       
  2913         $date = $this->copyPart(self::DATE_MEDIUM, $locale);
       
  2914         $date->addTimestamp($this->getGmtOffset());
       
  2915         self::$_options['format_type'] = $orig;
       
  2916 
       
  2917         return $date;
       
  2918     }
       
  2919 
       
  2920     /**
       
  2921      * Returns the calculated date
       
  2922      *
       
  2923      * @param  string                          $calc    Calculation to make
       
  2924      * @param  string|integer|array|Zend_Date  $date    Date to calculate with, if null the actual date is taken
       
  2925      * @param  string                          $format  Date format for parsing
       
  2926      * @param  string|Zend_Locale              $locale  Locale for parsing input
       
  2927      * @return integer|Zend_Date  new date
       
  2928      * @throws Zend_Date_Exception
       
  2929      */
       
  2930     private function _date($calc, $date, $format, $locale)
       
  2931     {
       
  2932         if ($date === null) {
       
  2933             require_once 'Zend/Date/Exception.php';
       
  2934             throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
       
  2935         }
       
  2936 
       
  2937         if ($date instanceof Zend_Date) {
       
  2938             // extract date from object
       
  2939             $date = $date->toString('d.M.y', 'iso');
       
  2940         } else {
       
  2941             if (is_array($date)) {
       
  2942                 if ((isset($date['year']) === true) or (isset($date['month']) === true) or
       
  2943                     (isset($date['day']) === true)) {
       
  2944                     $parsed = $date;
       
  2945                 } else {
       
  2946                     require_once 'Zend/Date/Exception.php';
       
  2947                     throw new Zend_Date_Exception("no day,month or year given in array");
       
  2948                 }
       
  2949             } else {
       
  2950                 if ((self::$_options['format_type'] == 'php') && !defined($format)) {
       
  2951                     $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
       
  2952                 }
       
  2953                 try {
       
  2954                     if ($locale === null) {
       
  2955                         $locale = $this->getLocale();
       
  2956                     }
       
  2957 
       
  2958                     $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
       
  2959                     if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
       
  2960                         $parsed['year'] = self::getFullYear($parsed['year']);
       
  2961                     }
       
  2962                 } catch (Zend_Locale_Exception $e) {
       
  2963                     require_once 'Zend/Date/Exception.php';
       
  2964                     throw new Zend_Date_Exception($e->getMessage(), 0, $e);
       
  2965                 }
       
  2966             }
       
  2967 
       
  2968             if (!array_key_exists('day', $parsed)) {
       
  2969                 $parsed['day'] = 1;
       
  2970             }
       
  2971 
       
  2972             if (!array_key_exists('month', $parsed)) {
       
  2973                 $parsed['month'] = 1;
       
  2974             }
       
  2975 
       
  2976             if (!array_key_exists('year', $parsed)) {
       
  2977                 $parsed['year'] = 0;
       
  2978             }
       
  2979 
       
  2980             $date  = $parsed['day'] . "." . $parsed['month'] . "." . $parsed['year'];
       
  2981         }
       
  2982 
       
  2983         $return = $this->_calcdetail($calc, $date, self::DATE_MEDIUM, 'de');
       
  2984         if ($calc != 'cmp') {
       
  2985             return $this;
       
  2986         }
       
  2987         return $return;
       
  2988     }
       
  2989 
       
  2990 
       
  2991     /**
       
  2992      * Sets a new date for the date object. Format defines how to parse the date string.
       
  2993      * Also a complete date with time can be given, but only the date is used for setting.
       
  2994      * For example: MMMM.yy HH:mm-> May.07 22:11 => 01.May.07 00:00
       
  2995      * Returned is the new date object and the existing time is left as it was before
       
  2996      *
       
  2997      * @param  string|integer|array|Zend_Date  $date    Date to set
       
  2998      * @param  string                          $format  OPTIONAL Date format for parsing
       
  2999      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3000      * @return Zend_Date Provides fluid interface
       
  3001      * @throws Zend_Date_Exception
       
  3002      */
       
  3003     public function setDate($date, $format = null, $locale = null)
       
  3004     {
       
  3005         return $this->_date('set', $date, $format, $locale);
       
  3006     }
       
  3007 
       
  3008 
       
  3009     /**
       
  3010      * Adds a date to the existing date object. Format defines how to parse the date string.
       
  3011      * If only parts are given the other parts are set to 0.
       
  3012      * If no format is given, the standardformat of this locale is used.
       
  3013      * For example: MM.dd.YYYY -> 10 -> +10 months
       
  3014      *
       
  3015      * @param  string|integer|array|Zend_Date  $date    Date to add
       
  3016      * @param  string                          $format  OPTIONAL Date format for parsing input
       
  3017      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3018      * @return Zend_Date Provides fluid interface
       
  3019      * @throws Zend_Date_Exception
       
  3020      */
       
  3021     public function addDate($date, $format = null, $locale = null)
       
  3022     {
       
  3023         return $this->_date('add', $date, $format, $locale);
       
  3024     }
       
  3025 
       
  3026 
       
  3027     /**
       
  3028      * Subtracts a date from the existing date object. Format defines how to parse the date string.
       
  3029      * If only parts are given the other parts are set to 0.
       
  3030      * If no format is given, the standardformat of this locale is used.
       
  3031      * For example: MM.dd.YYYY -> 10 -> -10 months
       
  3032      * Be aware: Subtracting 2 months is not equal to Adding -2 months !!!
       
  3033      *
       
  3034      * @param  string|integer|array|Zend_Date  $date    Date to sub
       
  3035      * @param  string                          $format  OPTIONAL Date format for parsing input
       
  3036      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3037      * @return Zend_Date Provides fluid interface
       
  3038      * @throws Zend_Date_Exception
       
  3039      */
       
  3040     public function subDate($date, $format = null, $locale = null)
       
  3041     {
       
  3042         return $this->_date('sub', $date, $format, $locale);
       
  3043     }
       
  3044 
       
  3045 
       
  3046     /**
       
  3047      * Compares the date from the existing date object, ignoring the time.
       
  3048      * Format defines how to parse the date string.
       
  3049      * If only parts are given the other parts are set to 0.
       
  3050      * If no format is given, the standardformat of this locale is used.
       
  3051      * For example: 10.01.2000 => 10.02.1999 -> false
       
  3052      *
       
  3053      * @param  string|integer|array|Zend_Date  $date    Date to compare
       
  3054      * @param  string                          $format  OPTIONAL Date format for parsing input
       
  3055      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3056      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  3057      * @throws Zend_Date_Exception
       
  3058      */
       
  3059     public function compareDate($date, $format = null, $locale = null)
       
  3060     {
       
  3061         return $this->_date('cmp', $date, $format, $locale);
       
  3062     }
       
  3063 
       
  3064 
       
  3065     /**
       
  3066      * Returns the full ISO 8601 date from the date object.
       
  3067      * Always the complete ISO 8601 specifiction is used. If an other ISO date is needed
       
  3068      * (ISO 8601 defines several formats) use toString() instead.
       
  3069      * This function does not return the ISO date as object. Use copy() instead.
       
  3070      *
       
  3071      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
  3072      * @return string
       
  3073      */
       
  3074     public function getIso($locale = null)
       
  3075     {
       
  3076         return $this->toString(self::ISO_8601, 'iso', $locale);
       
  3077     }
       
  3078 
       
  3079 
       
  3080     /**
       
  3081      * Sets a new date for the date object. Not given parts are set to default.
       
  3082      * Only supported ISO 8601 formats are accepted.
       
  3083      * For example: 050901 -> 01.Sept.2005 00:00:00, 20050201T10:00:30 -> 01.Feb.2005 10h00m30s
       
  3084      * Returned is the new date object
       
  3085      *
       
  3086      * @param  string|integer|Zend_Date  $date    ISO Date to set
       
  3087      * @param  string|Zend_Locale        $locale  OPTIONAL Locale for parsing input
       
  3088      * @return Zend_Date Provides fluid interface
       
  3089      * @throws Zend_Date_Exception
       
  3090      */
       
  3091     public function setIso($date, $locale = null)
       
  3092     {
       
  3093         return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
       
  3094     }
       
  3095 
       
  3096 
       
  3097     /**
       
  3098      * Adds a ISO date to the date object. Not given parts are set to default.
       
  3099      * Only supported ISO 8601 formats are accepted.
       
  3100      * For example: 050901 -> + 01.Sept.2005 00:00:00, 10:00:00 -> +10h
       
  3101      * Returned is the new date object
       
  3102      *
       
  3103      * @param  string|integer|Zend_Date  $date    ISO Date to add
       
  3104      * @param  string|Zend_Locale        $locale  OPTIONAL Locale for parsing input
       
  3105      * @return Zend_Date Provides fluid interface
       
  3106      * @throws Zend_Date_Exception
       
  3107      */
       
  3108     public function addIso($date, $locale = null)
       
  3109     {
       
  3110         return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
       
  3111     }
       
  3112 
       
  3113 
       
  3114     /**
       
  3115      * Subtracts a ISO date from the date object. Not given parts are set to default.
       
  3116      * Only supported ISO 8601 formats are accepted.
       
  3117      * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
       
  3118      * Returned is the new date object
       
  3119      *
       
  3120      * @param  string|integer|Zend_Date  $date    ISO Date to sub
       
  3121      * @param  string|Zend_Locale        $locale  OPTIONAL Locale for parsing input
       
  3122      * @return Zend_Date Provides fluid interface
       
  3123      * @throws Zend_Date_Exception
       
  3124      */
       
  3125     public function subIso($date, $locale = null)
       
  3126     {
       
  3127         return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
       
  3128     }
       
  3129 
       
  3130 
       
  3131     /**
       
  3132      * Compares a ISO date with the date object. Not given parts are set to default.
       
  3133      * Only supported ISO 8601 formats are accepted.
       
  3134      * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
       
  3135      * Returns if equal, earlier or later
       
  3136      *
       
  3137      * @param  string|integer|Zend_Date  $date    ISO Date to sub
       
  3138      * @param  string|Zend_Locale        $locale  OPTIONAL Locale for parsing input
       
  3139      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  3140      * @throws Zend_Date_Exception
       
  3141      */
       
  3142     public function compareIso($date, $locale = null)
       
  3143     {
       
  3144         return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
       
  3145     }
       
  3146 
       
  3147 
       
  3148     /**
       
  3149      * Returns a RFC 822 compilant datestring from the date object.
       
  3150      * This function does not return the RFC date as object. Use copy() instead.
       
  3151      *
       
  3152      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
  3153      * @return string
       
  3154      */
       
  3155     public function getArpa($locale = null)
       
  3156     {
       
  3157         if (self::$_options['format_type'] == 'php') {
       
  3158             $format = 'D\, d M y H\:i\:s O';
       
  3159         } else {
       
  3160             $format = self::RFC_822;
       
  3161         }
       
  3162 
       
  3163         return $this->toString($format, 'iso', $locale);
       
  3164     }
       
  3165 
       
  3166 
       
  3167     /**
       
  3168      * Sets a RFC 822 date as new date for the date object.
       
  3169      * Only RFC 822 compilant date strings are accepted.
       
  3170      * For example: Sat, 14 Feb 09 00:31:30 +0100
       
  3171      * Returned is the new date object
       
  3172      *
       
  3173      * @param  string|integer|Zend_Date  $date    RFC 822 to set
       
  3174      * @param  string|Zend_Locale        $locale  OPTIONAL Locale for parsing input
       
  3175      * @return Zend_Date Provides fluid interface
       
  3176      * @throws Zend_Date_Exception
       
  3177      */
       
  3178     public function setArpa($date, $locale = null)
       
  3179     {
       
  3180         return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
       
  3181     }
       
  3182 
       
  3183 
       
  3184     /**
       
  3185      * Adds a RFC 822 date to the date object.
       
  3186      * ARPA messages are used in emails or HTTP Headers.
       
  3187      * Only RFC 822 compilant date strings are accepted.
       
  3188      * For example: Sat, 14 Feb 09 00:31:30 +0100
       
  3189      * Returned is the new date object
       
  3190      *
       
  3191      * @param  string|integer|Zend_Date  $date    RFC 822 Date to add
       
  3192      * @param  string|Zend_Locale        $locale  OPTIONAL Locale for parsing input
       
  3193      * @return Zend_Date Provides fluid interface
       
  3194      * @throws Zend_Date_Exception
       
  3195      */
       
  3196     public function addArpa($date, $locale = null)
       
  3197     {
       
  3198         return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
       
  3199     }
       
  3200 
       
  3201 
       
  3202     /**
       
  3203      * Subtracts a RFC 822 date from the date object.
       
  3204      * ARPA messages are used in emails or HTTP Headers.
       
  3205      * Only RFC 822 compilant date strings are accepted.
       
  3206      * For example: Sat, 14 Feb 09 00:31:30 +0100
       
  3207      * Returned is the new date object
       
  3208      *
       
  3209      * @param  string|integer|Zend_Date  $date    RFC 822 Date to sub
       
  3210      * @param  string|Zend_Locale        $locale  OPTIONAL Locale for parsing input
       
  3211      * @return Zend_Date Provides fluid interface
       
  3212      * @throws Zend_Date_Exception
       
  3213      */
       
  3214     public function subArpa($date, $locale = null)
       
  3215     {
       
  3216         return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
       
  3217     }
       
  3218 
       
  3219 
       
  3220     /**
       
  3221      * Compares a RFC 822 compilant date with the date object.
       
  3222      * ARPA messages are used in emails or HTTP Headers.
       
  3223      * Only RFC 822 compilant date strings are accepted.
       
  3224      * For example: Sat, 14 Feb 09 00:31:30 +0100
       
  3225      * Returns if equal, earlier or later
       
  3226      *
       
  3227      * @param  string|integer|Zend_Date  $date    RFC 822 Date to sub
       
  3228      * @param  string|Zend_Locale        $locale  OPTIONAL Locale for parsing input
       
  3229      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  3230      * @throws Zend_Date_Exception
       
  3231      */
       
  3232     public function compareArpa($date, $locale = null)
       
  3233     {
       
  3234         return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
       
  3235     }
       
  3236 
       
  3237 
       
  3238     /**
       
  3239      * Check if location is supported
       
  3240      *
       
  3241      * @param $location array - locations array
       
  3242      * @return $horizon float
       
  3243      */
       
  3244     private function _checkLocation($location)
       
  3245     {
       
  3246         if (!isset($location['longitude']) or !isset($location['latitude'])) {
       
  3247             require_once 'Zend/Date/Exception.php';
       
  3248             throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', 0, null, $location);
       
  3249         }
       
  3250         if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
       
  3251             require_once 'Zend/Date/Exception.php';
       
  3252             throw new Zend_Date_Exception('Longitude must be between -180 and 180', 0, null, $location);
       
  3253         }
       
  3254         if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
       
  3255             require_once 'Zend/Date/Exception.php';
       
  3256             throw new Zend_Date_Exception('Latitude must be between -90 and 90', 0, null, $location);
       
  3257         }
       
  3258 
       
  3259         if (!isset($location['horizon'])){
       
  3260             $location['horizon'] = 'effective';
       
  3261         }
       
  3262 
       
  3263         switch ($location['horizon']) {
       
  3264             case 'civil' :
       
  3265                 return -0.104528;
       
  3266                 break;
       
  3267             case 'nautic' :
       
  3268                 return -0.207912;
       
  3269                 break;
       
  3270             case 'astronomic' :
       
  3271                 return -0.309017;
       
  3272                 break;
       
  3273             default :
       
  3274                 return -0.0145439;
       
  3275                 break;
       
  3276         }
       
  3277     }
       
  3278 
       
  3279 
       
  3280     /**
       
  3281      * Returns the time of sunrise for this date and a given location as new date object
       
  3282      * For a list of cities and correct locations use the class Zend_Date_Cities
       
  3283      *
       
  3284      * @param  $location array - location of sunrise
       
  3285      *                   ['horizon']   -> civil, nautic, astronomical, effective (default)
       
  3286      *                   ['longitude'] -> longitude of location
       
  3287      *                   ['latitude']  -> latitude of location
       
  3288      * @return Zend_Date
       
  3289      * @throws Zend_Date_Exception
       
  3290      */
       
  3291     public function getSunrise($location)
       
  3292     {
       
  3293         $horizon = $this->_checkLocation($location);
       
  3294         $result = clone $this;
       
  3295         $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
       
  3296         return $result;
       
  3297     }
       
  3298 
       
  3299 
       
  3300     /**
       
  3301      * Returns the time of sunset for this date and a given location as new date object
       
  3302      * For a list of cities and correct locations use the class Zend_Date_Cities
       
  3303      *
       
  3304      * @param  $location array - location of sunset
       
  3305      *                   ['horizon']   -> civil, nautic, astronomical, effective (default)
       
  3306      *                   ['longitude'] -> longitude of location
       
  3307      *                   ['latitude']  -> latitude of location
       
  3308      * @return Zend_Date
       
  3309      * @throws Zend_Date_Exception
       
  3310      */
       
  3311     public function getSunset($location)
       
  3312     {
       
  3313         $horizon = $this->_checkLocation($location);
       
  3314         $result = clone $this;
       
  3315         $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
       
  3316         return $result;
       
  3317     }
       
  3318 
       
  3319 
       
  3320     /**
       
  3321      * Returns an array with the sunset and sunrise dates for all horizon types
       
  3322      * For a list of cities and correct locations use the class Zend_Date_Cities
       
  3323      *
       
  3324      * @param  $location array - location of suninfo
       
  3325      *                   ['horizon']   -> civil, nautic, astronomical, effective (default)
       
  3326      *                   ['longitude'] -> longitude of location
       
  3327      *                   ['latitude']  -> latitude of location
       
  3328      * @return array - [sunset|sunrise][effective|civil|nautic|astronomic]
       
  3329      * @throws Zend_Date_Exception
       
  3330      */
       
  3331     public function getSunInfo($location)
       
  3332     {
       
  3333         $suninfo = array();
       
  3334         for ($i = 0; $i < 4; ++$i) {
       
  3335             switch ($i) {
       
  3336                 case 0 :
       
  3337                     $location['horizon'] = 'effective';
       
  3338                     break;
       
  3339                 case 1 :
       
  3340                     $location['horizon'] = 'civil';
       
  3341                     break;
       
  3342                 case 2 :
       
  3343                     $location['horizon'] = 'nautic';
       
  3344                     break;
       
  3345                 case 3 :
       
  3346                     $location['horizon'] = 'astronomic';
       
  3347                     break;
       
  3348             }
       
  3349             $horizon = $this->_checkLocation($location);
       
  3350             $result = clone $this;
       
  3351             $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
       
  3352             $suninfo['sunrise'][$location['horizon']] = $result;
       
  3353             $result = clone $this;
       
  3354             $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
       
  3355             $suninfo['sunset'][$location['horizon']]  = $result;
       
  3356         }
       
  3357         return $suninfo;
       
  3358     }
       
  3359 
       
  3360 
       
  3361     /**
       
  3362      * Check a given year for leap year.
       
  3363      *
       
  3364      * @param  integer|array|Zend_Date  $year  Year to check
       
  3365      * @return boolean
       
  3366      */
       
  3367     public static function checkLeapYear($year)
       
  3368     {
       
  3369         if ($year instanceof Zend_Date) {
       
  3370             $year = (int) $year->toString(self::YEAR, 'iso');
       
  3371         }
       
  3372 
       
  3373         if (is_array($year)) {
       
  3374             if (isset($year['year']) === true) {
       
  3375                 $year = $year['year'];
       
  3376             } else {
       
  3377                 require_once 'Zend/Date/Exception.php';
       
  3378                 throw new Zend_Date_Exception("no year given in array");
       
  3379             }
       
  3380         }
       
  3381 
       
  3382         if (!is_numeric($year)) {
       
  3383             require_once 'Zend/Date/Exception.php';
       
  3384             throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", 0, null, $year);
       
  3385         }
       
  3386 
       
  3387         return (bool) parent::isYearLeapYear($year);
       
  3388     }
       
  3389 
       
  3390 
       
  3391     /**
       
  3392      * Returns true, if the year is a leap year.
       
  3393      *
       
  3394      * @return boolean
       
  3395      */
       
  3396     public function isLeapYear()
       
  3397     {
       
  3398         return self::checkLeapYear($this);
       
  3399     }
       
  3400 
       
  3401 
       
  3402     /**
       
  3403      * Returns if the set date is todays date
       
  3404      *
       
  3405      * @return boolean
       
  3406      */
       
  3407     public function isToday()
       
  3408     {
       
  3409         $today = $this->date('Ymd', $this->_getTime());
       
  3410         $day   = $this->date('Ymd', $this->getUnixTimestamp());
       
  3411         return ($today == $day);
       
  3412     }
       
  3413 
       
  3414 
       
  3415     /**
       
  3416      * Returns if the set date is yesterdays date
       
  3417      *
       
  3418      * @return boolean
       
  3419      */
       
  3420     public function isYesterday()
       
  3421     {
       
  3422         list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
       
  3423         // adjusts for leap days and DST changes that are timezone specific
       
  3424         $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
       
  3425         $day   = $this->date('Ymd', $this->getUnixTimestamp());
       
  3426         return $day == $yesterday;
       
  3427     }
       
  3428 
       
  3429 
       
  3430     /**
       
  3431      * Returns if the set date is tomorrows date
       
  3432      *
       
  3433      * @return boolean
       
  3434      */
       
  3435     public function isTomorrow()
       
  3436     {
       
  3437         list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
       
  3438         // adjusts for leap days and DST changes that are timezone specific
       
  3439         $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
       
  3440         $day   = $this->date('Ymd', $this->getUnixTimestamp());
       
  3441         return $day == $tomorrow;
       
  3442     }
       
  3443 
       
  3444     /**
       
  3445      * Returns the actual date as new date object
       
  3446      *
       
  3447      * @param  string|Zend_Locale        $locale  OPTIONAL Locale for parsing input
       
  3448      * @return Zend_Date
       
  3449      */
       
  3450     public static function now($locale = null)
       
  3451     {
       
  3452         return new Zend_Date(time(), self::TIMESTAMP, $locale);
       
  3453     }
       
  3454 
       
  3455     /**
       
  3456      * Calculate date details
       
  3457      *
       
  3458      * @param  string                          $calc    Calculation to make
       
  3459      * @param  string|integer|array|Zend_Date  $date    Date or Part to calculate
       
  3460      * @param  string                          $part    Datepart for Calculation
       
  3461      * @param  string|Zend_Locale              $locale  Locale for parsing input
       
  3462      * @return integer|string  new date
       
  3463      * @throws Zend_Date_Exception
       
  3464      */
       
  3465     private function _calcdetail($calc, $date, $type, $locale)
       
  3466     {
       
  3467         $old = false;
       
  3468         if (self::$_options['format_type'] == 'php') {
       
  3469             self::$_options['format_type'] = 'iso';
       
  3470             $old = true;
       
  3471         }
       
  3472 
       
  3473         switch($calc) {
       
  3474             case 'set' :
       
  3475                 $return = $this->set($date, $type, $locale);
       
  3476                 break;
       
  3477             case 'add' :
       
  3478                 $return = $this->add($date, $type, $locale);
       
  3479                 break;
       
  3480             case 'sub' :
       
  3481                 $return = $this->sub($date, $type, $locale);
       
  3482                 break;
       
  3483             default :
       
  3484                 $return = $this->compare($date, $type, $locale);
       
  3485                 break;
       
  3486         }
       
  3487 
       
  3488         if ($old) {
       
  3489             self::$_options['format_type'] = 'php';
       
  3490         }
       
  3491 
       
  3492         return $return;
       
  3493     }
       
  3494 
       
  3495     /**
       
  3496      * Internal calculation, returns the requested date type
       
  3497      *
       
  3498      * @param  string                    $calc    Calculation to make
       
  3499      * @param  string|integer|Zend_Date  $value   Datevalue to calculate with, if null the actual value is taken
       
  3500      * @param  string|Zend_Locale        $locale  Locale for parsing input
       
  3501      * @return integer|Zend_Date  new date
       
  3502      * @throws Zend_Date_Exception
       
  3503      */
       
  3504     private function _calcvalue($calc, $value, $type, $parameter, $locale)
       
  3505     {
       
  3506         if ($value === null) {
       
  3507             require_once 'Zend/Date/Exception.php';
       
  3508             throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
       
  3509         }
       
  3510 
       
  3511         if ($locale === null) {
       
  3512             $locale = $this->getLocale();
       
  3513         }
       
  3514 
       
  3515         if ($value instanceof Zend_Date) {
       
  3516             // extract value from object
       
  3517             $value = $value->toString($parameter, 'iso', $locale);
       
  3518         } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
       
  3519             require_once 'Zend/Date/Exception.php';
       
  3520             throw new Zend_Date_Exception("invalid $type ($value) operand", 0, null, $value);
       
  3521         }
       
  3522 
       
  3523         $return = $this->_calcdetail($calc, $value, $parameter, $locale);
       
  3524         if ($calc != 'cmp') {
       
  3525             return $this;
       
  3526         }
       
  3527         return $return;
       
  3528     }
       
  3529 
       
  3530 
       
  3531     /**
       
  3532      * Returns only the year from the date object as new object.
       
  3533      * For example: 10.May.2000 10:30:00 -> 01.Jan.2000 00:00:00
       
  3534      *
       
  3535      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
  3536      * @return Zend_Date
       
  3537      */
       
  3538     public function getYear($locale = null)
       
  3539     {
       
  3540         if (self::$_options['format_type'] == 'php') {
       
  3541             $format = 'Y';
       
  3542         } else {
       
  3543             $format = self::YEAR;
       
  3544         }
       
  3545 
       
  3546         return $this->copyPart($format, $locale);
       
  3547     }
       
  3548 
       
  3549 
       
  3550     /**
       
  3551      * Sets a new year
       
  3552      * If the year is between 0 and 69, 2000 will be set (2000-2069)
       
  3553      * If the year if between 70 and 99, 1999 will be set (1970-1999)
       
  3554      * 3 or 4 digit years are set as expected. If you need to set year 0-99
       
  3555      * use set() instead.
       
  3556      * Returned is the new date object
       
  3557      *
       
  3558      * @param  string|integer|array|Zend_Date  $date    Year to set
       
  3559      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3560      * @return Zend_Date Provides fluid interface
       
  3561      * @throws Zend_Date_Exception
       
  3562      */
       
  3563     public function setYear($year, $locale = null)
       
  3564     {
       
  3565         return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
       
  3566     }
       
  3567 
       
  3568 
       
  3569     /**
       
  3570      * Adds the year to the existing date object
       
  3571      * If the year is between 0 and 69, 2000 will be added (2000-2069)
       
  3572      * If the year if between 70 and 99, 1999 will be added (1970-1999)
       
  3573      * 3 or 4 digit years are added as expected. If you need to add years from 0-99
       
  3574      * use add() instead.
       
  3575      * Returned is the new date object
       
  3576      *
       
  3577      * @param  string|integer|array|Zend_Date  $date    Year to add
       
  3578      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3579      * @return Zend_Date Provides fluid interface
       
  3580      * @throws Zend_Date_Exception
       
  3581      */
       
  3582     public function addYear($year, $locale = null)
       
  3583     {
       
  3584         return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
       
  3585     }
       
  3586 
       
  3587 
       
  3588     /**
       
  3589      * Subs the year from the existing date object
       
  3590      * If the year is between 0 and 69, 2000 will be subtracted (2000-2069)
       
  3591      * If the year if between 70 and 99, 1999 will be subtracted (1970-1999)
       
  3592      * 3 or 4 digit years are subtracted as expected. If you need to subtract years from 0-99
       
  3593      * use sub() instead.
       
  3594      * Returned is the new date object
       
  3595      *
       
  3596      * @param  string|integer|array|Zend_Date  $date    Year to sub
       
  3597      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3598      * @return Zend_Date Provides fluid interface
       
  3599      * @throws Zend_Date_Exception
       
  3600      */
       
  3601     public function subYear($year, $locale = null)
       
  3602     {
       
  3603         return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
       
  3604     }
       
  3605 
       
  3606 
       
  3607     /**
       
  3608      * Compares the year with the existing date object, ignoring other date parts.
       
  3609      * For example: 10.03.2000 -> 15.02.2000 -> true
       
  3610      * Returns if equal, earlier or later
       
  3611      *
       
  3612      * @param  string|integer|array|Zend_Date  $year    Year to compare
       
  3613      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3614      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  3615      * @throws Zend_Date_Exception
       
  3616      */
       
  3617     public function compareYear($year, $locale = null)
       
  3618     {
       
  3619         return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
       
  3620     }
       
  3621 
       
  3622 
       
  3623     /**
       
  3624      * Returns only the month from the date object as new object.
       
  3625      * For example: 10.May.2000 10:30:00 -> 01.May.1970 00:00:00
       
  3626      *
       
  3627      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
  3628      * @return Zend_Date
       
  3629      */
       
  3630     public function getMonth($locale = null)
       
  3631     {
       
  3632         if (self::$_options['format_type'] == 'php') {
       
  3633             $format = 'm';
       
  3634         } else {
       
  3635             $format = self::MONTH;
       
  3636         }
       
  3637 
       
  3638         return $this->copyPart($format, $locale);
       
  3639     }
       
  3640 
       
  3641 
       
  3642     /**
       
  3643      * Returns the calculated month
       
  3644      *
       
  3645      * @param  string                          $calc    Calculation to make
       
  3646      * @param  string|integer|array|Zend_Date  $month   Month to calculate with, if null the actual month is taken
       
  3647      * @param  string|Zend_Locale              $locale  Locale for parsing input
       
  3648      * @return integer|Zend_Date  new time
       
  3649      * @throws Zend_Date_Exception
       
  3650      */
       
  3651     private function _month($calc, $month, $locale)
       
  3652     {
       
  3653         if ($month === null) {
       
  3654             require_once 'Zend/Date/Exception.php';
       
  3655             throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
       
  3656         }
       
  3657 
       
  3658         if ($locale === null) {
       
  3659             $locale = $this->getLocale();
       
  3660         }
       
  3661 
       
  3662         if ($month instanceof Zend_Date) {
       
  3663             // extract month from object
       
  3664             $found = $month->toString(self::MONTH_SHORT, 'iso', $locale);
       
  3665         } else {
       
  3666             if (is_numeric($month)) {
       
  3667                 $found = $month;
       
  3668             } else if (is_array($month)) {
       
  3669                 if (isset($month['month']) === true) {
       
  3670                     $month = $month['month'];
       
  3671                 } else {
       
  3672                     require_once 'Zend/Date/Exception.php';
       
  3673                     throw new Zend_Date_Exception("no month given in array");
       
  3674                 }
       
  3675             } else {
       
  3676                 $monthlist  = Zend_Locale_Data::getList($locale, 'month');
       
  3677                 $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
       
  3678 
       
  3679                 $monthlist = array_merge($monthlist, $monthlist2);
       
  3680                 $found = 0;
       
  3681                 $cnt = 0;
       
  3682                 foreach ($monthlist as $key => $value) {
       
  3683                     if (strtoupper($value) == strtoupper($month)) {
       
  3684                         $found = ($key % 12) + 1;
       
  3685                         break;
       
  3686                     }
       
  3687                     ++$cnt;
       
  3688                 }
       
  3689                 if ($found == 0) {
       
  3690                     foreach ($monthlist2 as $key => $value) {
       
  3691                         if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
       
  3692                             $found = $key + 1;
       
  3693                             break;
       
  3694                         }
       
  3695                         ++$cnt;
       
  3696                     }
       
  3697                 }
       
  3698                 if ($found == 0) {
       
  3699                     require_once 'Zend/Date/Exception.php';
       
  3700                     throw new Zend_Date_Exception("unknown month name ($month)", 0, null, $month);
       
  3701                 }
       
  3702             }
       
  3703         }
       
  3704         $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
       
  3705         if ($calc != 'cmp') {
       
  3706             return $this;
       
  3707         }
       
  3708         return $return;
       
  3709     }
       
  3710 
       
  3711 
       
  3712     /**
       
  3713      * Sets a new month
       
  3714      * The month can be a number or a string. Setting months lower then 0 and greater then 12
       
  3715      * will result in adding or subtracting the relevant year. (12 months equal one year)
       
  3716      * If a localized monthname is given it will be parsed with the default locale or the optional
       
  3717      * set locale.
       
  3718      * Returned is the new date object
       
  3719      *
       
  3720      * @param  string|integer|array|Zend_Date  $month   Month to set
       
  3721      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3722      * @return Zend_Date Provides fluid interface
       
  3723      * @throws Zend_Date_Exception
       
  3724      */
       
  3725     public function setMonth($month, $locale = null)
       
  3726     {
       
  3727         return $this->_month('set', $month, $locale);
       
  3728     }
       
  3729 
       
  3730 
       
  3731     /**
       
  3732      * Adds months to the existing date object.
       
  3733      * The month can be a number or a string. Adding months lower then 0 and greater then 12
       
  3734      * will result in adding or subtracting the relevant year. (12 months equal one year)
       
  3735      * If a localized monthname is given it will be parsed with the default locale or the optional
       
  3736      * set locale.
       
  3737      * Returned is the new date object
       
  3738      *
       
  3739      * @param  string|integer|array|Zend_Date  $month   Month to add
       
  3740      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3741      * @return Zend_Date Provides fluid interface
       
  3742      * @throws Zend_Date_Exception
       
  3743      */
       
  3744     public function addMonth($month, $locale = null)
       
  3745     {
       
  3746         return $this->_month('add', $month, $locale);
       
  3747     }
       
  3748 
       
  3749 
       
  3750     /**
       
  3751      * Subtracts months from the existing date object.
       
  3752      * The month can be a number or a string. Subtracting months lower then 0 and greater then 12
       
  3753      * will result in adding or subtracting the relevant year. (12 months equal one year)
       
  3754      * If a localized monthname is given it will be parsed with the default locale or the optional
       
  3755      * set locale.
       
  3756      * Returned is the new date object
       
  3757      *
       
  3758      * @param  string|integer|array|Zend_Date  $month   Month to sub
       
  3759      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3760      * @return Zend_Date Provides fluid interface
       
  3761      * @throws Zend_Date_Exception
       
  3762      */
       
  3763     public function subMonth($month, $locale = null)
       
  3764     {
       
  3765         return $this->_month('sub', $month, $locale);
       
  3766     }
       
  3767 
       
  3768 
       
  3769     /**
       
  3770      * Compares the month with the existing date object, ignoring other date parts.
       
  3771      * For example: 10.03.2000 -> 15.03.1950 -> true
       
  3772      * Returns if equal, earlier or later
       
  3773      *
       
  3774      * @param  string|integer|array|Zend_Date  $month   Month to compare
       
  3775      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3776      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  3777      * @throws Zend_Date_Exception
       
  3778      */
       
  3779     public function compareMonth($month, $locale = null)
       
  3780     {
       
  3781         return $this->_month('cmp', $month, $locale);
       
  3782     }
       
  3783 
       
  3784 
       
  3785     /**
       
  3786      * Returns the day as new date object
       
  3787      * Example: 20.May.1986 -> 20.Jan.1970 00:00:00
       
  3788      *
       
  3789      * @param $locale  string|Zend_Locale  OPTIONAL Locale for parsing input
       
  3790      * @return Zend_Date
       
  3791      */
       
  3792     public function getDay($locale = null)
       
  3793     {
       
  3794         return $this->copyPart(self::DAY_SHORT, $locale);
       
  3795     }
       
  3796 
       
  3797 
       
  3798     /**
       
  3799      * Returns the calculated day
       
  3800      *
       
  3801      * @param $calc    string                    Type of calculation to make
       
  3802      * @param $day     string|integer|Zend_Date  Day to calculate, when null the actual day is calculated
       
  3803      * @param $locale  string|Zend_Locale        Locale for parsing input
       
  3804      * @return Zend_Date|integer
       
  3805      */
       
  3806     private function _day($calc, $day, $locale)
       
  3807     {
       
  3808         if ($day === null) {
       
  3809             require_once 'Zend/Date/Exception.php';
       
  3810             throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
       
  3811         }
       
  3812 
       
  3813         if ($locale === null) {
       
  3814             $locale = $this->getLocale();
       
  3815         }
       
  3816 
       
  3817         if ($day instanceof Zend_Date) {
       
  3818             $day = $day->toString(self::DAY_SHORT, 'iso', $locale);
       
  3819         }
       
  3820 
       
  3821         if (is_numeric($day)) {
       
  3822             $type = self::DAY_SHORT;
       
  3823         } else if (is_array($day)) {
       
  3824             if (isset($day['day']) === true) {
       
  3825                 $day = $day['day'];
       
  3826                 $type = self::WEEKDAY;
       
  3827             } else {
       
  3828                 require_once 'Zend/Date/Exception.php';
       
  3829                 throw new Zend_Date_Exception("no day given in array");
       
  3830             }
       
  3831         } else {
       
  3832             switch (iconv_strlen($day, 'UTF-8')) {
       
  3833                 case 1 :
       
  3834                    $type = self::WEEKDAY_NARROW;
       
  3835                     break;
       
  3836                 case 2:
       
  3837                     $type = self::WEEKDAY_NAME;
       
  3838                     break;
       
  3839                 case 3:
       
  3840                     $type = self::WEEKDAY_SHORT;
       
  3841                     break;
       
  3842                 default:
       
  3843                     $type = self::WEEKDAY;
       
  3844                     break;
       
  3845             }
       
  3846         }
       
  3847         $return = $this->_calcdetail($calc, $day, $type, $locale);
       
  3848         if ($calc != 'cmp') {
       
  3849             return $this;
       
  3850         }
       
  3851         return $return;
       
  3852     }
       
  3853 
       
  3854 
       
  3855     /**
       
  3856      * Sets a new day
       
  3857      * The day can be a number or a string. Setting days lower then 0 or greater than the number of this months days
       
  3858      * will result in adding or subtracting the relevant month.
       
  3859      * If a localized dayname is given it will be parsed with the default locale or the optional
       
  3860      * set locale.
       
  3861      * Returned is the new date object
       
  3862      * Example: setDay('Montag', 'de_AT'); will set the monday of this week as day.
       
  3863      *
       
  3864      * @param  string|integer|array|Zend_Date  $month   Day to set
       
  3865      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3866      * @return Zend_Date Provides fluid interface
       
  3867      * @throws Zend_Date_Exception
       
  3868      */
       
  3869     public function setDay($day, $locale = null)
       
  3870     {
       
  3871         return $this->_day('set', $day, $locale);
       
  3872     }
       
  3873 
       
  3874 
       
  3875     /**
       
  3876      * Adds days to the existing date object.
       
  3877      * The day can be a number or a string. Adding days lower then 0 or greater than the number of this months days
       
  3878      * will result in adding or subtracting the relevant month.
       
  3879      * If a localized dayname is given it will be parsed with the default locale or the optional
       
  3880      * set locale.
       
  3881      *
       
  3882      * @param  string|integer|array|Zend_Date  $month   Day to add
       
  3883      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3884      * @return Zend_Date Provides fluid interface
       
  3885      * @throws Zend_Date_Exception
       
  3886      */
       
  3887     public function addDay($day, $locale = null)
       
  3888     {
       
  3889         return $this->_day('add', $day, $locale);
       
  3890     }
       
  3891 
       
  3892 
       
  3893     /**
       
  3894      * Subtracts days from the existing date object.
       
  3895      * The day can be a number or a string. Subtracting days lower then 0 or greater than the number of this months days
       
  3896      * will result in adding or subtracting the relevant month.
       
  3897      * If a localized dayname is given it will be parsed with the default locale or the optional
       
  3898      * set locale.
       
  3899      *
       
  3900      * @param  string|integer|array|Zend_Date  $month   Day to sub
       
  3901      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3902      * @return Zend_Date Provides fluid interface
       
  3903      * @throws Zend_Date_Exception
       
  3904      */
       
  3905     public function subDay($day, $locale = null)
       
  3906     {
       
  3907         return $this->_day('sub', $day, $locale);
       
  3908     }
       
  3909 
       
  3910 
       
  3911     /**
       
  3912      * Compares the day with the existing date object, ignoring other date parts.
       
  3913      * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
       
  3914      * Returns if equal, earlier or later
       
  3915      *
       
  3916      * @param  string|integer|array|Zend_Date  $day     Day to compare
       
  3917      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  3918      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  3919      * @throws Zend_Date_Exception
       
  3920      */
       
  3921     public function compareDay($day, $locale = null)
       
  3922     {
       
  3923         return $this->_day('cmp', $day, $locale);
       
  3924     }
       
  3925 
       
  3926 
       
  3927     /**
       
  3928      * Returns the weekday as new date object
       
  3929      * Weekday is always from 1-7
       
  3930      * Example: 09-Jan-2007 -> 2 = Tuesday -> 02-Jan-1970 (when 02.01.1970 is also Tuesday)
       
  3931      *
       
  3932      * @param $locale  string|Zend_Locale  OPTIONAL Locale for parsing input
       
  3933      * @return Zend_Date
       
  3934      */
       
  3935     public function getWeekday($locale = null)
       
  3936     {
       
  3937         if (self::$_options['format_type'] == 'php') {
       
  3938             $format = 'l';
       
  3939         } else {
       
  3940             $format = self::WEEKDAY;
       
  3941         }
       
  3942 
       
  3943         return $this->copyPart($format, $locale);
       
  3944     }
       
  3945 
       
  3946 
       
  3947     /**
       
  3948      * Returns the calculated weekday
       
  3949      *
       
  3950      * @param  $calc     string                          Type of calculation to make
       
  3951      * @param  $weekday  string|integer|array|Zend_Date  Weekday to calculate, when null the actual weekday is calculated
       
  3952      * @param  $locale   string|Zend_Locale              Locale for parsing input
       
  3953      * @return Zend_Date|integer
       
  3954      * @throws Zend_Date_Exception
       
  3955      */
       
  3956     private function _weekday($calc, $weekday, $locale)
       
  3957     {
       
  3958         if ($weekday === null) {
       
  3959             require_once 'Zend/Date/Exception.php';
       
  3960             throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
       
  3961         }
       
  3962 
       
  3963         if ($locale === null) {
       
  3964             $locale = $this->getLocale();
       
  3965         }
       
  3966 
       
  3967         if ($weekday instanceof Zend_Date) {
       
  3968             $weekday = $weekday->toString(self::WEEKDAY_8601, 'iso', $locale);
       
  3969         }
       
  3970 
       
  3971         if (is_numeric($weekday)) {
       
  3972             $type = self::WEEKDAY_8601;
       
  3973         } else if (is_array($weekday)) {
       
  3974             if (isset($weekday['weekday']) === true) {
       
  3975                 $weekday = $weekday['weekday'];
       
  3976                 $type = self::WEEKDAY;
       
  3977             } else {
       
  3978                 require_once 'Zend/Date/Exception.php';
       
  3979                 throw new Zend_Date_Exception("no weekday given in array");
       
  3980             }
       
  3981         } else {
       
  3982             switch(iconv_strlen($weekday, 'UTF-8')) {
       
  3983                 case 1:
       
  3984                    $type = self::WEEKDAY_NARROW;
       
  3985                     break;
       
  3986                 case 2:
       
  3987                     $type = self::WEEKDAY_NAME;
       
  3988                     break;
       
  3989                 case 3:
       
  3990                     $type = self::WEEKDAY_SHORT;
       
  3991                     break;
       
  3992                 default:
       
  3993                     $type = self::WEEKDAY;
       
  3994                     break;
       
  3995             }
       
  3996         }
       
  3997         $return = $this->_calcdetail($calc, $weekday, $type, $locale);
       
  3998         if ($calc != 'cmp') {
       
  3999             return $this;
       
  4000         }
       
  4001         return $return;
       
  4002     }
       
  4003 
       
  4004 
       
  4005     /**
       
  4006      * Sets a new weekday
       
  4007      * The weekday can be a number or a string. If a localized weekday name is given,
       
  4008      * then it will be parsed as a date in $locale (defaults to the same locale as $this).
       
  4009      * Returned is the new date object.
       
  4010      * Example: setWeekday(3); will set the wednesday of this week as day.
       
  4011      *
       
  4012      * @param  string|integer|array|Zend_Date  $month   Weekday to set
       
  4013      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4014      * @return Zend_Date Provides fluid interface
       
  4015      * @throws Zend_Date_Exception
       
  4016      */
       
  4017     public function setWeekday($weekday, $locale = null)
       
  4018     {
       
  4019         return $this->_weekday('set', $weekday, $locale);
       
  4020     }
       
  4021 
       
  4022 
       
  4023     /**
       
  4024      * Adds weekdays to the existing date object.
       
  4025      * The weekday can be a number or a string.
       
  4026      * If a localized dayname is given it will be parsed with the default locale or the optional
       
  4027      * set locale.
       
  4028      * Returned is the new date object
       
  4029      * Example: addWeekday(3); will add the difference of days from the begining of the month until
       
  4030      * wednesday.
       
  4031      *
       
  4032      * @param  string|integer|array|Zend_Date  $month   Weekday to add
       
  4033      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4034      * @return Zend_Date Provides fluid interface
       
  4035      * @throws Zend_Date_Exception
       
  4036      */
       
  4037     public function addWeekday($weekday, $locale = null)
       
  4038     {
       
  4039         return $this->_weekday('add', $weekday, $locale);
       
  4040     }
       
  4041 
       
  4042 
       
  4043     /**
       
  4044      * Subtracts weekdays from the existing date object.
       
  4045      * The weekday can be a number or a string.
       
  4046      * If a localized dayname is given it will be parsed with the default locale or the optional
       
  4047      * set locale.
       
  4048      * Returned is the new date object
       
  4049      * Example: subWeekday(3); will subtract the difference of days from the begining of the month until
       
  4050      * wednesday.
       
  4051      *
       
  4052      * @param  string|integer|array|Zend_Date  $month   Weekday to sub
       
  4053      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4054      * @return Zend_Date Provides fluid interface
       
  4055      * @throws Zend_Date_Exception
       
  4056      */
       
  4057     public function subWeekday($weekday, $locale = null)
       
  4058     {
       
  4059         return $this->_weekday('sub', $weekday, $locale);
       
  4060     }
       
  4061 
       
  4062 
       
  4063     /**
       
  4064      * Compares the weekday with the existing date object, ignoring other date parts.
       
  4065      * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
       
  4066      * Returns if equal, earlier or later
       
  4067      *
       
  4068      * @param  string|integer|array|Zend_Date  $weekday  Weekday to compare
       
  4069      * @param  string|Zend_Locale              $locale   OPTIONAL Locale for parsing input
       
  4070      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  4071      * @throws Zend_Date_Exception
       
  4072      */
       
  4073     public function compareWeekday($weekday, $locale = null)
       
  4074     {
       
  4075         return $this->_weekday('cmp', $weekday, $locale);
       
  4076     }
       
  4077 
       
  4078 
       
  4079     /**
       
  4080      * Returns the day of year as new date object
       
  4081      * Example: 02.Feb.1986 10:00:00 -> 02.Feb.1970 00:00:00
       
  4082      *
       
  4083      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
  4084      * @return Zend_Date
       
  4085      */
       
  4086     public function getDayOfYear($locale = null)
       
  4087     {
       
  4088         if (self::$_options['format_type'] == 'php') {
       
  4089             $format = 'D';
       
  4090         } else {
       
  4091             $format = self::DAY_OF_YEAR;
       
  4092         }
       
  4093 
       
  4094         return $this->copyPart($format, $locale);
       
  4095     }
       
  4096 
       
  4097 
       
  4098     /**
       
  4099      * Sets a new day of year
       
  4100      * The day of year is always a number.
       
  4101      * Returned is the new date object
       
  4102      * Example: 04.May.2004 -> setDayOfYear(10) -> 10.Jan.2004
       
  4103      *
       
  4104      * @param  string|integer|array|Zend_Date  $day     Day of Year to set
       
  4105      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4106      * @return Zend_Date Provides fluid interface
       
  4107      * @throws Zend_Date_Exception
       
  4108      */
       
  4109     public function setDayOfYear($day, $locale = null)
       
  4110     {
       
  4111         return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
       
  4112     }
       
  4113 
       
  4114 
       
  4115     /**
       
  4116      * Adds a day of year to the existing date object.
       
  4117      * The day of year is always a number.
       
  4118      * Returned is the new date object
       
  4119      * Example: addDayOfYear(10); will add 10 days to the existing date object.
       
  4120      *
       
  4121      * @param  string|integer|array|Zend_Date  $day     Day of Year to add
       
  4122      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4123      * @return Zend_Date Provides fluid interface
       
  4124      * @throws Zend_Date_Exception
       
  4125      */
       
  4126     public function addDayOfYear($day, $locale = null)
       
  4127     {
       
  4128         return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
       
  4129     }
       
  4130 
       
  4131 
       
  4132     /**
       
  4133      * Subtracts a day of year from the existing date object.
       
  4134      * The day of year is always a number.
       
  4135      * Returned is the new date object
       
  4136      * Example: subDayOfYear(10); will subtract 10 days from the existing date object.
       
  4137      *
       
  4138      * @param  string|integer|array|Zend_Date  $day     Day of Year to sub
       
  4139      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4140      * @return Zend_Date Provides fluid interface
       
  4141      * @throws Zend_Date_Exception
       
  4142      */
       
  4143     public function subDayOfYear($day, $locale = null)
       
  4144     {
       
  4145         return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
       
  4146     }
       
  4147 
       
  4148 
       
  4149     /**
       
  4150      * Compares the day of year with the existing date object.
       
  4151      * For example: compareDayOfYear(33) -> 02.Feb.2007 -> 0
       
  4152      * Returns if equal, earlier or later
       
  4153      *
       
  4154      * @param  string|integer|array|Zend_Date  $day     Day of Year to compare
       
  4155      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4156      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  4157      * @throws Zend_Date_Exception
       
  4158      */
       
  4159     public function compareDayOfYear($day, $locale = null)
       
  4160     {
       
  4161         return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
       
  4162     }
       
  4163 
       
  4164 
       
  4165     /**
       
  4166      * Returns the hour as new date object
       
  4167      * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 10:00:00
       
  4168      *
       
  4169      * @param $locale  string|Zend_Locale  OPTIONAL Locale for parsing input
       
  4170      * @return Zend_Date
       
  4171      */
       
  4172     public function getHour($locale = null)
       
  4173     {
       
  4174         return $this->copyPart(self::HOUR, $locale);
       
  4175     }
       
  4176 
       
  4177 
       
  4178     /**
       
  4179      * Sets a new hour
       
  4180      * The hour is always a number.
       
  4181      * Returned is the new date object
       
  4182      * Example: 04.May.1993 13:07:25 -> setHour(7); -> 04.May.1993 07:07:25
       
  4183      *
       
  4184      * @param  string|integer|array|Zend_Date  $hour    Hour to set
       
  4185      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4186      * @return Zend_Date Provides fluid interface
       
  4187      * @throws Zend_Date_Exception
       
  4188      */
       
  4189     public function setHour($hour, $locale = null)
       
  4190     {
       
  4191         return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
       
  4192     }
       
  4193 
       
  4194 
       
  4195     /**
       
  4196      * Adds hours to the existing date object.
       
  4197      * The hour is always a number.
       
  4198      * Returned is the new date object
       
  4199      * Example: 04.May.1993 13:07:25 -> addHour(12); -> 05.May.1993 01:07:25
       
  4200      *
       
  4201      * @param  string|integer|array|Zend_Date  $hour    Hour to add
       
  4202      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4203      * @return Zend_Date Provides fluid interface
       
  4204      * @throws Zend_Date_Exception
       
  4205      */
       
  4206     public function addHour($hour, $locale = null)
       
  4207     {
       
  4208         return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
       
  4209     }
       
  4210 
       
  4211 
       
  4212     /**
       
  4213      * Subtracts hours from the existing date object.
       
  4214      * The hour is always a number.
       
  4215      * Returned is the new date object
       
  4216      * Example: 04.May.1993 13:07:25 -> subHour(6); -> 05.May.1993 07:07:25
       
  4217      *
       
  4218      * @param  string|integer|array|Zend_Date  $hour    Hour to sub
       
  4219      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4220      * @return Zend_Date Provides fluid interface
       
  4221      * @throws Zend_Date_Exception
       
  4222      */
       
  4223     public function subHour($hour, $locale = null)
       
  4224     {
       
  4225         return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
       
  4226     }
       
  4227 
       
  4228 
       
  4229     /**
       
  4230      * Compares the hour with the existing date object.
       
  4231      * For example: 10:30:25 -> compareHour(10) -> 0
       
  4232      * Returns if equal, earlier or later
       
  4233      *
       
  4234      * @param  string|integer|array|Zend_Date  $hour    Hour to compare
       
  4235      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4236      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  4237      * @throws Zend_Date_Exception
       
  4238      */
       
  4239     public function compareHour($hour, $locale = null)
       
  4240     {
       
  4241         return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
       
  4242     }
       
  4243 
       
  4244 
       
  4245     /**
       
  4246      * Returns the minute as new date object
       
  4247      * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:30:00
       
  4248      *
       
  4249      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
  4250      * @return Zend_Date
       
  4251      */
       
  4252     public function getMinute($locale = null)
       
  4253     {
       
  4254         if (self::$_options['format_type'] == 'php') {
       
  4255             $format = 'i';
       
  4256         } else {
       
  4257             $format = self::MINUTE;
       
  4258         }
       
  4259 
       
  4260         return $this->copyPart($format, $locale);
       
  4261     }
       
  4262 
       
  4263 
       
  4264     /**
       
  4265      * Sets a new minute
       
  4266      * The minute is always a number.
       
  4267      * Returned is the new date object
       
  4268      * Example: 04.May.1993 13:07:25 -> setMinute(29); -> 04.May.1993 13:29:25
       
  4269      *
       
  4270      * @param  string|integer|array|Zend_Date  $minute  Minute to set
       
  4271      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4272      * @return Zend_Date Provides fluid interface
       
  4273      * @throws Zend_Date_Exception
       
  4274      */
       
  4275     public function setMinute($minute, $locale = null)
       
  4276     {
       
  4277         return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
       
  4278     }
       
  4279 
       
  4280 
       
  4281     /**
       
  4282      * Adds minutes to the existing date object.
       
  4283      * The minute is always a number.
       
  4284      * Returned is the new date object
       
  4285      * Example: 04.May.1993 13:07:25 -> addMinute(65); -> 04.May.1993 13:12:25
       
  4286      *
       
  4287      * @param  string|integer|array|Zend_Date  $minute  Minute to add
       
  4288      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4289      * @return Zend_Date Provides fluid interface
       
  4290      * @throws Zend_Date_Exception
       
  4291      */
       
  4292     public function addMinute($minute, $locale = null)
       
  4293     {
       
  4294         return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
       
  4295     }
       
  4296 
       
  4297 
       
  4298     /**
       
  4299      * Subtracts minutes from the existing date object.
       
  4300      * The minute is always a number.
       
  4301      * Returned is the new date object
       
  4302      * Example: 04.May.1993 13:07:25 -> subMinute(9); -> 04.May.1993 12:58:25
       
  4303      *
       
  4304      * @param  string|integer|array|Zend_Date  $minute  Minute to sub
       
  4305      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4306      * @return Zend_Date Provides fluid interface
       
  4307      * @throws Zend_Date_Exception
       
  4308      */
       
  4309     public function subMinute($minute, $locale = null)
       
  4310     {
       
  4311         return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
       
  4312     }
       
  4313 
       
  4314 
       
  4315     /**
       
  4316      * Compares the minute with the existing date object.
       
  4317      * For example: 10:30:25 -> compareMinute(30) -> 0
       
  4318      * Returns if equal, earlier or later
       
  4319      *
       
  4320      * @param  string|integer|array|Zend_Date  $minute  Hour to compare
       
  4321      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4322      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  4323      * @throws Zend_Date_Exception
       
  4324      */
       
  4325     public function compareMinute($minute, $locale = null)
       
  4326     {
       
  4327         return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
       
  4328     }
       
  4329 
       
  4330 
       
  4331     /**
       
  4332      * Returns the second as new date object
       
  4333      * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:00:25
       
  4334      *
       
  4335      * @param  string|Zend_Locale  $locale  OPTIONAL Locale for parsing input
       
  4336      * @return Zend_Date
       
  4337      */
       
  4338     public function getSecond($locale = null)
       
  4339     {
       
  4340         if (self::$_options['format_type'] == 'php') {
       
  4341             $format = 's';
       
  4342         } else {
       
  4343             $format = self::SECOND;
       
  4344         }
       
  4345 
       
  4346         return $this->copyPart($format, $locale);
       
  4347     }
       
  4348 
       
  4349 
       
  4350     /**
       
  4351      * Sets new seconds to the existing date object.
       
  4352      * The second is always a number.
       
  4353      * Returned is the new date object
       
  4354      * Example: 04.May.1993 13:07:25 -> setSecond(100); -> 04.May.1993 13:08:40
       
  4355      *
       
  4356      * @param  string|integer|array|Zend_Date $second Second to set
       
  4357      * @param  string|Zend_Locale             $locale (Optional) Locale for parsing input
       
  4358      * @return Zend_Date Provides fluid interface
       
  4359      * @throws Zend_Date_Exception
       
  4360      */
       
  4361     public function setSecond($second, $locale = null)
       
  4362     {
       
  4363         return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
       
  4364     }
       
  4365 
       
  4366 
       
  4367     /**
       
  4368      * Adds seconds to the existing date object.
       
  4369      * The second is always a number.
       
  4370      * Returned is the new date object
       
  4371      * Example: 04.May.1993 13:07:25 -> addSecond(65); -> 04.May.1993 13:08:30
       
  4372      *
       
  4373      * @param  string|integer|array|Zend_Date $second Second to add
       
  4374      * @param  string|Zend_Locale             $locale (Optional) Locale for parsing input
       
  4375      * @return Zend_Date Provides fluid interface
       
  4376      * @throws Zend_Date_Exception
       
  4377      */
       
  4378     public function addSecond($second, $locale = null)
       
  4379     {
       
  4380         return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
       
  4381     }
       
  4382 
       
  4383 
       
  4384     /**
       
  4385      * Subtracts seconds from the existing date object.
       
  4386      * The second is always a number.
       
  4387      * Returned is the new date object
       
  4388      * Example: 04.May.1993 13:07:25 -> subSecond(10); -> 04.May.1993 13:07:15
       
  4389      *
       
  4390      * @param  string|integer|array|Zend_Date $second Second to sub
       
  4391      * @param  string|Zend_Locale             $locale (Optional) Locale for parsing input
       
  4392      * @return Zend_Date Provides fluid interface
       
  4393      * @throws Zend_Date_Exception
       
  4394      */
       
  4395     public function subSecond($second, $locale = null)
       
  4396     {
       
  4397         return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
       
  4398     }
       
  4399 
       
  4400 
       
  4401     /**
       
  4402      * Compares the second with the existing date object.
       
  4403      * For example: 10:30:25 -> compareSecond(25) -> 0
       
  4404      * Returns if equal, earlier or later
       
  4405      *
       
  4406      * @param  string|integer|array|Zend_Date $second Second to compare
       
  4407      * @param  string|Zend_Locale             $locale (Optional) Locale for parsing input
       
  4408      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  4409      * @throws Zend_Date_Exception
       
  4410      */
       
  4411     public function compareSecond($second, $locale = null)
       
  4412     {
       
  4413         return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
       
  4414     }
       
  4415 
       
  4416 
       
  4417     /**
       
  4418      * Returns the precision for fractional seconds
       
  4419      *
       
  4420      * @return integer
       
  4421      */
       
  4422     public function getFractionalPrecision()
       
  4423     {
       
  4424         return $this->_precision;
       
  4425     }
       
  4426 
       
  4427 
       
  4428     /**
       
  4429      * Sets a new precision for fractional seconds
       
  4430      *
       
  4431      * @param  integer $precision Precision for the fractional datepart 3 = milliseconds
       
  4432      * @throws Zend_Date_Exception
       
  4433      * @return Zend_Date Provides fluid interface
       
  4434      */
       
  4435     public function setFractionalPrecision($precision)
       
  4436     {
       
  4437         if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
       
  4438             require_once 'Zend/Date/Exception.php';
       
  4439             throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
       
  4440         }
       
  4441 
       
  4442         $this->_precision = (int) $precision;
       
  4443         if ($this->_precision < strlen($this->_fractional)) {
       
  4444             $this->_fractional = substr($this->_fractional, 0, $this->_precision);
       
  4445         } else {
       
  4446             $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_RIGHT);
       
  4447         }
       
  4448 
       
  4449         return $this;
       
  4450     }
       
  4451 
       
  4452 
       
  4453     /**
       
  4454      * Returns the milliseconds of the date object
       
  4455      *
       
  4456      * @return string
       
  4457      */
       
  4458     public function getMilliSecond()
       
  4459     {
       
  4460         return $this->_fractional;
       
  4461     }
       
  4462 
       
  4463 
       
  4464     /**
       
  4465      * Sets new milliseconds for the date object
       
  4466      * Example: setMilliSecond(550, 2) -> equals +5 Sec +50 MilliSec
       
  4467      *
       
  4468      * @param  integer|Zend_Date $milli     (Optional) Millisecond to set, when null the actual millisecond is set
       
  4469      * @param  integer           $precision (Optional) Fraction precision of the given milliseconds
       
  4470      * @return Zend_Date Provides fluid interface
       
  4471      */
       
  4472     public function setMilliSecond($milli = null, $precision = null)
       
  4473     {
       
  4474         if ($milli === null) {
       
  4475             list($milli, $time) = explode(" ", microtime());
       
  4476             $milli = intval($milli);
       
  4477             $precision = 6;
       
  4478         } else if (!is_numeric($milli)) {
       
  4479             require_once 'Zend/Date/Exception.php';
       
  4480             throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
       
  4481         }
       
  4482 
       
  4483         if ($precision === null) {
       
  4484             $precision = $this->_precision;
       
  4485         }
       
  4486 
       
  4487         if (!is_int($precision) || $precision < 1 || $precision > 9) {
       
  4488             require_once 'Zend/Date/Exception.php';
       
  4489             throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
       
  4490         }
       
  4491 
       
  4492         $this->_fractional = 0;
       
  4493         $this->addMilliSecond($milli, $precision);
       
  4494         return $this;
       
  4495     }
       
  4496 
       
  4497 
       
  4498     /**
       
  4499      * Adds milliseconds to the date object
       
  4500      *
       
  4501      * @param  integer|Zend_Date $milli     (Optional) Millisecond to add, when null the actual millisecond is added
       
  4502      * @param  integer           $precision (Optional) Fractional precision for the given milliseconds
       
  4503      * @return Zend_Date Provides fluid interface
       
  4504      */
       
  4505     public function addMilliSecond($milli = null, $precision = null)
       
  4506     {
       
  4507         if ($milli === null) {
       
  4508             list($milli, $time) = explode(" ", microtime());
       
  4509             $milli = intval($milli);
       
  4510         } else if (!is_numeric($milli)) {
       
  4511             require_once 'Zend/Date/Exception.php';
       
  4512             throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
       
  4513         }
       
  4514 
       
  4515         if ($precision === null) {
       
  4516             $precision = strlen($milli);
       
  4517             if ($milli < 0) {
       
  4518                 --$precision;
       
  4519             }
       
  4520         }
       
  4521 
       
  4522         if (!is_int($precision) || $precision < 1 || $precision > 9) {
       
  4523             require_once 'Zend/Date/Exception.php';
       
  4524             throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
       
  4525         }
       
  4526 
       
  4527         $this->_fractional += $milli;
       
  4528 
       
  4529         // Add/sub milliseconds + add/sub seconds
       
  4530         $max = pow(10, $this->_precision);
       
  4531         // Milli includes seconds
       
  4532         if ($this->_fractional >= $max) {
       
  4533             while ($this->_fractional >= $max) {
       
  4534                 $this->addSecond(1);
       
  4535                 $this->_fractional -= $max;
       
  4536             }
       
  4537         }
       
  4538 
       
  4539         if ($this->_fractional < 0) {
       
  4540             while ($this->_fractional < 0) {
       
  4541                 $this->subSecond(1);
       
  4542                 $this->_fractional += $max;
       
  4543             }
       
  4544         }
       
  4545 
       
  4546         if ($this->_precision > strlen($this->_fractional)) {
       
  4547             $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_LEFT);
       
  4548         }
       
  4549 
       
  4550         return $this;
       
  4551     }
       
  4552 
       
  4553 
       
  4554     /**
       
  4555      * Subtracts a millisecond
       
  4556      *
       
  4557      * @param  integer|Zend_Date $milli     (Optional) Millisecond to sub, when null the actual millisecond is subtracted
       
  4558      * @param  integer           $precision (Optional) Fractional precision for the given milliseconds
       
  4559      * @return Zend_Date Provides fluid interface
       
  4560      */
       
  4561     public function subMilliSecond($milli = null, $precision = null)
       
  4562     {
       
  4563         $this->addMilliSecond(0 - $milli, $precision);
       
  4564         return $this;
       
  4565     }
       
  4566 
       
  4567     /**
       
  4568      * Compares only the millisecond part, returning the difference
       
  4569      *
       
  4570      * @param  integer|Zend_Date  $milli  OPTIONAL Millisecond to compare, when null the actual millisecond is compared
       
  4571      * @param  integer            $precision  OPTIONAL Fractional precision for the given milliseconds
       
  4572      * @throws Zend_Date_Exception On invalid input
       
  4573      * @return integer  0 = equal, 1 = later, -1 = earlier
       
  4574      */
       
  4575     public function compareMilliSecond($milli = null, $precision = null)
       
  4576     {
       
  4577         if ($milli === null) {
       
  4578             list($milli, $time) = explode(" ", microtime());
       
  4579             $milli = intval($milli);
       
  4580         } else if (is_numeric($milli) === false) {
       
  4581             require_once 'Zend/Date/Exception.php';
       
  4582             throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
       
  4583         }
       
  4584 
       
  4585         if ($precision === null) {
       
  4586             $precision = strlen($milli);
       
  4587         } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
       
  4588             require_once 'Zend/Date/Exception.php';
       
  4589             throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
       
  4590         }
       
  4591 
       
  4592         if ($precision === 0) {
       
  4593             require_once 'Zend/Date/Exception.php';
       
  4594             throw new Zend_Date_Exception('precision is 0');
       
  4595         }
       
  4596 
       
  4597         if ($precision != $this->_precision) {
       
  4598             if ($precision > $this->_precision) {
       
  4599                 $diff = $precision - $this->_precision;
       
  4600                 $milli = (int) ($milli / (10 * $diff));
       
  4601             } else {
       
  4602                 $diff = $this->_precision - $precision;
       
  4603                 $milli = (int) ($milli * (10 * $diff));
       
  4604             }
       
  4605         }
       
  4606 
       
  4607         $comp = $this->_fractional - $milli;
       
  4608         if ($comp < 0) {
       
  4609             return -1;
       
  4610         } else if ($comp > 0) {
       
  4611             return 1;
       
  4612         }
       
  4613         return 0;
       
  4614     }
       
  4615 
       
  4616     /**
       
  4617      * Returns the week as new date object using monday as begining of the week
       
  4618      * Example: 12.Jan.2007 -> 08.Jan.1970 00:00:00
       
  4619      *
       
  4620      * @param $locale  string|Zend_Locale  OPTIONAL Locale for parsing input
       
  4621      * @return Zend_Date
       
  4622      */
       
  4623     public function getWeek($locale = null)
       
  4624     {
       
  4625         if (self::$_options['format_type'] == 'php') {
       
  4626             $format = 'W';
       
  4627         } else {
       
  4628             $format = self::WEEK;
       
  4629         }
       
  4630 
       
  4631         return $this->copyPart($format, $locale);
       
  4632     }
       
  4633 
       
  4634     /**
       
  4635      * Sets a new week. The week is always a number. The day of week is not changed.
       
  4636      * Returned is the new date object
       
  4637      * Example: 09.Jan.2007 13:07:25 -> setWeek(1); -> 02.Jan.2007 13:07:25
       
  4638      *
       
  4639      * @param  string|integer|array|Zend_Date  $week    Week to set
       
  4640      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4641      * @return Zend_Date Provides fluid interface
       
  4642      * @throws Zend_Date_Exception
       
  4643      */
       
  4644     public function setWeek($week, $locale = null)
       
  4645     {
       
  4646         return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
       
  4647     }
       
  4648 
       
  4649     /**
       
  4650      * Adds a week. The week is always a number. The day of week is not changed.
       
  4651      * Returned is the new date object
       
  4652      * Example: 09.Jan.2007 13:07:25 -> addWeek(1); -> 16.Jan.2007 13:07:25
       
  4653      *
       
  4654      * @param  string|integer|array|Zend_Date  $week    Week to add
       
  4655      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4656      * @return Zend_Date Provides fluid interface
       
  4657      * @throws Zend_Date_Exception
       
  4658      */
       
  4659     public function addWeek($week, $locale = null)
       
  4660     {
       
  4661         return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
       
  4662     }
       
  4663 
       
  4664     /**
       
  4665      * Subtracts a week. The week is always a number. The day of week is not changed.
       
  4666      * Returned is the new date object
       
  4667      * Example: 09.Jan.2007 13:07:25 -> subWeek(1); -> 02.Jan.2007 13:07:25
       
  4668      *
       
  4669      * @param  string|integer|array|Zend_Date  $week    Week to sub
       
  4670      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4671      * @return Zend_Date Provides fluid interface
       
  4672      * @throws Zend_Date_Exception
       
  4673      */
       
  4674     public function subWeek($week, $locale = null)
       
  4675     {
       
  4676         return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
       
  4677     }
       
  4678 
       
  4679     /**
       
  4680      * Compares only the week part, returning the difference
       
  4681      * Returned is the new date object
       
  4682      * Returns if equal, earlier or later
       
  4683      * Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
       
  4684      *
       
  4685      * @param  string|integer|array|Zend_Date  $week    Week to compare
       
  4686      * @param  string|Zend_Locale              $locale  OPTIONAL Locale for parsing input
       
  4687      * @return integer 0 = equal, 1 = later, -1 = earlier
       
  4688      */
       
  4689     public function compareWeek($week, $locale = null)
       
  4690     {
       
  4691         return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
       
  4692     }
       
  4693 
       
  4694     /**
       
  4695      * Sets a new standard locale for the date object.
       
  4696      * This locale will be used for all functions
       
  4697      * Returned is the really set locale.
       
  4698      * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
       
  4699      * 'xx_YY' will be set to 'root' because 'xx' does not exist
       
  4700      *
       
  4701      * @param  string|Zend_Locale $locale (Optional) Locale for parsing input
       
  4702      * @throws Zend_Date_Exception When the given locale does not exist
       
  4703      * @return Zend_Date Provides fluent interface
       
  4704      */
       
  4705     public function setLocale($locale = null)
       
  4706     {
       
  4707         try {
       
  4708             $this->_locale = Zend_Locale::findLocale($locale);
       
  4709         } catch (Zend_Locale_Exception $e) {
       
  4710             require_once 'Zend/Date/Exception.php';
       
  4711             throw new Zend_Date_Exception($e->getMessage(), 0, $e);
       
  4712         }
       
  4713 
       
  4714         return $this;
       
  4715     }
       
  4716 
       
  4717     /**
       
  4718      * Returns the actual set locale
       
  4719      *
       
  4720      * @return string
       
  4721      */
       
  4722     public function getLocale()
       
  4723     {
       
  4724         return $this->_locale;
       
  4725     }
       
  4726 
       
  4727     /**
       
  4728      * Checks if the given date is a real date or datepart.
       
  4729      * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
       
  4730      * But the check will only be done for the expected dateparts which are given by format.
       
  4731      * If no format is given the standard dateformat for the actual locale is used.
       
  4732      * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
       
  4733      *
       
  4734      * @param  string|array|Zend_Date $date   Date to parse for correctness
       
  4735      * @param  string                 $format (Optional) Format for parsing the date string
       
  4736      * @param  string|Zend_Locale     $locale (Optional) Locale for parsing date parts
       
  4737      * @return boolean                True when all date parts are correct
       
  4738      */
       
  4739     public static function isDate($date, $format = null, $locale = null)
       
  4740     {
       
  4741         if (!is_string($date) && !is_numeric($date) && !($date instanceof Zend_Date) &&
       
  4742             !is_array($date)) {
       
  4743             return false;
       
  4744         }
       
  4745 
       
  4746         if (($format !== null) && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
       
  4747             && (Zend_Locale::isLocale($format, null, false))) {
       
  4748             $locale = $format;
       
  4749             $format = null;
       
  4750         }
       
  4751 
       
  4752         $locale = Zend_Locale::findLocale($locale);
       
  4753 
       
  4754         if ($format === null) {
       
  4755             $format = Zend_Locale_Format::getDateFormat($locale);
       
  4756         } else if ((self::$_options['format_type'] == 'php') && !defined($format)) {
       
  4757             $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
       
  4758         }
       
  4759 
       
  4760         $format = self::_getLocalizedToken($format, $locale);
       
  4761         if (!is_array($date)) {
       
  4762             try {
       
  4763                 $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
       
  4764                                                       'date_format' => $format, 'format_type' => 'iso',
       
  4765                                                       'fix_date' => false));
       
  4766             } catch (Zend_Locale_Exception $e) {
       
  4767                 // Date can not be parsed
       
  4768                 return false;
       
  4769             }
       
  4770         } else {
       
  4771             $parsed = $date;
       
  4772         }
       
  4773 
       
  4774         if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
       
  4775             (!isset($parsed['year']))) {
       
  4776             // Year expected but not found
       
  4777                 return false;
       
  4778         }
       
  4779 
       
  4780         if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
       
  4781             // Month expected but not found
       
  4782             return false;
       
  4783         }
       
  4784 
       
  4785         if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
       
  4786             // Day expected but not found
       
  4787             return false;
       
  4788         }
       
  4789 
       
  4790         if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
       
  4791             (!isset($parsed['hour']))) {
       
  4792             // Hour expected but not found
       
  4793                 return false;
       
  4794         }
       
  4795 
       
  4796         if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
       
  4797             // Minute expected but not found
       
  4798             return false;
       
  4799         }
       
  4800 
       
  4801         if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
       
  4802             // Second expected  but not found
       
  4803             return false;
       
  4804         }
       
  4805 
       
  4806         // Set not given dateparts
       
  4807         if (isset($parsed['hour']) === false) {
       
  4808             $parsed['hour'] = 12;
       
  4809         }
       
  4810 
       
  4811         if (isset($parsed['minute']) === false) {
       
  4812             $parsed['minute'] = 0;
       
  4813         }
       
  4814 
       
  4815         if (isset($parsed['second']) === false) {
       
  4816             $parsed['second'] = 0;
       
  4817         }
       
  4818 
       
  4819         if (isset($parsed['month']) === false) {
       
  4820             $parsed['month'] = 1;
       
  4821         }
       
  4822 
       
  4823         if (isset($parsed['day']) === false) {
       
  4824             $parsed['day'] = 1;
       
  4825         }
       
  4826 
       
  4827         if (isset($parsed['year']) === false) {
       
  4828             $parsed['year'] = 1970;
       
  4829         }
       
  4830 
       
  4831         if (self::isYearLeapYear($parsed['year'])) {
       
  4832             $parsed['year'] = 1972;
       
  4833         } else {
       
  4834             $parsed['year'] = 1971;
       
  4835         }
       
  4836 
       
  4837         $date      = new self($parsed, null, $locale);
       
  4838         $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
       
  4839                                    $parsed['month'], $parsed['day'], $parsed['year']);
       
  4840 
       
  4841         if ($parsed['year'] != $date->date('Y', $timestamp)) {
       
  4842             // Given year differs from parsed year
       
  4843             return false;
       
  4844         }
       
  4845 
       
  4846         if ($parsed['month'] != $date->date('n', $timestamp)) {
       
  4847             // Given month differs from parsed month
       
  4848             return false;
       
  4849         }
       
  4850 
       
  4851         if ($parsed['day'] != $date->date('j', $timestamp)) {
       
  4852             // Given day differs from parsed day
       
  4853             return false;
       
  4854         }
       
  4855 
       
  4856         if ($parsed['hour'] != $date->date('G', $timestamp)) {
       
  4857             // Given hour differs from parsed hour
       
  4858             return false;
       
  4859         }
       
  4860 
       
  4861         if ($parsed['minute'] != $date->date('i', $timestamp)) {
       
  4862             // Given minute differs from parsed minute
       
  4863             return false;
       
  4864         }
       
  4865 
       
  4866         if ($parsed['second'] != $date->date('s', $timestamp)) {
       
  4867             // Given second differs from parsed second
       
  4868             return false;
       
  4869         }
       
  4870 
       
  4871         return true;
       
  4872     }
       
  4873 
       
  4874     /**
       
  4875      * Returns the ISO Token for all localized constants
       
  4876      *
       
  4877      * @param string $token Token to normalize
       
  4878      * @param string $locale Locale to search
       
  4879      * @return string
       
  4880      */
       
  4881     protected static function _getLocalizedToken($token, $locale)
       
  4882     {
       
  4883         switch($token) {
       
  4884             case self::ISO_8601 :
       
  4885                 return "yyyy-MM-ddThh:mm:ss";
       
  4886                 break;
       
  4887             case self::RFC_2822 :
       
  4888                 return "EEE, dd MMM yyyy HH:mm:ss";
       
  4889                 break;
       
  4890             case self::DATES :
       
  4891                 return Zend_Locale_Data::getContent($locale, 'date');
       
  4892                 break;
       
  4893             case self::DATE_FULL :
       
  4894                 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
       
  4895                 break;
       
  4896             case self::DATE_LONG :
       
  4897                 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
       
  4898                 break;
       
  4899             case self::DATE_MEDIUM :
       
  4900                 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
       
  4901                 break;
       
  4902             case self::DATE_SHORT :
       
  4903                 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
       
  4904                 break;
       
  4905             case self::TIMES :
       
  4906                 return Zend_Locale_Data::getContent($locale, 'time');
       
  4907                 break;
       
  4908             case self::TIME_FULL :
       
  4909                 return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
       
  4910                 break;
       
  4911             case self::TIME_LONG :
       
  4912                 return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
       
  4913                 break;
       
  4914             case self::TIME_MEDIUM :
       
  4915                 return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
       
  4916                 break;
       
  4917             case self::TIME_SHORT :
       
  4918                 return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
       
  4919                 break;
       
  4920             case self::DATETIME :
       
  4921                 return Zend_Locale_Data::getContent($locale, 'datetime');
       
  4922                 break;
       
  4923             case self::DATETIME_FULL :
       
  4924                 return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
       
  4925                 break;
       
  4926             case self::DATETIME_LONG :
       
  4927                 return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
       
  4928                 break;
       
  4929             case self::DATETIME_MEDIUM :
       
  4930                 return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
       
  4931                 break;
       
  4932             case self::DATETIME_SHORT :
       
  4933                 return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
       
  4934                 break;
       
  4935             case self::ATOM :
       
  4936             case self::RFC_3339 :
       
  4937             case self::W3C :
       
  4938                 return "yyyy-MM-DD HH:mm:ss";
       
  4939                 break;
       
  4940             case self::COOKIE :
       
  4941             case self::RFC_850 :
       
  4942                 return "EEEE, dd-MM-yyyy HH:mm:ss";
       
  4943                 break;
       
  4944             case self::RFC_822 :
       
  4945             case self::RFC_1036 :
       
  4946             case self::RFC_1123 :
       
  4947             case self::RSS :
       
  4948                 return "EEE, dd MM yyyy HH:mm:ss";
       
  4949                 break;
       
  4950         }
       
  4951 
       
  4952         return $token;
       
  4953     }
       
  4954 }