Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Date.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Date/DateObject.php';
26 #require_once 'Zend/Locale.php';
27 #require_once 'Zend/Locale/Format.php';
28 #require_once 'Zend/Locale/Math.php';
29 
36 class Zend_Date extends Zend_Date_DateObject
37 {
38  private $_locale = null;
39 
40  // Fractional second variables
41  private $_fractional = 0;
42  private $_precision = 3;
43 
44  private static $_options = array(
45  'format_type' => 'iso', // format for date strings 'iso' or 'php'
46  'fix_dst' => true, // fix dst on summer/winter time change
47  'extend_month' => false, // false - addMonth like SQL, true like excel
48  'cache' => null, // cache to set
49  'timesync' => null // timesync server to set
50  );
51 
52  // Class wide Date Constants
53  const DAY = 'dd';
54  const DAY_SHORT = 'd';
55  const DAY_SUFFIX = 'SS';
56  const DAY_OF_YEAR = 'D';
57  const WEEKDAY = 'EEEE';
58  const WEEKDAY_SHORT = 'EEE';
59  const WEEKDAY_NARROW = 'E';
60  const WEEKDAY_NAME = 'EE';
61  const WEEKDAY_8601 = 'eee';
62  const WEEKDAY_DIGIT = 'e';
63  const WEEK = 'ww';
64  const MONTH = 'MM';
65  const MONTH_SHORT = 'M';
66  const MONTH_DAYS = 'ddd';
67  const MONTH_NAME = 'MMMM';
68  const MONTH_NAME_SHORT = 'MMM';
69  const MONTH_NAME_NARROW = 'MMMMM';
70  const YEAR = 'y';
71  const YEAR_SHORT = 'yy';
72  const YEAR_8601 = 'Y';
73  const YEAR_SHORT_8601 = 'YY';
74  const LEAPYEAR = 'l';
75  const MERIDIEM = 'a';
76  const SWATCH = 'B';
77  const HOUR = 'HH';
78  const HOUR_SHORT = 'H';
79  const HOUR_AM = 'hh';
80  const HOUR_SHORT_AM = 'h';
81  const MINUTE = 'mm';
82  const MINUTE_SHORT = 'm';
83  const SECOND = 'ss';
84  const SECOND_SHORT = 's';
85  const MILLISECOND = 'S';
86  const TIMEZONE_NAME = 'zzzz';
87  const DAYLIGHT = 'I';
88  const GMT_DIFF = 'Z';
89  const GMT_DIFF_SEP = 'ZZZZ';
90  const TIMEZONE = 'z';
91  const TIMEZONE_SECS = 'X';
92  const ISO_8601 = 'c';
93  const RFC_2822 = 'r';
94  const TIMESTAMP = 'U';
95  const ERA = 'G';
96  const ERA_NAME = 'GGGG';
97  const ERA_NARROW = 'GGGGG';
98  const DATES = 'F';
99  const DATE_FULL = 'FFFFF';
100  const DATE_LONG = 'FFFF';
101  const DATE_MEDIUM = 'FFF';
102  const DATE_SHORT = 'FF';
103  const TIMES = 'WW';
104  const TIME_FULL = 'TTTTT';
105  const TIME_LONG = 'TTTT';
106  const TIME_MEDIUM = 'TTT';
107  const TIME_SHORT = 'TT';
108  const DATETIME = 'K';
109  const DATETIME_FULL = 'KKKKK';
110  const DATETIME_LONG = 'KKKK';
111  const DATETIME_MEDIUM = 'KKK';
112  const DATETIME_SHORT = 'KK';
113  const ATOM = 'OOO';
114  const COOKIE = 'CCC';
115  const RFC_822 = 'R';
116  const RFC_850 = 'RR';
117  const RFC_1036 = 'RRR';
118  const RFC_1123 = 'RRRR';
119  const RFC_3339 = 'RRRRR';
120  const RSS = 'SSS';
121  const W3C = 'WWW';
122 
126  const YEAR_MIN_VALUE = -10000;
127 
131  const YEAR_MAX_VALUE = 10000;
132 
147  public function __construct($date = null, $part = null, $locale = null)
148  {
149  if (is_object($date) and !($date instanceof Zend_TimeSync_Protocol) and
150  !($date instanceof Zend_Date)) {
151  if ($locale instanceof Zend_Locale) {
152  $locale = $date;
153  $date = null;
154  $part = null;
155  } else {
156  $date = (string) $date;
157  }
158  }
159 
160  if (($date !== null) and !is_array($date) and !($date instanceof Zend_TimeSync_Protocol) and
161  !($date instanceof Zend_Date) and !defined($date) and Zend_Locale::isLocale($date, true, false)) {
162  $locale = $date;
163  $date = null;
164  $part = null;
165  } else if (($part !== null) and !defined($part) and Zend_Locale::isLocale($part, true, false)) {
166  $locale = $part;
167  $part = null;
168  }
169 
170  $this->setLocale($locale);
171  if (is_string($date) && ($part === null) && (strlen($date) <= 5)) {
172  $part = $date;
173  $date = null;
174  }
175 
176  if ($date === null) {
177  if ($part === null) {
178  $date = time();
179  } else if ($part !== self::TIMESTAMP) {
180  $date = self::now($locale);
181  $date = $date->get($part);
182  }
183  }
184 
185  if ($date instanceof Zend_TimeSync_Protocol) {
186  $date = $date->getInfo();
187  $date = $this->_getTime($date['offset']);
188  $part = null;
189  } else if (parent::$_defaultOffset != 0) {
190  $date = $this->_getTime(parent::$_defaultOffset);
191  }
192 
193  // set the timezone and offset for $this
194  $zone = @date_default_timezone_get();
195  $this->setTimezone($zone);
196 
197  // try to get timezone from date-string
198  if (!is_int($date)) {
199  $zone = $this->getTimezoneFromString($date);
200  $this->setTimezone($zone);
201  }
202 
203  // set datepart
204  if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
205  // switch off dst handling for value setting
206  $this->setUnixTimestamp($this->getGmtOffset());
207  $this->set($date, $part, $this->_locale);
208 
209  // DST fix
210  if (is_array($date) === true) {
211  if (!isset($date['hour'])) {
212  $date['hour'] = 0;
213  }
214 
215  $hour = $this->toString('H', 'iso', true);
216  $hour = $date['hour'] - $hour;
217  switch ($hour) {
218  case 1 :
219  case -23 :
220  $this->addTimestamp(3600);
221  break;
222  case -1 :
223  case 23 :
224  $this->subTimestamp(3600);
225  break;
226  case 2 :
227  case -22 :
228  $this->addTimestamp(7200);
229  break;
230  case -2 :
231  case 22 :
232  $this->subTimestamp(7200);
233  break;
234  }
235  }
236  } else {
237  $this->setUnixTimestamp($date);
238  }
239  }
240 
248  public static function setOptions(array $options = array())
249  {
250  if (empty($options)) {
251  return self::$_options;
252  }
253 
254  foreach ($options as $name => $value) {
255  $name = strtolower($name);
256 
257  if (array_key_exists($name, self::$_options)) {
258  switch($name) {
259  case 'format_type' :
260  if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
261  #require_once 'Zend/Date/Exception.php';
262  throw new Zend_Date_Exception("Unknown format type ($value) for dates, only 'iso' and 'php' supported", 0, null, $value);
263  }
264  break;
265  case 'fix_dst' :
266  if (!is_bool($value)) {
267  #require_once 'Zend/Date/Exception.php';
268  throw new Zend_Date_Exception("'fix_dst' has to be boolean", 0, null, $value);
269  }
270  break;
271  case 'extend_month' :
272  if (!is_bool($value)) {
273  #require_once 'Zend/Date/Exception.php';
274  throw new Zend_Date_Exception("'extend_month' has to be boolean", 0, null, $value);
275  }
276  break;
277  case 'cache' :
278  if ($value === null) {
279  parent::$_cache = null;
280  } else {
281  if (!$value instanceof Zend_Cache_Core) {
282  #require_once 'Zend/Date/Exception.php';
283  throw new Zend_Date_Exception("Instance of Zend_Cache expected");
284  }
285 
286  parent::$_cache = $value;
287  parent::$_cacheTags = Zend_Date_DateObject::_getTagSupportForCache();
289  }
290  break;
291  case 'timesync' :
292  if ($value === null) {
293  parent::$_defaultOffset = 0;
294  } else {
295  if (!$value instanceof Zend_TimeSync_Protocol) {
296  #require_once 'Zend/Date/Exception.php';
297  throw new Zend_Date_Exception("Instance of Zend_TimeSync expected");
298  }
299 
300  $date = $value->getInfo();
301  parent::$_defaultOffset = $date['offset'];
302  }
303  break;
304  }
305  self::$_options[$name] = $value;
306  }
307  else {
308  #require_once 'Zend/Date/Exception.php';
309  throw new Zend_Date_Exception("Unknown option: $name = $value");
310  }
311  }
312  }
313 
322  public function getTimestamp()
323  {
324  return $this->getUnixTimestamp();
325  }
326 
336  private function _timestamp($calc, $stamp)
337  {
338  if ($stamp instanceof Zend_Date) {
339  // extract timestamp from object
340  $stamp = $stamp->getTimestamp();
341  }
342 
343  if (is_array($stamp)) {
344  if (isset($stamp['timestamp']) === true) {
345  $stamp = $stamp['timestamp'];
346  } else {
347  #require_once 'Zend/Date/Exception.php';
348  throw new Zend_Date_Exception('no timestamp given in array');
349  }
350  }
351 
352  if ($calc === 'set') {
353  $return = $this->setUnixTimestamp($stamp);
354  } else {
355  $return = $this->_calcdetail($calc, $stamp, self::TIMESTAMP, null);
356  }
357  if ($calc != 'cmp') {
358  return $this;
359  }
360  return $return;
361  }
362 
370  public function setTimestamp($timestamp)
371  {
372  return $this->_timestamp('set', $timestamp);
373  }
374 
382  public function addTimestamp($timestamp)
383  {
384  return $this->_timestamp('add', $timestamp);
385  }
386 
394  public function subTimestamp($timestamp)
395  {
396  return $this->_timestamp('sub', $timestamp);
397  }
398 
406  public function compareTimestamp($timestamp)
407  {
408  return $this->_timestamp('cmp', $timestamp);
409  }
410 
433  public function toString($format = null, $type = null, $locale = null)
434  {
435  if (is_object($format)) {
436  if ($format instanceof Zend_Locale) {
437  $locale = $format;
438  $format = null;
439  } else {
440  $format = (string) $format;
441  }
442  }
443 
444  if (is_object($type)) {
445  if ($type instanceof Zend_Locale) {
446  $locale = $type;
447  $type = null;
448  } else {
449  $type = (string) $type;
450  }
451  }
452 
453  if (($format !== null) && !defined($format)
454  && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
455  && Zend_Locale::isLocale($format, null, false)) {
456  $locale = $format;
457  $format = null;
458  }
459 
460  if (($type !== null) and ($type != 'php') and ($type != 'iso') and
461  Zend_Locale::isLocale($type, null, false)) {
462  $locale = $type;
463  $type = null;
464  }
465 
466  if ($locale === null) {
467  $locale = $this->getLocale();
468  }
469 
470  if ($format === null) {
472  } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
474  }
475 
476  return $this->date($this->_toToken($format, $locale), $this->getUnixTimestamp(), false);
477  }
478 
484  public function __toString()
485  {
486  return $this->toString(null, $this->_locale);
487  }
488 
496  public function toValue($part = null)
497  {
498  $result = $this->get($part);
499  if (is_numeric($result)) {
500  return intval("$result");
501  } else {
502  return false;
503  }
504  }
505 
511  public function toArray()
512  {
513  return array('day' => $this->toString(self::DAY_SHORT, 'iso'),
514  'month' => $this->toString(self::MONTH_SHORT, 'iso'),
515  'year' => $this->toString(self::YEAR, 'iso'),
516  'hour' => $this->toString(self::HOUR_SHORT, 'iso'),
517  'minute' => $this->toString(self::MINUTE_SHORT, 'iso'),
518  'second' => $this->toString(self::SECOND_SHORT, 'iso'),
519  'timezone' => $this->toString(self::TIMEZONE, 'iso'),
520  'timestamp' => $this->toString(self::TIMESTAMP, 'iso'),
521  'weekday' => $this->toString(self::WEEKDAY_8601, 'iso'),
522  'dayofyear' => $this->toString(self::DAY_OF_YEAR, 'iso'),
523  'week' => $this->toString(self::WEEK, 'iso'),
524  'gmtsecs' => $this->toString(self::TIMEZONE_SECS, 'iso'));
525  }
526 
537  public function get($part = null, $locale = null)
538  {
539  if ($locale === null) {
540  $locale = $this->getLocale();
541  }
542 
543  if (($part !== null) && !defined($part)
544  && ($part != 'ee') && ($part != 'ss') && ($part != 'GG') && ($part != 'MM') && ($part != 'EE') && ($part != 'TT')
545  && Zend_Locale::isLocale($part, null, false)) {
546  $locale = $part;
547  $part = null;
548  }
549 
550  if ($part === null) {
551  $part = self::TIMESTAMP;
552  } else if (self::$_options['format_type'] == 'php') {
554  }
555 
556  return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);
557  }
558 
566  private function _toToken($part, $locale) {
567  // get format tokens
568  $comment = false;
569  $format = '';
570  $orig = '';
571  for ($i = 0; isset($part[$i]); ++$i) {
572  if ($part[$i] == "'") {
573  $comment = $comment ? false : true;
574  if (isset($part[$i+1]) && ($part[$i+1] == "'")) {
575  $comment = $comment ? false : true;
576  $format .= "\\'";
577  ++$i;
578  }
579 
580  $orig = '';
581  continue;
582  }
583 
584  if ($comment) {
585  $format .= '\\' . $part[$i];
586  $orig = '';
587  } else {
588  $orig .= $part[$i];
589  if (!isset($part[$i+1]) || (isset($orig[0]) && ($orig[0] != $part[$i+1]))) {
590  $format .= $this->_parseIsoToDate($orig, $locale);
591  $orig = '';
592  }
593  }
594  }
595 
596  return $format;
597  }
598 
606  private function _parseIsoToDate($token, $locale) {
607  switch($token) {
608  case self::DAY :
609  return 'd';
610  break;
611 
612  case self::WEEKDAY_SHORT :
613  $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
614  $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
615  return $this->_toComment(iconv_substr($day, 0, 3, 'UTF-8'));
616  break;
617 
618  case self::DAY_SHORT :
619  return 'j';
620  break;
621 
622  case self::WEEKDAY :
623  $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
624  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)));
625  break;
626 
627  case self::WEEKDAY_8601 :
628  return 'N';
629  break;
630 
631  case 'ee' :
632  return $this->_toComment(str_pad($this->date('N', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
633  break;
634 
635  case self::DAY_SUFFIX :
636  return 'S';
637  break;
638 
639  case self::WEEKDAY_DIGIT :
640  return 'w';
641  break;
642 
643  case self::DAY_OF_YEAR :
644  return 'z';
645  break;
646 
647  case 'DDD' :
648  return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 3, '0', STR_PAD_LEFT));
649  break;
650 
651  case 'DD' :
652  return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
653  break;
654 
655  case self::WEEKDAY_NARROW :
656  case 'EEEEE' :
657  $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
658  $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
659  return $this->_toComment(iconv_substr($day, 0, 1, 'UTF-8'));
660  break;
661 
662  case self::WEEKDAY_NAME :
663  $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
664  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)));
665  break;
666 
667  case 'w' :
668  $week = $this->date('W', $this->getUnixTimestamp(), false);
669  return $this->_toComment(($week[0] == '0') ? $week[1] : $week);
670  break;
671 
672  case self::WEEK :
673  return 'W';
674  break;
675 
676  case self::MONTH_NAME :
677  $month = $this->date('n', $this->getUnixTimestamp(), false);
678  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)));
679  break;
680 
681  case self::MONTH :
682  return 'm';
683  break;
684 
685  case self::MONTH_NAME_SHORT :
686  $month = $this->date('n', $this->getUnixTimestamp(), false);
687  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)));
688  break;
689 
690  case self::MONTH_SHORT :
691  return 'n';
692  break;
693 
694  case self::MONTH_DAYS :
695  return 't';
696  break;
697 
698  case self::MONTH_NAME_NARROW :
699  $month = $this->date('n', $this->getUnixTimestamp(), false);
700  $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
701  return $this->_toComment(iconv_substr($mon, 0, 1, 'UTF-8'));
702  break;
703 
704  case self::LEAPYEAR :
705  return 'L';
706  break;
707 
708  case self::YEAR_8601 :
709  return 'o';
710  break;
711 
712  case self::YEAR :
713  return 'Y';
714  break;
715 
716  case self::YEAR_SHORT :
717  return 'y';
718  break;
719 
720  case self::YEAR_SHORT_8601 :
721  return $this->_toComment(substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2));
722  break;
723 
724  case self::MERIDIEM :
725  $am = $this->date('a', $this->getUnixTimestamp(), false);
726  if ($am == 'am') {
727  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'am'));
728  }
729 
730  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'pm'));
731  break;
732 
733  case self::SWATCH :
734  return 'B';
735  break;
736 
737  case self::HOUR_SHORT_AM :
738  return 'g';
739  break;
740 
741  case self::HOUR_SHORT :
742  return 'G';
743  break;
744 
745  case self::HOUR_AM :
746  return 'h';
747  break;
748 
749  case self::HOUR :
750  return 'H';
751  break;
752 
753  case self::MINUTE :
754  return $this->_toComment(str_pad($this->date('i', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
755  break;
756 
757  case self::SECOND :
758  return $this->_toComment(str_pad($this->date('s', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
759  break;
760 
761  case self::MINUTE_SHORT :
762  return 'i';
763  break;
764 
765  case self::SECOND_SHORT :
766  return 's';
767  break;
768 
769  case self::MILLISECOND :
770  return $this->_toComment($this->getMilliSecond());
771  break;
772 
773  case self::TIMEZONE_NAME :
774  case 'vvvv' :
775  return 'e';
776  break;
777 
778  case self::DAYLIGHT :
779  return 'I';
780  break;
781 
782  case self::GMT_DIFF :
783  case 'ZZ' :
784  case 'ZZZ' :
785  return 'O';
786  break;
787 
788  case self::GMT_DIFF_SEP :
789  return 'P';
790  break;
791 
792  case self::TIMEZONE :
793  case 'v' :
794  case 'zz' :
795  case 'zzz' :
796  return 'T';
797  break;
798 
799  case self::TIMEZONE_SECS :
800  return 'Z';
801  break;
802 
803  case self::ISO_8601 :
804  return 'c';
805  break;
806 
807  case self::RFC_2822 :
808  return 'r';
809  break;
810 
811  case self::TIMESTAMP :
812  return 'U';
813  break;
814 
815  case self::ERA :
816  case 'GG' :
817  case 'GGG' :
818  $year = $this->date('Y', $this->getUnixTimestamp(), false);
819  if ($year < 0) {
820  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')));
821  }
822 
823  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')));
824  break;
825 
826  case self::ERA_NARROW :
827  $year = $this->date('Y', $this->getUnixTimestamp(), false);
828  if ($year < 0) {
829  return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')), 0, 1, 'UTF-8')) . '.';
830  }
831 
832  return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')), 0, 1, 'UTF-8')) . '.';
833  break;
834 
835  case self::ERA_NAME :
836  $year = $this->date('Y', $this->getUnixTimestamp(), false);
837  if ($year < 0) {
838  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0')));
839  }
840 
841  return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1')));
842  break;
843 
844  case self::DATES :
845  return $this->_toToken(Zend_Locale_Format::getDateFormat($locale), $locale);
846  break;
847 
848  case self::DATE_FULL :
849  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full')), $locale);
850  break;
851 
852  case self::DATE_LONG :
853  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long')), $locale);
854  break;
855 
856  case self::DATE_MEDIUM :
857  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium')), $locale);
858  break;
859 
860  case self::DATE_SHORT :
861  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short')), $locale);
862  break;
863 
864  case self::TIMES :
865  return $this->_toToken(Zend_Locale_Format::getTimeFormat($locale), $locale);
866  break;
867 
868  case self::TIME_FULL :
869  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'full'), $locale);
870  break;
871 
872  case self::TIME_LONG :
873  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'long'), $locale);
874  break;
875 
876  case self::TIME_MEDIUM :
877  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'medium'), $locale);
878  break;
879 
880  case self::TIME_SHORT :
881  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'short'), $locale);
882  break;
883 
884  case self::DATETIME :
885  return $this->_toToken(Zend_Locale_Format::getDateTimeFormat($locale), $locale);
886  break;
887 
888  case self::DATETIME_FULL :
889  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full')), $locale);
890  break;
891 
892  case self::DATETIME_LONG :
893  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long')), $locale);
894  break;
895 
896  case self::DATETIME_MEDIUM :
897  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium')), $locale);
898  break;
899 
900  case self::DATETIME_SHORT :
901  return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short')), $locale);
902  break;
903 
904  case self::ATOM :
905  return 'Y\-m\-d\TH\:i\:sP';
906  break;
907 
908  case self::COOKIE :
909  return 'l\, d\-M\-y H\:i\:s e';
910  break;
911 
912  case self::RFC_822 :
913  return 'D\, d M y H\:i\:s O';
914  break;
915 
916  case self::RFC_850 :
917  return 'l\, d\-M\-y H\:i\:s e';
918  break;
919 
920  case self::RFC_1036 :
921  return 'D\, d M y H\:i\:s O';
922  break;
923 
924  case self::RFC_1123 :
925  return 'D\, d M Y H\:i\:s O';
926  break;
927 
928  case self::RFC_3339 :
929  return 'Y\-m\-d\TH\:i\:sP';
930  break;
931 
932  case self::RSS :
933  return 'D\, d M Y H\:i\:s O';
934  break;
935 
936  case self::W3C :
937  return 'Y\-m\-d\TH\:i\:sP';
938  break;
939  }
940 
941  if ($token == '') {
942  return '';
943  }
944 
945  switch ($token[0]) {
946  case 'y' :
947  if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
948  return 'Y';
949  }
950 
951  $length = iconv_strlen($token, 'UTF-8');
952  return $this->_toComment(str_pad($this->date('Y', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
953  break;
954 
955  case 'Y' :
956  if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
957  return 'o';
958  }
959 
960  $length = iconv_strlen($token, 'UTF-8');
961  return $this->_toComment(str_pad($this->date('o', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
962  break;
963 
964  case 'A' :
965  $length = iconv_strlen($token, 'UTF-8');
966  $result = substr($this->getMilliSecond(), 0, 3);
967  $result += $this->date('s', $this->getUnixTimestamp(), false) * 1000;
968  $result += $this->date('i', $this->getUnixTimestamp(), false) * 60000;
969  $result += $this->date('H', $this->getUnixTimestamp(), false) * 3600000;
970 
971  return $this->_toComment(str_pad($result, $length, '0', STR_PAD_LEFT));
972  break;
973  }
974 
975  return $this->_toComment($token);
976  }
977 
984  private function _toComment($token)
985  {
986  $token = str_split($token);
987  $result = '';
988  foreach ($token as $tok) {
989  $result .= '\\' . $tok;
990  }
991 
992  return $result;
993  }
994 
1003  private function _getDigitFromName($name)
1004  {
1005  switch($name) {
1006  case "Jan":
1007  return 1;
1008 
1009  case "Feb":
1010  return 2;
1011 
1012  case "Mar":
1013  return 3;
1014 
1015  case "Apr":
1016  return 4;
1017 
1018  case "May":
1019  return 5;
1020 
1021  case "Jun":
1022  return 6;
1023 
1024  case "Jul":
1025  return 7;
1026 
1027  case "Aug":
1028  return 8;
1029 
1030  case "Sep":
1031  return 9;
1032 
1033  case "Oct":
1034  return 10;
1035 
1036  case "Nov":
1037  return 11;
1038 
1039  case "Dec":
1040  return 12;
1041 
1042  default:
1043  #require_once 'Zend/Date/Exception.php';
1044  throw new Zend_Date_Exception('Month ($name) is not a known month');
1045  }
1046  }
1047 
1055  public static function getFullYear($value)
1056  {
1057  if ($value >= 0) {
1058  if ($value < 70) {
1059  $value += 2000;
1060  } else if ($value < 100) {
1061  $value += 1900;
1062  }
1063  }
1064  return $value;
1065  }
1066 
1079  public function set($date, $part = null, $locale = null)
1080  {
1081  if (self::$_options['format_type'] == 'php') {
1083  }
1084 
1085  $zone = $this->getTimezoneFromString($date);
1086  $this->setTimezone($zone);
1087 
1088  $this->_calculate('set', $date, $part, $locale);
1089  return $this;
1090  }
1091 
1107  public function add($date, $part = self::TIMESTAMP, $locale = null)
1108  {
1109  if (self::$_options['format_type'] == 'php') {
1111  }
1112 
1113  $this->_calculate('add', $date, $part, $locale);
1114  return $this;
1115  }
1116 
1130  public function sub($date, $part = self::TIMESTAMP, $locale = null)
1131  {
1132  if (self::$_options['format_type'] == 'php') {
1134  }
1135 
1136  $this->_calculate('sub', $date, $part, $locale);
1137  return $this;
1138  }
1139 
1150  public function compare($date, $part = self::TIMESTAMP, $locale = null)
1151  {
1152  if (self::$_options['format_type'] == 'php') {
1154  }
1155 
1156  $compare = $this->_calculate('cmp', $date, $part, $locale);
1157 
1158  if ($compare > 0) {
1159  return 1;
1160  } else if ($compare < 0) {
1161  return -1;
1162  }
1163  return 0;
1164  }
1165 
1180  public function copyPart($part, $locale = null)
1181  {
1182  $clone = clone $this; // copy all instance variables
1183  $clone->setUnixTimestamp(0); // except the timestamp
1184  if ($locale != null) {
1185  $clone->setLocale($locale); // set an other locale if selected
1186  }
1187  $clone->set($this, $part);
1188  return $clone;
1189  }
1190 
1197  public function getTimezoneFromString($zone)
1198  {
1199  if (is_array($zone)) {
1200  return $this->getTimezone();
1201  }
1202 
1203  if ($zone instanceof Zend_Date) {
1204  return $zone->getTimezone();
1205  }
1206 
1207  $match = array();
1208  preg_match('/\dZ$/', $zone, $match);
1209  if (!empty($match)) {
1210  return "Etc/UTC";
1211  }
1212 
1213  preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
1214  if (!empty($match) and ($match[count($match) - 1] <= 14) and ($match[count($match) - 1] >= -12)) {
1215  $zone = "Etc/GMT";
1216  $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
1217  $zone .= (int) abs($match[count($match) - 1]);
1218  return $zone;
1219  }
1220 
1221  preg_match('/([[:alpha:]\/_]{3,30})(?!.*([[:alpha:]\/]{3,30}))/', $zone, $match);
1222  try {
1223  if (!empty($match) and (!is_int($match[count($match) - 1]))) {
1224  $oldzone = $this->getTimezone();
1225  $this->setTimezone($match[count($match) - 1]);
1226  $result = $this->getTimezone();
1227  $this->setTimezone($oldzone);
1228  if ($result !== $oldzone) {
1229  return $match[count($match) - 1];
1230  }
1231  }
1232  } catch (Exception $e) {
1233  // fall through
1234  }
1235 
1236  return $this->getTimezone();
1237  }
1238 
1248  private function _assign($calc, $date, $comp = 0, $dst = false)
1249  {
1250  switch ($calc) {
1251  case 'set' :
1252  if (!empty($comp)) {
1254  }
1256  $value = $this->getUnixTimestamp();
1257  break;
1258  case 'add' :
1260  $value = $this->getUnixTimestamp();
1261  break;
1262  case 'sub' :
1264  $value = $this->getUnixTimestamp();
1265  break;
1266  default :
1267  // cmp - compare
1268  return call_user_func(Zend_Locale_Math::$comp, $comp, $date);
1269  break;
1270  }
1271 
1272  // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
1273  if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
1274  $hour = $this->toString(self::HOUR, 'iso');
1275  if ($hour != $dst) {
1276  if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
1277  $value += 3600;
1278  } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
1279  $value -= 3600;
1280  }
1281  $this->setUnixTimestamp($value);
1282  }
1283  }
1284  return $this->getUnixTimestamp();
1285  }
1286 
1287 
1298  private function _calculate($calc, $date, $part, $locale)
1299  {
1300  if ($date === null) {
1301  #require_once 'Zend/Date/Exception.php';
1302  throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
1303  }
1304 
1305  if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale::isLocale($part, null, false))) {
1306  $locale = $part;
1307  $part = null;
1308  }
1309 
1310  if ($locale === null) {
1311  $locale = $this->getLocale();
1312  }
1313 
1314  $locale = (string) $locale;
1315 
1316  // Create date parts
1317  $year = $this->toString(self::YEAR, 'iso');
1318  $month = $this->toString(self::MONTH_SHORT, 'iso');
1319  $day = $this->toString(self::DAY_SHORT, 'iso');
1320  $hour = $this->toString(self::HOUR_SHORT, 'iso');
1321  $minute = $this->toString(self::MINUTE_SHORT, 'iso');
1322  $second = $this->toString(self::SECOND_SHORT, 'iso');
1323  // If object extract value
1324  if ($date instanceof Zend_Date) {
1325  $date = $date->toString($part, 'iso', $locale);
1326  }
1327 
1328  if (is_array($date) === true) {
1329  if (empty($part) === false) {
1330  switch($part) {
1331  // Fall through
1332  case self::DAY:
1333  case self::DAY_SHORT:
1334  if (isset($date['day']) === true) {
1335  $date = $date['day'];
1336  }
1337  break;
1338  // Fall through
1339  case self::WEEKDAY_SHORT:
1340  case self::WEEKDAY:
1341  case self::WEEKDAY_8601:
1342  case self::WEEKDAY_DIGIT:
1343  case self::WEEKDAY_NARROW:
1344  case self::WEEKDAY_NAME:
1345  if (isset($date['weekday']) === true) {
1346  $date = $date['weekday'];
1347  $part = self::WEEKDAY_DIGIT;
1348  }
1349  break;
1350  case self::DAY_OF_YEAR:
1351  if (isset($date['day_of_year']) === true) {
1352  $date = $date['day_of_year'];
1353  }
1354  break;
1355  // Fall through
1356  case self::MONTH:
1357  case self::MONTH_SHORT:
1358  case self::MONTH_NAME:
1359  case self::MONTH_NAME_SHORT:
1360  case self::MONTH_NAME_NARROW:
1361  if (isset($date['month']) === true) {
1362  $date = $date['month'];
1363  }
1364  break;
1365  // Fall through
1366  case self::YEAR:
1367  case self::YEAR_SHORT:
1368  case self::YEAR_8601:
1369  case self::YEAR_SHORT_8601:
1370  if (isset($date['year']) === true) {
1371  $date = $date['year'];
1372  }
1373  break;
1374  // Fall through
1375  case self::HOUR:
1376  case self::HOUR_AM:
1377  case self::HOUR_SHORT:
1378  case self::HOUR_SHORT_AM:
1379  if (isset($date['hour']) === true) {
1380  $date = $date['hour'];
1381  }
1382  break;
1383  // Fall through
1384  case self::MINUTE:
1385  case self::MINUTE_SHORT:
1386  if (isset($date['minute']) === true) {
1387  $date = $date['minute'];
1388  }
1389  break;
1390  // Fall through
1391  case self::SECOND:
1392  case self::SECOND_SHORT:
1393  if (isset($date['second']) === true) {
1394  $date = $date['second'];
1395  }
1396  break;
1397  // Fall through
1398  case self::TIMEZONE:
1399  case self::TIMEZONE_NAME:
1400  if (isset($date['timezone']) === true) {
1401  $date = $date['timezone'];
1402  }
1403  break;
1404  case self::TIMESTAMP:
1405  if (isset($date['timestamp']) === true) {
1406  $date = $date['timestamp'];
1407  }
1408  break;
1409  case self::WEEK:
1410  if (isset($date['week']) === true) {
1411  $date = $date['week'];
1412  }
1413  break;
1414  case self::TIMEZONE_SECS:
1415  if (isset($date['gmtsecs']) === true) {
1416  $date = $date['gmtsecs'];
1417  }
1418  break;
1419  default:
1420  #require_once 'Zend/Date/Exception.php';
1421  throw new Zend_Date_Exception("datepart for part ($part) not found in array");
1422  break;
1423  }
1424  } else {
1425  $hours = 0;
1426  if (isset($date['hour']) === true) {
1427  $hours = $date['hour'];
1428  }
1429  $minutes = 0;
1430  if (isset($date['minute']) === true) {
1431  $minutes = $date['minute'];
1432  }
1433  $seconds = 0;
1434  if (isset($date['second']) === true) {
1435  $seconds = $date['second'];
1436  }
1437  $months = 0;
1438  if (isset($date['month']) === true) {
1439  $months = $date['month'];
1440  }
1441  $days = 0;
1442  if (isset($date['day']) === true) {
1443  $days = $date['day'];
1444  }
1445  $years = 0;
1446  if (isset($date['year']) === true) {
1447  $years = $date['year'];
1448  }
1449  return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true),
1450  $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour);
1451  }
1452  }
1453 
1454  // $date as object, part of foreign date as own date
1455  switch($part) {
1456 
1457  // day formats
1458  case self::DAY:
1459  if (is_numeric($date)) {
1460  return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
1461  $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
1462  }
1463 
1464  #require_once 'Zend/Date/Exception.php';
1465  throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
1466  break;
1467 
1468  case self::WEEKDAY_SHORT:
1469  $daylist = Zend_Locale_Data::getList($locale, 'day');
1470  $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
1471  $cnt = 0;
1472 
1473  foreach ($daylist as $key => $value) {
1474  if (strtoupper(iconv_substr($value, 0, 3, 'UTF-8')) == strtoupper($date)) {
1475  $found = $cnt;
1476  break;
1477  }
1478  ++$cnt;
1479  }
1480 
1481  // Weekday found
1482  if ($cnt < 7) {
1483  return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
1484  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
1485  }
1486 
1487  // Weekday not found
1488  #require_once 'Zend/Date/Exception.php';
1489  throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1490  break;
1491 
1492  case self::DAY_SHORT:
1493  if (is_numeric($date)) {
1494  return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
1495  $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
1496  }
1497 
1498  #require_once 'Zend/Date/Exception.php';
1499  throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
1500  break;
1501 
1502  case self::WEEKDAY:
1503  $daylist = Zend_Locale_Data::getList($locale, 'day');
1504  $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
1505  $cnt = 0;
1506 
1507  foreach ($daylist as $key => $value) {
1508  if (strtoupper($value) == strtoupper($date)) {
1509  $found = $cnt;
1510  break;
1511  }
1512  ++$cnt;
1513  }
1514 
1515  // Weekday found
1516  if ($cnt < 7) {
1517  return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
1518  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
1519  }
1520 
1521  // Weekday not found
1522  #require_once 'Zend/Date/Exception.php';
1523  throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1524  break;
1525 
1526  case self::WEEKDAY_8601:
1527  $weekday = (int) $this->toString(self::WEEKDAY_8601, 'iso', $locale);
1528  if ((intval($date) > 0) and (intval($date) < 8)) {
1529  return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
1530  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
1531  }
1532 
1533  // Weekday not found
1534  #require_once 'Zend/Date/Exception.php';
1535  throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1536  break;
1537 
1538  case self::DAY_SUFFIX:
1539  #require_once 'Zend/Date/Exception.php';
1540  throw new Zend_Date_Exception('day suffix not supported', 0, null, $date);
1541  break;
1542 
1543  case self::WEEKDAY_DIGIT:
1544  $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
1545  if (is_numeric($date) and (intval($date) >= 0) and (intval($date) < 7)) {
1546  return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
1547  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
1548  }
1549 
1550  // Weekday not found
1551  #require_once 'Zend/Date/Exception.php';
1552  throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1553  break;
1554 
1555  case self::DAY_OF_YEAR:
1556  if (is_numeric($date)) {
1557  if (($calc == 'add') || ($calc == 'sub')) {
1558  $year = 1970;
1559  ++$date;
1560  ++$day;
1561  }
1562 
1563  return $this->_assign($calc, $this->mktime(0, 0, 0, 1, $date, $year, true),
1564  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1565  }
1566 
1567  #require_once 'Zend/Date/Exception.php';
1568  throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
1569  break;
1570 
1571  case self::WEEKDAY_NARROW:
1572  $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
1573  $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
1574  $cnt = 0;
1575  foreach ($daylist as $key => $value) {
1576  if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($date)) {
1577  $found = $cnt;
1578  break;
1579  }
1580  ++$cnt;
1581  }
1582 
1583  // Weekday found
1584  if ($cnt < 7) {
1585  return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
1586  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
1587  }
1588 
1589  // Weekday not found
1590  #require_once 'Zend/Date/Exception.php';
1591  throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1592  break;
1593 
1594  case self::WEEKDAY_NAME:
1595  $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
1596  $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
1597  $cnt = 0;
1598  foreach ($daylist as $key => $value) {
1599  if (strtoupper($value) == strtoupper($date)) {
1600  $found = $cnt;
1601  break;
1602  }
1603  ++$cnt;
1604  }
1605 
1606  // Weekday found
1607  if ($cnt < 7) {
1608  return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
1609  $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
1610  }
1611 
1612  // Weekday not found
1613  #require_once 'Zend/Date/Exception.php';
1614  throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1615  break;
1616 
1617  // week formats
1618  case self::WEEK:
1619  if (is_numeric($date)) {
1620  $week = (int) $this->toString(self::WEEK, 'iso', $locale);
1621  return $this->_assign($calc, parent::mktime(0, 0, 0, 1, 1 + ($date * 7), 1970, true),
1622  parent::mktime(0, 0, 0, 1, 1 + ($week * 7), 1970, true), $hour);
1623  }
1624 
1625  #require_once 'Zend/Date/Exception.php';
1626  throw new Zend_Date_Exception("invalid date ($date) operand, week expected", 0, null, $date);
1627  break;
1628 
1629  // month formats
1630  case self::MONTH_NAME:
1631  $monthlist = Zend_Locale_Data::getList($locale, 'month');
1632  $cnt = 0;
1633  foreach ($monthlist as $key => $value) {
1634  if (strtoupper($value) == strtoupper($date)) {
1635  $found = $key;
1636  break;
1637  }
1638  ++$cnt;
1639  }
1640  $date = array_search($date, $monthlist);
1641 
1642  // Monthname found
1643  if ($cnt < 12) {
1644  $fixday = 0;
1645  if ($calc == 'add') {
1646  $date += $found;
1647  $calc = 'set';
1648  if (self::$_options['extend_month'] == false) {
1649  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1650  if ($parts['mday'] != $day) {
1651  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1652  }
1653  }
1654  } else if ($calc == 'sub') {
1655  $date = $month - $found;
1656  $calc = 'set';
1657  if (self::$_options['extend_month'] == false) {
1658  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1659  if ($parts['mday'] != $day) {
1660  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1661  }
1662  }
1663  }
1664  return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
1665  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1666  }
1667 
1668  // Monthname not found
1669  #require_once 'Zend/Date/Exception.php';
1670  throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1671  break;
1672 
1673  case self::MONTH:
1674  if (is_numeric($date)) {
1675  $fixday = 0;
1676  if ($calc == 'add') {
1677  $date += $month;
1678  $calc = 'set';
1679  if (self::$_options['extend_month'] == false) {
1680  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1681  if ($parts['mday'] != $day) {
1682  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1683  }
1684  }
1685  } else if ($calc == 'sub') {
1686  $date = $month - $date;
1687  $calc = 'set';
1688  if (self::$_options['extend_month'] == false) {
1689  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1690  if ($parts['mday'] != $day) {
1691  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1692  }
1693  }
1694  }
1695  return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
1696  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1697  }
1698 
1699  #require_once 'Zend/Date/Exception.php';
1700  throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1701  break;
1702 
1703  case self::MONTH_NAME_SHORT:
1704  $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
1705  $cnt = 0;
1706  foreach ($monthlist as $key => $value) {
1707  if (strtoupper($value) == strtoupper($date)) {
1708  $found = $key;
1709  break;
1710  }
1711  ++$cnt;
1712  }
1713  $date = array_search($date, $monthlist);
1714 
1715  // Monthname found
1716  if ($cnt < 12) {
1717  $fixday = 0;
1718  if ($calc == 'add') {
1719  $date += $found;
1720  $calc = 'set';
1721  if (self::$_options['extend_month'] === false) {
1722  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1723  if ($parts['mday'] != $day) {
1724  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1725  }
1726  }
1727  } else if ($calc == 'sub') {
1728  $date = $month - $found;
1729  $calc = 'set';
1730  if (self::$_options['extend_month'] === false) {
1731  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1732  if ($parts['mday'] != $day) {
1733  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1734  }
1735  }
1736  }
1737  return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
1738  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1739  }
1740 
1741  // Monthname not found
1742  #require_once 'Zend/Date/Exception.php';
1743  throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1744  break;
1745 
1746  case self::MONTH_SHORT:
1747  if (is_numeric($date) === true) {
1748  $fixday = 0;
1749  if ($calc === 'add') {
1750  $date += $month;
1751  $calc = 'set';
1752  if (self::$_options['extend_month'] === false) {
1753  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1754  if ($parts['mday'] != $day) {
1755  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1756  }
1757  }
1758  } else if ($calc === 'sub') {
1759  $date = $month - $date;
1760  $calc = 'set';
1761  if (self::$_options['extend_month'] === false) {
1762  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1763  if ($parts['mday'] != $day) {
1764  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1765  }
1766  }
1767  }
1768 
1769  return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
1770  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1771  }
1772 
1773  #require_once 'Zend/Date/Exception.php';
1774  throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1775  break;
1776 
1777  case self::MONTH_DAYS:
1778  #require_once 'Zend/Date/Exception.php';
1779  throw new Zend_Date_Exception('month days not supported', 0, null, $date);
1780  break;
1781 
1782  case self::MONTH_NAME_NARROW:
1783  $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow'));
1784  $cnt = 0;
1785  foreach ($monthlist as $key => $value) {
1786  if (strtoupper($value) === strtoupper($date)) {
1787  $found = $key;
1788  break;
1789  }
1790  ++$cnt;
1791  }
1792  $date = array_search($date, $monthlist);
1793 
1794  // Monthname found
1795  if ($cnt < 12) {
1796  $fixday = 0;
1797  if ($calc === 'add') {
1798  $date += $found;
1799  $calc = 'set';
1800  if (self::$_options['extend_month'] === false) {
1801  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1802  if ($parts['mday'] != $day) {
1803  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1804  }
1805  }
1806  } else if ($calc === 'sub') {
1807  $date = $month - $found;
1808  $calc = 'set';
1809  if (self::$_options['extend_month'] === false) {
1810  $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1811  if ($parts['mday'] != $day) {
1812  $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
1813  }
1814  }
1815  }
1816  return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
1817  $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1818  }
1819 
1820  // Monthname not found
1821  #require_once 'Zend/Date/Exception.php';
1822  throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1823  break;
1824 
1825  // year formats
1826  case self::LEAPYEAR:
1827  #require_once 'Zend/Date/Exception.php';
1828  throw new Zend_Date_Exception('leap year not supported', 0, null, $date);
1829  break;
1830 
1831  case self::YEAR_8601:
1832  if (is_numeric($date)) {
1833  if ($calc === 'add') {
1834  $date += $year;
1835  $calc = 'set';
1836  } else if ($calc === 'sub') {
1837  $date = $year - $date;
1838  $calc = 'set';
1839  }
1840 
1841  return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
1842  $this->mktime(0, 0, 0, $month, $day, $year, true), false);
1843  }
1844 
1845  #require_once 'Zend/Date/Exception.php';
1846  throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
1847  break;
1848 
1849  case self::YEAR:
1850  if (is_numeric($date)) {
1851  if ($calc === 'add') {
1852  $date += $year;
1853  $calc = 'set';
1854  } else if ($calc === 'sub') {
1855  $date = $year - $date;
1856  $calc = 'set';
1857  }
1858 
1859  return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
1860  $this->mktime(0, 0, 0, $month, $day, $year, true), false);
1861  }
1862 
1863  #require_once 'Zend/Date/Exception.php';
1864  throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
1865  break;
1866 
1867  case self::YEAR_SHORT:
1868  if (is_numeric($date)) {
1869  $date = intval($date);
1870  if (($calc == 'set') || ($calc == 'cmp')) {
1871  $date = self::getFullYear($date);
1872  }
1873  if ($calc === 'add') {
1874  $date += $year;
1875  $calc = 'set';
1876  } else if ($calc === 'sub') {
1877  $date = $year - $date;
1878  $calc = 'set';
1879  }
1880 
1881  return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
1882  $this->mktime(0, 0, 0, $month, $day, $year, true), false);
1883  }
1884 
1885  #require_once 'Zend/Date/Exception.php';
1886  throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
1887  break;
1888 
1889  case self::YEAR_SHORT_8601:
1890  if (is_numeric($date)) {
1891  $date = intval($date);
1892  if (($calc === 'set') || ($calc === 'cmp')) {
1893  $date = self::getFullYear($date);
1894  }
1895  if ($calc === 'add') {
1896  $date += $year;
1897  $calc = 'set';
1898  } else if ($calc === 'sub') {
1899  $date = $year - $date;
1900  $calc = 'set';
1901  }
1902 
1903  return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
1904  $this->mktime(0, 0, 0, $month, $day, $year, true), false);
1905  }
1906 
1907  #require_once 'Zend/Date/Exception.php';
1908  throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
1909  break;
1910 
1911  // time formats
1912  case self::MERIDIEM:
1913  #require_once 'Zend/Date/Exception.php';
1914  throw new Zend_Date_Exception('meridiem not supported', 0, null, $date);
1915  break;
1916 
1917  case self::SWATCH:
1918  if (is_numeric($date)) {
1919  $rest = intval($date);
1920  $hours = floor($rest * 24 / 1000);
1921  $rest = $rest - ($hours * 1000 / 24);
1922  $minutes = floor($rest * 1440 / 1000);
1923  $rest = $rest - ($minutes * 1000 / 1440);
1924  $seconds = floor($rest * 86400 / 1000);
1925  return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true),
1926  $this->mktime($hour, $minute, $second, 1, 1, 1970, true), false);
1927  }
1928 
1929  #require_once 'Zend/Date/Exception.php';
1930  throw new Zend_Date_Exception("invalid date ($date) operand, swatchstamp expected", 0, null, $date);
1931  break;
1932 
1933  case self::HOUR_SHORT_AM:
1934  if (is_numeric($date)) {
1935  return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
1936  $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
1937  }
1938 
1939  #require_once 'Zend/Date/Exception.php';
1940  throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
1941  break;
1942 
1943  case self::HOUR_SHORT:
1944  if (is_numeric($date)) {
1945  return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
1946  $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
1947  }
1948 
1949  #require_once 'Zend/Date/Exception.php';
1950  throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
1951  break;
1952 
1953  case self::HOUR_AM:
1954  if (is_numeric($date)) {
1955  return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
1956  $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
1957  }
1958 
1959  #require_once 'Zend/Date/Exception.php';
1960  throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
1961  break;
1962 
1963  case self::HOUR:
1964  if (is_numeric($date)) {
1965  return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
1966  $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
1967  }
1968 
1969  #require_once 'Zend/Date/Exception.php';
1970  throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
1971  break;
1972 
1973  case self::MINUTE:
1974  if (is_numeric($date)) {
1975  return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
1976  $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
1977  }
1978 
1979  #require_once 'Zend/Date/Exception.php';
1980  throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
1981  break;
1982 
1983  case self::SECOND:
1984  if (is_numeric($date)) {
1985  return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
1986  $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
1987  }
1988 
1989  #require_once 'Zend/Date/Exception.php';
1990  throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
1991  break;
1992 
1993  case self::MILLISECOND:
1994  if (is_numeric($date)) {
1995  switch($calc) {
1996  case 'set' :
1997  return $this->setMillisecond($date);
1998  break;
1999  case 'add' :
2000  return $this->addMillisecond($date);
2001  break;
2002  case 'sub' :
2003  return $this->subMillisecond($date);
2004  break;
2005  }
2006 
2007  return $this->compareMillisecond($date);
2008  }
2009 
2010  #require_once 'Zend/Date/Exception.php';
2011  throw new Zend_Date_Exception("invalid date ($date) operand, milliseconds expected", 0, null, $date);
2012  break;
2013 
2014  case self::MINUTE_SHORT:
2015  if (is_numeric($date)) {
2016  return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
2017  $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
2018  }
2019 
2020  #require_once 'Zend/Date/Exception.php';
2021  throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
2022  break;
2023 
2024  case self::SECOND_SHORT:
2025  if (is_numeric($date)) {
2026  return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
2027  $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
2028  }
2029 
2030  #require_once 'Zend/Date/Exception.php';
2031  throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
2032  break;
2033 
2034  // timezone formats
2035  // break intentionally omitted
2036  case self::TIMEZONE_NAME:
2037  case self::TIMEZONE:
2038  case self::TIMEZONE_SECS:
2039  #require_once 'Zend/Date/Exception.php';
2040  throw new Zend_Date_Exception('timezone not supported', 0, null, $date);
2041  break;
2042 
2043  case self::DAYLIGHT:
2044  #require_once 'Zend/Date/Exception.php';
2045  throw new Zend_Date_Exception('daylight not supported', 0, null, $date);
2046  break;
2047 
2048  case self::GMT_DIFF:
2049  case self::GMT_DIFF_SEP:
2050  #require_once 'Zend/Date/Exception.php';
2051  throw new Zend_Date_Exception('gmtdiff not supported', 0, null, $date);
2052  break;
2053 
2054  // date strings
2055  case self::ISO_8601:
2056  // (-)YYYY-MM-dd
2057  preg_match('/^(-{0,1}\d{4})-(\d{2})-(\d{2})/', $date, $datematch);
2058  // (-)YY-MM-dd
2059  if (empty($datematch)) {
2060  preg_match('/^(-{0,1}\d{2})-(\d{2})-(\d{2})/', $date, $datematch);
2061  }
2062  // (-)YYYYMMdd
2063  if (empty($datematch)) {
2064  preg_match('/^(-{0,1}\d{4})(\d{2})(\d{2})/', $date, $datematch);
2065  }
2066  // (-)YYMMdd
2067  if (empty($datematch)) {
2068  preg_match('/^(-{0,1}\d{2})(\d{2})(\d{2})/', $date, $datematch);
2069  }
2070  $tmpdate = $date;
2071  if (!empty($datematch)) {
2072  $dateMatchCharCount = iconv_strlen($datematch[0], 'UTF-8');
2073  $tmpdate = iconv_substr($date,
2074  $dateMatchCharCount,
2075  iconv_strlen($date, 'UTF-8') - $dateMatchCharCount,
2076  'UTF-8');
2077  }
2078  // (T)hh:mm:ss
2079  preg_match('/[T,\s]{0,1}(\d{2}):(\d{2}):(\d{2})/', $tmpdate, $timematch);
2080  // (T)hhmmss
2081  if (empty($timematch)) {
2082  preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
2083  }
2084  // (T)hh:mm
2085  if (empty($timematch)) {
2086  preg_match('/[T,\s]{0,1}(\d{2}):(\d{2})/', $tmpdate, $timematch);
2087  }
2088  // (T)hhmm
2089  if (empty($timematch)) {
2090  preg_match('/[T,\s]{0,1}(\d{2})(\d{2})/', $tmpdate, $timematch);
2091  }
2092  if (empty($datematch) and empty($timematch)) {
2093  #require_once 'Zend/Date/Exception.php';
2094  throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", 0, null, $date);
2095  }
2096  if (!empty($timematch)) {
2097  $timeMatchCharCount = iconv_strlen($timematch[0], 'UTF-8');
2098  $tmpdate = iconv_substr($tmpdate,
2099  $timeMatchCharCount,
2100  iconv_strlen($tmpdate, 'UTF-8') - $timeMatchCharCount,
2101  'UTF-8');
2102  }
2103  if (empty($datematch)) {
2104  $datematch[1] = 1970;
2105  $datematch[2] = 1;
2106  $datematch[3] = 1;
2107  } else if (iconv_strlen($datematch[1], 'UTF-8') == 2) {
2108  $datematch[1] = self::getFullYear($datematch[1]);
2109  }
2110  if (empty($timematch)) {
2111  $timematch[1] = 0;
2112  $timematch[2] = 0;
2113  $timematch[3] = 0;
2114  }
2115  if (!isset($timematch[3])) {
2116  $timematch[3] = 0;
2117  }
2118 
2119  if (($calc == 'set') || ($calc == 'cmp')) {
2120  --$datematch[2];
2121  --$month;
2122  --$datematch[3];
2123  --$day;
2124  $datematch[1] -= 1970;
2125  $year -= 1970;
2126  }
2127  return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false),
2128  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
2129  break;
2130 
2131  case self::RFC_2822:
2132  $result = preg_match('/^\w{3},\s(\d{1,2})\s(\w{3})\s(\d{4})\s'
2133  . '(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]'
2134  . '{1}\d{4}|\w{1,20})$/', $date, $match);
2135 
2136  if (!$result) {
2137  #require_once 'Zend/Date/Exception.php';
2138  throw new Zend_Date_Exception("no RFC 2822 format ($date)", 0, null, $date);
2139  }
2140 
2141  $months = $this->_getDigitFromName($match[2]);
2142 
2143  if (($calc == 'set') || ($calc == 'cmp')) {
2144  --$months;
2145  --$month;
2146  --$match[1];
2147  --$day;
2148  $match[3] -= 1970;
2149  $year -= 1970;
2150  }
2151  return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
2152  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
2153  break;
2154 
2155  case self::TIMESTAMP:
2156  if (is_numeric($date)) {
2157  return $this->_assign($calc, $date, $this->getUnixTimestamp());
2158  }
2159 
2160  #require_once 'Zend/Date/Exception.php';
2161  throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", 0, null, $date);
2162  break;
2163 
2164  // additional formats
2165  // break intentionally omitted
2166  case self::ERA:
2167  case self::ERA_NAME:
2168  #require_once 'Zend/Date/Exception.php';
2169  throw new Zend_Date_Exception('era not supported', 0, null, $date);
2170  break;
2171 
2172  case self::DATES:
2173  try {
2174  $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
2175 
2176  if (($calc == 'set') || ($calc == 'cmp')) {
2177  --$parsed['month'];
2178  --$month;
2179  --$parsed['day'];
2180  --$day;
2181  $parsed['year'] -= 1970;
2182  $year -= 1970;
2183  }
2184 
2185  return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2186  $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2187  } catch (Zend_Locale_Exception $e) {
2188  #require_once 'Zend/Date/Exception.php';
2189  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2190  }
2191  break;
2192 
2193  case self::DATE_FULL:
2194  try {
2195  $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
2196  $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2197 
2198  if (($calc == 'set') || ($calc == 'cmp')) {
2199  --$parsed['month'];
2200  --$month;
2201  --$parsed['day'];
2202  --$day;
2203  $parsed['year'] -= 1970;
2204  $year -= 1970;
2205  }
2206  return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2207  $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2208  } catch (Zend_Locale_Exception $e) {
2209  #require_once 'Zend/Date/Exception.php';
2210  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2211  }
2212  break;
2213 
2214  case self::DATE_LONG:
2215  try {
2216  $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
2217  $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2218 
2219  if (($calc == 'set') || ($calc == 'cmp')){
2220  --$parsed['month'];
2221  --$month;
2222  --$parsed['day'];
2223  --$day;
2224  $parsed['year'] -= 1970;
2225  $year -= 1970;
2226  }
2227  return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2228  $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2229  } catch (Zend_Locale_Exception $e) {
2230  #require_once 'Zend/Date/Exception.php';
2231  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2232  }
2233  break;
2234 
2235  case self::DATE_MEDIUM:
2236  try {
2237  $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
2238  $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2239 
2240  if (($calc == 'set') || ($calc == 'cmp')) {
2241  --$parsed['month'];
2242  --$month;
2243  --$parsed['day'];
2244  --$day;
2245  $parsed['year'] -= 1970;
2246  $year -= 1970;
2247  }
2248  return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2249  $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2250  } catch (Zend_Locale_Exception $e) {
2251  #require_once 'Zend/Date/Exception.php';
2252  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2253  }
2254  break;
2255 
2256  case self::DATE_SHORT:
2257  try {
2258  $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
2259  $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2260 
2261  $parsed['year'] = self::getFullYear($parsed['year']);
2262 
2263  if (($calc == 'set') || ($calc == 'cmp')) {
2264  --$parsed['month'];
2265  --$month;
2266  --$parsed['day'];
2267  --$day;
2268  $parsed['year'] -= 1970;
2269  $year -= 1970;
2270  }
2271  return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2272  $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2273  } catch (Zend_Locale_Exception $e) {
2274  #require_once 'Zend/Date/Exception.php';
2275  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2276  }
2277  break;
2278 
2279  case self::TIMES:
2280  try {
2281  if ($calc != 'set') {
2282  $month = 1;
2283  $day = 1;
2284  $year = 1970;
2285  }
2286  $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
2287  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2288  $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2289  } catch (Zend_Locale_Exception $e) {
2290  #require_once 'Zend/Date/Exception.php';
2291  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2292  }
2293  break;
2294 
2295  case self::TIME_FULL:
2296  try {
2297  $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
2298  $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2299  if ($calc != 'set') {
2300  $month = 1;
2301  $day = 1;
2302  $year = 1970;
2303  }
2304 
2305  if (!isset($parsed['second'])) {
2306  $parsed['second'] = 0;
2307  }
2308 
2309  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2310  $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2311  } catch (Zend_Locale_Exception $e) {
2312  #require_once 'Zend/Date/Exception.php';
2313  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2314  }
2315  break;
2316 
2317  case self::TIME_LONG:
2318  try {
2319  $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
2320  $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2321  if ($calc != 'set') {
2322  $month = 1;
2323  $day = 1;
2324  $year = 1970;
2325  }
2326  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2327  $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2328  } catch (Zend_Locale_Exception $e) {
2329  #require_once 'Zend/Date/Exception.php';
2330  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2331  }
2332  break;
2333 
2334  case self::TIME_MEDIUM:
2335  try {
2336  $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
2337  $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2338  if ($calc != 'set') {
2339  $month = 1;
2340  $day = 1;
2341  $year = 1970;
2342  }
2343  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2344  $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2345  } catch (Zend_Locale_Exception $e) {
2346  #require_once 'Zend/Date/Exception.php';
2347  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2348  }
2349  break;
2350 
2351  case self::TIME_SHORT:
2352  try {
2353  $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
2354  $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2355  if ($calc != 'set') {
2356  $month = 1;
2357  $day = 1;
2358  $year = 1970;
2359  }
2360 
2361  if (!isset($parsed['second'])) {
2362  $parsed['second'] = 0;
2363  }
2364 
2365  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2366  $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2367  } catch (Zend_Locale_Exception $e) {
2368  #require_once 'Zend/Date/Exception.php';
2369  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2370  }
2371  break;
2372 
2373  case self::DATETIME:
2374  try {
2375  $parsed = Zend_Locale_Format::getDateTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
2376  if (($calc == 'set') || ($calc == 'cmp')) {
2377  --$parsed['month'];
2378  --$month;
2379  --$parsed['day'];
2380  --$day;
2381  $parsed['year'] -= 1970;
2382  $year -= 1970;
2383  }
2384  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2385  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2386  } catch (Zend_Locale_Exception $e) {
2387  #require_once 'Zend/Date/Exception.php';
2388  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2389  }
2390  break;
2391 
2392  case self::DATETIME_FULL:
2393  try {
2394  $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
2395  $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2396 
2397  if (($calc == 'set') || ($calc == 'cmp')) {
2398  --$parsed['month'];
2399  --$month;
2400  --$parsed['day'];
2401  --$day;
2402  $parsed['year'] -= 1970;
2403  $year -= 1970;
2404  }
2405 
2406  if (!isset($parsed['second'])) {
2407  $parsed['second'] = 0;
2408  }
2409 
2410  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2411  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2412  } catch (Zend_Locale_Exception $e) {
2413  #require_once 'Zend/Date/Exception.php';
2414  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2415  }
2416  break;
2417 
2418  case self::DATETIME_LONG:
2419  try {
2420  $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
2421  $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2422 
2423  if (($calc == 'set') || ($calc == 'cmp')){
2424  --$parsed['month'];
2425  --$month;
2426  --$parsed['day'];
2427  --$day;
2428  $parsed['year'] -= 1970;
2429  $year -= 1970;
2430  }
2431  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2432  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2433  } catch (Zend_Locale_Exception $e) {
2434  #require_once 'Zend/Date/Exception.php';
2435  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2436  }
2437  break;
2438 
2439  case self::DATETIME_MEDIUM:
2440  try {
2441  $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
2442  $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2443  if (($calc == 'set') || ($calc == 'cmp')) {
2444  --$parsed['month'];
2445  --$month;
2446  --$parsed['day'];
2447  --$day;
2448  $parsed['year'] -= 1970;
2449  $year -= 1970;
2450  }
2451  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2452  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2453  } catch (Zend_Locale_Exception $e) {
2454  #require_once 'Zend/Date/Exception.php';
2455  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2456  }
2457  break;
2458 
2459  case self::DATETIME_SHORT:
2460  try {
2461  $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
2462  $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2463 
2464  $parsed['year'] = self::getFullYear($parsed['year']);
2465 
2466  if (($calc == 'set') || ($calc == 'cmp')) {
2467  --$parsed['month'];
2468  --$month;
2469  --$parsed['day'];
2470  --$day;
2471  $parsed['year'] -= 1970;
2472  $year -= 1970;
2473  }
2474 
2475  if (!isset($parsed['second'])) {
2476  $parsed['second'] = 0;
2477  }
2478 
2479  return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
2480  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
2481  } catch (Zend_Locale_Exception $e) {
2482  #require_once 'Zend/Date/Exception.php';
2483  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2484  }
2485  break;
2486 
2487  // ATOM and RFC_3339 are identical
2488  case self::ATOM:
2489  case self::RFC_3339:
2490  $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);
2491  if (!$result) {
2492  #require_once 'Zend/Date/Exception.php';
2493  throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", 0, null, $date);
2494  }
2495 
2496  if (($calc == 'set') || ($calc == 'cmp')) {
2497  --$match[2];
2498  --$month;
2499  --$match[3];
2500  --$day;
2501  $match[1] -= 1970;
2502  $year -= 1970;
2503  }
2504  return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
2505  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
2506  break;
2507 
2508  case self::COOKIE:
2509  $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);
2510  if (!$result) {
2511  #require_once 'Zend/Date/Exception.php';
2512  throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", 0, null, $date);
2513  }
2514  $matchStartPos = iconv_strpos($match[0], ' ', 0, 'UTF-8') + 1;
2515  $match[0] = iconv_substr($match[0],
2516  $matchStartPos,
2517  iconv_strlen($match[0], 'UTF-8') - $matchStartPos,
2518  'UTF-8');
2519 
2520  $months = $this->_getDigitFromName($match[2]);
2521  $match[3] = self::getFullYear($match[3]);
2522 
2523  if (($calc == 'set') || ($calc == 'cmp')) {
2524  --$months;
2525  --$month;
2526  --$match[1];
2527  --$day;
2528  $match[3] -= 1970;
2529  $year -= 1970;
2530  }
2531  return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
2532  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
2533  break;
2534 
2535  case self::RFC_822:
2536  case self::RFC_1036:
2537  // new RFC 822 format, identical to RFC 1036 standard
2538  $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);
2539  if (!$result) {
2540  #require_once 'Zend/Date/Exception.php';
2541  throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", 0, null, $date);
2542  }
2543 
2544  $months = $this->_getDigitFromName($match[2]);
2545  $match[3] = self::getFullYear($match[3]);
2546 
2547  if (($calc == 'set') || ($calc == 'cmp')) {
2548  --$months;
2549  --$month;
2550  --$match[1];
2551  --$day;
2552  $match[3] -= 1970;
2553  $year -= 1970;
2554  }
2555  return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
2556  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
2557  break;
2558 
2559  case self::RFC_850:
2560  $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);
2561  if (!$result) {
2562  #require_once 'Zend/Date/Exception.php';
2563  throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", 0, null, $date);
2564  }
2565 
2566  $months = $this->_getDigitFromName($match[2]);
2567  $match[3] = self::getFullYear($match[3]);
2568 
2569  if (($calc == 'set') || ($calc == 'cmp')) {
2570  --$months;
2571  --$month;
2572  --$match[1];
2573  --$day;
2574  $match[3] -= 1970;
2575  $year -= 1970;
2576  }
2577  return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
2578  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
2579  break;
2580 
2581  case self::RFC_1123:
2582  $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);
2583  if (!$result) {
2584  #require_once 'Zend/Date/Exception.php';
2585  throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", 0, null, $date);
2586  }
2587 
2588  $months = $this->_getDigitFromName($match[2]);
2589 
2590  if (($calc == 'set') || ($calc == 'cmp')) {
2591  --$months;
2592  --$month;
2593  --$match[1];
2594  --$day;
2595  $match[3] -= 1970;
2596  $year -= 1970;
2597  }
2598  return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
2599  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
2600  break;
2601 
2602  case self::RSS:
2603  $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);
2604  if (!$result) {
2605  #require_once 'Zend/Date/Exception.php';
2606  throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", 0, null, $date);
2607  }
2608 
2609  $months = $this->_getDigitFromName($match[2]);
2610  $match[3] = self::getFullYear($match[3]);
2611 
2612  if (($calc == 'set') || ($calc == 'cmp')) {
2613  --$months;
2614  --$month;
2615  --$match[1];
2616  --$day;
2617  $match[3] -= 1970;
2618  $year -= 1970;
2619  }
2620  return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
2621  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
2622  break;
2623 
2624  case self::W3C:
2625  $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
2626  if (!$result) {
2627  #require_once 'Zend/Date/Exception.php';
2628  throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", 0, null, $date);
2629  }
2630 
2631  if (($calc == 'set') || ($calc == 'cmp')) {
2632  --$match[2];
2633  --$month;
2634  --$match[3];
2635  --$day;
2636  $match[1] -= 1970;
2637  $year -= 1970;
2638  }
2639  return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
2640  $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
2641  break;
2642 
2643  default:
2644  if (!is_numeric($date) || !empty($part)) {
2645  try {
2646  if (empty($part)) {
2647  $part = Zend_Locale_Format::getDateFormat($locale) . " ";
2648  $part .= Zend_Locale_Format::getTimeFormat($locale);
2649  }
2650 
2651  $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
2652  if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
2653  $parsed['year'] = self::getFullYear($parsed['year']);
2654  }
2655 
2656  if (($calc == 'set') || ($calc == 'cmp')) {
2657  if (isset($parsed['month'])) {
2658  --$parsed['month'];
2659  } else {
2660  $parsed['month'] = 0;
2661  }
2662 
2663  if (isset($parsed['day'])) {
2664  --$parsed['day'];
2665  } else {
2666  $parsed['day'] = 0;
2667  }
2668 
2669  if (!isset($parsed['year'])) {
2670  $parsed['year'] = 1970;
2671  }
2672  }
2673 
2674  return $this->_assign($calc, $this->mktime(
2675  isset($parsed['hour']) ? $parsed['hour'] : 0,
2676  isset($parsed['minute']) ? $parsed['minute'] : 0,
2677  isset($parsed['second']) ? $parsed['second'] : 0,
2678  isset($parsed['month']) ? (1 + $parsed['month']) : 1,
2679  isset($parsed['day']) ? (1 + $parsed['day']) : 1,
2680  $parsed['year'],
2681  false), $this->getUnixTimestamp(), false);
2682  } catch (Zend_Locale_Exception $e) {
2683  if (!is_numeric($date)) {
2684  #require_once 'Zend/Date/Exception.php';
2685  throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2686  }
2687  }
2688  }
2689 
2690  return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
2691  break;
2692  }
2693  }
2694 
2706  public function equals($date, $part = self::TIMESTAMP, $locale = null)
2707  {
2708  $result = $this->compare($date, $part, $locale);
2709 
2710  if ($result == 0) {
2711  return true;
2712  }
2713 
2714  return false;
2715  }
2716 
2728  public function isEarlier($date, $part = null, $locale = null)
2729  {
2730  $result = $this->compare($date, $part, $locale);
2731 
2732  if ($result == -1) {
2733  return true;
2734  }
2735 
2736  return false;
2737  }
2738 
2751  public function isLater($date, $part = null, $locale = null)
2752  {
2753  $result = $this->compare($date, $part, $locale);
2754 
2755  if ($result == 1) {
2756  return true;
2757  }
2758 
2759  return false;
2760  }
2761 
2770  public function getTime($locale = null)
2771  {
2772  if (self::$_options['format_type'] == 'php') {
2773  $format = 'H:i:s';
2774  } else {
2775  $format = self::TIME_MEDIUM;
2776  }
2777 
2778  return $this->copyPart($format, $locale);
2779  }
2780 
2791  private function _time($calc, $time, $format, $locale)
2792  {
2793  if ($time === null) {
2794  #require_once 'Zend/Date/Exception.php';
2795  throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
2796  }
2797 
2798  if ($time instanceof Zend_Date) {
2799  // extract time from object
2800  $time = $time->toString('HH:mm:ss', 'iso');
2801  } else {
2802  if (is_array($time)) {
2803  if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
2804  (isset($time['second']) === true)) {
2805  $parsed = $time;
2806  } else {
2807  #require_once 'Zend/Date/Exception.php';
2808  throw new Zend_Date_Exception("no hour, minute or second given in array");
2809  }
2810  } else {
2811  if (self::$_options['format_type'] == 'php') {
2813  }
2814  try {
2815  if ($locale === null) {
2816  $locale = $this->getLocale();
2817  }
2818 
2819  $parsed = Zend_Locale_Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
2820  } catch (Zend_Locale_Exception $e) {
2821  #require_once 'Zend/Date/Exception.php';
2822  throw new Zend_Date_Exception($e->getMessage(), 0, $e);
2823  }
2824  }
2825 
2826  if (!array_key_exists('hour', $parsed)) {
2827  $parsed['hour'] = 0;
2828  }
2829 
2830  if (!array_key_exists('minute', $parsed)) {
2831  $parsed['minute'] = 0;
2832  }
2833 
2834  if (!array_key_exists('second', $parsed)) {
2835  $parsed['second'] = 0;
2836  }
2837 
2838  $time = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT) . ":";
2839  $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT) . ":";
2840  $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT);
2841  }
2842 
2843  $return = $this->_calcdetail($calc, $time, self::TIMES, 'de');
2844  if ($calc != 'cmp') {
2845  return $this;
2846  }
2847 
2848  return $return;
2849  }
2850 
2851 
2864  public function setTime($time, $format = null, $locale = null)
2865  {
2866  return $this->_time('set', $time, $format, $locale);
2867  }
2868 
2869 
2882  public function addTime($time, $format = null, $locale = null)
2883  {
2884  return $this->_time('add', $time, $format, $locale);
2885  }
2886 
2887 
2900  public function subTime($time, $format = null, $locale = null)
2901  {
2902  return $this->_time('sub', $time, $format, $locale);
2903  }
2904 
2905 
2918  public function compareTime($time, $format = null, $locale = null)
2919  {
2920  return $this->_time('cmp', $time, $format, $locale);
2921  }
2922 
2929  public function getDate($locale = null)
2930  {
2931  $orig = self::$_options['format_type'];
2932  if (self::$_options['format_type'] == 'php') {
2933  self::$_options['format_type'] = 'iso';
2934  }
2935 
2936  $date = $this->copyPart(self::DATE_MEDIUM, $locale);
2937  $date->addTimestamp($this->getGmtOffset());
2938  self::$_options['format_type'] = $orig;
2939 
2940  return $date;
2941  }
2942 
2953  private function _date($calc, $date, $format, $locale)
2954  {
2955  if ($date === null) {
2956  #require_once 'Zend/Date/Exception.php';
2957  throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
2958  }
2959 
2960  if ($date instanceof Zend_Date) {
2961  // extract date from object
2962  $date = $date->toString('d.M.y', 'iso');
2963  } else {
2964  if (is_array($date)) {
2965  if ((isset($date['year']) === true) or (isset($date['month']) === true) or
2966  (isset($date['day']) === true)) {
2967  $parsed = $date;
2968  } else {
2969  #require_once 'Zend/Date/Exception.php';
2970  throw new Zend_Date_Exception("no day,month or year given in array");
2971  }
2972  } else {
2973  if ((self::$_options['format_type'] == 'php') && !defined($format)) {
2975  }
2976  try {
2977  if ($locale === null) {
2978  $locale = $this->getLocale();
2979  }
2980 
2981  $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
2982  if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
2983  $parsed['year'] = self::getFullYear($parsed['year']);
2984  }
2985  } catch (Zend_Locale_Exception $e) {
2986  #require_once 'Zend/Date/Exception.php';
2987  throw new Zend_Date_Exception($e->getMessage(), 0, $e);
2988  }
2989  }
2990 
2991  if (!array_key_exists('day', $parsed)) {
2992  $parsed['day'] = 1;
2993  }
2994 
2995  if (!array_key_exists('month', $parsed)) {
2996  $parsed['month'] = 1;
2997  }
2998 
2999  if (!array_key_exists('year', $parsed)) {
3000  $parsed['year'] = 0;
3001  }
3002 
3003  $date = $parsed['day'] . "." . $parsed['month'] . "." . $parsed['year'];
3004  }
3005 
3006  $return = $this->_calcdetail($calc, $date, self::DATE_MEDIUM, 'de');
3007  if ($calc != 'cmp') {
3008  return $this;
3009  }
3010  return $return;
3011  }
3012 
3013 
3026  public function setDate($date, $format = null, $locale = null)
3027  {
3028  return $this->_date('set', $date, $format, $locale);
3029  }
3030 
3031 
3044  public function addDate($date, $format = null, $locale = null)
3045  {
3046  return $this->_date('add', $date, $format, $locale);
3047  }
3048 
3049 
3063  public function subDate($date, $format = null, $locale = null)
3064  {
3065  return $this->_date('sub', $date, $format, $locale);
3066  }
3067 
3068 
3082  public function compareDate($date, $format = null, $locale = null)
3083  {
3084  return $this->_date('cmp', $date, $format, $locale);
3085  }
3086 
3087 
3097  public function getIso($locale = null)
3098  {
3099  return $this->toString(self::ISO_8601, 'iso', $locale);
3100  }
3101 
3102 
3114  public function setIso($date, $locale = null)
3115  {
3116  return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
3117  }
3118 
3119 
3131  public function addIso($date, $locale = null)
3132  {
3133  return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
3134  }
3135 
3136 
3148  public function subIso($date, $locale = null)
3149  {
3150  return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
3151  }
3152 
3153 
3165  public function compareIso($date, $locale = null)
3166  {
3167  return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
3168  }
3169 
3170 
3178  public function getArpa($locale = null)
3179  {
3180  if (self::$_options['format_type'] == 'php') {
3181  $format = 'D\, d M y H\:i\:s O';
3182  } else {
3183  $format = self::RFC_822;
3184  }
3185 
3186  return $this->toString($format, 'iso', $locale);
3187  }
3188 
3189 
3201  public function setArpa($date, $locale = null)
3202  {
3203  return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
3204  }
3205 
3206 
3219  public function addArpa($date, $locale = null)
3220  {
3221  return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
3222  }
3223 
3224 
3237  public function subArpa($date, $locale = null)
3238  {
3239  return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
3240  }
3241 
3242 
3255  public function compareArpa($date, $locale = null)
3256  {
3257  return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
3258  }
3259 
3267  private function _checkLocation($location)
3268  {
3269  if (!isset($location['longitude']) or !isset($location['latitude'])) {
3270  #require_once 'Zend/Date/Exception.php';
3271  throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', 0, null, $location);
3272  }
3273  if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
3274  #require_once 'Zend/Date/Exception.php';
3275  throw new Zend_Date_Exception('Longitude must be between -180 and 180', 0, null, $location);
3276  }
3277  if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
3278  #require_once 'Zend/Date/Exception.php';
3279  throw new Zend_Date_Exception('Latitude must be between -90 and 90', 0, null, $location);
3280  }
3281 
3282  if (!isset($location['horizon'])){
3283  $location['horizon'] = 'effective';
3284  }
3285 
3286  switch ($location['horizon']) {
3287  case 'civil' :
3288  return -0.104528;
3289  break;
3290  case 'nautic' :
3291  return -0.207912;
3292  break;
3293  case 'astronomic' :
3294  return -0.309017;
3295  break;
3296  default :
3297  return -0.0145439;
3298  break;
3299  }
3300  }
3301 
3302 
3314  public function getSunrise($location)
3315  {
3316  $horizon = $this->_checkLocation($location);
3317  $result = clone $this;
3318  $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
3319  return $result;
3320  }
3321 
3322 
3334  public function getSunset($location)
3335  {
3336  $horizon = $this->_checkLocation($location);
3337  $result = clone $this;
3338  $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
3339  return $result;
3340  }
3341 
3342 
3354  public function getSunInfo($location)
3355  {
3356  $suninfo = array();
3357  for ($i = 0; $i < 4; ++$i) {
3358  switch ($i) {
3359  case 0 :
3360  $location['horizon'] = 'effective';
3361  break;
3362  case 1 :
3363  $location['horizon'] = 'civil';
3364  break;
3365  case 2 :
3366  $location['horizon'] = 'nautic';
3367  break;
3368  case 3 :
3369  $location['horizon'] = 'astronomic';
3370  break;
3371  }
3372  $horizon = $this->_checkLocation($location);
3373  $result = clone $this;
3374  $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
3375  $suninfo['sunrise'][$location['horizon']] = $result;
3376  $result = clone $this;
3377  $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
3378  $suninfo['sunset'][$location['horizon']] = $result;
3379  }
3380  return $suninfo;
3381  }
3382 
3390  public static function checkLeapYear($year)
3391  {
3392  if ($year instanceof Zend_Date) {
3393  $year = (int) $year->toString(self::YEAR, 'iso');
3394  }
3395 
3396  if (is_array($year)) {
3397  if (isset($year['year']) === true) {
3398  $year = $year['year'];
3399  } else {
3400  #require_once 'Zend/Date/Exception.php';
3401  throw new Zend_Date_Exception("no year given in array");
3402  }
3403  }
3404 
3405  if (!is_numeric($year)) {
3406  #require_once 'Zend/Date/Exception.php';
3407  throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", 0, null, $year);
3408  }
3409 
3410  return (bool) parent::isYearLeapYear($year);
3411  }
3412 
3413 
3419  public function isLeapYear()
3420  {
3421  return self::checkLeapYear($this);
3422  }
3423 
3424 
3430  public function isToday()
3431  {
3432  $today = $this->date('Ymd', $this->_getTime());
3433  $day = $this->date('Ymd', $this->getUnixTimestamp());
3434  return ($today == $day);
3435  }
3436 
3437 
3443  public function isYesterday()
3444  {
3445  list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
3446  // adjusts for leap days and DST changes that are timezone specific
3447  $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
3448  $day = $this->date('Ymd', $this->getUnixTimestamp());
3449  return $day == $yesterday;
3450  }
3451 
3452 
3458  public function isTomorrow()
3459  {
3460  list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
3461  // adjusts for leap days and DST changes that are timezone specific
3462  $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
3463  $day = $this->date('Ymd', $this->getUnixTimestamp());
3464  return $day == $tomorrow;
3465  }
3466 
3473  public static function now($locale = null)
3474  {
3475  return new Zend_Date(time(), self::TIMESTAMP, $locale);
3476  }
3477 
3488  private function _calcdetail($calc, $date, $type, $locale)
3489  {
3490  $old = false;
3491  if (self::$_options['format_type'] == 'php') {
3492  self::$_options['format_type'] = 'iso';
3493  $old = true;
3494  }
3495 
3496  switch($calc) {
3497  case 'set' :
3498  $return = $this->set($date, $type, $locale);
3499  break;
3500  case 'add' :
3501  $return = $this->add($date, $type, $locale);
3502  break;
3503  case 'sub' :
3504  $return = $this->sub($date, $type, $locale);
3505  break;
3506  default :
3507  $return = $this->compare($date, $type, $locale);
3508  break;
3509  }
3510 
3511  if ($old) {
3512  self::$_options['format_type'] = 'php';
3513  }
3514 
3515  return $return;
3516  }
3517 
3529  private function _calcvalue($calc, $value, $type, $parameter, $locale)
3530  {
3531  if ($value === null) {
3532  #require_once 'Zend/Date/Exception.php';
3533  throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
3534  }
3535 
3536  if ($locale === null) {
3537  $locale = $this->getLocale();
3538  }
3539 
3540  if ($value instanceof Zend_Date) {
3541  // extract value from object
3542  $value = $value->toString($parameter, 'iso', $locale);
3543  } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
3544  #require_once 'Zend/Date/Exception.php';
3545  throw new Zend_Date_Exception("invalid $type ($value) operand", 0, null, $value);
3546  }
3547 
3548  $return = $this->_calcdetail($calc, $value, $parameter, $locale);
3549  if ($calc != 'cmp') {
3550  return $this;
3551  }
3552  return $return;
3553  }
3554 
3555 
3563  public function getYear($locale = null)
3564  {
3565  if (self::$_options['format_type'] == 'php') {
3566  $format = 'Y';
3567  } else {
3568  $format = self::YEAR;
3569  }
3570 
3571  return $this->copyPart($format, $locale);
3572  }
3573 
3574 
3588  public function setYear($year, $locale = null)
3589  {
3590  return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
3591  }
3592 
3593 
3607  public function addYear($year, $locale = null)
3608  {
3609  return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
3610  }
3611 
3612 
3626  public function subYear($year, $locale = null)
3627  {
3628  return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
3629  }
3630 
3631 
3642  public function compareYear($year, $locale = null)
3643  {
3644  return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
3645  }
3646 
3647 
3655  public function getMonth($locale = null)
3656  {
3657  if (self::$_options['format_type'] == 'php') {
3658  $format = 'm';
3659  } else {
3660  $format = self::MONTH;
3661  }
3662 
3663  return $this->copyPart($format, $locale);
3664  }
3665 
3666 
3676  private function _month($calc, $month, $locale)
3677  {
3678  if ($month === null) {
3679  #require_once 'Zend/Date/Exception.php';
3680  throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
3681  }
3682 
3683  if ($locale === null) {
3684  $locale = $this->getLocale();
3685  }
3686 
3687  if ($month instanceof Zend_Date) {
3688  // extract month from object
3689  $found = $month->toString(self::MONTH_SHORT, 'iso', $locale);
3690  } else {
3691  if (is_numeric($month)) {
3692  $found = $month;
3693  } else if (is_array($month)) {
3694  if (isset($month['month']) === true) {
3695  $month = $month['month'];
3696  } else {
3697  #require_once 'Zend/Date/Exception.php';
3698  throw new Zend_Date_Exception("no month given in array");
3699  }
3700  } else {
3701  $monthlist = Zend_Locale_Data::getList($locale, 'month');
3702  $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
3703 
3704  $monthlist = array_merge($monthlist, $monthlist2);
3705  $found = 0;
3706  $cnt = 0;
3707  foreach ($monthlist as $key => $value) {
3708  if (strtoupper($value) == strtoupper($month)) {
3709  $found = ($key % 12) + 1;
3710  break;
3711  }
3712  ++$cnt;
3713  }
3714  if ($found == 0) {
3715  foreach ($monthlist2 as $key => $value) {
3716  if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
3717  $found = $key + 1;
3718  break;
3719  }
3720  ++$cnt;
3721  }
3722  }
3723  if ($found == 0) {
3724  #require_once 'Zend/Date/Exception.php';
3725  throw new Zend_Date_Exception("unknown month name ($month)", 0, null, $month);
3726  }
3727  }
3728  }
3729  $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
3730  if ($calc != 'cmp') {
3731  return $this;
3732  }
3733  return $return;
3734  }
3735 
3736 
3750  public function setMonth($month, $locale = null)
3751  {
3752  return $this->_month('set', $month, $locale);
3753  }
3754 
3755 
3769  public function addMonth($month, $locale = null)
3770  {
3771  return $this->_month('add', $month, $locale);
3772  }
3773 
3774 
3788  public function subMonth($month, $locale = null)
3789  {
3790  return $this->_month('sub', $month, $locale);
3791  }
3792 
3793 
3804  public function compareMonth($month, $locale = null)
3805  {
3806  return $this->_month('cmp', $month, $locale);
3807  }
3808 
3809 
3817  public function getDay($locale = null)
3818  {
3819  return $this->copyPart(self::DAY_SHORT, $locale);
3820  }
3821 
3831  private function _day($calc, $day, $locale)
3832  {
3833  if ($day === null) {
3834  #require_once 'Zend/Date/Exception.php';
3835  throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
3836  }
3837 
3838  if ($locale === null) {
3839  $locale = $this->getLocale();
3840  }
3841 
3842  if ($day instanceof Zend_Date) {
3843  $day = $day->toString(self::DAY_SHORT, 'iso', $locale);
3844  }
3845 
3846  if (is_numeric($day)) {
3847  $type = self::DAY_SHORT;
3848  } else if (is_array($day)) {
3849  if (isset($day['day']) === true) {
3850  $day = $day['day'];
3851  $type = self::WEEKDAY;
3852  } else {
3853  #require_once 'Zend/Date/Exception.php';
3854  throw new Zend_Date_Exception("no day given in array");
3855  }
3856  } else {
3857  switch (iconv_strlen($day, 'UTF-8')) {
3858  case 1 :
3859  $type = self::WEEKDAY_NARROW;
3860  break;
3861  case 2:
3862  $type = self::WEEKDAY_NAME;
3863  break;
3864  case 3:
3865  $type = self::WEEKDAY_SHORT;
3866  break;
3867  default:
3868  $type = self::WEEKDAY;
3869  break;
3870  }
3871  }
3872  $return = $this->_calcdetail($calc, $day, $type, $locale);
3873  if ($calc != 'cmp') {
3874  return $this;
3875  }
3876  return $return;
3877  }
3878 
3879 
3894  public function setDay($day, $locale = null)
3895  {
3896  return $this->_day('set', $day, $locale);
3897  }
3898 
3899 
3912  public function addDay($day, $locale = null)
3913  {
3914  return $this->_day('add', $day, $locale);
3915  }
3916 
3917 
3930  public function subDay($day, $locale = null)
3931  {
3932  return $this->_day('sub', $day, $locale);
3933  }
3934 
3935 
3946  public function compareDay($day, $locale = null)
3947  {
3948  return $this->_day('cmp', $day, $locale);
3949  }
3950 
3951 
3960  public function getWeekday($locale = null)
3961  {
3962  if (self::$_options['format_type'] == 'php') {
3963  $format = 'l';
3964  } else {
3965  $format = self::WEEKDAY;
3966  }
3967 
3968  return $this->copyPart($format, $locale);
3969  }
3970 
3971 
3981  private function _weekday($calc, $weekday, $locale)
3982  {
3983  if ($weekday === null) {
3984  #require_once 'Zend/Date/Exception.php';
3985  throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
3986  }
3987 
3988  if ($locale === null) {
3989  $locale = $this->getLocale();
3990  }
3991 
3992  if ($weekday instanceof Zend_Date) {
3993  $weekday = $weekday->toString(self::WEEKDAY_8601, 'iso', $locale);
3994  }
3995 
3996  if (is_numeric($weekday)) {
3997  $type = self::WEEKDAY_8601;
3998  } else if (is_array($weekday)) {
3999  if (isset($weekday['weekday']) === true) {
4000  $weekday = $weekday['weekday'];
4001  $type = self::WEEKDAY;
4002  } else {
4003  #require_once 'Zend/Date/Exception.php';
4004  throw new Zend_Date_Exception("no weekday given in array");
4005  }
4006  } else {
4007  switch(iconv_strlen($weekday, 'UTF-8')) {
4008  case 1:
4009  $type = self::WEEKDAY_NARROW;
4010  break;
4011  case 2:
4012  $type = self::WEEKDAY_NAME;
4013  break;
4014  case 3:
4015  $type = self::WEEKDAY_SHORT;
4016  break;
4017  default:
4018  $type = self::WEEKDAY;
4019  break;
4020  }
4021  }
4022  $return = $this->_calcdetail($calc, $weekday, $type, $locale);
4023  if ($calc != 'cmp') {
4024  return $this;
4025  }
4026  return $return;
4027  }
4028 
4029 
4042  public function setWeekday($weekday, $locale = null)
4043  {
4044  return $this->_weekday('set', $weekday, $locale);
4045  }
4046 
4047 
4062  public function addWeekday($weekday, $locale = null)
4063  {
4064  return $this->_weekday('add', $weekday, $locale);
4065  }
4066 
4067 
4082  public function subWeekday($weekday, $locale = null)
4083  {
4084  return $this->_weekday('sub', $weekday, $locale);
4085  }
4086 
4087 
4098  public function compareWeekday($weekday, $locale = null)
4099  {
4100  return $this->_weekday('cmp', $weekday, $locale);
4101  }
4102 
4103 
4111  public function getDayOfYear($locale = null)
4112  {
4113  if (self::$_options['format_type'] == 'php') {
4114  $format = 'D';
4115  } else {
4116  $format = self::DAY_OF_YEAR;
4117  }
4118 
4119  return $this->copyPart($format, $locale);
4120  }
4121 
4122 
4134  public function setDayOfYear($day, $locale = null)
4135  {
4136  return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
4137  }
4138 
4139 
4151  public function addDayOfYear($day, $locale = null)
4152  {
4153  return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
4154  }
4155 
4156 
4168  public function subDayOfYear($day, $locale = null)
4169  {
4170  return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
4171  }
4172 
4173 
4184  public function compareDayOfYear($day, $locale = null)
4185  {
4186  return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
4187  }
4188 
4189 
4197  public function getHour($locale = null)
4198  {
4199  return $this->copyPart(self::HOUR, $locale);
4200  }
4201 
4202 
4214  public function setHour($hour, $locale = null)
4215  {
4216  return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
4217  }
4218 
4219 
4231  public function addHour($hour, $locale = null)
4232  {
4233  return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
4234  }
4235 
4236 
4248  public function subHour($hour, $locale = null)
4249  {
4250  return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
4251  }
4252 
4253 
4264  public function compareHour($hour, $locale = null)
4265  {
4266  return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
4267  }
4268 
4269 
4277  public function getMinute($locale = null)
4278  {
4279  if (self::$_options['format_type'] == 'php') {
4280  $format = 'i';
4281  } else {
4282  $format = self::MINUTE;
4283  }
4284 
4285  return $this->copyPart($format, $locale);
4286  }
4287 
4288 
4300  public function setMinute($minute, $locale = null)
4301  {
4302  return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
4303  }
4304 
4305 
4317  public function addMinute($minute, $locale = null)
4318  {
4319  return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
4320  }
4321 
4322 
4334  public function subMinute($minute, $locale = null)
4335  {
4336  return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
4337  }
4338 
4339 
4350  public function compareMinute($minute, $locale = null)
4351  {
4352  return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
4353  }
4354 
4355 
4363  public function getSecond($locale = null)
4364  {
4365  if (self::$_options['format_type'] == 'php') {
4366  $format = 's';
4367  } else {
4368  $format = self::SECOND;
4369  }
4370 
4371  return $this->copyPart($format, $locale);
4372  }
4373 
4374 
4386  public function setSecond($second, $locale = null)
4387  {
4388  return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
4389  }
4390 
4391 
4403  public function addSecond($second, $locale = null)
4404  {
4405  return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
4406  }
4407 
4408 
4420  public function subSecond($second, $locale = null)
4421  {
4422  return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
4423  }
4424 
4425 
4436  public function compareSecond($second, $locale = null)
4437  {
4438  return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
4439  }
4440 
4441 
4447  public function getFractionalPrecision()
4448  {
4449  return $this->_precision;
4450  }
4451 
4452 
4460  public function setFractionalPrecision($precision)
4461  {
4462  if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
4463  #require_once 'Zend/Date/Exception.php';
4464  throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
4465  }
4466 
4467  $this->_precision = (int) $precision;
4468  if ($this->_precision < strlen($this->_fractional)) {
4469  $this->_fractional = substr($this->_fractional, 0, $this->_precision);
4470  } else {
4471  $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_RIGHT);
4472  }
4473 
4474  return $this;
4475  }
4476 
4477 
4483  public function getMilliSecond()
4484  {
4485  return $this->_fractional;
4486  }
4487 
4497  public function setMilliSecond($milli = null, $precision = null)
4498  {
4499  if ($milli === null) {
4500  list($milli, $time) = explode(" ", microtime());
4501  $milli = intval($milli);
4502  $precision = 6;
4503  } else if (!is_numeric($milli)) {
4504  #require_once 'Zend/Date/Exception.php';
4505  throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
4506  }
4507 
4508  if ($precision === null) {
4509  $precision = $this->_precision;
4510  }
4511 
4512  if (!is_int($precision) || $precision < 1 || $precision > 9) {
4513  #require_once 'Zend/Date/Exception.php';
4514  throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
4515  }
4516 
4517  $this->_fractional = 0;
4518  $this->addMilliSecond($milli, $precision);
4519  return $this;
4520  }
4521 
4530  public function addMilliSecond($milli = null, $precision = null)
4531  {
4532  if ($milli === null) {
4533  list($milli, $time) = explode(" ", microtime());
4534  $milli = intval($milli);
4535  } else if (!is_numeric($milli)) {
4536  #require_once 'Zend/Date/Exception.php';
4537  throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
4538  }
4539 
4540  if ($precision === null) {
4541  // Use internal default precision
4542  // Is not as logic as using the length of the input. But this would break tests and maybe other things
4543  // as an input value of integer 10, which is used in tests, must be parsed as 10 milliseconds (real milliseconds, precision 3)
4544  // but with auto-detect of precision, 100 milliseconds would be added.
4545  $precision = $this->_precision;
4546  }
4547 
4548  if (!is_int($precision) || $precision < 1 || $precision > 9) {
4549  #require_once 'Zend/Date/Exception.php';
4550  throw new Zend_Date_Exception(
4551  "precision ($precision) must be a positive integer less than 10", 0, null, $precision
4552  );
4553  }
4554 
4555  if ($this->_precision > $precision) {
4556  $milli = $milli * pow(10, $this->_precision - $precision);
4557  } elseif ($this->_precision < $precision) {
4558  $milli = round($milli / pow(10, $precision - $this->_precision));
4559  }
4560 
4561  $this->_fractional += $milli;
4562 
4563  // Add/sub milliseconds + add/sub seconds
4564  $max = pow(10, $this->_precision);
4565  // Milli includes seconds
4566  if ($this->_fractional >= $max) {
4567  while ($this->_fractional >= $max) {
4568  $this->addSecond(1);
4569  $this->_fractional -= $max;
4570  }
4571  }
4572 
4573  if ($this->_fractional < 0) {
4574  while ($this->_fractional < 0) {
4575  $this->subSecond(1);
4576  $this->_fractional += $max;
4577  }
4578  }
4579 
4580  if ($this->_precision > strlen($this->_fractional)) {
4581  $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_LEFT);
4582  }
4583 
4584  return $this;
4585  }
4586 
4587 
4595  public function subMilliSecond($milli = null, $precision = null)
4596  {
4597  $this->addMilliSecond(0 - $milli, $precision);
4598  return $this;
4599  }
4600 
4609  public function compareMilliSecond($milli = null, $precision = null)
4610  {
4611  if ($milli === null) {
4612  list($milli, $time) = explode(" ", microtime());
4613  $milli = intval($milli);
4614  } else if (is_numeric($milli) === false) {
4615  #require_once 'Zend/Date/Exception.php';
4616  throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
4617  }
4618 
4619  if ($precision === null) {
4620  $precision = strlen($milli);
4621  } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
4622  #require_once 'Zend/Date/Exception.php';
4623  throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
4624  }
4625 
4626  if ($precision === 0) {
4627  #require_once 'Zend/Date/Exception.php';
4628  throw new Zend_Date_Exception('precision is 0');
4629  }
4630 
4631  if ($precision != $this->_precision) {
4632  if ($precision > $this->_precision) {
4633  $diff = $precision - $this->_precision;
4634  $milli = (int) ($milli / (10 * $diff));
4635  } else {
4636  $diff = $this->_precision - $precision;
4637  $milli = (int) ($milli * (10 * $diff));
4638  }
4639  }
4640 
4641  $comp = $this->_fractional - $milli;
4642  if ($comp < 0) {
4643  return -1;
4644  } else if ($comp > 0) {
4645  return 1;
4646  }
4647  return 0;
4648  }
4649 
4657  public function getWeek($locale = null)
4658  {
4659  if (self::$_options['format_type'] == 'php') {
4660  $format = 'W';
4661  } else {
4662  $format = self::WEEK;
4663  }
4664 
4665  return $this->copyPart($format, $locale);
4666  }
4667 
4678  public function setWeek($week, $locale = null)
4679  {
4680  return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
4681  }
4682 
4693  public function addWeek($week, $locale = null)
4694  {
4695  return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
4696  }
4697 
4708  public function subWeek($week, $locale = null)
4709  {
4710  return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
4711  }
4712 
4723  public function compareWeek($week, $locale = null)
4724  {
4725  return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
4726  }
4727 
4739  public function setLocale($locale = null)
4740  {
4741  try {
4742  $this->_locale = Zend_Locale::findLocale($locale);
4743  } catch (Zend_Locale_Exception $e) {
4744  #require_once 'Zend/Date/Exception.php';
4745  throw new Zend_Date_Exception($e->getMessage(), 0, $e);
4746  }
4747 
4748  return $this;
4749  }
4750 
4756  public function getLocale()
4757  {
4758  return $this->_locale;
4759  }
4760 
4773  public static function isDate($date, $format = null, $locale = null)
4774  {
4775  if (!is_string($date) && !is_numeric($date) && !($date instanceof Zend_Date) &&
4776  !is_array($date)) {
4777  return false;
4778  }
4779 
4780  if (($format !== null) && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
4781  && (Zend_Locale::isLocale($format, null, false))) {
4782  $locale = $format;
4783  $format = null;
4784  }
4785 
4786  $locale = Zend_Locale::findLocale($locale);
4787 
4788  if ($format === null) {
4790  } else if ((self::$_options['format_type'] == 'php') && !defined($format)) {
4792  }
4793 
4794  $format = self::_getLocalizedToken($format, $locale);
4795  if (!is_array($date)) {
4796  try {
4797  $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
4798  'date_format' => $format, 'format_type' => 'iso',
4799  'fix_date' => false));
4800  } catch (Zend_Locale_Exception $e) {
4801  // Date can not be parsed
4802  return false;
4803  }
4804  } else {
4805  $parsed = $date;
4806  }
4807 
4808  if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
4809  (!isset($parsed['year']))) {
4810  // Year expected but not found
4811  return false;
4812  }
4813 
4814  if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
4815  // Month expected but not found
4816  return false;
4817  }
4818 
4819  if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
4820  // Day expected but not found
4821  return false;
4822  }
4823 
4824  if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
4825  (!isset($parsed['hour']))) {
4826  // Hour expected but not found
4827  return false;
4828  }
4829 
4830  if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
4831  // Minute expected but not found
4832  return false;
4833  }
4834 
4835  if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
4836  // Second expected but not found
4837  return false;
4838  }
4839 
4840  // Set not given dateparts
4841  if (isset($parsed['hour']) === false) {
4842  $parsed['hour'] = 12;
4843  }
4844 
4845  if (isset($parsed['minute']) === false) {
4846  $parsed['minute'] = 0;
4847  }
4848 
4849  if (isset($parsed['second']) === false) {
4850  $parsed['second'] = 0;
4851  }
4852 
4853  if (isset($parsed['month']) === false) {
4854  $parsed['month'] = 1;
4855  }
4856 
4857  if (isset($parsed['day']) === false) {
4858  $parsed['day'] = 1;
4859  }
4860 
4861  if (isset($parsed['year']) === false) {
4862  $parsed['year'] = 1970;
4863  }
4864 
4865  if (self::isYearLeapYear($parsed['year'])) {
4866  $parsed['year'] = 1972;
4867  } else {
4868  $parsed['year'] = 1971;
4869  }
4870 
4871  $date = new self($parsed, null, $locale);
4872  $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
4873  $parsed['month'], $parsed['day'], $parsed['year']);
4874 
4875  if ($parsed['year'] != $date->date('Y', $timestamp)) {
4876  // Given year differs from parsed year
4877  return false;
4878  }
4879 
4880  if ($parsed['month'] != $date->date('n', $timestamp)) {
4881  // Given month differs from parsed month
4882  return false;
4883  }
4884 
4885  if ($parsed['day'] != $date->date('j', $timestamp)) {
4886  // Given day differs from parsed day
4887  return false;
4888  }
4889 
4890  if ($parsed['hour'] != $date->date('G', $timestamp)) {
4891  // Given hour differs from parsed hour
4892  return false;
4893  }
4894 
4895  if ($parsed['minute'] != $date->date('i', $timestamp)) {
4896  // Given minute differs from parsed minute
4897  return false;
4898  }
4899 
4900  if ($parsed['second'] != $date->date('s', $timestamp)) {
4901  // Given second differs from parsed second
4902  return false;
4903  }
4904 
4905  return true;
4906  }
4907 
4915  protected static function _getLocalizedToken($token, $locale)
4916  {
4917  switch($token) {
4918  case self::ISO_8601 :
4919  return "yyyy-MM-ddThh:mm:ss";
4920  break;
4921  case self::RFC_2822 :
4922  return "EEE, dd MMM yyyy HH:mm:ss";
4923  break;
4924  case self::DATES :
4925  return Zend_Locale_Data::getContent($locale, 'date');
4926  break;
4927  case self::DATE_FULL :
4928  return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
4929  break;
4930  case self::DATE_LONG :
4931  return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
4932  break;
4933  case self::DATE_MEDIUM :
4934  return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
4935  break;
4936  case self::DATE_SHORT :
4937  return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
4938  break;
4939  case self::TIMES :
4940  return Zend_Locale_Data::getContent($locale, 'time');
4941  break;
4942  case self::TIME_FULL :
4943  return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
4944  break;
4945  case self::TIME_LONG :
4946  return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
4947  break;
4948  case self::TIME_MEDIUM :
4949  return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
4950  break;
4951  case self::TIME_SHORT :
4952  return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
4953  break;
4954  case self::DATETIME :
4955  return Zend_Locale_Data::getContent($locale, 'datetime');
4956  break;
4957  case self::DATETIME_FULL :
4958  return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
4959  break;
4960  case self::DATETIME_LONG :
4961  return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
4962  break;
4963  case self::DATETIME_MEDIUM :
4964  return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
4965  break;
4966  case self::DATETIME_SHORT :
4967  return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
4968  break;
4969  case self::ATOM :
4970  case self::RFC_3339 :
4971  case self::W3C :
4972  return "yyyy-MM-DD HH:mm:ss";
4973  break;
4974  case self::COOKIE :
4975  case self::RFC_850 :
4976  return "EEEE, dd-MM-yyyy HH:mm:ss";
4977  break;
4978  case self::RFC_822 :
4979  case self::RFC_1036 :
4980  case self::RFC_1123 :
4981  case self::RSS :
4982  return "EEE, dd MM yyyy HH:mm:ss";
4983  break;
4984  }
4985 
4986  return $token;
4987  }
4988 
5005  protected function mktime($hour, $minute, $second, $month, $day, $year, $gmt = false)
5006  {
5007  $day = intval($day);
5008  $month = intval($month);
5009  $year = intval($year);
5010 
5011  // correct months > 12 and months < 1
5012  if ($month > 12) {
5013  $overlap = floor($month / 12);
5014  $year += $overlap;
5015  $month -= $overlap * 12;
5016  } else {
5017  $overlap = ceil((1 - $month) / 12);
5018  $year -= $overlap;
5019  $month += $overlap * 12;
5020  }
5021 
5022  if ($year > self::YEAR_MAX_VALUE || $year < self::YEAR_MIN_VALUE) {
5023  throw new Zend_Date_Exception('Invalid year, it must be between ' . self::YEAR_MIN_VALUE . ' and '
5024  . self::YEAR_MAX_VALUE);
5025  }
5026 
5027  return parent::mktime($hour, $minute, $second, $month, $day, $year, $gmt);
5028  }
5029 }
$old
Definition: website.php:27
static $sub
Definition: Math.php:41
calcSun($location, $horizon, $rise=false)
Definition: DateObject.php:913
static $comp
Definition: Math.php:45
date($format, $timestamp=null, $gmt=false)
Definition: DateObject.php:301
mktime($hour, $minute, $second, $month, $day, $year, $gmt=false)
Definition: DateObject.php:146
static convertPhpToIsoFormat($format)
Definition: Format.php:746
static getList($locale, $path, $value=false)
Definition: Data.php:318
static getDate($date, array $options=array())
Definition: Format.php:1154
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
return false
Definition: gallery.phtml:36
static _getTagSupportForCache()
static getContent($locale, $path, $value=false)
Definition: Data.php:968
setTimezone($zone=null)
static $add
Definition: Math.php:40
static getDateFormat($locale=null)
Definition: Format.php:1130
setUnixTimestamp($timestamp=null)
Definition: DateObject.php:79
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
static isLocale($locale, $strict=false, $compatible=true)
Definition: Locale.php:1683
static findLocale($locale=null)
Definition: Locale.php:1740
static getDateTimeFormat($locale=null)
Definition: Format.php:1269
getDateParts($timestamp=null, $fast=null)
Definition: DateObject.php:674
static getDateTime($datetime, array $options=array())
Definition: Format.php:1291
static setCache(Zend_Cache_Core $cache)
Definition: Data.php:1527
static getTime($time, array $options=array())
Definition: Format.php:1252
$i
Definition: gallery.phtml:31
static getTimeFormat($locale=null)
Definition: Format.php:1230
_getTime($sync=null)
Definition: DateObject.php:119
if(!isset($_GET['name'])) $name
Definition: log.php:14