00001 <?php
00025 require_once 'Zend/Date/DateObject.php';
00026 require_once 'Zend/Locale.php';
00027 require_once 'Zend/Locale/Format.php';
00028 require_once 'Zend/Locale/Math.php';
00029
00036 class Zend_Date extends Zend_Date_DateObject
00037 {
00038 private $_locale = null;
00039
00040
00041 private $_fractional = 0;
00042 private $_precision = 3;
00043
00044 private static $_options = array(
00045 'format_type' => 'iso',
00046 'fix_dst' => true,
00047 'extend_month' => false,
00048 'cache' => null,
00049 'timesync' => null
00050 );
00051
00052
00053 const DAY = 'dd';
00054 const DAY_SHORT = 'd';
00055 const DAY_SUFFIX = 'SS';
00056 const DAY_OF_YEAR = 'D';
00057 const WEEKDAY = 'EEEE';
00058 const WEEKDAY_SHORT = 'EEE';
00059 const WEEKDAY_NARROW = 'E';
00060 const WEEKDAY_NAME = 'EE';
00061 const WEEKDAY_8601 = 'eee';
00062 const WEEKDAY_DIGIT = 'e';
00063 const WEEK = 'ww';
00064 const MONTH = 'MM';
00065 const MONTH_SHORT = 'M';
00066 const MONTH_DAYS = 'ddd';
00067 const MONTH_NAME = 'MMMM';
00068 const MONTH_NAME_SHORT = 'MMM';
00069 const MONTH_NAME_NARROW = 'MMMMM';
00070 const YEAR = 'y';
00071 const YEAR_SHORT = 'yy';
00072 const YEAR_8601 = 'Y';
00073 const YEAR_SHORT_8601 = 'YY';
00074 const LEAPYEAR = 'l';
00075 const MERIDIEM = 'a';
00076 const SWATCH = 'B';
00077 const HOUR = 'HH';
00078 const HOUR_SHORT = 'H';
00079 const HOUR_AM = 'hh';
00080 const HOUR_SHORT_AM = 'h';
00081 const MINUTE = 'mm';
00082 const MINUTE_SHORT = 'm';
00083 const SECOND = 'ss';
00084 const SECOND_SHORT = 's';
00085 const MILLISECOND = 'S';
00086 const TIMEZONE_NAME = 'zzzz';
00087 const DAYLIGHT = 'I';
00088 const GMT_DIFF = 'Z';
00089 const GMT_DIFF_SEP = 'ZZZZ';
00090 const TIMEZONE = 'z';
00091 const TIMEZONE_SECS = 'X';
00092 const ISO_8601 = 'c';
00093 const RFC_2822 = 'r';
00094 const TIMESTAMP = 'U';
00095 const ERA = 'G';
00096 const ERA_NAME = 'GGGG';
00097 const ERA_NARROW = 'GGGGG';
00098 const DATES = 'F';
00099 const DATE_FULL = 'FFFFF';
00100 const DATE_LONG = 'FFFF';
00101 const DATE_MEDIUM = 'FFF';
00102 const DATE_SHORT = 'FF';
00103 const TIMES = 'WW';
00104 const TIME_FULL = 'TTTTT';
00105 const TIME_LONG = 'TTTT';
00106 const TIME_MEDIUM = 'TTT';
00107 const TIME_SHORT = 'TT';
00108 const DATETIME = 'K';
00109 const DATETIME_FULL = 'KKKKK';
00110 const DATETIME_LONG = 'KKKK';
00111 const DATETIME_MEDIUM = 'KKK';
00112 const DATETIME_SHORT = 'KK';
00113 const ATOM = 'OOO';
00114 const COOKIE = 'CCC';
00115 const RFC_822 = 'R';
00116 const RFC_850 = 'RR';
00117 const RFC_1036 = 'RRR';
00118 const RFC_1123 = 'RRRR';
00119 const RFC_3339 = 'RRRRR';
00120 const RSS = 'SSS';
00121 const W3C = 'WWW';
00122
00137 public function __construct($date = null, $part = null, $locale = null)
00138 {
00139 if (is_object($date) and !($date instanceof Zend_TimeSync_Protocol) and
00140 !($date instanceof Zend_Date)) {
00141 if ($locale instanceof Zend_Locale) {
00142 $locale = $date;
00143 $date = null;
00144 $part = null;
00145 } else {
00146 $date = (string) $date;
00147 }
00148 }
00149
00150 if (($date !== null) and !is_array($date) and !($date instanceof Zend_TimeSync_Protocol) and
00151 !($date instanceof Zend_Date) and !defined($date) and Zend_Locale::isLocale($date, true, false)) {
00152 $locale = $date;
00153 $date = null;
00154 $part = null;
00155 } else if (($part !== null) and !defined($part) and Zend_Locale::isLocale($part, true, false)) {
00156 $locale = $part;
00157 $part = null;
00158 }
00159
00160 $this->setLocale($locale);
00161 if (is_string($date) && ($part === null) && (strlen($date) <= 5)) {
00162 $part = $date;
00163 $date = null;
00164 }
00165
00166 if ($date === null) {
00167 if ($part === null) {
00168 $date = time();
00169 } else if ($part !== self::TIMESTAMP) {
00170 $date = self::now($locale);
00171 $date = $date->get($part);
00172 }
00173 }
00174
00175 if ($date instanceof Zend_TimeSync_Protocol) {
00176 $date = $date->getInfo();
00177 $date = $this->_getTime($date['offset']);
00178 $part = null;
00179 } else if (parent::$_defaultOffset != 0) {
00180 $date = $this->_getTime(parent::$_defaultOffset);
00181 }
00182
00183
00184 $zone = @date_default_timezone_get();
00185 $this->setTimezone($zone);
00186
00187
00188 if (!is_int($date)) {
00189 $zone = $this->getTimezoneFromString($date);
00190 $this->setTimezone($zone);
00191 }
00192
00193
00194 if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
00195
00196 $this->setUnixTimestamp($this->getGmtOffset());
00197 $this->set($date, $part, $this->_locale);
00198
00199
00200 if (is_array($date) === true) {
00201 if (!isset($date['hour'])) {
00202 $date['hour'] = 0;
00203 }
00204
00205 $hour = $this->toString('H');
00206 $hour = $date['hour'] - $hour;
00207 switch ($hour) {
00208 case 1 :
00209 case -23 :
00210 $this->addTimestamp(3600);
00211 break;
00212 case -1 :
00213 case 23 :
00214 $this->subTimestamp(3600);
00215 break;
00216 case 2 :
00217 case -22 :
00218 $this->addTimestamp(7200);
00219 break;
00220 case -2 :
00221 case 22 :
00222 $this->subTimestamp(7200);
00223 break;
00224 }
00225 }
00226 } else {
00227 $this->setUnixTimestamp($date);
00228 }
00229 }
00230
00238 public static function setOptions(array $options = array())
00239 {
00240 if (empty($options)) {
00241 return self::$_options;
00242 }
00243 foreach ($options as $name => $value) {
00244 $name = strtolower($name);
00245
00246 if (array_key_exists($name, self::$_options)) {
00247 switch($name) {
00248 case 'format_type' :
00249 if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
00250 require_once 'Zend/Date/Exception.php';
00251 throw new Zend_Date_Exception("Unknown format type ($value) for dates, only 'iso' and 'php' supported", $value);
00252 }
00253 break;
00254 case 'fix_dst' :
00255 if (!is_bool($value)) {
00256 require_once 'Zend/Date/Exception.php';
00257 throw new Zend_Date_Exception("'fix_dst' has to be boolean", $value);
00258 }
00259 break;
00260 case 'extend_month' :
00261 if (!is_bool($value)) {
00262 require_once 'Zend/Date/Exception.php';
00263 throw new Zend_Date_Exception("'extend_month' has to be boolean", $value);
00264 }
00265 break;
00266 case 'cache' :
00267 if (!$value instanceof Zend_Cache_Core) {
00268 require_once 'Zend/Date/Exception.php';
00269 throw new Zend_Date_Exception("Instance of Zend_Cache expected");
00270 }
00271 parent::$_cache = $value;
00272 Zend_Locale_Data::setCache($value);
00273 break;
00274 case 'timesync' :
00275 if (!$value instanceof Zend_TimeSync_Protocol) {
00276 require_once 'Zend/Date/Exception.php';
00277 throw new Zend_Date_Exception("Instance of Zend_TimeSync expected");
00278 }
00279 $date = $value->getInfo();
00280 parent::$_defaultOffset = $date['offset'];
00281 break;
00282 }
00283 self::$_options[$name] = $value;
00284 }
00285 else {
00286 require_once 'Zend/Date/Exception.php';
00287 throw new Zend_Date_Exception("Unknown option: $name = $value");
00288 }
00289 }
00290 }
00291
00300 public function getTimestamp()
00301 {
00302 return $this->getUnixTimestamp();
00303 }
00304
00314 private function _timestamp($calc, $stamp)
00315 {
00316 if ($stamp instanceof Zend_Date) {
00317
00318 $stamp = $stamp->get(self::TIMESTAMP, true);
00319 }
00320
00321 if (is_array($stamp)) {
00322 if (isset($stamp['timestamp']) === true) {
00323 $stamp = $stamp['timestamp'];
00324 } else {
00325 require_once 'Zend/Date/Exception.php';
00326 throw new Zend_Date_Exception('no timestamp given in array');
00327 }
00328 }
00329
00330 if ($calc === 'set') {
00331 $return = $this->setUnixTimestamp($stamp);
00332 } else {
00333 $return = $this->_calcdetail($calc, $stamp, self::TIMESTAMP, null);
00334 }
00335 if ($calc != 'cmp') {
00336 return $this;
00337 }
00338 return $return;
00339 }
00340
00348 public function setTimestamp($timestamp)
00349 {
00350 return $this->_timestamp('set', $timestamp);
00351 }
00352
00360 public function addTimestamp($timestamp)
00361 {
00362 return $this->_timestamp('add', $timestamp);
00363 }
00364
00372 public function subTimestamp($timestamp)
00373 {
00374 return $this->_timestamp('sub', $timestamp);
00375 }
00376
00384 public function compareTimestamp($timestamp)
00385 {
00386 return $this->_timestamp('cmp', $timestamp);
00387 }
00388
00411 public function toString($format = null, $type = null, $locale = null)
00412 {
00413 if (is_object($format)) {
00414 if ($format instanceof Zend_Locale) {
00415 $locale = $format;
00416 $format = null;
00417 } else {
00418 $format = (string) $format;
00419 }
00420 }
00421
00422 if (is_object($type)) {
00423 if ($type instanceof Zend_Locale) {
00424 $locale = $type;
00425 $type = null;
00426 } else {
00427 $type = (string) $type;
00428 }
00429 }
00430
00431 if (($format !== null) and !defined($format) and
00432 ($format != 'ee') and ($format != 'ss') and Zend_Locale::isLocale($format, null, false)) {
00433 $locale = $format;
00434 $format = null;
00435 }
00436
00437 if (($type !== null) and ($type != 'php') and ($type != 'iso') and
00438 Zend_Locale::isLocale($type, null, false)) {
00439 $locale = $type;
00440 $type = null;
00441 }
00442
00443 if ($locale === null) {
00444 $locale = $this->getLocale();
00445 }
00446
00447 if ($format === null) {
00448 $format = Zend_Locale_Format::getDateFormat($locale) . ' ' . Zend_Locale_Format::getTimeFormat($locale);
00449 } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
00450 $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
00451 }
00452
00453 return $this->get($format, $locale);
00454 }
00455
00461 public function __toString()
00462 {
00463 return $this->toString(null, $this->_locale);
00464 }
00465
00473 public function toValue($part = null)
00474 {
00475 $result = $this->get($part);
00476 if (is_numeric($result)) {
00477 return intval("$result");
00478 } else {
00479 return false;
00480 }
00481 }
00482
00488 public function toArray()
00489 {
00490 return array('day' => $this->get(self::DAY_SHORT),
00491 'month' => $this->get(self::MONTH_SHORT),
00492 'year' => $this->get(self::YEAR),
00493 'hour' => $this->get(self::HOUR_SHORT),
00494 'minute' => $this->get(self::MINUTE_SHORT),
00495 'second' => $this->get(self::SECOND_SHORT),
00496 'timezone' => $this->get(self::TIMEZONE),
00497 'timestamp' => $this->get(self::TIMESTAMP),
00498 'weekday' => $this->get(self::WEEKDAY_8601),
00499 'dayofyear' => $this->get(self::DAY_OF_YEAR),
00500 'week' => $this->get(self::WEEK),
00501 'gmtsecs' => $this->get(self::TIMEZONE_SECS));
00502 }
00503
00514 public function get($part = null, $locale = null)
00515 {
00516 if ($locale === null) {
00517 $locale = $this->getLocale();
00518 }
00519
00520 if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale::isLocale($part, null, false))) {
00521 $locale = $part;
00522 $part = null;
00523 }
00524
00525 if ($part === null) {
00526 $part = self::TIMESTAMP;
00527 }
00528
00529 return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);
00530 }
00531
00539 private function _toToken($part, $locale) {
00540
00541 $comment = false;
00542 $format = '';
00543 $orig = '';
00544 for ($i = 0; $i < strlen($part); ++$i) {
00545 if ($part[$i] == "'") {
00546 $comment = $comment ? false : true;
00547 if (isset($part[$i+1]) && ($part[$i+1] == "'")) {
00548 $comment = $comment ? false : true;
00549 $format .= "\\'";
00550 ++$i;
00551 }
00552
00553 $orig = '';
00554 continue;
00555 }
00556
00557 if ($comment) {
00558 $format .= '\\' . $part[$i];
00559 $orig = '';
00560 } else {
00561 $orig .= $part[$i];
00562 if (!isset($part[$i+1]) || (isset($orig[0]) && ($orig[0] != $part[$i+1]))) {
00563 $format .= $this->_parseIsoToDate($orig, $locale);
00564 $orig = '';
00565 }
00566 }
00567 }
00568
00569 return $format;
00570 }
00571
00579 private function _parseIsoToDate($token, $locale) {
00580 switch($token) {
00581 case self::DAY :
00582 return 'd';
00583 break;
00584
00585 case self::WEEKDAY_SHORT :
00586 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
00587 $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
00588 return $this->_toComment(iconv_substr($day, 0, 3, 'UTF-8'));
00589 break;
00590
00591 case self::DAY_SHORT :
00592 return 'j';
00593 break;
00594
00595 case self::WEEKDAY :
00596 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
00597 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)));
00598 break;
00599
00600 case self::WEEKDAY_8601 :
00601 return 'N';
00602 break;
00603
00604 case 'ee' :
00605 return $this->_toComment(str_pad($this->date('N', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
00606 break;
00607
00608 case self::DAY_SUFFIX :
00609 return 'S';
00610 break;
00611
00612 case self::WEEKDAY_DIGIT :
00613 return 'w';
00614 break;
00615
00616 case self::DAY_OF_YEAR :
00617 return 'z';
00618 break;
00619
00620 case 'DDD' :
00621 return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 3, '0', STR_PAD_LEFT));
00622 break;
00623
00624 case 'DD' :
00625 return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
00626 break;
00627
00628 case self::WEEKDAY_NARROW :
00629 case 'EEEEE' :
00630 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
00631 $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
00632 return $this->_toComment(iconv_substr($day, 0, 1, 'UTF-8'));
00633 break;
00634
00635 case self::WEEKDAY_NAME :
00636 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
00637 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)));
00638 break;
00639
00640 case 'w' :
00641 $week = $this->date('W', $this->getUnixTimestamp(), false);
00642 return $this->_toComment(($week[0] == '0') ? $week[1] : $week);
00643 break;
00644
00645 case self::WEEK :
00646 return 'W';
00647 break;
00648
00649 case self::MONTH_NAME :
00650 $month = $this->date('n', $this->getUnixTimestamp(), false);
00651 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)));
00652 break;
00653
00654 case self::MONTH :
00655 return 'm';
00656 break;
00657
00658 case self::MONTH_NAME_SHORT :
00659 $month = $this->date('n', $this->getUnixTimestamp(), false);
00660 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)));
00661 break;
00662
00663 case self::MONTH_SHORT :
00664 return 'n';
00665 break;
00666
00667 case self::MONTH_DAYS :
00668 return 't';
00669 break;
00670
00671 case self::MONTH_NAME_NARROW :
00672 $month = $this->date('n', $this->getUnixTimestamp(), false);
00673 $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
00674 return $this->_toComment(iconv_substr($mon, 0, 1, 'UTF-8'));
00675 break;
00676
00677 case self::LEAPYEAR :
00678 return 'L';
00679 break;
00680
00681 case self::YEAR_8601 :
00682 return 'o';
00683 break;
00684
00685 case self::YEAR :
00686 return 'Y';
00687 break;
00688
00689 case self::YEAR_SHORT :
00690 return 'y';
00691 break;
00692
00693 case self::YEAR_SHORT_8601 :
00694 return $this->_toComment(substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2));
00695 break;
00696
00697 case self::MERIDIEM :
00698 $am = $this->date('a', $this->getUnixTimestamp(), false);
00699 if ($am == 'am') {
00700 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'am'));
00701 }
00702
00703 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'pm'));
00704 break;
00705
00706 case self::SWATCH :
00707 return 'B';
00708 break;
00709
00710 case self::HOUR_SHORT_AM :
00711 return 'g';
00712 break;
00713
00714 case self::HOUR_SHORT :
00715 return 'G';
00716 break;
00717
00718 case self::HOUR_AM :
00719 return 'h';
00720 break;
00721
00722 case self::HOUR :
00723 return 'H';
00724 break;
00725
00726 case self::MINUTE :
00727 return $this->_toComment(str_pad($this->date('i', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
00728 break;
00729
00730 case self::SECOND :
00731 return $this->_toComment(str_pad($this->date('s', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
00732 break;
00733
00734 case self::MINUTE_SHORT :
00735 return 'i';
00736 break;
00737
00738 case self::SECOND_SHORT :
00739 return 's';
00740 break;
00741
00742 case self::MILLISECOND :
00743 return $this->_toComment($this->_fractional);
00744 break;
00745
00746 case self::TIMEZONE_NAME :
00747 case 'vvvv' :
00748 return 'e';
00749 break;
00750
00751 case self::DAYLIGHT :
00752 return 'I';
00753 break;
00754
00755 case self::GMT_DIFF :
00756 case 'ZZ' :
00757 case 'ZZZ' :
00758 return 'O';
00759 break;
00760
00761 case self::GMT_DIFF_SEP :
00762 return 'P';
00763 break;
00764
00765 case self::TIMEZONE :
00766 case 'v' :
00767 case 'zz' :
00768 case 'zzz' :
00769 return 'T';
00770 break;
00771
00772 case self::TIMEZONE_SECS :
00773 return 'Z';
00774 break;
00775
00776 case self::ISO_8601 :
00777 return 'c';
00778 break;
00779
00780 case self::RFC_2822 :
00781 return 'r';
00782 break;
00783
00784 case self::TIMESTAMP :
00785 return 'U';
00786 break;
00787
00788 case self::ERA :
00789 case 'GG' :
00790 case 'GGG' :
00791 $year = $this->date('Y', $this->getUnixTimestamp(), false);
00792 if ($year < 0) {
00793 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')));
00794 }
00795
00796 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')));
00797 break;
00798
00799 case self::ERA_NARROW :
00800 $year = $this->date('Y', $this->getUnixTimestamp(), false);
00801 if ($year < 0) {
00802 return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')), 0, 1, 'UTF-8')) . '.';
00803 }
00804
00805 return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')), 0, 1, 'UTF-8')) . '.';
00806 break;
00807
00808 case self::ERA_NAME :
00809 $year = $this->date('Y', $this->getUnixTimestamp(), false);
00810 if ($year < 0) {
00811 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0')));
00812 }
00813
00814 return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1')));
00815 break;
00816
00817 case self::DATES :
00818 return $this->_toToken(Zend_Locale_Format::getDateFormat($locale), $locale);
00819 break;
00820
00821 case self::DATE_FULL :
00822 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full')), $locale);
00823 break;
00824
00825 case self::DATE_LONG :
00826 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long')), $locale);
00827 break;
00828
00829 case self::DATE_MEDIUM :
00830 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium')), $locale);
00831 break;
00832
00833 case self::DATE_SHORT :
00834 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short')), $locale);
00835 break;
00836
00837 case self::TIMES :
00838 return $this->_toToken(Zend_Locale_Format::getTimeFormat($locale), $locale);
00839 break;
00840
00841 case self::TIME_FULL :
00842 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'full'), $locale);
00843 break;
00844
00845 case self::TIME_LONG :
00846 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'long'), $locale);
00847 break;
00848
00849 case self::TIME_MEDIUM :
00850 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'medium'), $locale);
00851 break;
00852
00853 case self::TIME_SHORT :
00854 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'short'), $locale);
00855 break;
00856
00857 case self::DATETIME :
00858 return $this->_toToken(Zend_Locale_Format::getDateTimeFormat($locale), $locale);
00859 break;
00860
00861 case self::DATETIME_FULL :
00862 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full')), $locale);
00863 break;
00864
00865 case self::DATETIME_LONG :
00866 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long')), $locale);
00867 break;
00868
00869 case self::DATETIME_MEDIUM :
00870 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium')), $locale);
00871 break;
00872
00873 case self::DATETIME_SHORT :
00874 return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short')), $locale);
00875 break;
00876
00877 case self::ATOM :
00878 return 'Y\-m\-d\TH\:i\:sP';
00879 break;
00880
00881 case self::COOKIE :
00882 return 'l\, d\-M\-y H\:i\:s e';
00883 break;
00884
00885 case self::RFC_822 :
00886 return 'D\, d M y H\:i\:s O';
00887 break;
00888
00889 case self::RFC_850 :
00890 return 'l\, d\-M\-y H\:i\:s e';
00891 break;
00892
00893 case self::RFC_1036 :
00894 return 'D\, d M y H\:i\:s O';
00895 break;
00896
00897 case self::RFC_1123 :
00898 return 'D\, d M Y H\:i\:s O';
00899 break;
00900
00901 case self::RFC_3339 :
00902 return 'Y\-m\-d\TH\:i\:sP';
00903 break;
00904
00905 case self::RSS :
00906 return 'D\, d M Y H\:i\:s O';
00907 break;
00908
00909 case self::W3C :
00910 return 'Y\-m\-d\TH\:i\:sP';
00911 break;
00912 }
00913
00914 if ($token == '') {
00915 return '';
00916 }
00917
00918 switch ($token[0]) {
00919 case 'y' :
00920 if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
00921 return 'Y';
00922 }
00923
00924 $length = iconv_strlen($token, 'UTF-8');
00925 return $this->_toComment(str_pad($this->date('Y', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
00926 break;
00927
00928 case 'Y' :
00929 if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
00930 return 'o';
00931 }
00932
00933 $length = iconv_strlen($token, 'UTF-8');
00934 return $this->_toComment(str_pad($this->date('o', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
00935 break;
00936
00937 case 'A' :
00938 $length = iconv_strlen($token, 'UTF-8');
00939 $result = $this->_fractional;
00940 $result += $this->date('s', $this->getUnixTimestamp(), false) * 1000;
00941 $result += $this->date('i', $this->getUnixTimestamp(), false) * 60000;
00942 $result += $this->date('H', $this->getUnixTimestamp(), false) * 3600000;
00943
00944 return $this->_toComment(str_pad($result, $length, '0', STR_PAD_LEFT));
00945 break;
00946 }
00947
00948 return $this->_toComment($token);
00949 }
00950
00957 private function _toComment($token)
00958 {
00959 $token = str_split($token);
00960 $result = '';
00961 foreach ($token as $tok) {
00962 $result .= '\\' . $tok;
00963 }
00964
00965 return $result;
00966 }
00967
00976 private function _getDigitFromName($name)
00977 {
00978 switch($name) {
00979 case "Jan":
00980 return 1;
00981
00982 case "Feb":
00983 return 2;
00984
00985 case "Mar":
00986 return 3;
00987
00988 case "Apr":
00989 return 4;
00990
00991 case "May":
00992 return 5;
00993
00994 case "Jun":
00995 return 6;
00996
00997 case "Jul":
00998 return 7;
00999
01000 case "Aug":
01001 return 8;
01002
01003 case "Sep":
01004 return 9;
01005
01006 case "Oct":
01007 return 10;
01008
01009 case "Nov":
01010 return 11;
01011
01012 case "Dec":
01013 return 12;
01014
01015 default:
01016 require_once 'Zend/Date/Exception.php';
01017 throw new Zend_Date_Exception('Month ($name) is not a known month');
01018 }
01019 }
01020
01028 public static function getFullYear($value)
01029 {
01030 if ($value >= 0) {
01031 if ($value < 70) {
01032 $value += 2000;
01033 } else if ($value < 100) {
01034 $value += 1900;
01035 }
01036 }
01037 return $value;
01038 }
01039
01052 public function set($date, $part = null, $locale = null)
01053 {
01054 $zone = $this->getTimezoneFromString($date);
01055 $this->setTimezone($zone);
01056
01057 $result = $this->_calculate('set', $date, $part, $locale);
01058 return $result;
01059 }
01060
01076 public function add($date, $part = null, $locale = null)
01077 {
01078 $this->_calculate('add', $date, $part, $locale);
01079 $result = $this->get($part, $locale);
01080
01081 return $result;
01082 }
01083
01097 public function sub($date, $part = null, $locale = null)
01098 {
01099 $this->_calculate('sub', $date, $part, $locale);
01100 $result = $this->get($part, $locale);
01101
01102 return $result;
01103 }
01104
01115 public function compare($date, $part = null, $locale = null)
01116 {
01117 $compare = $this->_calculate('cmp', $date, $part, $locale);
01118
01119 if ($compare > 0) {
01120 return 1;
01121 } else if ($compare < 0) {
01122 return -1;
01123 }
01124 return 0;
01125 }
01126
01141 public function copyPart($part, $locale = null)
01142 {
01143 $clone = clone $this;
01144 $clone->setUnixTimestamp(0);
01145 if ($locale != null) {
01146 $clone->setLocale($locale);
01147 }
01148 $clone->set($this, $part);
01149 return $clone;
01150 }
01151
01158 public function getTimezoneFromString($zone)
01159 {
01160 if (is_array($zone)) {
01161 return $this->getTimezone();
01162 }
01163
01164 if ($zone instanceof Zend_Date) {
01165 return $zone->getTimezone();
01166 }
01167
01168 $match = array();
01169 preg_match('/\dZ$/', $zone, $match);
01170 if (!empty($match)) {
01171 return "Etc/UTC";
01172 }
01173
01174 preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
01175 if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
01176 $zone = "Etc/GMT";
01177 $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
01178 $zone .= (int) abs($match[count($match) - 1]);
01179 return $zone;
01180 }
01181
01182 preg_match('/([[:alpha:]\/]{3,30})(?!.*([[:alpha:]\/]{3,30}))/', $zone, $match);
01183 try {
01184 if (!empty($match) and (!is_int($match[count($match) - 1]))) {
01185 $oldzone = $this->getTimezone();
01186 $this->setTimezone($match[count($match) - 1]);
01187 $result = $this->getTimezone();
01188 $this->setTimezone($oldzone);
01189 if ($result !== $oldzone) {
01190 return $match[count($match) - 1];
01191 }
01192 }
01193 } catch (Exception $e) {
01194
01195 }
01196
01197 return $this->getTimezone();
01198 }
01199
01209 private function _assign($calc, $date, $comp = 0, $dst = false)
01210 {
01211 switch ($calc) {
01212 case 'set' :
01213 if (!empty($comp)) {
01214 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $comp));
01215 }
01216 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
01217 $value = $this->getUnixTimestamp();
01218 break;
01219 case 'add' :
01220 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
01221 $value = $this->getUnixTimestamp();
01222 break;
01223 case 'sub' :
01224 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $date));
01225 $value = $this->getUnixTimestamp();
01226 break;
01227 default :
01228
01229 return call_user_func(Zend_Locale_Math::$comp, $comp, $date);
01230 break;
01231 }
01232
01233
01234 if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
01235 $hour = $this->get(self::HOUR);
01236 if ($hour != $dst) {
01237 if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
01238 $value += 3600;
01239 } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
01240 $value -= 3600;
01241 }
01242 $this->setUnixTimestamp($value);
01243 }
01244 }
01245 return $this->getUnixTimestamp();
01246 }
01247
01248
01259 private function _calculate($calc, $date, $part, $locale)
01260 {
01261 if ($date === null) {
01262 require_once 'Zend/Date/Exception.php';
01263 throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
01264 }
01265
01266 if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale::isLocale($part, null, false))) {
01267 $locale = $part;
01268 $part = null;
01269 }
01270
01271 if ($locale === null) {
01272 $locale = $this->getLocale();
01273 }
01274
01275 $locale = (string) $locale;
01276
01277
01278 $year = $this->get(self::YEAR);
01279 $month = $this->get(self::MONTH_SHORT);
01280 $day = $this->get(self::DAY_SHORT);
01281 $hour = $this->get(self::HOUR_SHORT);
01282 $minute = $this->get(self::MINUTE_SHORT);
01283 $second = $this->get(self::SECOND_SHORT);
01284
01285 if ($date instanceof Zend_Date) {
01286 $date = $date->get($part, $locale);
01287 }
01288
01289 if (is_array($date) === true) {
01290 if (empty($part) === false) {
01291 switch($part) {
01292
01293 case self::DAY:
01294 case self::DAY_SHORT:
01295 if (isset($date['day']) === true) {
01296 $date = $date['day'];
01297 }
01298 break;
01299
01300 case self::WEEKDAY_SHORT:
01301 case self::WEEKDAY:
01302 case self::WEEKDAY_8601:
01303 case self::WEEKDAY_DIGIT:
01304 case self::WEEKDAY_NARROW:
01305 case self::WEEKDAY_NAME:
01306 if (isset($date['weekday']) === true) {
01307 $date = $date['weekday'];
01308 $part = self::WEEKDAY_DIGIT;
01309 }
01310 break;
01311 case self::DAY_OF_YEAR:
01312 if (isset($date['day_of_year']) === true) {
01313 $date = $date['day_of_year'];
01314 }
01315 break;
01316
01317 case self::MONTH:
01318 case self::MONTH_SHORT:
01319 case self::MONTH_NAME:
01320 case self::MONTH_NAME_SHORT:
01321 case self::MONTH_NAME_NARROW:
01322 if (isset($date['month']) === true) {
01323 $date = $date['month'];
01324 }
01325 break;
01326
01327 case self::YEAR:
01328 case self::YEAR_SHORT:
01329 case self::YEAR_8601:
01330 case self::YEAR_SHORT_8601:
01331 if (isset($date['year']) === true) {
01332 $date = $date['year'];
01333 }
01334 break;
01335
01336 case self::HOUR:
01337 case self::HOUR_AM:
01338 case self::HOUR_SHORT:
01339 case self::HOUR_SHORT_AM:
01340 if (isset($date['hour']) === true) {
01341 $date = $date['hour'];
01342 }
01343 break;
01344
01345 case self::MINUTE:
01346 case self::MINUTE_SHORT:
01347 if (isset($date['minute']) === true) {
01348 $date = $date['minute'];
01349 }
01350 break;
01351
01352 case self::SECOND:
01353 case self::SECOND_SHORT:
01354 if (isset($date['second']) === true) {
01355 $date = $date['second'];
01356 }
01357 break;
01358
01359 case self::TIMEZONE:
01360 case self::TIMEZONE_NAME:
01361 if (isset($date['timezone']) === true) {
01362 $date = $date['timezone'];
01363 }
01364 break;
01365 case self::TIMESTAMP:
01366 if (isset($date['timestamp']) === true) {
01367 $date = $date['timestamp'];
01368 }
01369 break;
01370 case self::WEEK:
01371 if (isset($date['week']) === true) {
01372 $date = $date['week'];
01373 }
01374 break;
01375 case self::TIMEZONE_SECS:
01376 if (isset($date['gmtsecs']) === true) {
01377 $date = $date['gmtsecs'];
01378 }
01379 break;
01380 default:
01381 require_once 'Zend/Date/Exception.php';
01382 throw new Zend_Date_Exception("datepart for part ($part) not found in array");
01383 break;
01384 }
01385 } else {
01386 $hours = 0;
01387 if (isset($date['hour']) === true) {
01388 $hours = $date['hour'];
01389 }
01390 $minutes = 0;
01391 if (isset($date['minute']) === true) {
01392 $minutes = $date['minute'];
01393 }
01394 $seconds = 0;
01395 if (isset($date['second']) === true) {
01396 $seconds = $date['second'];
01397 }
01398 $months = 0;
01399 if (isset($date['month']) === true) {
01400 $months = $date['month'];
01401 }
01402 $days = 0;
01403 if (isset($date['day']) === true) {
01404 $days = $date['day'];
01405 }
01406 $years = 0;
01407 if (isset($date['year']) === true) {
01408 $years = $date['year'];
01409 }
01410 return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true),
01411 $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour);
01412 }
01413 }
01414
01415
01416 switch($part) {
01417
01418
01419 case self::DAY:
01420 if (is_numeric($date)) {
01421 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
01422 $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
01423 }
01424 require_once 'Zend/Date/Exception.php';
01425 throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
01426 break;
01427
01428 case self::WEEKDAY_SHORT:
01429 $daylist = Zend_Locale_Data::getList($locale, 'day');
01430 $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
01431 $cnt = 0;
01432
01433 foreach ($daylist as $key => $value) {
01434 if (strtoupper(iconv_substr($value, 0, 3, 'UTF-8')) == strtoupper($date)) {
01435 $found = $cnt;
01436 break;
01437 }
01438 ++$cnt;
01439 }
01440
01441
01442 if ($cnt < 7) {
01443 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
01444 $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
01445 }
01446
01447
01448 require_once 'Zend/Date/Exception.php';
01449 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
01450 break;
01451
01452 case self::DAY_SHORT:
01453 if (is_numeric($date)) {
01454 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
01455 $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
01456 }
01457 require_once 'Zend/Date/Exception.php';
01458 throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
01459 break;
01460
01461 case self::WEEKDAY:
01462 $daylist = Zend_Locale_Data::getList($locale, 'day');
01463 $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
01464 $cnt = 0;
01465
01466 foreach ($daylist as $key => $value) {
01467 if (strtoupper($value) == strtoupper($date)) {
01468 $found = $cnt;
01469 break;
01470 }
01471 ++$cnt;
01472 }
01473
01474
01475 if ($cnt < 7) {
01476 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
01477 $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
01478 }
01479
01480
01481 require_once 'Zend/Date/Exception.php';
01482 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
01483 break;
01484
01485 case self::WEEKDAY_8601:
01486 $weekday = (int) $this->get(self::WEEKDAY_8601, $locale);
01487 if ((intval($date) > 0) and (intval($date) < 8)) {
01488 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
01489 $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
01490 }
01491
01492
01493 require_once 'Zend/Date/Exception.php';
01494 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
01495 break;
01496
01497 case self::DAY_SUFFIX:
01498 require_once 'Zend/Date/Exception.php';
01499 throw new Zend_Date_Exception('day suffix not supported', $date);
01500 break;
01501
01502 case self::WEEKDAY_DIGIT:
01503 $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
01504 if (is_numeric($date) and (intval($date) >= 0) and (intval($date) < 7)) {
01505 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
01506 $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
01507 }
01508
01509
01510 require_once 'Zend/Date/Exception.php';
01511 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
01512 break;
01513
01514 case self::DAY_OF_YEAR:
01515 if (is_numeric($date)) {
01516 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
01517 $this->mktime(0, 0, 0, $month, 1 + $day, 1970, true), $hour);
01518 }
01519 require_once 'Zend/Date/Exception.php';
01520 throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
01521 break;
01522
01523 case self::WEEKDAY_NARROW:
01524 $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
01525 $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
01526 $cnt = 0;
01527 foreach ($daylist as $key => $value) {
01528 if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($date)) {
01529 $found = $cnt;
01530 break;
01531 }
01532 ++$cnt;
01533 }
01534
01535
01536 if ($cnt < 7) {
01537 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
01538 $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
01539 }
01540
01541
01542 require_once 'Zend/Date/Exception.php';
01543 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
01544 break;
01545
01546 case self::WEEKDAY_NAME:
01547 $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
01548 $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
01549 $cnt = 0;
01550 foreach ($daylist as $key => $value) {
01551 if (strtoupper($value) == strtoupper($date)) {
01552 $found = $cnt;
01553 break;
01554 }
01555 ++$cnt;
01556 }
01557
01558
01559 if ($cnt < 7) {
01560 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
01561 $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
01562 }
01563
01564
01565 require_once 'Zend/Date/Exception.php';
01566 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
01567 break;
01568
01569
01570 case self::WEEK:
01571 if (is_numeric($date)) {
01572 $week = (int) $this->get(self::WEEK, $locale);
01573 return $this->_assign($calc, parent::mktime(0, 0, 0, 1, 1 + ($date * 7), 1970, true),
01574 parent::mktime(0, 0, 0, 1, 1 + ($week * 7), 1970, true), $hour);
01575 }
01576 require_once 'Zend/Date/Exception.php';
01577 throw new Zend_Date_Exception("invalid date ($date) operand, week expected", $date);
01578 break;
01579
01580
01581 case self::MONTH_NAME:
01582 $monthlist = Zend_Locale_Data::getList($locale, 'month');
01583 $cnt = 0;
01584 foreach ($monthlist as $key => $value) {
01585 if (strtoupper($value) == strtoupper($date)) {
01586 $found = $key;
01587 break;
01588 }
01589 ++$cnt;
01590 }
01591 $date = array_search($date, $monthlist);
01592
01593
01594 if ($cnt < 12) {
01595 $fixday = 0;
01596 if ($calc == 'add') {
01597 $date += $found;
01598 $calc = 'set';
01599 if (self::$_options['extend_month'] == false) {
01600 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01601 if ($parts['mday'] != $day) {
01602 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01603 }
01604 }
01605 } else if ($calc == 'sub') {
01606 $date = $month - $found;
01607 $calc = 'set';
01608 if (self::$_options['extend_month'] == false) {
01609 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01610 if ($parts['mday'] != $day) {
01611 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01612 }
01613 }
01614 }
01615 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
01616 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
01617 }
01618
01619
01620 require_once 'Zend/Date/Exception.php';
01621 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
01622 break;
01623
01624 case self::MONTH:
01625 if (is_numeric($date)) {
01626 $fixday = 0;
01627 if ($calc == 'add') {
01628 $date += $month;
01629 $calc = 'set';
01630 if (self::$_options['extend_month'] == false) {
01631 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01632 if ($parts['mday'] != $day) {
01633 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01634 }
01635 }
01636 } else if ($calc == 'sub') {
01637 $date = $month - $date;
01638 $calc = 'set';
01639 if (self::$_options['extend_month'] == false) {
01640 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01641 if ($parts['mday'] != $day) {
01642 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01643 }
01644 }
01645 }
01646 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
01647 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
01648 }
01649 require_once 'Zend/Date/Exception.php';
01650 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
01651 break;
01652
01653 case self::MONTH_NAME_SHORT:
01654 $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
01655 $cnt = 0;
01656 foreach ($monthlist as $key => $value) {
01657 if (strtoupper($value) == strtoupper($date)) {
01658 $found = $key;
01659 break;
01660 }
01661 ++$cnt;
01662 }
01663 $date = array_search($date, $monthlist);
01664
01665
01666 if ($cnt < 12) {
01667 $fixday = 0;
01668 if ($calc == 'add') {
01669 $date += $found;
01670 $calc = 'set';
01671 if (self::$_options['extend_month'] === false) {
01672 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01673 if ($parts['mday'] != $day) {
01674 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01675 }
01676 }
01677 } else if ($calc == 'sub') {
01678 $date = $month - $found;
01679 $calc = 'set';
01680 if (self::$_options['extend_month'] === false) {
01681 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01682 if ($parts['mday'] != $day) {
01683 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01684 }
01685 }
01686 }
01687 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
01688 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
01689 }
01690
01691
01692 require_once 'Zend/Date/Exception.php';
01693 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
01694 break;
01695
01696 case self::MONTH_SHORT:
01697 if (is_numeric($date) === true) {
01698 $fixday = 0;
01699 if ($calc === 'add') {
01700 $date += $month;
01701 $calc = 'set';
01702 if (self::$_options['extend_month'] === false) {
01703 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01704 if ($parts['mday'] != $day) {
01705 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01706 }
01707 }
01708 } else if ($calc === 'sub') {
01709 $date = $month - $date;
01710 $calc = 'set';
01711 if (self::$_options['extend_month'] === false) {
01712 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01713 if ($parts['mday'] != $day) {
01714 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01715 }
01716 }
01717 }
01718
01719 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
01720 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
01721 }
01722 require_once 'Zend/Date/Exception.php';
01723 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
01724 break;
01725
01726 case self::MONTH_DAYS:
01727 require_once 'Zend/Date/Exception.php';
01728 throw new Zend_Date_Exception('month days not supported', $date);
01729 break;
01730
01731 case self::MONTH_NAME_NARROW:
01732 $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow'));
01733 $cnt = 0;
01734 foreach ($monthlist as $key => $value) {
01735 if (strtoupper($value) === strtoupper($date)) {
01736 $found = $key;
01737 break;
01738 }
01739 ++$cnt;
01740 }
01741 $date = array_search($date, $monthlist);
01742
01743
01744 if ($cnt < 12) {
01745 $fixday = 0;
01746 if ($calc === 'add') {
01747 $date += $found;
01748 $calc = 'set';
01749 if (self::$_options['extend_month'] === false) {
01750 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01751 if ($parts['mday'] != $day) {
01752 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01753 }
01754 }
01755 } else if ($calc === 'sub') {
01756 $date = $month - $found;
01757 $calc = 'set';
01758 if (self::$_options['extend_month'] === false) {
01759 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
01760 if ($parts['mday'] != $day) {
01761 $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
01762 }
01763 }
01764 }
01765 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
01766 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
01767 }
01768
01769
01770 require_once 'Zend/Date/Exception.php';
01771 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
01772 break;
01773
01774
01775 case self::LEAPYEAR:
01776 require_once 'Zend/Date/Exception.php';
01777 throw new Zend_Date_Exception('leap year not supported', $date);
01778 break;
01779
01780 case self::YEAR_8601:
01781 if (is_numeric($date)) {
01782 if ($calc === 'add') {
01783 $date += $year;
01784 $calc = 'set';
01785 } else if ($calc === 'sub') {
01786 $date = $year - $date;
01787 $calc = 'set';
01788 }
01789 return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
01790 $this->mktime(0, 0, 0, $month, $day, $year, true), false);
01791 }
01792 require_once 'Zend/Date/Exception.php';
01793 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
01794 break;
01795
01796 case self::YEAR:
01797 if (is_numeric($date)) {
01798 if ($calc === 'add') {
01799 $date += $year;
01800 $calc = 'set';
01801 } else if ($calc === 'sub') {
01802 $date = $year - $date;
01803 $calc = 'set';
01804 }
01805 return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
01806 $this->mktime(0, 0, 0, $month, $day, $year, true), false);
01807 }
01808 require_once 'Zend/Date/Exception.php';
01809 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
01810 break;
01811
01812 case self::YEAR_SHORT:
01813 if (is_numeric($date)) {
01814 $date = intval($date);
01815 if (($calc == 'set') || ($calc == 'cmp')) {
01816 $date = self::getFullYear($date);
01817 }
01818 if ($calc === 'add') {
01819 $date += $year;
01820 $calc = 'set';
01821 } else if ($calc === 'sub') {
01822 $date = $year - $date;
01823 $calc = 'set';
01824 }
01825 return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
01826 $this->mktime(0, 0, 0, $month, $day, $year, true), false);
01827 }
01828 require_once 'Zend/Date/Exception.php';
01829 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
01830 break;
01831
01832 case self::YEAR_SHORT_8601:
01833 if (is_numeric($date)) {
01834 $date = intval($date);
01835 if (($calc === 'set') || ($calc === 'cmp')) {
01836 $date = self::getFullYear($date);
01837 }
01838 if ($calc === 'add') {
01839 $date += $year;
01840 $calc = 'set';
01841 } else if ($calc === 'sub') {
01842 $date = $year - $date;
01843 $calc = 'set';
01844 }
01845 return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
01846 $this->mktime(0, 0, 0, $month, $day, $year, true), false);
01847 }
01848 require_once 'Zend/Date/Exception.php';
01849 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
01850 break;
01851
01852
01853 case self::MERIDIEM:
01854 require_once 'Zend/Date/Exception.php';
01855 throw new Zend_Date_Exception('meridiem not supported', $date);
01856 break;
01857
01858 case self::SWATCH:
01859 if (is_numeric($date)) {
01860 $rest = intval($date);
01861 $hours = floor($rest * 24 / 1000);
01862 $rest = $rest - ($hours * 1000 / 24);
01863 $minutes = floor($rest * 1440 / 1000);
01864 $rest = $rest - ($minutes * 1000 / 1440);
01865 $seconds = floor($rest * 86400 / 1000);
01866 return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true),
01867 $this->mktime($hour, $minute, $second, 1, 1, 1970, true), false);
01868 }
01869 require_once 'Zend/Date/Exception.php';
01870 throw new Zend_Date_Exception("invalid date ($date) operand, swatchstamp expected", $date);
01871 break;
01872
01873 case self::HOUR_SHORT_AM:
01874 if (is_numeric($date)) {
01875 return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
01876 $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
01877 }
01878 require_once 'Zend/Date/Exception.php';
01879 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
01880 break;
01881
01882 case self::HOUR_SHORT:
01883 if (is_numeric($date)) {
01884 return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
01885 $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
01886 }
01887 require_once 'Zend/Date/Exception.php';
01888 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
01889 break;
01890
01891 case self::HOUR_AM:
01892 if (is_numeric($date)) {
01893 return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
01894 $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
01895 }
01896 require_once 'Zend/Date/Exception.php';
01897 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
01898 break;
01899
01900 case self::HOUR:
01901 if (is_numeric($date)) {
01902 return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
01903 $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
01904 }
01905 require_once 'Zend/Date/Exception.php';
01906 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
01907 break;
01908
01909 case self::MINUTE:
01910 if (is_numeric($date)) {
01911 return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
01912 $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
01913 }
01914 require_once 'Zend/Date/Exception.php';
01915 throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", $date);
01916 break;
01917
01918 case self::SECOND:
01919 if (is_numeric($date)) {
01920 return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
01921 $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
01922 }
01923 require_once 'Zend/Date/Exception.php';
01924 throw new Zend_Date_Exception("invalid date ($date) operand, second expected", $date);
01925 break;
01926
01927 case self::MILLISECOND:
01928 if (is_numeric($date)) {
01929 switch($calc) {
01930 case 'set' :
01931 return $this->setMillisecond($date);
01932 break;
01933 case 'add' :
01934 return $this->addMillisecond($date);
01935 break;
01936 case 'sub' :
01937 return $this->subMillisecond($date);
01938 break;
01939 }
01940 return $this->compareMillisecond($date);
01941 }
01942 require_once 'Zend/Date/Exception.php';
01943 throw new Zend_Date_Exception("invalid date ($date) operand, milliseconds expected", $date);
01944 break;
01945
01946 case self::MINUTE_SHORT:
01947 if (is_numeric($date)) {
01948 return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
01949 $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
01950 }
01951 require_once 'Zend/Date/Exception.php';
01952 throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", $date);
01953 break;
01954
01955 case self::SECOND_SHORT:
01956 if (is_numeric($date)) {
01957 return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
01958 $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
01959 }
01960 require_once 'Zend/Date/Exception.php';
01961 throw new Zend_Date_Exception("invalid date ($date) operand, second expected", $date);
01962 break;
01963
01964
01965
01966 case self::TIMEZONE_NAME:
01967 case self::TIMEZONE:
01968 case self::TIMEZONE_SECS:
01969 require_once 'Zend/Date/Exception.php';
01970 throw new Zend_Date_Exception('timezone not supported', $date);
01971 break;
01972
01973 case self::DAYLIGHT:
01974 require_once 'Zend/Date/Exception.php';
01975 throw new Zend_Date_Exception('daylight not supported', $date);
01976 break;
01977
01978 case self::GMT_DIFF:
01979 case self::GMT_DIFF_SEP:
01980 require_once 'Zend/Date/Exception.php';
01981 throw new Zend_Date_Exception('gmtdiff not supported', $date);
01982 break;
01983
01984
01985 case self::ISO_8601:
01986
01987 preg_match('/^(-{0,1}\d{4})-(\d{2})-(\d{2})/', $date, $datematch);
01988
01989 if (empty($datematch)) {
01990 preg_match('/^(-{0,1}\d{2})-(\d{2})-(\d{2})/', $date, $datematch);
01991 }
01992
01993 if (empty($datematch)) {
01994 preg_match('/^(-{0,1}\d{4})(\d{2})(\d{2})/', $date, $datematch);
01995 }
01996
01997 if (empty($datematch)) {
01998 preg_match('/^(-{0,1}\d{2})(\d{2})(\d{2})/', $date, $datematch);
01999 }
02000 $tmpdate = $date;
02001 if (!empty($datematch)) {
02002 $dateMatchCharCount = iconv_strlen($datematch[0], 'UTF-8');
02003 $tmpdate = iconv_substr($date,
02004 $dateMatchCharCount,
02005 iconv_strlen($date, 'UTF-8') - $dateMatchCharCount,
02006 'UTF-8');
02007 }
02008
02009 preg_match('/[T,\s]{0,1}(\d{2}):(\d{2}):(\d{2})/', $tmpdate, $timematch);
02010 if (empty($timematch)) {
02011 preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
02012 }
02013 if (empty($datematch) and empty($timematch)) {
02014 require_once 'Zend/Date/Exception.php';
02015 throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", $date);
02016 }
02017 if (!empty($timematch)) {
02018 $timeMatchCharCount = iconv_strlen($timematch[0], 'UTF-8');
02019 $tmpdate = iconv_substr($tmpdate,
02020 $timeMatchCharCount,
02021 iconv_strlen($tmpdate, 'UTF-8') - $timeMatchCharCount,
02022 'UTF-8');
02023 }
02024 if (empty($datematch)) {
02025 $datematch[1] = 1970;
02026 $datematch[2] = 1;
02027 $datematch[3] = 1;
02028 } else if (iconv_strlen($datematch[1], 'UTF-8') == 2) {
02029 $datematch[1] = self::getFullYear($datematch[1]);
02030 }
02031 if (empty($timematch)) {
02032 $timematch[1] = 0;
02033 $timematch[2] = 0;
02034 $timematch[3] = 0;
02035 }
02036
02037 if (($calc == 'set') || ($calc == 'cmp')) {
02038 --$datematch[2];
02039 --$month;
02040 --$datematch[3];
02041 --$day;
02042 $datematch[1] -= 1970;
02043 $year -= 1970;
02044 }
02045 return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false),
02046 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
02047 break;
02048
02049 case self::RFC_2822:
02050 $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);
02051 if (!$result) {
02052 require_once 'Zend/Date/Exception.php';
02053 throw new Zend_Date_Exception("no RFC 2822 format ($date)", $date);
02054 }
02055
02056 $months = $this->_getDigitFromName($match[2]);
02057
02058 if (($calc == 'set') || ($calc == 'cmp')) {
02059 --$months;
02060 --$month;
02061 --$match[1];
02062 --$day;
02063 $match[3] -= 1970;
02064 $year -= 1970;
02065 }
02066 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
02067 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
02068 break;
02069
02070 case self::TIMESTAMP:
02071 if (is_numeric($date)) {
02072 return $this->_assign($calc, $date, $this->getUnixTimestamp());
02073 }
02074 require_once 'Zend/Date/Exception.php';
02075 throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", $date);
02076 break;
02077
02078
02079
02080 case self::ERA:
02081 case self::ERA_NAME:
02082 require_once 'Zend/Date/Exception.php';
02083 throw new Zend_Date_Exception('era not supported', $date);
02084 break;
02085
02086 case self::DATES:
02087 try {
02088 $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
02089
02090 if (($calc == 'set') || ($calc == 'cmp')) {
02091 --$parsed['month'];
02092 --$month;
02093 --$parsed['day'];
02094 --$day;
02095 $parsed['year'] -= 1970;
02096 $year -= 1970;
02097 }
02098 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02099 $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02100 } catch (Zend_Locale_Exception $e) {
02101 require_once 'Zend/Date/Exception.php';
02102 throw new Zend_Date_Exception($e->getMessage(), $date);
02103 }
02104 break;
02105
02106 case self::DATE_FULL:
02107 try {
02108 $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
02109 $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02110
02111 if (($calc == 'set') || ($calc == 'cmp')) {
02112 --$parsed['month'];
02113 --$month;
02114 --$parsed['day'];
02115 --$day;
02116 $parsed['year'] -= 1970;
02117 $year -= 1970;
02118 }
02119 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02120 $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02121 } catch (Zend_Locale_Exception $e) {
02122 require_once 'Zend/Date/Exception.php';
02123 throw new Zend_Date_Exception($e->getMessage(), $date);
02124 }
02125 break;
02126
02127 case self::DATE_LONG:
02128 try {
02129 $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
02130 $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02131
02132 if (($calc == 'set') || ($calc == 'cmp')){
02133 --$parsed['month'];
02134 --$month;
02135 --$parsed['day'];
02136 --$day;
02137 $parsed['year'] -= 1970;
02138 $year -= 1970;
02139 }
02140 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02141 $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02142 } catch (Zend_Locale_Exception $e) {
02143 require_once 'Zend/Date/Exception.php';
02144 throw new Zend_Date_Exception($e->getMessage(), $date);
02145 }
02146 break;
02147
02148 case self::DATE_MEDIUM:
02149 try {
02150 $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
02151 $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02152
02153 if (($calc == 'set') || ($calc == 'cmp')) {
02154 --$parsed['month'];
02155 --$month;
02156 --$parsed['day'];
02157 --$day;
02158 $parsed['year'] -= 1970;
02159 $year -= 1970;
02160 }
02161 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02162 $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02163 } catch (Zend_Locale_Exception $e) {
02164 require_once 'Zend/Date/Exception.php';
02165 throw new Zend_Date_Exception($e->getMessage(), $date);
02166 }
02167 break;
02168
02169 case self::DATE_SHORT:
02170 try {
02171 $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
02172 $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02173
02174 $parsed['year'] = self::getFullYear($parsed['year']);
02175
02176 if (($calc == 'set') || ($calc == 'cmp')) {
02177 --$parsed['month'];
02178 --$month;
02179 --$parsed['day'];
02180 --$day;
02181 $parsed['year'] -= 1970;
02182 $year -= 1970;
02183 }
02184 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02185 $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02186 } catch (Zend_Locale_Exception $e) {
02187 require_once 'Zend/Date/Exception.php';
02188 throw new Zend_Date_Exception($e->getMessage(), $date);
02189 }
02190 break;
02191
02192 case self::TIMES:
02193 try {
02194 if ($calc != 'set') {
02195 $month = 1;
02196 $day = 1;
02197 $year = 1970;
02198 }
02199 $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
02200 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
02201 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
02202 } catch (Zend_Locale_Exception $e) {
02203 require_once 'Zend/Date/Exception.php';
02204 throw new Zend_Date_Exception($e->getMessage(), $date);
02205 }
02206 break;
02207
02208 case self::TIME_FULL:
02209 try {
02210 $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
02211 $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02212 if ($calc != 'set') {
02213 $month = 1;
02214 $day = 1;
02215 $year = 1970;
02216 }
02217
02218 if (!isset($parsed['second'])) {
02219 $parsed['second'] = 0;
02220 }
02221
02222 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
02223 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
02224 } catch (Zend_Locale_Exception $e) {
02225 require_once 'Zend/Date/Exception.php';
02226 throw new Zend_Date_Exception($e->getMessage(), $date);
02227 }
02228 break;
02229
02230 case self::TIME_LONG:
02231 try {
02232 $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
02233 $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02234 if ($calc != 'set') {
02235 $month = 1;
02236 $day = 1;
02237 $year = 1970;
02238 }
02239 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
02240 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
02241 } catch (Zend_Locale_Exception $e) {
02242 require_once 'Zend/Date/Exception.php';
02243 throw new Zend_Date_Exception($e->getMessage(), $date);
02244 }
02245 break;
02246
02247 case self::TIME_MEDIUM:
02248 try {
02249 $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
02250 $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02251 if ($calc != 'set') {
02252 $month = 1;
02253 $day = 1;
02254 $year = 1970;
02255 }
02256 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
02257 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
02258 } catch (Zend_Locale_Exception $e) {
02259 require_once 'Zend/Date/Exception.php';
02260 throw new Zend_Date_Exception($e->getMessage(), $date);
02261 }
02262 break;
02263
02264 case self::TIME_SHORT:
02265 try {
02266 $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
02267 $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02268 if ($calc != 'set') {
02269 $month = 1;
02270 $day = 1;
02271 $year = 1970;
02272 }
02273
02274 if (!isset($parsed['second'])) {
02275 $parsed['second'] = 0;
02276 }
02277
02278 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
02279 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
02280 } catch (Zend_Locale_Exception $e) {
02281 require_once 'Zend/Date/Exception.php';
02282 throw new Zend_Date_Exception($e->getMessage(), $date);
02283 }
02284 break;
02285
02286 case self::DATETIME:
02287 try {
02288 $parsed = Zend_Locale_Format::getDateTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
02289 if (($calc == 'set') || ($calc == 'cmp')) {
02290 --$parsed['month'];
02291 --$month;
02292 --$parsed['day'];
02293 --$day;
02294 $parsed['year'] -= 1970;
02295 $year -= 1970;
02296 }
02297 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02298 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02299 } catch (Zend_Locale_Exception $e) {
02300 require_once 'Zend/Date/Exception.php';
02301 throw new Zend_Date_Exception($e->getMessage(), $date);
02302 }
02303 break;
02304
02305 case self::DATETIME_FULL:
02306 try {
02307 $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
02308 $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02309
02310 if (($calc == 'set') || ($calc == 'cmp')) {
02311 --$parsed['month'];
02312 --$month;
02313 --$parsed['day'];
02314 --$day;
02315 $parsed['year'] -= 1970;
02316 $year -= 1970;
02317 }
02318
02319 if (!isset($parsed['second'])) {
02320 $parsed['second'] = 0;
02321 }
02322
02323 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02324 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02325 } catch (Zend_Locale_Exception $e) {
02326 require_once 'Zend/Date/Exception.php';
02327 throw new Zend_Date_Exception($e->getMessage(), $date);
02328 }
02329 break;
02330
02331 case self::DATETIME_LONG:
02332 try {
02333 $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
02334 $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02335
02336 if (($calc == 'set') || ($calc == 'cmp')){
02337 --$parsed['month'];
02338 --$month;
02339 --$parsed['day'];
02340 --$day;
02341 $parsed['year'] -= 1970;
02342 $year -= 1970;
02343 }
02344 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02345 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02346 } catch (Zend_Locale_Exception $e) {
02347 require_once 'Zend/Date/Exception.php';
02348 throw new Zend_Date_Exception($e->getMessage(), $date);
02349 }
02350 break;
02351
02352 case self::DATETIME_MEDIUM:
02353 try {
02354 $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
02355 $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02356 if (($calc == 'set') || ($calc == 'cmp')) {
02357 --$parsed['month'];
02358 --$month;
02359 --$parsed['day'];
02360 --$day;
02361 $parsed['year'] -= 1970;
02362 $year -= 1970;
02363 }
02364 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02365 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02366 } catch (Zend_Locale_Exception $e) {
02367 require_once 'Zend/Date/Exception.php';
02368 throw new Zend_Date_Exception($e->getMessage(), $date);
02369 }
02370 break;
02371
02372 case self::DATETIME_SHORT:
02373 try {
02374 $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
02375 $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
02376
02377 $parsed['year'] = self::getFullYear($parsed['year']);
02378
02379 if (($calc == 'set') || ($calc == 'cmp')) {
02380 --$parsed['month'];
02381 --$month;
02382 --$parsed['day'];
02383 --$day;
02384 $parsed['year'] -= 1970;
02385 $year -= 1970;
02386 }
02387
02388 if (!isset($parsed['second'])) {
02389 $parsed['second'] = 0;
02390 }
02391
02392 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
02393 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
02394 } catch (Zend_Locale_Exception $e) {
02395 require_once 'Zend/Date/Exception.php';
02396 throw new Zend_Date_Exception($e->getMessage(), $date);
02397 }
02398 break;
02399
02400
02401 case self::ATOM:
02402 case self::RFC_3339:
02403 $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);
02404 if (!$result) {
02405 require_once 'Zend/Date/Exception.php';
02406 throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", $date);
02407 }
02408
02409 if (($calc == 'set') || ($calc == 'cmp')) {
02410 --$match[2];
02411 --$month;
02412 --$match[3];
02413 --$day;
02414 $match[1] -= 1970;
02415 $year -= 1970;
02416 }
02417 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
02418 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
02419 break;
02420
02421 case self::COOKIE:
02422 $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);
02423 if (!$result) {
02424 require_once 'Zend/Date/Exception.php';
02425 throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", $date);
02426 }
02427 $matchStartPos = iconv_strpos($match[0], ' ', 0, 'UTF-8') + 1;
02428 $match[0] = iconv_substr($match[0],
02429 $matchStartPos,
02430 iconv_strlen($match[0], 'UTF-8') - $matchStartPos,
02431 'UTF-8');
02432
02433 $months = $this->_getDigitFromName($match[2]);
02434 $match[3] = self::getFullYear($match[3]);
02435
02436 if (($calc == 'set') || ($calc == 'cmp')) {
02437 --$months;
02438 --$month;
02439 --$match[1];
02440 --$day;
02441 $match[3] -= 1970;
02442 $year -= 1970;
02443 }
02444 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
02445 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
02446 break;
02447
02448 case self::RFC_822:
02449 case self::RFC_1036:
02450
02451 $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);
02452 if (!$result) {
02453 require_once 'Zend/Date/Exception.php';
02454 throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", $date);
02455 }
02456
02457 $months = $this->_getDigitFromName($match[2]);
02458 $match[3] = self::getFullYear($match[3]);
02459
02460 if (($calc == 'set') || ($calc == 'cmp')) {
02461 --$months;
02462 --$month;
02463 --$match[1];
02464 --$day;
02465 $match[3] -= 1970;
02466 $year -= 1970;
02467 }
02468 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
02469 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
02470 break;
02471
02472 case self::RFC_850:
02473 $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);
02474 if (!$result) {
02475 require_once 'Zend/Date/Exception.php';
02476 throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", $date);
02477 }
02478
02479 $months = $this->_getDigitFromName($match[2]);
02480 $match[3] = self::getFullYear($match[3]);
02481
02482 if (($calc == 'set') || ($calc == 'cmp')) {
02483 --$months;
02484 --$month;
02485 --$match[1];
02486 --$day;
02487 $match[3] -= 1970;
02488 $year -= 1970;
02489 }
02490 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
02491 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
02492 break;
02493
02494 case self::RFC_1123:
02495 $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);
02496 if (!$result) {
02497 require_once 'Zend/Date/Exception.php';
02498 throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", $date);
02499 }
02500
02501 $months = $this->_getDigitFromName($match[2]);
02502
02503 if (($calc == 'set') || ($calc == 'cmp')) {
02504 --$months;
02505 --$month;
02506 --$match[1];
02507 --$day;
02508 $match[3] -= 1970;
02509 $year -= 1970;
02510 }
02511 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
02512 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
02513 break;
02514
02515 case self::RSS:
02516 $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);
02517 if (!$result) {
02518 require_once 'Zend/Date/Exception.php';
02519 throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", $date);
02520 }
02521
02522 $months = $this->_getDigitFromName($match[2]);
02523 $match[3] = self::getFullYear($match[3]);
02524
02525 if (($calc == 'set') || ($calc == 'cmp')) {
02526 --$months;
02527 --$month;
02528 --$match[1];
02529 --$day;
02530 $match[3] -= 1970;
02531 $year -= 1970;
02532 }
02533 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
02534 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
02535 break;
02536
02537 case self::W3C:
02538 $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
02539 if (!$result) {
02540 require_once 'Zend/Date/Exception.php';
02541 throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", $date);
02542 }
02543
02544 if (($calc == 'set') || ($calc == 'cmp')) {
02545 --$match[2];
02546 --$month;
02547 --$match[3];
02548 --$day;
02549 $match[1] -= 1970;
02550 $year -= 1970;
02551 }
02552 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
02553 $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
02554 break;
02555
02556 default:
02557 if (!is_numeric($date) || !empty($part)) {
02558 try {
02559 if (self::$_options['format_type'] == 'php') {
02560 $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
02561 }
02562 if (empty($part)) {
02563 $part = Zend_Locale_Format::getDateFormat($locale) . " ";
02564 $part .= Zend_Locale_Format::getTimeFormat($locale);
02565 }
02566 $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
02567 if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
02568 $parsed['year'] = self::getFullYear($parsed['year']);
02569 }
02570 if (($calc == 'set') || ($calc == 'cmp')) {
02571 if (isset($parsed['month'])) {
02572 --$parsed['month'];
02573 } else {
02574 $parsed['month'] = 0;
02575 }
02576 if (isset($parsed['day'])) {
02577 --$parsed['day'];
02578 } else {
02579 $parsed['day'] = 0;
02580 }
02581 if (isset($parsed['year'])) {
02582 $parsed['year'] -= 1970;
02583 } else {
02584 $parsed['year'] = 0;
02585 }
02586 }
02587 return $this->_assign($calc, $this->mktime(
02588 isset($parsed['hour']) ? $parsed['hour'] : 0,
02589 isset($parsed['minute']) ? $parsed['minute'] : 0,
02590 isset($parsed['second']) ? $parsed['second'] : 0,
02591 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'],
02592 false), $this->getUnixTimestamp(), false);
02593 } catch (Zend_Locale_Exception $e) {
02594 if (!is_numeric($date)) {
02595 require_once 'Zend/Date/Exception.php';
02596 throw new Zend_Date_Exception($e->getMessage(), $date);
02597 }
02598 }
02599 }
02600 return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
02601 break;
02602 }
02603 }
02604
02616 public function equals($date, $part = null, $locale = null)
02617 {
02618 $result = $this->compare($date, $part, $locale);
02619
02620 if ($result == 0) {
02621 return true;
02622 }
02623 return false;
02624 }
02625
02637 public function isEarlier($date, $part = null, $locale = null)
02638 {
02639 $result = $this->compare($date, $part, $locale);
02640
02641 if ($result == -1) {
02642 return true;
02643 }
02644 return false;
02645 }
02646
02659 public function isLater($date, $part = null, $locale = null)
02660 {
02661 $result = $this->compare($date, $part, $locale);
02662
02663 if ($result == 1) {
02664 return true;
02665 }
02666 return false;
02667 }
02668
02677 public function getTime($locale = null)
02678 {
02679 return $this->copyPart(self::TIME_MEDIUM, $locale);
02680 }
02681
02692 private function _time($calc, $time, $format, $locale)
02693 {
02694 if ($time === null) {
02695 require_once 'Zend/Date/Exception.php';
02696 throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
02697 }
02698
02699 if ($time instanceof Zend_Date) {
02700
02701 $time = $time->get('HH:mm:ss');
02702 } else {
02703 if (is_array($time)) {
02704 if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
02705 (isset($time['second']) === true)) {
02706 $parsed = $time;
02707 } else {
02708 require_once 'Zend/Date/Exception.php';
02709 throw new Zend_Date_Exception("no hour, minute or second given in array");
02710 }
02711 } else {
02712 if (self::$_options['format_type'] == 'php') {
02713 $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
02714 }
02715 try {
02716 if ($locale === null) {
02717 $locale = $this->getLocale();
02718 }
02719
02720 $parsed = Zend_Locale_Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
02721 } catch (Zend_Locale_Exception $e) {
02722 require_once 'Zend/Date/Exception.php';
02723 throw new Zend_Date_Exception($e->getMessage());
02724 }
02725 }
02726 $time = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT) . ":";
02727 $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT) . ":";
02728 $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT);
02729 }
02730
02731 $return = $this->_calcdetail($calc, $time, self::TIMES, 'de');
02732 if ($calc != 'cmp') {
02733 return $this;
02734 }
02735 return $return;
02736 }
02737
02738
02751 public function setTime($time, $format = null, $locale = null)
02752 {
02753 return $this->_time('set', $time, $format, $locale);
02754 }
02755
02756
02769 public function addTime($time, $format = null, $locale = null)
02770 {
02771 return $this->_time('add', $time, $format, $locale);
02772 }
02773
02774
02787 public function subTime($time, $format = null, $locale = null)
02788 {
02789 return $this->_time('sub', $time, $format, $locale);
02790 }
02791
02792
02805 public function compareTime($time, $format = null, $locale = null)
02806 {
02807 return $this->_time('cmp', $time, $format, $locale);
02808 }
02809
02816 public function getDate($locale = null)
02817 {
02818 $date = $this->copyPart(self::DATE_MEDIUM, $locale);
02819 $date->addTimestamp($this->getGmtOffset());
02820 return $date;
02821 }
02822
02833 private function _date($calc, $date, $format, $locale)
02834 {
02835 if ($date === null) {
02836 require_once 'Zend/Date/Exception.php';
02837 throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
02838 }
02839
02840 if ($date instanceof Zend_Date) {
02841
02842 $date = $date->get('d.M.Y');
02843 } else {
02844 if (is_array($date)) {
02845 if ((isset($date['year']) === true) or (isset($date['month']) === true) or
02846 (isset($date['day']) === true)) {
02847 $parsed = $date;
02848 } else {
02849 require_once 'Zend/Date/Exception.php';
02850 throw new Zend_Date_Exception("no day,month or year given in array");
02851 }
02852 } else {
02853 if (self::$_options['format_type'] == 'php') {
02854 $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
02855 }
02856 try {
02857 if ($locale === null) {
02858 $locale = $this->getLocale();
02859 }
02860
02861 $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
02862 if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
02863 $parsed['year'] = self::getFullYear($parsed['year']);
02864 }
02865 } catch (Zend_Locale_Exception $e) {
02866 require_once 'Zend/Date/Exception.php';
02867 throw new Zend_Date_Exception($e->getMessage());
02868 }
02869 }
02870 $date = $parsed['day'] . "." . $parsed['month'] . "." . $parsed['year'];
02871 }
02872
02873 $return = $this->_calcdetail($calc, $date, self::DATE_MEDIUM, 'de');
02874 if ($calc != 'cmp') {
02875 return $this;
02876 }
02877 return $return;
02878 }
02879
02880
02893 public function setDate($date, $format = null, $locale = null)
02894 {
02895 return $this->_date('set', $date, $format, $locale);
02896 }
02897
02898
02911 public function addDate($date, $format = null, $locale = null)
02912 {
02913 return $this->_date('add', $date, $format, $locale);
02914 }
02915
02916
02930 public function subDate($date, $format = null, $locale = null)
02931 {
02932 return $this->_date('sub', $date, $format, $locale);
02933 }
02934
02935
02949 public function compareDate($date, $format = null, $locale = null)
02950 {
02951 return $this->_date('cmp', $date, $format, $locale);
02952 }
02953
02954
02964 public function getIso($locale = null)
02965 {
02966 return $this->get(self::ISO_8601, $locale);
02967 }
02968
02969
02981 public function setIso($date, $locale = null)
02982 {
02983 return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
02984 }
02985
02986
02998 public function addIso($date, $locale = null)
02999 {
03000 return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
03001 }
03002
03003
03015 public function subIso($date, $locale = null)
03016 {
03017 return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
03018 }
03019
03020
03032 public function compareIso($date, $locale = null)
03033 {
03034 return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
03035 }
03036
03037
03045 public function getArpa($locale = null)
03046 {
03047 return $this->get(self::RFC_822, $locale);
03048 }
03049
03050
03062 public function setArpa($date, $locale = null)
03063 {
03064 return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
03065 }
03066
03067
03080 public function addArpa($date, $locale = null)
03081 {
03082 return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
03083 }
03084
03085
03098 public function subArpa($date, $locale = null)
03099 {
03100 return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
03101 }
03102
03103
03116 public function compareArpa($date, $locale = null)
03117 {
03118 return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
03119 }
03120
03121
03128 private function _checkLocation($location)
03129 {
03130 if (!isset($location['longitude']) or !isset($location['latitude'])) {
03131 require_once 'Zend/Date/Exception.php';
03132 throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', $location);
03133 }
03134 if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
03135 require_once 'Zend/Date/Exception.php';
03136 throw new Zend_Date_Exception('Longitude must be between -180 and 180', $location);
03137 }
03138 if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
03139 require_once 'Zend/Date/Exception.php';
03140 throw new Zend_Date_Exception('Latitude must be between -90 and 90', $location);
03141 }
03142
03143 if (!isset($location['horizon'])){
03144 $location['horizon'] = 'effective';
03145 }
03146
03147 switch ($location['horizon']) {
03148 case 'civil' :
03149 return -0.104528;
03150 break;
03151 case 'nautic' :
03152 return -0.207912;
03153 break;
03154 case 'astronomic' :
03155 return -0.309017;
03156 break;
03157 default :
03158 return -0.0145439;
03159 break;
03160 }
03161 }
03162
03163
03175 public function getSunrise($location)
03176 {
03177 $horizon = $this->_checkLocation($location);
03178 $result = clone $this;
03179 $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
03180 return $result;
03181 }
03182
03183
03195 public function getSunset($location)
03196 {
03197 $horizon = $this->_checkLocation($location);
03198 $result = clone $this;
03199 $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
03200 return $result;
03201 }
03202
03203
03215 public function getSunInfo($location)
03216 {
03217 $suninfo = array();
03218 for ($i = 0; $i < 4; ++$i) {
03219 switch ($i) {
03220 case 0 :
03221 $location['horizon'] = 'effective';
03222 break;
03223 case 1 :
03224 $location['horizon'] = 'civil';
03225 break;
03226 case 2 :
03227 $location['horizon'] = 'nautic';
03228 break;
03229 case 3 :
03230 $location['horizon'] = 'astronomic';
03231 break;
03232 }
03233 $horizon = $this->_checkLocation($location);
03234 $result = clone $this;
03235 $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
03236 $suninfo['sunrise'][$location['horizon']] = $result;
03237 $result = clone $this;
03238 $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
03239 $suninfo['sunset'][$location['horizon']] = $result;
03240 }
03241 return $suninfo;
03242 }
03243
03244
03251 public static function checkLeapYear($year)
03252 {
03253 if ($year instanceof Zend_Date) {
03254 $year = (int) $year->get(self::YEAR);
03255 }
03256 if (is_array($year)) {
03257 if (isset($year['year']) === true) {
03258 $year = $year['year'];
03259 } else {
03260 require_once 'Zend/Date/Exception.php';
03261 throw new Zend_Date_Exception("no year given in array");
03262 }
03263 }
03264 if (!is_numeric($year)) {
03265 require_once 'Zend/Date/Exception.php';
03266 throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", $year);
03267 }
03268
03269 return (bool) parent::isYearLeapYear($year);
03270 }
03271
03272
03278 public function isLeapYear()
03279 {
03280 return self::checkLeapYear($this);
03281 }
03282
03283
03289 public function isToday()
03290 {
03291 $today = $this->date('Ymd', $this->_getTime());
03292 $day = $this->date('Ymd', $this->getUnixTimestamp());
03293 return ($today == $day);
03294 }
03295
03296
03302 public function isYesterday()
03303 {
03304 list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
03305
03306 $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
03307 $day = $this->date('Ymd', $this->getUnixTimestamp());
03308 return $day == $yesterday;
03309 }
03310
03311
03317 public function isTomorrow()
03318 {
03319 list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
03320
03321 $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
03322 $day = $this->date('Ymd', $this->getUnixTimestamp());
03323 return $day == $tomorrow;
03324 }
03325
03332 public static function now($locale = null)
03333 {
03334 return new Zend_Date(time(), self::TIMESTAMP, $locale);
03335 }
03336
03347 private function _calcdetail($calc, $date, $type, $locale)
03348 {
03349 switch($calc) {
03350 case 'set' :
03351 return $this->set($date, $type, $locale);
03352 break;
03353 case 'add' :
03354 return $this->add($date, $type, $locale);
03355 break;
03356 case 'sub' :
03357 return $this->sub($date, $type, $locale);
03358 break;
03359 }
03360 return $this->compare($date, $type, $locale);
03361 }
03362
03372 private function _calcvalue($calc, $value, $type, $parameter, $locale)
03373 {
03374 if ($value === null) {
03375 require_once 'Zend/Date/Exception.php';
03376 throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
03377 }
03378
03379 if ($locale === null) {
03380 $locale = $this->getLocale();
03381 }
03382
03383 if ($value instanceof Zend_Date) {
03384
03385 $value = $value->get($parameter, $locale);
03386 } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
03387 require_once 'Zend/Date/Exception.php';
03388 throw new Zend_Date_Exception("invalid $type ($value) operand", $value);
03389 }
03390
03391 $return = $this->_calcdetail($calc, $value, $parameter, $locale);
03392 if ($calc != 'cmp') {
03393 return $this;
03394 }
03395 return $return;
03396 }
03397
03398
03406 public function getYear($locale = null)
03407 {
03408 return $this->copyPart(self::YEAR, $locale);
03409 }
03410
03411
03425 public function setYear($year, $locale = null)
03426 {
03427 return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
03428 }
03429
03430
03444 public function addYear($year, $locale = null)
03445 {
03446 return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
03447 }
03448
03449
03463 public function subYear($year, $locale = null)
03464 {
03465 return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
03466 }
03467
03468
03479 public function compareYear($year, $locale = null)
03480 {
03481 return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
03482 }
03483
03484
03492 public function getMonth($locale = null)
03493 {
03494 return $this->copyPart(self::MONTH, $locale);
03495 }
03496
03497
03507 private function _month($calc, $month, $locale)
03508 {
03509 if ($month === null) {
03510 require_once 'Zend/Date/Exception.php';
03511 throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
03512 }
03513
03514 if ($locale === null) {
03515 $locale = $this->getLocale();
03516 }
03517
03518 if ($month instanceof Zend_Date) {
03519
03520 $found = $month->get(self::MONTH_SHORT, $locale);
03521 } else {
03522 if (is_numeric($month)) {
03523 $found = $month;
03524 } else if (is_array($month)) {
03525 if (isset($month['month']) === true) {
03526 $month = $month['month'];
03527 } else {
03528 require_once 'Zend/Date/Exception.php';
03529 throw new Zend_Date_Exception("no month given in array");
03530 }
03531 } else {
03532 $monthlist = Zend_Locale_Data::getList($locale, 'month');
03533 $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
03534
03535 $monthlist = array_merge($monthlist, $monthlist2);
03536 $found = 0;
03537 $cnt = 0;
03538 foreach ($monthlist as $key => $value) {
03539 if (strtoupper($value) == strtoupper($month)) {
03540 $found = ($key % 12) + 1;
03541 break;
03542 }
03543 ++$cnt;
03544 }
03545 if ($found == 0) {
03546 foreach ($monthlist2 as $key => $value) {
03547 if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
03548 $found = $key + 1;
03549 break;
03550 }
03551 ++$cnt;
03552 }
03553 }
03554 if ($found == 0) {
03555 require_once 'Zend/Date/Exception.php';
03556 throw new Zend_Date_Exception("unknown month name ($month)", $month);
03557 }
03558 }
03559 }
03560 $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
03561 if ($calc != 'cmp') {
03562 return $this;
03563 }
03564 return $return;
03565 }
03566
03567
03581 public function setMonth($month, $locale = null)
03582 {
03583 return $this->_month('set', $month, $locale);
03584 }
03585
03586
03600 public function addMonth($month, $locale = null)
03601 {
03602 return $this->_month('add', $month, $locale);
03603 }
03604
03605
03619 public function subMonth($month, $locale = null)
03620 {
03621 return $this->_month('sub', $month, $locale);
03622 }
03623
03624
03635 public function compareMonth($month, $locale = null)
03636 {
03637 return $this->_month('cmp', $month, $locale);
03638 }
03639
03640
03648 public function getDay($locale = null)
03649 {
03650 return $this->copyPart(self::DAY_SHORT, $locale);
03651 }
03652
03653
03662 private function _day($calc, $day, $locale)
03663 {
03664 if ($day === null) {
03665 require_once 'Zend/Date/Exception.php';
03666 throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
03667 }
03668
03669 if ($locale === null) {
03670 $locale = $this->getLocale();
03671 }
03672
03673 if ($day instanceof Zend_Date) {
03674 $day = $day->get(self::DAY_SHORT, $locale);
03675 }
03676
03677 if (is_numeric($day)) {
03678 $type = self::DAY_SHORT;
03679 } else if (is_array($day)) {
03680 if (isset($day['day']) === true) {
03681 $day = $day['day'];
03682 $type = self::WEEKDAY;
03683 } else {
03684 require_once 'Zend/Date/Exception.php';
03685 throw new Zend_Date_Exception("no day given in array");
03686 }
03687 } else {
03688 switch (iconv_strlen($day, 'UTF-8')) {
03689 case 1 :
03690 $type = self::WEEKDAY_NARROW;
03691 break;
03692 case 2:
03693 $type = self::WEEKDAY_NAME;
03694 break;
03695 case 3:
03696 $type = self::WEEKDAY_SHORT;
03697 break;
03698 default:
03699 $type = self::WEEKDAY;
03700 break;
03701 }
03702 }
03703 $return = $this->_calcdetail($calc, $day, $type, $locale);
03704 if ($calc != 'cmp') {
03705 return $this;
03706 }
03707 return $return;
03708 }
03709
03710
03725 public function setDay($day, $locale = null)
03726 {
03727 return $this->_day('set', $day, $locale);
03728 }
03729
03730
03745 public function addDay($day, $locale = null)
03746 {
03747 return $this->_day('add', $day, $locale);
03748 }
03749
03750
03765 public function subDay($day, $locale = null)
03766 {
03767 return $this->_day('sub', $day, $locale);
03768 }
03769
03770
03781 public function compareDay($day, $locale = null)
03782 {
03783 return $this->_day('cmp', $day, $locale);
03784 }
03785
03786
03795 public function getWeekday($locale = null)
03796 {
03797 return $this->copyPart(self::WEEKDAY, $locale);
03798 }
03799
03800
03810 private function _weekday($calc, $weekday, $locale)
03811 {
03812 if ($weekday === null) {
03813 require_once 'Zend/Date/Exception.php';
03814 throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
03815 }
03816
03817 if ($locale === null) {
03818 $locale = $this->getLocale();
03819 }
03820
03821 if ($weekday instanceof Zend_Date) {
03822 $weekday = $weekday->get(self::WEEKDAY_8601, $locale);
03823 }
03824
03825 if (is_numeric($weekday)) {
03826 $type = self::WEEKDAY_8601;
03827 } else if (is_array($weekday)) {
03828 if (isset($weekday['weekday']) === true) {
03829 $weekday = $weekday['weekday'];
03830 $type = self::WEEKDAY;
03831 } else {
03832 require_once 'Zend/Date/Exception.php';
03833 throw new Zend_Date_Exception("no weekday given in array");
03834 }
03835 } else {
03836 switch(iconv_strlen($weekday, 'UTF-8')) {
03837 case 1:
03838 $type = self::WEEKDAY_NARROW;
03839 break;
03840 case 2:
03841 $type = self::WEEKDAY_NAME;
03842 break;
03843 case 3:
03844 $type = self::WEEKDAY_SHORT;
03845 break;
03846 default:
03847 $type = self::WEEKDAY;
03848 break;
03849 }
03850 }
03851 $return = $this->_calcdetail($calc, $weekday, $type, $locale);
03852 if ($calc != 'cmp') {
03853 return $this;
03854 }
03855 return $return;
03856 }
03857
03858
03871 public function setWeekday($weekday, $locale = null)
03872 {
03873 return $this->_weekday('set', $weekday, $locale);
03874 }
03875
03876
03891 public function addWeekday($weekday, $locale = null)
03892 {
03893 return $this->_weekday('add', $weekday, $locale);
03894 }
03895
03896
03911 public function subWeekday($weekday, $locale = null)
03912 {
03913 return $this->_weekday('sub', $weekday, $locale);
03914 }
03915
03916
03927 public function compareWeekday($weekday, $locale = null)
03928 {
03929 return $this->_weekday('cmp', $weekday, $locale);
03930 }
03931
03932
03940 public function getDayOfYear($locale = null)
03941 {
03942 return $this->copyPart(self::DAY_OF_YEAR, $locale);
03943 }
03944
03945
03957 public function setDayOfYear($day, $locale = null)
03958 {
03959 return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
03960 }
03961
03962
03974 public function addDayOfYear($day, $locale = null)
03975 {
03976 return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
03977 }
03978
03979
03991 public function subDayOfYear($day, $locale = null)
03992 {
03993 return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
03994 }
03995
03996
04007 public function compareDayOfYear($day, $locale = null)
04008 {
04009 return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
04010 }
04011
04012
04020 public function getHour($locale = null)
04021 {
04022 return $this->copyPart(self::HOUR, $locale);
04023 }
04024
04025
04037 public function setHour($hour, $locale = null)
04038 {
04039 return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
04040 }
04041
04042
04054 public function addHour($hour, $locale = null)
04055 {
04056 return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
04057 }
04058
04059
04071 public function subHour($hour, $locale = null)
04072 {
04073 return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
04074 }
04075
04076
04087 public function compareHour($hour, $locale = null)
04088 {
04089 return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
04090 }
04091
04092
04100 public function getMinute($locale = null)
04101 {
04102 return $this->copyPart(self::MINUTE, $locale);
04103 }
04104
04105
04117 public function setMinute($minute, $locale = null)
04118 {
04119 return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
04120 }
04121
04122
04134 public function addMinute($minute, $locale = null)
04135 {
04136 return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
04137 }
04138
04139
04151 public function subMinute($minute, $locale = null)
04152 {
04153 return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
04154 }
04155
04156
04167 public function compareMinute($minute, $locale = null)
04168 {
04169 return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
04170 }
04171
04172
04180 public function getSecond($locale = null)
04181 {
04182 return $this->copyPart(self::SECOND, $locale);
04183 }
04184
04185
04197 public function setSecond($second, $locale = null)
04198 {
04199 return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
04200 }
04201
04202
04214 public function addSecond($second, $locale = null)
04215 {
04216 return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
04217 }
04218
04219
04231 public function subSecond($second, $locale = null)
04232 {
04233 return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
04234 }
04235
04236
04247 public function compareSecond($second, $locale = null)
04248 {
04249 return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
04250 }
04251
04252
04258 public function getFractionalPrecision()
04259 {
04260 return $this->_precision;
04261 }
04262
04263
04271 public function setFractionalPrecision($precision)
04272 {
04273 if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
04274 require_once 'Zend/Date/Exception.php';
04275 throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
04276 }
04277 $this->_precision = (int) $precision;
04278 }
04279
04280
04286 public function getMilliSecond()
04287 {
04288 return $this->_fractional;
04289 }
04290
04291
04300 public function setMilliSecond($milli = null, $precision = null)
04301 {
04302 if ($milli === null) {
04303 list($milli, $time) = explode(" ", microtime());
04304 $milli = intval($milli);
04305 $precision = 6;
04306 } else if (!is_numeric($milli)) {
04307 require_once 'Zend/Date/Exception.php';
04308 throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
04309 }
04310
04311 if ($precision === null) {
04312 $precision = $this->_precision;
04313 } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
04314 require_once 'Zend/Date/Exception.php';
04315 throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
04316 }
04317
04318 $this->_fractional = 0;
04319 $this->addMilliSecond($milli, $precision);
04320 return $this->_fractional;
04321 }
04322
04323
04331 public function addMilliSecond($milli = null, $precision = null)
04332 {
04333 if ($milli === null) {
04334 list($milli, $time) = explode(" ", microtime());
04335 $milli = intval($milli);
04336 } else if (!is_numeric($milli)) {
04337 require_once 'Zend/Date/Exception.php';
04338 throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
04339 }
04340
04341 if ($precision === null) {
04342 $precision = $this->_precision;
04343 } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
04344 require_once 'Zend/Date/Exception.php';
04345 throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
04346 }
04347
04348 if ($precision != $this->_precision) {
04349 if ($precision > $this->_precision) {
04350 $diff = $precision - $this->_precision;
04351 $milli = (int) ($milli / (10 * $diff));
04352 } else {
04353 $diff = $this->_precision - $precision;
04354 $milli = (int) ($milli * (10 * $diff));
04355 }
04356 }
04357
04358 $this->_fractional += $milli;
04359
04360
04361 $max = pow(10, $this->_precision);
04362
04363 if ($this->_fractional >= $max) {
04364 while ($this->_fractional >= $max) {
04365 $this->addSecond(1);
04366 $this->_fractional -= $max;
04367 }
04368 }
04369
04370 if ($this->_fractional < 0) {
04371 while ($this->_fractional < 0) {
04372 $this->subSecond(1);
04373 $this->_fractional += $max;
04374 }
04375 }
04376
04377 return $this->_fractional;
04378 }
04379
04380
04388 public function subMilliSecond($milli = null, $precision = null)
04389 {
04390 return $this->addMilliSecond(0 - $milli, $precision);
04391 }
04392
04400 public function compareMilliSecond($milli = null, $precision = null)
04401 {
04402 if ($milli === null) {
04403 list($milli, $time) = explode(" ", microtime());
04404 $milli = intval($milli);
04405 } else if (is_numeric($milli) === false) {
04406 require_once 'Zend/Date/Exception.php';
04407 throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
04408 }
04409
04410 if ($precision === null) {
04411 $precision = $this->_precision;
04412 } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
04413 require_once 'Zend/Date/Exception.php';
04414 throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
04415 }
04416
04417 if ($precision === 0) {
04418 require_once 'Zend/Date/Exception.php';
04419 throw new Zend_Date_Exception('precision is 0');
04420 }
04421
04422 if ($precision != $this->_precision) {
04423 if ($precision > $this->_precision) {
04424 $diff = $precision - $this->_precision;
04425 $milli = (int) ($milli / (10 * $diff));
04426 } else {
04427 $diff = $this->_precision - $precision;
04428 $milli = (int) ($milli * (10 * $diff));
04429 }
04430 }
04431
04432 $comp = $this->_fractional - $milli;
04433 if ($comp < 0) {
04434 return -1;
04435 } else if ($comp > 0) {
04436 return 1;
04437 }
04438 return 0;
04439 }
04440
04448 public function getWeek($locale = null)
04449 {
04450 return $this->copyPart(self::WEEK, $locale);
04451 }
04452
04463 public function setWeek($week, $locale = null)
04464 {
04465 return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
04466 }
04467
04478 public function addWeek($week, $locale = null)
04479 {
04480 return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
04481 }
04482
04493 public function subWeek($week, $locale = null)
04494 {
04495 return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
04496 }
04497
04508 public function compareWeek($week, $locale = null)
04509 {
04510 return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
04511 }
04512
04524 public function setLocale($locale = null)
04525 {
04526 try {
04527 $this->_locale = Zend_Locale::findLocale($locale);
04528 } catch (Zend_Locale_Exception $e) {
04529 require_once 'Zend/Date/Exception.php';
04530 throw new Zend_Date_Exception($e->getMessage());
04531 }
04532
04533 return $this;
04534 }
04535
04541 public function getLocale()
04542 {
04543 return $this->_locale;
04544 }
04545
04558 public static function isDate($date, $format = null, $locale = null)
04559 {
04560 if (!is_string($date) && !is_numeric($date) && !($date instanceof Zend_Date) &&
04561 !is_array($date)) {
04562 return false;
04563 }
04564
04565 if (($format !== null) and (Zend_Locale::isLocale($format, null, false))) {
04566 $locale = $format;
04567 $format = null;
04568 }
04569
04570 $locale = Zend_Locale::findLocale($locale);
04571
04572 if ($format === null) {
04573 $format = Zend_Locale_Format::getDateFormat($locale);
04574 } else if (self::$_options['format_type'] == 'php') {
04575 $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
04576 }
04577
04578 $format = self::_getLocalizedToken($format, $locale);
04579 if (!is_array($date)) {
04580 try {
04581 $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
04582 'date_format' => $format, 'format_type' => 'iso',
04583 'fix_date' => false));
04584 } catch (Zend_Locale_Exception $e) {
04585
04586 return false;
04587 }
04588 } else {
04589 $parsed = $date;
04590 }
04591
04592 if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
04593 (!isset($parsed['year']))) {
04594
04595 return false;
04596 }
04597
04598 if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
04599
04600 return false;
04601 }
04602
04603 if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
04604
04605 return false;
04606 }
04607
04608 if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
04609 (!isset($parsed['hour']))) {
04610
04611 return false;
04612 }
04613
04614 if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
04615
04616 return false;
04617 }
04618
04619 if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
04620
04621 return false;
04622 }
04623
04624
04625 if (isset($parsed['hour']) === false) {
04626 $parsed['hour'] = 12;
04627 }
04628
04629 if (isset($parsed['minute']) === false) {
04630 $parsed['minute'] = 0;
04631 }
04632
04633 if (isset($parsed['second']) === false) {
04634 $parsed['second'] = 0;
04635 }
04636
04637 if (isset($parsed['month']) === false) {
04638 $parsed['month'] = 1;
04639 }
04640
04641 if (isset($parsed['day']) === false) {
04642 $parsed['day'] = 1;
04643 }
04644
04645 if (isset($parsed['year']) === false) {
04646 $parsed['year'] = 1970;
04647 }
04648
04649 if (self::isYearLeapYear($parsed['year'])) {
04650 $parsed['year'] = 1972;
04651 } else {
04652 $parsed['year'] = 1971;
04653 }
04654
04655 $date = new self($parsed, null, $locale);
04656 $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
04657 $parsed['month'], $parsed['day'], $parsed['year']);
04658
04659 if ($parsed['year'] != $date->date('Y', $timestamp)) {
04660
04661 return false;
04662 }
04663
04664 if ($parsed['month'] != $date->date('n', $timestamp)) {
04665
04666 return false;
04667 }
04668
04669 if ($parsed['day'] != $date->date('j', $timestamp)) {
04670
04671 return false;
04672 }
04673
04674 if ($parsed['hour'] != $date->date('G', $timestamp)) {
04675
04676 return false;
04677 }
04678
04679 if ($parsed['minute'] != $date->date('i', $timestamp)) {
04680
04681 return false;
04682 }
04683
04684 if ($parsed['second'] != $date->date('s', $timestamp)) {
04685
04686 return false;
04687 }
04688
04689 return true;
04690 }
04691
04699 protected static function _getLocalizedToken($token, $locale)
04700 {
04701 switch($token) {
04702 case self::ISO_8601 :
04703 return "dd mm yy";
04704 break;
04705 case self::RFC_2822 :
04706 return "EEE, dd MMM yyyy HH:mm:ss";
04707 break;
04708 case self::DATES :
04709 return Zend_Locale_Data::getContent($locale, 'date');
04710 break;
04711 case self::DATE_FULL :
04712 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
04713 break;
04714 case self::DATE_LONG :
04715 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
04716 break;
04717 case self::DATE_MEDIUM :
04718 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
04719 break;
04720 case self::DATE_SHORT :
04721 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
04722 break;
04723 case self::TIMES :
04724 return Zend_Locale_Data::getContent($locale, 'date');
04725 break;
04726 case self::TIME_FULL :
04727 return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
04728 break;
04729 case self::TIME_LONG :
04730 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
04731 break;
04732 case self::TIME_MEDIUM :
04733 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
04734 break;
04735 case self::TIME_SHORT :
04736 return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
04737 break;
04738 case self::DATETIME :
04739 return Zend_Locale_Data::getContent($locale, 'datetime');
04740 break;
04741 case self::DATETIME_FULL :
04742 return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
04743 break;
04744 case self::DATETIME_LONG :
04745 return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
04746 break;
04747 case self::DATETIME_MEDIUM :
04748 return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
04749 break;
04750 case self::DATETIME_SHORT :
04751 return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
04752 break;
04753 case self::ATOM :
04754 case self::RFC_3339 :
04755 case self::W3C :
04756 return "yyyy-MM-DD HH:mm:ss";
04757 break;
04758 case self::COOKIE :
04759 case self::RFC_850 :
04760 return "EEEE, dd-MM-yyyy HH:mm:ss";
04761 break;
04762 case self::RFC_822 :
04763 case self::RFC_1036 :
04764 case self::RFC_1123 :
04765 case self::RSS :
04766 return "EEE, dd MM yyyy HH:mm:ss";
04767 break;
04768 }
04769
04770 return $token;
04771 }
04772 }