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