PageRenderTime 69ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 1ms

/app/code/core/Zend/Date.php

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