PageRenderTime 74ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/mkrasuski/magento-ce
PHP | 5024 lines | 3099 code | 506 blank | 1419 comment | 647 complexity | 95bf218944785f606f22c2ea0c077f5d 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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  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-2015 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 a fluent 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 a fluent 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 a fluent 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 a fluent 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 a fluent 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 a fluent 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] <= 14) 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. // (T)hhmmss
  1826. if (empty($timematch)) {
  1827. preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
  1828. }
  1829. if (empty($datematch) and empty($timematch)) {
  1830. #require_once 'Zend/Date/Exception.php';
  1831. throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", 0, null, $date);
  1832. }
  1833. if (!empty($timematch)) {
  1834. $timeMatchCharCount = iconv_strlen($timematch[0], 'UTF-8');
  1835. $tmpdate = iconv_substr($tmpdate,
  1836. $timeMatchCharCount,
  1837. iconv_strlen($tmpdate, 'UTF-8') - $timeMatchCharCount,
  1838. 'UTF-8');
  1839. }
  1840. if (empty($datematch)) {
  1841. $datematch[1] = 1970;
  1842. $datematch[2] = 1;
  1843. $datematch[3] = 1;
  1844. } else if (iconv_strlen($datematch[1], 'UTF-8') == 2) {
  1845. $datematch[1] = self::getFullYear($datematch[1]);
  1846. }
  1847. if (empty($timematch)) {
  1848. $timematch[1] = 0;
  1849. $timematch[2] = 0;
  1850. $timematch[3] = 0;
  1851. }
  1852. if (!isset($timematch[3])) {
  1853. $timematch[3] = 0;
  1854. }
  1855. if (($calc == 'set') || ($calc == 'cmp')) {
  1856. --$datematch[2];
  1857. --$month;
  1858. --$datematch[3];
  1859. --$day;
  1860. $datematch[1] -= 1970;
  1861. $year -= 1970;
  1862. }
  1863. return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false),
  1864. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1865. break;
  1866. case self::RFC_2822:
  1867. $result = preg_match('/^\w{3},\s(\d{1,2})\s(\w{3})\s(\d{4})\s'
  1868. . '(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]'
  1869. . '{1}\d{4}|\w{1,20})$/', $date, $match);
  1870. if (!$result) {
  1871. #require_once 'Zend/Date/Exception.php';
  1872. throw new Zend_Date_Exception("no RFC 2822 format ($date)", 0, null, $date);
  1873. }
  1874. $months = $this->_getDigitFromName($match[2]);
  1875. if (($calc == 'set') || ($calc == 'cmp')) {
  1876. --$months;
  1877. --$month;
  1878. --$match[1];
  1879. --$day;
  1880. $match[3] -= 1970;
  1881. $year -= 1970;
  1882. }
  1883. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  1884. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1885. break;
  1886. case self::TIMESTAMP:
  1887. if (is_numeric($date)) {
  1888. return $this->_assign($calc, $date, $this->getUnixTimestamp());
  1889. }
  1890. #require_once 'Zend/Date/Exception.php';
  1891. throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", 0, null, $date);
  1892. break;
  1893. // additional formats
  1894. // break intentionally omitted
  1895. case self::ERA:
  1896. case self::ERA_NAME:
  1897. #require_once 'Zend/Date/Exception.php';
  1898. throw new Zend_Date_Exception('era not supported', 0, null, $date);
  1899. break;
  1900. case self::DATES:
  1901. try {
  1902. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  1903. if (($calc == 'set') || ($calc == 'cmp')) {
  1904. --$parsed['month'];
  1905. --$month;
  1906. --$parsed['day'];
  1907. --$day;
  1908. $parsed['year'] -= 1970;
  1909. $year -= 1970;
  1910. }
  1911. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1912. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1913. } catch (Zend_Locale_Exception $e) {
  1914. #require_once 'Zend/Date/Exception.php';
  1915. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1916. }
  1917. break;
  1918. case self::DATE_FULL:
  1919. try {
  1920. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  1921. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1922. if (($calc == 'set') || ($calc == 'cmp')) {
  1923. --$parsed['month'];
  1924. --$month;
  1925. --$parsed['day'];
  1926. --$day;
  1927. $parsed['year'] -= 1970;
  1928. $year -= 1970;
  1929. }
  1930. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1931. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1932. } catch (Zend_Locale_Exception $e) {
  1933. #require_once 'Zend/Date/Exception.php';
  1934. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1935. }
  1936. break;
  1937. case self::DATE_LONG:
  1938. try {
  1939. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  1940. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1941. if (($calc == 'set') || ($calc == 'cmp')){
  1942. --$parsed['month'];
  1943. --$month;
  1944. --$parsed['day'];
  1945. --$day;
  1946. $parsed['year'] -= 1970;
  1947. $year -= 1970;
  1948. }
  1949. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1950. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1951. } catch (Zend_Locale_Exception $e) {
  1952. #require_once 'Zend/Date/Exception.php';
  1953. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1954. }
  1955. break;
  1956. case self::DATE_MEDIUM:
  1957. try {
  1958. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  1959. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1960. if (($calc == 'set') || ($calc == 'cmp')) {
  1961. --$parsed['month'];
  1962. --$month;
  1963. --$parsed['day'];
  1964. --$day;
  1965. $parsed['year'] -= 1970;
  1966. $year -= 1970;
  1967. }
  1968. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1969. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1970. } catch (Zend_Locale_Exception $e) {
  1971. #require_once 'Zend/Date/Exception.php';
  1972. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1973. }
  1974. break;
  1975. case self::DATE_SHORT:
  1976. try {
  1977. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  1978. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1979. $parsed['year'] = self::getFullYear($parsed['year']);
  1980. if (($calc == 'set') || ($calc == 'cmp')) {
  1981. --$parsed['month'];
  1982. --$month;
  1983. --$parsed['day'];
  1984. --$day;
  1985. $parsed['year'] -= 1970;
  1986. $year -= 1970;
  1987. }
  1988. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1989. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1990. } catch (Zend_Locale_Exception $e) {
  1991. #require_once 'Zend/Date/Exception.php';
  1992. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1993. }
  1994. break;
  1995. case self::TIMES:
  1996. try {
  1997. if ($calc != 'set') {
  1998. $month = 1;
  1999. $day = 1;
  2000. $year = 1970;
  2001. }
  2002. $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  2003. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2004. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2005. } catch (Zend_Locale_Exception $e) {
  2006. #require_once 'Zend/Date/Exception.php';
  2007. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2008. }
  2009. break;
  2010. case self::TIME_FULL:
  2011. try {
  2012. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  2013. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2014. if ($calc != 'set') {
  2015. $month = 1;
  2016. $day = 1;
  2017. $year = 1970;
  2018. }
  2019. if (!isset($parsed['second'])) {
  2020. $parsed['second'] = 0;
  2021. }
  2022. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2023. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2024. } catch (Zend_Locale_Exception $e) {
  2025. #require_once 'Zend/Date/Exception.php';
  2026. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2027. }
  2028. break;
  2029. case self::TIME_LONG:
  2030. try {
  2031. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
  2032. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2033. if ($calc != 'set') {
  2034. $month = 1;
  2035. $day = 1;
  2036. $year = 1970;
  2037. }
  2038. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2039. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2040. } catch (Zend_Locale_Exception $e) {
  2041. #require_once 'Zend/Date/Exception.php';
  2042. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2043. }
  2044. break;
  2045. case self::TIME_MEDIUM:
  2046. try {
  2047. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
  2048. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2049. if ($calc != 'set') {
  2050. $month = 1;
  2051. $day = 1;
  2052. $year = 1970;
  2053. }
  2054. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2055. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2056. } catch (Zend_Locale_Exception $e) {
  2057. #require_once 'Zend/Date/Exception.php';
  2058. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2059. }
  2060. break;
  2061. case self::TIME_SHORT:
  2062. try {
  2063. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
  2064. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2065. if ($calc != 'set') {
  2066. $month = 1;
  2067. $day = 1;
  2068. $year = 1970;
  2069. }
  2070. if (!isset($parsed['second'])) {
  2071. $parsed['second'] = 0;
  2072. }
  2073. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2074. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2075. } catch (Zend_Locale_Exception $e) {
  2076. #require_once 'Zend/Date/Exception.php';
  2077. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2078. }
  2079. break;
  2080. case self::DATETIME:
  2081. try {
  2082. $parsed = Zend_Locale_Format::getDateTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  2083. if (($calc == 'set') || ($calc == 'cmp')) {
  2084. --$parsed['month'];
  2085. --$month;
  2086. --$parsed['day'];
  2087. --$day;
  2088. $parsed['year'] -= 1970;
  2089. $year -= 1970;
  2090. }
  2091. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2092. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2093. } catch (Zend_Locale_Exception $e) {
  2094. #require_once 'Zend/Date/Exception.php';
  2095. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2096. }
  2097. break;
  2098. case self::DATETIME_FULL:
  2099. try {
  2100. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
  2101. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2102. if (($calc == 'set') || ($calc == 'cmp')) {
  2103. --$parsed['month'];
  2104. --$month;
  2105. --$parsed['day'];
  2106. --$day;
  2107. $parsed['year'] -= 1970;
  2108. $year -= 1970;
  2109. }
  2110. if (!isset($parsed['second'])) {
  2111. $parsed['second'] = 0;
  2112. }
  2113. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2114. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2115. } catch (Zend_Locale_Exception $e) {
  2116. #require_once 'Zend/Date/Exception.php';
  2117. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2118. }
  2119. break;
  2120. case self::DATETIME_LONG:
  2121. try {
  2122. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
  2123. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2124. if (($calc == 'set') || ($calc == 'cmp')){
  2125. --$parsed['month'];
  2126. --$month;
  2127. --$parsed['day'];
  2128. --$day;
  2129. $parsed['year'] -= 1970;
  2130. $year -= 1970;
  2131. }
  2132. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2133. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2134. } catch (Zend_Locale_Exception $e) {
  2135. #require_once 'Zend/Date/Exception.php';
  2136. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2137. }
  2138. break;
  2139. case self::DATETIME_MEDIUM:
  2140. try {
  2141. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
  2142. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2143. if (($calc == 'set') || ($calc == 'cmp')) {
  2144. --$parsed['month'];
  2145. --$month;
  2146. --$parsed['day'];
  2147. --$day;
  2148. $parsed['year'] -= 1970;
  2149. $year -= 1970;
  2150. }
  2151. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2152. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2153. } catch (Zend_Locale_Exception $e) {
  2154. #require_once 'Zend/Date/Exception.php';
  2155. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2156. }
  2157. break;
  2158. case self::DATETIME_SHORT:
  2159. try {
  2160. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
  2161. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2162. $parsed['year'] = self::getFullYear($parsed['year']);
  2163. if (($calc == 'set') || ($calc == 'cmp')) {
  2164. --$parsed['month'];
  2165. --$month;
  2166. --$parsed['day'];
  2167. --$day;
  2168. $parsed['year'] -= 1970;
  2169. $year -= 1970;
  2170. }
  2171. if (!isset($parsed['second'])) {
  2172. $parsed['second'] = 0;
  2173. }
  2174. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2175. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2176. } catch (Zend_Locale_Exception $e) {
  2177. #require_once 'Zend/Date/Exception.php';
  2178. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2179. }
  2180. break;
  2181. // ATOM and RFC_3339 are identical
  2182. case self::ATOM:
  2183. case self::RFC_3339:
  2184. $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);
  2185. if (!$result) {
  2186. #require_once 'Zend/Date/Exception.php';
  2187. throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", 0, null, $date);
  2188. }
  2189. if (($calc == 'set') || ($calc == 'cmp')) {
  2190. --$match[2];
  2191. --$month;
  2192. --$match[3];
  2193. --$day;
  2194. $match[1] -= 1970;
  2195. $year -= 1970;
  2196. }
  2197. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2198. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2199. break;
  2200. case self::COOKIE:
  2201. $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);
  2202. if (!$result) {
  2203. #require_once 'Zend/Date/Exception.php';
  2204. throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", 0, null, $date);
  2205. }
  2206. $matchStartPos = iconv_strpos($match[0], ' ', 0, 'UTF-8') + 1;
  2207. $match[0] = iconv_substr($match[0],
  2208. $matchStartPos,
  2209. iconv_strlen($match[0], 'UTF-8') - $matchStartPos,
  2210. 'UTF-8');
  2211. $months = $this->_getDigitFromName($match[2]);
  2212. $match[3] = self::getFullYear($match[3]);
  2213. if (($calc == 'set') || ($calc == 'cmp')) {
  2214. --$months;
  2215. --$month;
  2216. --$match[1];
  2217. --$day;
  2218. $match[3] -= 1970;
  2219. $year -= 1970;
  2220. }
  2221. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2222. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2223. break;
  2224. case self::RFC_822:
  2225. case self::RFC_1036:
  2226. // new RFC 822 format, identical to RFC 1036 standard
  2227. $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);
  2228. if (!$result) {
  2229. #require_once 'Zend/Date/Exception.php';
  2230. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", 0, null, $date);
  2231. }
  2232. $months = $this->_getDigitFromName($match[2]);
  2233. $match[3] = self::getFullYear($match[3]);
  2234. if (($calc == 'set') || ($calc == 'cmp')) {
  2235. --$months;
  2236. --$month;
  2237. --$match[1];
  2238. --$day;
  2239. $match[3] -= 1970;
  2240. $year -= 1970;
  2241. }
  2242. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  2243. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  2244. break;
  2245. case self::RFC_850:
  2246. $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);
  2247. if (!$result) {
  2248. #require_once 'Zend/Date/Exception.php';
  2249. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", 0, null, $date);
  2250. }
  2251. $months = $this->_getDigitFromName($match[2]);
  2252. $match[3] = self::getFullYear($match[3]);
  2253. if (($calc == 'set') || ($calc == 'cmp')) {
  2254. --$months;
  2255. --$month;
  2256. --$match[1];
  2257. --$day;
  2258. $match[3] -= 1970;
  2259. $year -= 1970;
  2260. }
  2261. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2262. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2263. break;
  2264. case self::RFC_1123:
  2265. $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);
  2266. if (!$result) {
  2267. #require_once 'Zend/Date/Exception.php';
  2268. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", 0, null, $date);
  2269. }
  2270. $months = $this->_getDigitFromName($match[2]);
  2271. if (($calc == 'set') || ($calc == 'cmp')) {
  2272. --$months;
  2273. --$month;
  2274. --$match[1];
  2275. --$day;
  2276. $match[3] -= 1970;
  2277. $year -= 1970;
  2278. }
  2279. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2280. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2281. break;
  2282. case self::RSS:
  2283. $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);
  2284. if (!$result) {
  2285. #require_once 'Zend/Date/Exception.php';
  2286. throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", 0, null, $date);
  2287. }
  2288. $months = $this->_getDigitFromName($match[2]);
  2289. $match[3] = self::getFullYear($match[3]);
  2290. if (($calc == 'set') || ($calc == 'cmp')) {
  2291. --$months;
  2292. --$month;
  2293. --$match[1];
  2294. --$day;
  2295. $match[3] -= 1970;
  2296. $year -= 1970;
  2297. }
  2298. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2299. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2300. break;
  2301. case self::W3C:
  2302. $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
  2303. if (!$result) {
  2304. #require_once 'Zend/Date/Exception.php';
  2305. throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", 0, null, $date);
  2306. }
  2307. if (($calc == 'set') || ($calc == 'cmp')) {
  2308. --$match[2];
  2309. --$month;
  2310. --$match[3];
  2311. --$day;
  2312. $match[1] -= 1970;
  2313. $year -= 1970;
  2314. }
  2315. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2316. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2317. break;
  2318. default:
  2319. if (!is_numeric($date) || !empty($part)) {
  2320. try {
  2321. if (empty($part)) {
  2322. $part = Zend_Locale_Format::getDateFormat($locale) . " ";
  2323. $part .= Zend_Locale_Format::getTimeFormat($locale);
  2324. }
  2325. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
  2326. if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
  2327. $parsed['year'] = self::getFullYear($parsed['year']);
  2328. }
  2329. if (($calc == 'set') || ($calc == 'cmp')) {
  2330. if (isset($parsed['month'])) {
  2331. --$parsed['month'];
  2332. } else {
  2333. $parsed['month'] = 0;
  2334. }
  2335. if (isset($parsed['day'])) {
  2336. --$parsed['day'];
  2337. } else {
  2338. $parsed['day'] = 0;
  2339. }
  2340. if (!isset($parsed['year'])) {
  2341. $parsed['year'] = 1970;
  2342. }
  2343. }
  2344. return $this->_assign($calc, $this->mktime(
  2345. isset($parsed['hour']) ? $parsed['hour'] : 0,
  2346. isset($parsed['minute']) ? $parsed['minute'] : 0,
  2347. isset($parsed['second']) ? $parsed['second'] : 0,
  2348. isset($parsed['month']) ? (1 + $parsed['month']) : 1,
  2349. isset($parsed['day']) ? (1 + $parsed['day']) : 1,
  2350. $parsed['year'],
  2351. false), $this->getUnixTimestamp(), false);
  2352. } catch (Zend_Locale_Exception $e) {
  2353. if (!is_numeric($date)) {
  2354. #require_once 'Zend/Date/Exception.php';
  2355. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2356. }
  2357. }
  2358. }
  2359. return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
  2360. break;
  2361. }
  2362. }
  2363. /**
  2364. * Returns true when both date objects or date parts are equal.
  2365. * For example:
  2366. * 15.May.2000 <-> 15.June.2000 Equals only for Day or Year... all other will return false
  2367. *
  2368. * @param string|integer|array|Zend_Date $date Date or datepart to equal with
  2369. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2370. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2371. * @return boolean
  2372. * @throws Zend_Date_Exception
  2373. */
  2374. public function equals($date, $part = self::TIMESTAMP, $locale = null)
  2375. {
  2376. $result = $this->compare($date, $part, $locale);
  2377. if ($result == 0) {
  2378. return true;
  2379. }
  2380. return false;
  2381. }
  2382. /**
  2383. * Returns if the given date or datepart is earlier
  2384. * For example:
  2385. * 15.May.2000 <-> 13.June.1999 will return true for day, year and date, but not for month
  2386. *
  2387. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2388. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2389. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2390. * @return boolean
  2391. * @throws Zend_Date_Exception
  2392. */
  2393. public function isEarlier($date, $part = null, $locale = null)
  2394. {
  2395. $result = $this->compare($date, $part, $locale);
  2396. if ($result == -1) {
  2397. return true;
  2398. }
  2399. return false;
  2400. }
  2401. /**
  2402. * Returns if the given date or datepart is later
  2403. * For example:
  2404. * 15.May.2000 <-> 13.June.1999 will return true for month but false for day, year and date
  2405. * Returns if the given date is later
  2406. *
  2407. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2408. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2409. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2410. * @return boolean
  2411. * @throws Zend_Date_Exception
  2412. */
  2413. public function isLater($date, $part = null, $locale = null)
  2414. {
  2415. $result = $this->compare($date, $part, $locale);
  2416. if ($result == 1) {
  2417. return true;
  2418. }
  2419. return false;
  2420. }
  2421. /**
  2422. * Returns only the time of the date as new Zend_Date object
  2423. * For example:
  2424. * 15.May.2000 10:11:23 will return a dateobject equal to 01.Jan.1970 10:11:23
  2425. *
  2426. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2427. * @return Zend_Date
  2428. */
  2429. public function getTime($locale = null)
  2430. {
  2431. if (self::$_options['format_type'] == 'php') {
  2432. $format = 'H:i:s';
  2433. } else {
  2434. $format = self::TIME_MEDIUM;
  2435. }
  2436. return $this->copyPart($format, $locale);
  2437. }
  2438. /**
  2439. * Returns the calculated time
  2440. *
  2441. * @param string $calc Calculation to make
  2442. * @param string|integer|array|Zend_Date $time Time to calculate with, if null the actual time is taken
  2443. * @param string $format Timeformat for parsing input
  2444. * @param string|Zend_Locale $locale Locale for parsing input
  2445. * @return integer|Zend_Date new time
  2446. * @throws Zend_Date_Exception
  2447. */
  2448. private function _time($calc, $time, $format, $locale)
  2449. {
  2450. if ($time === null) {
  2451. #require_once 'Zend/Date/Exception.php';
  2452. throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
  2453. }
  2454. if ($time instanceof Zend_Date) {
  2455. // extract time from object
  2456. $time = $time->toString('HH:mm:ss', 'iso');
  2457. } else {
  2458. if (is_array($time)) {
  2459. if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
  2460. (isset($time['second']) === true)) {
  2461. $parsed = $time;
  2462. } else {
  2463. #require_once 'Zend/Date/Exception.php';
  2464. throw new Zend_Date_Exception("no hour, minute or second given in array");
  2465. }
  2466. } else {
  2467. if (self::$_options['format_type'] == 'php') {
  2468. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2469. }
  2470. try {
  2471. if ($locale === null) {
  2472. $locale = $this->getLocale();
  2473. }
  2474. $parsed = Zend_Locale_Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2475. } catch (Zend_Locale_Exception $e) {
  2476. #require_once 'Zend/Date/Exception.php';
  2477. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  2478. }
  2479. }
  2480. if (!array_key_exists('hour', $parsed)) {
  2481. $parsed['hour'] = 0;
  2482. }
  2483. if (!array_key_exists('minute', $parsed)) {
  2484. $parsed['minute'] = 0;
  2485. }
  2486. if (!array_key_exists('second', $parsed)) {
  2487. $parsed['second'] = 0;
  2488. }
  2489. $time = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT) . ":";
  2490. $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT) . ":";
  2491. $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT);
  2492. }
  2493. $return = $this->_calcdetail($calc, $time, self::TIMES, 'de');
  2494. if ($calc != 'cmp') {
  2495. return $this;
  2496. }
  2497. return $return;
  2498. }
  2499. /**
  2500. * Sets a new time for the date object. Format defines how to parse the time string.
  2501. * Also a complete date can be given, but only the time is used for setting.
  2502. * For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
  2503. * Returned is the new date object and the existing date is left as it was before
  2504. *
  2505. * @param string|integer|array|Zend_Date $time Time to set
  2506. * @param string $format OPTIONAL Timeformat for parsing input
  2507. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2508. * @return Zend_Date Provides a fluent interface
  2509. * @throws Zend_Date_Exception
  2510. */
  2511. public function setTime($time, $format = null, $locale = null)
  2512. {
  2513. return $this->_time('set', $time, $format, $locale);
  2514. }
  2515. /**
  2516. * Adds a time to the existing date. Format defines how to parse the time string.
  2517. * If only parts are given the other parts are set to 0.
  2518. * If no format is given, the standardformat of this locale is used.
  2519. * For example: HH:mm:ss -> 10 -> +10 hours
  2520. *
  2521. * @param string|integer|array|Zend_Date $time Time to add
  2522. * @param string $format OPTIONAL Timeformat for parsing input
  2523. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2524. * @return Zend_Date Provides a fluent interface
  2525. * @throws Zend_Date_Exception
  2526. */
  2527. public function addTime($time, $format = null, $locale = null)
  2528. {
  2529. return $this->_time('add', $time, $format, $locale);
  2530. }
  2531. /**
  2532. * Subtracts a time from the existing date. Format defines how to parse the time string.
  2533. * If only parts are given the other parts are set to 0.
  2534. * If no format is given, the standardformat of this locale is used.
  2535. * For example: HH:mm:ss -> 10 -> -10 hours
  2536. *
  2537. * @param string|integer|array|Zend_Date $time Time to sub
  2538. * @param string $format OPTIONAL Timeformat for parsing input
  2539. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2540. * @return Zend_Date Provides a fluent inteface
  2541. * @throws Zend_Date_Exception
  2542. */
  2543. public function subTime($time, $format = null, $locale = null)
  2544. {
  2545. return $this->_time('sub', $time, $format, $locale);
  2546. }
  2547. /**
  2548. * Compares the time from the existing date. Format defines how to parse the time string.
  2549. * If only parts are given the other parts are set to default.
  2550. * If no format us given, the standardformat of this locale is used.
  2551. * For example: HH:mm:ss -> 10 -> 10 hours
  2552. *
  2553. * @param string|integer|array|Zend_Date $time Time to compare
  2554. * @param string $format OPTIONAL Timeformat for parsing input
  2555. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2556. * @return integer 0 = equal, 1 = later, -1 = earlier
  2557. * @throws Zend_Date_Exception
  2558. */
  2559. public function compareTime($time, $format = null, $locale = null)
  2560. {
  2561. return $this->_time('cmp', $time, $format, $locale);
  2562. }
  2563. /**
  2564. * Returns a clone of $this, with the time part set to 00:00:00.
  2565. *
  2566. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2567. * @return Zend_Date
  2568. */
  2569. public function getDate($locale = null)
  2570. {
  2571. $orig = self::$_options['format_type'];
  2572. if (self::$_options['format_type'] == 'php') {
  2573. self::$_options['format_type'] = 'iso';
  2574. }
  2575. $date = $this->copyPart(self::DATE_MEDIUM, $locale);
  2576. $date->addTimestamp($this->getGmtOffset());
  2577. self::$_options['format_type'] = $orig;
  2578. return $date;
  2579. }
  2580. /**
  2581. * Returns the calculated date
  2582. *
  2583. * @param string $calc Calculation to make
  2584. * @param string|integer|array|Zend_Date $date Date to calculate with, if null the actual date is taken
  2585. * @param string $format Date format for parsing
  2586. * @param string|Zend_Locale $locale Locale for parsing input
  2587. * @return integer|Zend_Date new date
  2588. * @throws Zend_Date_Exception
  2589. */
  2590. private function _date($calc, $date, $format, $locale)
  2591. {
  2592. if ($date === null) {
  2593. #require_once 'Zend/Date/Exception.php';
  2594. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  2595. }
  2596. if ($date instanceof Zend_Date) {
  2597. // extract date from object
  2598. $date = $date->toString('d.M.y', 'iso');
  2599. } else {
  2600. if (is_array($date)) {
  2601. if ((isset($date['year']) === true) or (isset($date['month']) === true) or
  2602. (isset($date['day']) === true)) {
  2603. $parsed = $date;
  2604. } else {
  2605. #require_once 'Zend/Date/Exception.php';
  2606. throw new Zend_Date_Exception("no day,month or year given in array");
  2607. }
  2608. } else {
  2609. if ((self::$_options['format_type'] == 'php') && !defined($format)) {
  2610. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2611. }
  2612. try {
  2613. if ($locale === null) {
  2614. $locale = $this->getLocale();
  2615. }
  2616. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2617. if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
  2618. $parsed['year'] = self::getFullYear($parsed['year']);
  2619. }
  2620. } catch (Zend_Locale_Exception $e) {
  2621. #require_once 'Zend/Date/Exception.php';
  2622. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  2623. }
  2624. }
  2625. if (!array_key_exists('day', $parsed)) {
  2626. $parsed['day'] = 1;
  2627. }
  2628. if (!array_key_exists('month', $parsed)) {
  2629. $parsed['month'] = 1;
  2630. }
  2631. if (!array_key_exists('year', $parsed)) {
  2632. $parsed['year'] = 0;
  2633. }
  2634. $date = $parsed['day'] . "." . $parsed['month'] . "." . $parsed['year'];
  2635. }
  2636. $return = $this->_calcdetail($calc, $date, self::DATE_MEDIUM, 'de');
  2637. if ($calc != 'cmp') {
  2638. return $this;
  2639. }
  2640. return $return;
  2641. }
  2642. /**
  2643. * Sets a new date for the date object. Format defines how to parse the date string.
  2644. * Also a complete date with time can be given, but only the date is used for setting.
  2645. * For example: MMMM.yy HH:mm-> May.07 22:11 => 01.May.07 00:00
  2646. * Returned is the new date object and the existing time is left as it was before
  2647. *
  2648. * @param string|integer|array|Zend_Date $date Date to set
  2649. * @param string $format OPTIONAL Date format for parsing
  2650. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2651. * @return Zend_Date Provides a fluent interface
  2652. * @throws Zend_Date_Exception
  2653. */
  2654. public function setDate($date, $format = null, $locale = null)
  2655. {
  2656. return $this->_date('set', $date, $format, $locale);
  2657. }
  2658. /**
  2659. * Adds a date to the existing date object. Format defines how to parse the date string.
  2660. * If only parts are given the other parts are set to 0.
  2661. * If no format is given, the standardformat of this locale is used.
  2662. * For example: MM.dd.YYYY -> 10 -> +10 months
  2663. *
  2664. * @param string|integer|array|Zend_Date $date Date to add
  2665. * @param string $format OPTIONAL Date format for parsing input
  2666. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2667. * @return Zend_Date Provides a fluent interface
  2668. * @throws Zend_Date_Exception
  2669. */
  2670. public function addDate($date, $format = null, $locale = null)
  2671. {
  2672. return $this->_date('add', $date, $format, $locale);
  2673. }
  2674. /**
  2675. * Subtracts a date from the existing date object. Format defines how to parse the date string.
  2676. * If only parts are given the other parts are set to 0.
  2677. * If no format is given, the standardformat of this locale is used.
  2678. * For example: MM.dd.YYYY -> 10 -> -10 months
  2679. * Be aware: Subtracting 2 months is not equal to Adding -2 months !!!
  2680. *
  2681. * @param string|integer|array|Zend_Date $date Date to sub
  2682. * @param string $format OPTIONAL Date format for parsing input
  2683. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2684. * @return Zend_Date Provides a fluent interface
  2685. * @throws Zend_Date_Exception
  2686. */
  2687. public function subDate($date, $format = null, $locale = null)
  2688. {
  2689. return $this->_date('sub', $date, $format, $locale);
  2690. }
  2691. /**
  2692. * Compares the date from the existing date object, ignoring the time.
  2693. * Format defines how to parse the date string.
  2694. * If only parts are given the other parts are set to 0.
  2695. * If no format is given, the standardformat of this locale is used.
  2696. * For example: 10.01.2000 => 10.02.1999 -> false
  2697. *
  2698. * @param string|integer|array|Zend_Date $date Date to compare
  2699. * @param string $format OPTIONAL Date format for parsing input
  2700. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2701. * @return integer 0 = equal, 1 = later, -1 = earlier
  2702. * @throws Zend_Date_Exception
  2703. */
  2704. public function compareDate($date, $format = null, $locale = null)
  2705. {
  2706. return $this->_date('cmp', $date, $format, $locale);
  2707. }
  2708. /**
  2709. * Returns the full ISO 8601 date from the date object.
  2710. * Always the complete ISO 8601 specifiction is used. If an other ISO date is needed
  2711. * (ISO 8601 defines several formats) use toString() instead.
  2712. * This function does not return the ISO date as object. Use copy() instead.
  2713. *
  2714. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2715. * @return string
  2716. */
  2717. public function getIso($locale = null)
  2718. {
  2719. return $this->toString(self::ISO_8601, 'iso', $locale);
  2720. }
  2721. /**
  2722. * Sets a new date for the date object. Not given parts are set to default.
  2723. * Only supported ISO 8601 formats are accepted.
  2724. * For example: 050901 -> 01.Sept.2005 00:00:00, 20050201T10:00:30 -> 01.Feb.2005 10h00m30s
  2725. * Returned is the new date object
  2726. *
  2727. * @param string|integer|Zend_Date $date ISO Date to set
  2728. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2729. * @return Zend_Date Provides a fluent interface
  2730. * @throws Zend_Date_Exception
  2731. */
  2732. public function setIso($date, $locale = null)
  2733. {
  2734. return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
  2735. }
  2736. /**
  2737. * Adds a ISO date to the date object. Not given parts are set to default.
  2738. * Only supported ISO 8601 formats are accepted.
  2739. * For example: 050901 -> + 01.Sept.2005 00:00:00, 10:00:00 -> +10h
  2740. * Returned is the new date object
  2741. *
  2742. * @param string|integer|Zend_Date $date ISO Date to add
  2743. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2744. * @return Zend_Date Provides a fluent interface
  2745. * @throws Zend_Date_Exception
  2746. */
  2747. public function addIso($date, $locale = null)
  2748. {
  2749. return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
  2750. }
  2751. /**
  2752. * Subtracts a ISO date from the date object. Not given parts are set to default.
  2753. * Only supported ISO 8601 formats are accepted.
  2754. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2755. * Returned is the new date object
  2756. *
  2757. * @param string|integer|Zend_Date $date ISO Date to sub
  2758. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2759. * @return Zend_Date Provides a fluent interface
  2760. * @throws Zend_Date_Exception
  2761. */
  2762. public function subIso($date, $locale = null)
  2763. {
  2764. return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
  2765. }
  2766. /**
  2767. * Compares a ISO date with the date object. Not given parts are set to default.
  2768. * Only supported ISO 8601 formats are accepted.
  2769. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2770. * Returns if equal, earlier or later
  2771. *
  2772. * @param string|integer|Zend_Date $date ISO Date to sub
  2773. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2774. * @return integer 0 = equal, 1 = later, -1 = earlier
  2775. * @throws Zend_Date_Exception
  2776. */
  2777. public function compareIso($date, $locale = null)
  2778. {
  2779. return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
  2780. }
  2781. /**
  2782. * Returns a RFC 822 compilant datestring from the date object.
  2783. * This function does not return the RFC date as object. Use copy() instead.
  2784. *
  2785. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2786. * @return string
  2787. */
  2788. public function getArpa($locale = null)
  2789. {
  2790. if (self::$_options['format_type'] == 'php') {
  2791. $format = 'D\, d M y H\:i\:s O';
  2792. } else {
  2793. $format = self::RFC_822;
  2794. }
  2795. return $this->toString($format, 'iso', $locale);
  2796. }
  2797. /**
  2798. * Sets a RFC 822 date as new date for the date object.
  2799. * Only RFC 822 compilant date strings are accepted.
  2800. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2801. * Returned is the new date object
  2802. *
  2803. * @param string|integer|Zend_Date $date RFC 822 to set
  2804. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2805. * @return Zend_Date Provides a fluent interface
  2806. * @throws Zend_Date_Exception
  2807. */
  2808. public function setArpa($date, $locale = null)
  2809. {
  2810. return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
  2811. }
  2812. /**
  2813. * Adds a RFC 822 date to the date object.
  2814. * ARPA messages are used in emails or HTTP Headers.
  2815. * Only RFC 822 compilant date strings are accepted.
  2816. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2817. * Returned is the new date object
  2818. *
  2819. * @param string|integer|Zend_Date $date RFC 822 Date to add
  2820. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2821. * @return Zend_Date Provides a fluent interface
  2822. * @throws Zend_Date_Exception
  2823. */
  2824. public function addArpa($date, $locale = null)
  2825. {
  2826. return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
  2827. }
  2828. /**
  2829. * Subtracts a RFC 822 date from the date object.
  2830. * ARPA messages are used in emails or HTTP Headers.
  2831. * Only RFC 822 compilant date strings are accepted.
  2832. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2833. * Returned is the new date object
  2834. *
  2835. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2836. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2837. * @return Zend_Date Provides a fluent interface
  2838. * @throws Zend_Date_Exception
  2839. */
  2840. public function subArpa($date, $locale = null)
  2841. {
  2842. return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
  2843. }
  2844. /**
  2845. * Compares a RFC 822 compilant date with the date object.
  2846. * ARPA messages are used in emails or HTTP Headers.
  2847. * Only RFC 822 compilant date strings are accepted.
  2848. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2849. * Returns if equal, earlier or later
  2850. *
  2851. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2852. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2853. * @return integer 0 = equal, 1 = later, -1 = earlier
  2854. * @throws Zend_Date_Exception
  2855. */
  2856. public function compareArpa($date, $locale = null)
  2857. {
  2858. return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
  2859. }
  2860. /**
  2861. * Check if location is supported
  2862. *
  2863. * @param array $location locations array
  2864. * @throws Zend_Date_Exception
  2865. * @return float $horizon float
  2866. */
  2867. private function _checkLocation($location)
  2868. {
  2869. if (!isset($location['longitude']) or !isset($location['latitude'])) {
  2870. #require_once 'Zend/Date/Exception.php';
  2871. throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', 0, null, $location);
  2872. }
  2873. if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
  2874. #require_once 'Zend/Date/Exception.php';
  2875. throw new Zend_Date_Exception('Longitude must be between -180 and 180', 0, null, $location);
  2876. }
  2877. if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
  2878. #require_once 'Zend/Date/Exception.php';
  2879. throw new Zend_Date_Exception('Latitude must be between -90 and 90', 0, null, $location);
  2880. }
  2881. if (!isset($location['horizon'])){
  2882. $location['horizon'] = 'effective';
  2883. }
  2884. switch ($location['horizon']) {
  2885. case 'civil' :
  2886. return -0.104528;
  2887. break;
  2888. case 'nautic' :
  2889. return -0.207912;
  2890. break;
  2891. case 'astronomic' :
  2892. return -0.309017;
  2893. break;
  2894. default :
  2895. return -0.0145439;
  2896. break;
  2897. }
  2898. }
  2899. /**
  2900. * Returns the time of sunrise for this date and a given location as new date object
  2901. * For a list of cities and correct locations use the class Zend_Date_Cities
  2902. *
  2903. * @param array $location location of sunrise
  2904. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2905. * ['longitude'] -> longitude of location
  2906. * ['latitude'] -> latitude of location
  2907. * @return Zend_Date
  2908. * @throws Zend_Date_Exception
  2909. */
  2910. public function getSunrise($location)
  2911. {
  2912. $horizon = $this->_checkLocation($location);
  2913. $result = clone $this;
  2914. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2915. return $result;
  2916. }
  2917. /**
  2918. * Returns the time of sunset for this date and a given location as new date object
  2919. * For a list of cities and correct locations use the class Zend_Date_Cities
  2920. *
  2921. * @param array $location location of sunset
  2922. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2923. * ['longitude'] -> longitude of location
  2924. * ['latitude'] -> latitude of location
  2925. * @return Zend_Date
  2926. * @throws Zend_Date_Exception
  2927. */
  2928. public function getSunset($location)
  2929. {
  2930. $horizon = $this->_checkLocation($location);
  2931. $result = clone $this;
  2932. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2933. return $result;
  2934. }
  2935. /**
  2936. * Returns an array with the sunset and sunrise dates for all horizon types
  2937. * For a list of cities and correct locations use the class Zend_Date_Cities
  2938. *
  2939. * @param array $location location of suninfo
  2940. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2941. * ['longitude'] -> longitude of location
  2942. * ['latitude'] -> latitude of location
  2943. * @return array - [sunset|sunrise][effective|civil|nautic|astronomic]
  2944. * @throws Zend_Date_Exception
  2945. */
  2946. public function getSunInfo($location)
  2947. {
  2948. $suninfo = array();
  2949. for ($i = 0; $i < 4; ++$i) {
  2950. switch ($i) {
  2951. case 0 :
  2952. $location['horizon'] = 'effective';
  2953. break;
  2954. case 1 :
  2955. $location['horizon'] = 'civil';
  2956. break;
  2957. case 2 :
  2958. $location['horizon'] = 'nautic';
  2959. break;
  2960. case 3 :
  2961. $location['horizon'] = 'astronomic';
  2962. break;
  2963. }
  2964. $horizon = $this->_checkLocation($location);
  2965. $result = clone $this;
  2966. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2967. $suninfo['sunrise'][$location['horizon']] = $result;
  2968. $result = clone $this;
  2969. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2970. $suninfo['sunset'][$location['horizon']] = $result;
  2971. }
  2972. return $suninfo;
  2973. }
  2974. /**
  2975. * Check a given year for leap year.
  2976. *
  2977. * @param integer|array|Zend_Date $year Year to check
  2978. * @throws Zend_Date_Exception
  2979. * @return boolean
  2980. */
  2981. public static function checkLeapYear($year)
  2982. {
  2983. if ($year instanceof Zend_Date) {
  2984. $year = (int) $year->toString(self::YEAR, 'iso');
  2985. }
  2986. if (is_array($year)) {
  2987. if (isset($year['year']) === true) {
  2988. $year = $year['year'];
  2989. } else {
  2990. #require_once 'Zend/Date/Exception.php';
  2991. throw new Zend_Date_Exception("no year given in array");
  2992. }
  2993. }
  2994. if (!is_numeric($year)) {
  2995. #require_once 'Zend/Date/Exception.php';
  2996. throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", 0, null, $year);
  2997. }
  2998. return (bool) parent::isYearLeapYear($year);
  2999. }
  3000. /**
  3001. * Returns true, if the year is a leap year.
  3002. *
  3003. * @return boolean
  3004. */
  3005. public function isLeapYear()
  3006. {
  3007. return self::checkLeapYear($this);
  3008. }
  3009. /**
  3010. * Returns if the set date is todays date
  3011. *
  3012. * @return boolean
  3013. */
  3014. public function isToday()
  3015. {
  3016. $today = $this->date('Ymd', $this->_getTime());
  3017. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3018. return ($today == $day);
  3019. }
  3020. /**
  3021. * Returns if the set date is yesterdays date
  3022. *
  3023. * @return boolean
  3024. */
  3025. public function isYesterday()
  3026. {
  3027. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  3028. // adjusts for leap days and DST changes that are timezone specific
  3029. $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
  3030. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3031. return $day == $yesterday;
  3032. }
  3033. /**
  3034. * Returns if the set date is tomorrows date
  3035. *
  3036. * @return boolean
  3037. */
  3038. public function isTomorrow()
  3039. {
  3040. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  3041. // adjusts for leap days and DST changes that are timezone specific
  3042. $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
  3043. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3044. return $day == $tomorrow;
  3045. }
  3046. /**
  3047. * Returns the actual date as new date object
  3048. *
  3049. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3050. * @return Zend_Date
  3051. */
  3052. public static function now($locale = null)
  3053. {
  3054. return new Zend_Date(time(), self::TIMESTAMP, $locale);
  3055. }
  3056. /**
  3057. * Calculate date details
  3058. *
  3059. * @param string $calc Calculation to make
  3060. * @param string|integer|array|Zend_Date $date Date or Part to calculate
  3061. * @param string $type Datepart for Calculation
  3062. * @param string|Zend_Locale $locale Locale for parsing input
  3063. * @return integer|string new date
  3064. * @throws Zend_Date_Exception
  3065. */
  3066. private function _calcdetail($calc, $date, $type, $locale)
  3067. {
  3068. $old = false;
  3069. if (self::$_options['format_type'] == 'php') {
  3070. self::$_options['format_type'] = 'iso';
  3071. $old = true;
  3072. }
  3073. switch($calc) {
  3074. case 'set' :
  3075. $return = $this->set($date, $type, $locale);
  3076. break;
  3077. case 'add' :
  3078. $return = $this->add($date, $type, $locale);
  3079. break;
  3080. case 'sub' :
  3081. $return = $this->sub($date, $type, $locale);
  3082. break;
  3083. default :
  3084. $return = $this->compare($date, $type, $locale);
  3085. break;
  3086. }
  3087. if ($old) {
  3088. self::$_options['format_type'] = 'php';
  3089. }
  3090. return $return;
  3091. }
  3092. /**
  3093. * Internal calculation, returns the requested date type
  3094. *
  3095. * @param string $calc Calculation to make
  3096. * @param string|integer|Zend_Date $value Datevalue to calculate with, if null the actual value is taken
  3097. * @param string $type
  3098. * @param string $parameter
  3099. * @param string|Zend_Locale $locale Locale for parsing input
  3100. * @throws Zend_Date_Exception
  3101. * @return integer|Zend_Date new date
  3102. */
  3103. private function _calcvalue($calc, $value, $type, $parameter, $locale)
  3104. {
  3105. if ($value === null) {
  3106. #require_once 'Zend/Date/Exception.php';
  3107. throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
  3108. }
  3109. if ($locale === null) {
  3110. $locale = $this->getLocale();
  3111. }
  3112. if ($value instanceof Zend_Date) {
  3113. // extract value from object
  3114. $value = $value->toString($parameter, 'iso', $locale);
  3115. } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
  3116. #require_once 'Zend/Date/Exception.php';
  3117. throw new Zend_Date_Exception("invalid $type ($value) operand", 0, null, $value);
  3118. }
  3119. $return = $this->_calcdetail($calc, $value, $parameter, $locale);
  3120. if ($calc != 'cmp') {
  3121. return $this;
  3122. }
  3123. return $return;
  3124. }
  3125. /**
  3126. * Returns only the year from the date object as new object.
  3127. * For example: 10.May.2000 10:30:00 -> 01.Jan.2000 00:00:00
  3128. *
  3129. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3130. * @return Zend_Date
  3131. */
  3132. public function getYear($locale = null)
  3133. {
  3134. if (self::$_options['format_type'] == 'php') {
  3135. $format = 'Y';
  3136. } else {
  3137. $format = self::YEAR;
  3138. }
  3139. return $this->copyPart($format, $locale);
  3140. }
  3141. /**
  3142. * Sets a new year
  3143. * If the year is between 0 and 69, 2000 will be set (2000-2069)
  3144. * If the year if between 70 and 99, 1999 will be set (1970-1999)
  3145. * 3 or 4 digit years are set as expected. If you need to set year 0-99
  3146. * use set() instead.
  3147. * Returned is the new date object
  3148. *
  3149. * @param string|integer|array|Zend_Date $year Year to set
  3150. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3151. * @return Zend_Date Provides a fluent interface
  3152. * @throws Zend_Date_Exception
  3153. */
  3154. public function setYear($year, $locale = null)
  3155. {
  3156. return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
  3157. }
  3158. /**
  3159. * Adds the year to the existing date object
  3160. * If the year is between 0 and 69, 2000 will be added (2000-2069)
  3161. * If the year if between 70 and 99, 1999 will be added (1970-1999)
  3162. * 3 or 4 digit years are added as expected. If you need to add years from 0-99
  3163. * use add() instead.
  3164. * Returned is the new date object
  3165. *
  3166. * @param string|integer|array|Zend_Date $year Year to add
  3167. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3168. * @return Zend_Date Provides a fluent interface
  3169. * @throws Zend_Date_Exception
  3170. */
  3171. public function addYear($year, $locale = null)
  3172. {
  3173. return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
  3174. }
  3175. /**
  3176. * Subs the year from the existing date object
  3177. * If the year is between 0 and 69, 2000 will be subtracted (2000-2069)
  3178. * If the year if between 70 and 99, 1999 will be subtracted (1970-1999)
  3179. * 3 or 4 digit years are subtracted as expected. If you need to subtract years from 0-99
  3180. * use sub() instead.
  3181. * Returned is the new date object
  3182. *
  3183. * @param string|integer|array|Zend_Date $year Year to sub
  3184. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3185. * @return Zend_Date Provides a fluent interface
  3186. * @throws Zend_Date_Exception
  3187. */
  3188. public function subYear($year, $locale = null)
  3189. {
  3190. return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
  3191. }
  3192. /**
  3193. * Compares the year with the existing date object, ignoring other date parts.
  3194. * For example: 10.03.2000 -> 15.02.2000 -> true
  3195. * Returns if equal, earlier or later
  3196. *
  3197. * @param string|integer|array|Zend_Date $year Year to compare
  3198. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3199. * @return integer 0 = equal, 1 = later, -1 = earlier
  3200. * @throws Zend_Date_Exception
  3201. */
  3202. public function compareYear($year, $locale = null)
  3203. {
  3204. return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
  3205. }
  3206. /**
  3207. * Returns only the month from the date object as new object.
  3208. * For example: 10.May.2000 10:30:00 -> 01.May.1970 00:00:00
  3209. *
  3210. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3211. * @return Zend_Date
  3212. */
  3213. public function getMonth($locale = null)
  3214. {
  3215. if (self::$_options['format_type'] == 'php') {
  3216. $format = 'm';
  3217. } else {
  3218. $format = self::MONTH;
  3219. }
  3220. return $this->copyPart($format, $locale);
  3221. }
  3222. /**
  3223. * Returns the calculated month
  3224. *
  3225. * @param string $calc Calculation to make
  3226. * @param string|integer|array|Zend_Date $month Month to calculate with, if null the actual month is taken
  3227. * @param string|Zend_Locale $locale Locale for parsing input
  3228. * @return integer|Zend_Date new time
  3229. * @throws Zend_Date_Exception
  3230. */
  3231. private function _month($calc, $month, $locale)
  3232. {
  3233. if ($month === null) {
  3234. #require_once 'Zend/Date/Exception.php';
  3235. throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
  3236. }
  3237. if ($locale === null) {
  3238. $locale = $this->getLocale();
  3239. }
  3240. if ($month instanceof Zend_Date) {
  3241. // extract month from object
  3242. $found = $month->toString(self::MONTH_SHORT, 'iso', $locale);
  3243. } else {
  3244. if (is_numeric($month)) {
  3245. $found = $month;
  3246. } else if (is_array($month)) {
  3247. if (isset($month['month']) === true) {
  3248. $month = $month['month'];
  3249. } else {
  3250. #require_once 'Zend/Date/Exception.php';
  3251. throw new Zend_Date_Exception("no month given in array");
  3252. }
  3253. } else {
  3254. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  3255. $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  3256. $monthlist = array_merge($monthlist, $monthlist2);
  3257. $found = 0;
  3258. $cnt = 0;
  3259. foreach ($monthlist as $key => $value) {
  3260. if (strtoupper($value) == strtoupper($month)) {
  3261. $found = ($key % 12) + 1;
  3262. break;
  3263. }
  3264. ++$cnt;
  3265. }
  3266. if ($found == 0) {
  3267. foreach ($monthlist2 as $key => $value) {
  3268. if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
  3269. $found = $key + 1;
  3270. break;
  3271. }
  3272. ++$cnt;
  3273. }
  3274. }
  3275. if ($found == 0) {
  3276. #require_once 'Zend/Date/Exception.php';
  3277. throw new Zend_Date_Exception("unknown month name ($month)", 0, null, $month);
  3278. }
  3279. }
  3280. }
  3281. $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
  3282. if ($calc != 'cmp') {
  3283. return $this;
  3284. }
  3285. return $return;
  3286. }
  3287. /**
  3288. * Sets a new month
  3289. * The month can be a number or a string. Setting months lower then 0 and greater then 12
  3290. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3291. * If a localized monthname is given it will be parsed with the default locale or the optional
  3292. * set locale.
  3293. * Returned is the new date object
  3294. *
  3295. * @param string|integer|array|Zend_Date $month Month to set
  3296. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3297. * @return Zend_Date Provides a fluent interface
  3298. * @throws Zend_Date_Exception
  3299. */
  3300. public function setMonth($month, $locale = null)
  3301. {
  3302. return $this->_month('set', $month, $locale);
  3303. }
  3304. /**
  3305. * Adds months to the existing date object.
  3306. * The month can be a number or a string. Adding months lower then 0 and greater then 12
  3307. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3308. * If a localized monthname is given it will be parsed with the default locale or the optional
  3309. * set locale.
  3310. * Returned is the new date object
  3311. *
  3312. * @param string|integer|array|Zend_Date $month Month to add
  3313. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3314. * @return Zend_Date Provides a fluent interface
  3315. * @throws Zend_Date_Exception
  3316. */
  3317. public function addMonth($month, $locale = null)
  3318. {
  3319. return $this->_month('add', $month, $locale);
  3320. }
  3321. /**
  3322. * Subtracts months from the existing date object.
  3323. * The month can be a number or a string. Subtracting months lower then 0 and greater then 12
  3324. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3325. * If a localized monthname is given it will be parsed with the default locale or the optional
  3326. * set locale.
  3327. * Returned is the new date object
  3328. *
  3329. * @param string|integer|array|Zend_Date $month Month to sub
  3330. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3331. * @return Zend_Date Provides a fluent interface
  3332. * @throws Zend_Date_Exception
  3333. */
  3334. public function subMonth($month, $locale = null)
  3335. {
  3336. return $this->_month('sub', $month, $locale);
  3337. }
  3338. /**
  3339. * Compares the month with the existing date object, ignoring other date parts.
  3340. * For example: 10.03.2000 -> 15.03.1950 -> true
  3341. * Returns if equal, earlier or later
  3342. *
  3343. * @param string|integer|array|Zend_Date $month Month to compare
  3344. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3345. * @return integer 0 = equal, 1 = later, -1 = earlier
  3346. * @throws Zend_Date_Exception
  3347. */
  3348. public function compareMonth($month, $locale = null)
  3349. {
  3350. return $this->_month('cmp', $month, $locale);
  3351. }
  3352. /**
  3353. * Returns the day as new date object
  3354. * Example: 20.May.1986 -> 20.Jan.1970 00:00:00
  3355. *
  3356. * @param Zend_Locale $locale OPTIONAL Locale for parsing input
  3357. * @return Zend_Date
  3358. */
  3359. public function getDay($locale = null)
  3360. {
  3361. return $this->copyPart(self::DAY_SHORT, $locale);
  3362. }
  3363. /**
  3364. * Returns the calculated day
  3365. *
  3366. * @param string $calc Type of calculation to make
  3367. * @param Zend_Date $day Day to calculate, when null the actual day is calculated
  3368. * @param Zend_Locale $locale Locale for parsing input
  3369. * @throws Zend_Date_Exception
  3370. * @return Zend_Date|integer
  3371. */
  3372. private function _day($calc, $day, $locale)
  3373. {
  3374. if ($day === null) {
  3375. #require_once 'Zend/Date/Exception.php';
  3376. throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
  3377. }
  3378. if ($locale === null) {
  3379. $locale = $this->getLocale();
  3380. }
  3381. if ($day instanceof Zend_Date) {
  3382. $day = $day->toString(self::DAY_SHORT, 'iso', $locale);
  3383. }
  3384. if (is_numeric($day)) {
  3385. $type = self::DAY_SHORT;
  3386. } else if (is_array($day)) {
  3387. if (isset($day['day']) === true) {
  3388. $day = $day['day'];
  3389. $type = self::WEEKDAY;
  3390. } else {
  3391. #require_once 'Zend/Date/Exception.php';
  3392. throw new Zend_Date_Exception("no day given in array");
  3393. }
  3394. } else {
  3395. switch (iconv_strlen($day, 'UTF-8')) {
  3396. case 1 :
  3397. $type = self::WEEKDAY_NARROW;
  3398. break;
  3399. case 2:
  3400. $type = self::WEEKDAY_NAME;
  3401. break;
  3402. case 3:
  3403. $type = self::WEEKDAY_SHORT;
  3404. break;
  3405. default:
  3406. $type = self::WEEKDAY;
  3407. break;
  3408. }
  3409. }
  3410. $return = $this->_calcdetail($calc, $day, $type, $locale);
  3411. if ($calc != 'cmp') {
  3412. return $this;
  3413. }
  3414. return $return;
  3415. }
  3416. /**
  3417. * Sets a new day
  3418. * The day can be a number or a string. Setting days lower then 0 or greater than the number of this months days
  3419. * will result in adding or subtracting the relevant month.
  3420. * If a localized dayname is given it will be parsed with the default locale or the optional
  3421. * set locale.
  3422. * Returned is the new date object
  3423. * Example: setDay('Montag', 'de_AT'); will set the monday of this week as day.
  3424. *
  3425. * @param string|integer|array|Zend_Date $day Day to set
  3426. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3427. * @return Zend_Date Provides a fluent interface
  3428. * @throws Zend_Date_Exception
  3429. */
  3430. public function setDay($day, $locale = null)
  3431. {
  3432. return $this->_day('set', $day, $locale);
  3433. }
  3434. /**
  3435. * Adds days to the existing date object.
  3436. * The day can be a number or a string. Adding days lower then 0 or greater than the number of this months days
  3437. * will result in adding or subtracting the relevant month.
  3438. * If a localized dayname is given it will be parsed with the default locale or the optional
  3439. * set locale.
  3440. *
  3441. * @param string|integer|array|Zend_Date $day Day to add
  3442. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3443. * @return Zend_Date Provides a fluent interface
  3444. * @throws Zend_Date_Exception
  3445. */
  3446. public function addDay($day, $locale = null)
  3447. {
  3448. return $this->_day('add', $day, $locale);
  3449. }
  3450. /**
  3451. * Subtracts days from the existing date object.
  3452. * The day can be a number or a string. Subtracting days lower then 0 or greater than the number of this months days
  3453. * will result in adding or subtracting the relevant month.
  3454. * If a localized dayname is given it will be parsed with the default locale or the optional
  3455. * set locale.
  3456. *
  3457. * @param string|integer|array|Zend_Date $day Day to sub
  3458. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3459. * @return Zend_Date Provides a fluent interface
  3460. * @throws Zend_Date_Exception
  3461. */
  3462. public function subDay($day, $locale = null)
  3463. {
  3464. return $this->_day('sub', $day, $locale);
  3465. }
  3466. /**
  3467. * Compares the day with the existing date object, ignoring other date parts.
  3468. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3469. * Returns if equal, earlier or later
  3470. *
  3471. * @param string|integer|array|Zend_Date $day Day to compare
  3472. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3473. * @return integer 0 = equal, 1 = later, -1 = earlier
  3474. * @throws Zend_Date_Exception
  3475. */
  3476. public function compareDay($day, $locale = null)
  3477. {
  3478. return $this->_day('cmp', $day, $locale);
  3479. }
  3480. /**
  3481. * Returns the weekday as new date object
  3482. * Weekday is always from 1-7
  3483. * Example: 09-Jan-2007 -> 2 = Tuesday -> 02-Jan-1970 (when 02.01.1970 is also Tuesday)
  3484. *
  3485. * @param Zend_Locale $locale OPTIONAL Locale for parsing input
  3486. * @return Zend_Date
  3487. */
  3488. public function getWeekday($locale = null)
  3489. {
  3490. if (self::$_options['format_type'] == 'php') {
  3491. $format = 'l';
  3492. } else {
  3493. $format = self::WEEKDAY;
  3494. }
  3495. return $this->copyPart($format, $locale);
  3496. }
  3497. /**
  3498. * Returns the calculated weekday
  3499. *
  3500. * @param string $calc Type of calculation to make
  3501. * @param Zend_Date $weekday Weekday to calculate, when null the actual weekday is calculated
  3502. * @param Zend_Locale $locale Locale for parsing input
  3503. * @return Zend_Date|integer
  3504. * @throws Zend_Date_Exception
  3505. */
  3506. private function _weekday($calc, $weekday, $locale)
  3507. {
  3508. if ($weekday === null) {
  3509. #require_once 'Zend/Date/Exception.php';
  3510. throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
  3511. }
  3512. if ($locale === null) {
  3513. $locale = $this->getLocale();
  3514. }
  3515. if ($weekday instanceof Zend_Date) {
  3516. $weekday = $weekday->toString(self::WEEKDAY_8601, 'iso', $locale);
  3517. }
  3518. if (is_numeric($weekday)) {
  3519. $type = self::WEEKDAY_8601;
  3520. } else if (is_array($weekday)) {
  3521. if (isset($weekday['weekday']) === true) {
  3522. $weekday = $weekday['weekday'];
  3523. $type = self::WEEKDAY;
  3524. } else {
  3525. #require_once 'Zend/Date/Exception.php';
  3526. throw new Zend_Date_Exception("no weekday given in array");
  3527. }
  3528. } else {
  3529. switch(iconv_strlen($weekday, 'UTF-8')) {
  3530. case 1:
  3531. $type = self::WEEKDAY_NARROW;
  3532. break;
  3533. case 2:
  3534. $type = self::WEEKDAY_NAME;
  3535. break;
  3536. case 3:
  3537. $type = self::WEEKDAY_SHORT;
  3538. break;
  3539. default:
  3540. $type = self::WEEKDAY;
  3541. break;
  3542. }
  3543. }
  3544. $return = $this->_calcdetail($calc, $weekday, $type, $locale);
  3545. if ($calc != 'cmp') {
  3546. return $this;
  3547. }
  3548. return $return;
  3549. }
  3550. /**
  3551. * Sets a new weekday
  3552. * The weekday can be a number or a string. If a localized weekday name is given,
  3553. * then it will be parsed as a date in $locale (defaults to the same locale as $this).
  3554. * Returned is the new date object.
  3555. * Example: setWeekday(3); will set the wednesday of this week as day.
  3556. *
  3557. * @param string|integer|array|Zend_Date $weekday Weekday to set
  3558. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3559. * @return Zend_Date Provides a fluent interface
  3560. * @throws Zend_Date_Exception
  3561. */
  3562. public function setWeekday($weekday, $locale = null)
  3563. {
  3564. return $this->_weekday('set', $weekday, $locale);
  3565. }
  3566. /**
  3567. * Adds weekdays to the existing date object.
  3568. * The weekday can be a number or a string.
  3569. * If a localized dayname is given it will be parsed with the default locale or the optional
  3570. * set locale.
  3571. * Returned is the new date object
  3572. * Example: addWeekday(3); will add the difference of days from the begining of the month until
  3573. * wednesday.
  3574. *
  3575. * @param string|integer|array|Zend_Date $weekday Weekday to add
  3576. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3577. * @return Zend_Date Provides a fluent interface
  3578. * @throws Zend_Date_Exception
  3579. */
  3580. public function addWeekday($weekday, $locale = null)
  3581. {
  3582. return $this->_weekday('add', $weekday, $locale);
  3583. }
  3584. /**
  3585. * Subtracts weekdays from the existing date object.
  3586. * The weekday can be a number or a string.
  3587. * If a localized dayname is given it will be parsed with the default locale or the optional
  3588. * set locale.
  3589. * Returned is the new date object
  3590. * Example: subWeekday(3); will subtract the difference of days from the begining of the month until
  3591. * wednesday.
  3592. *
  3593. * @param string|integer|array|Zend_Date $weekday Weekday to sub
  3594. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3595. * @return Zend_Date Provides a fluent interface
  3596. * @throws Zend_Date_Exception
  3597. */
  3598. public function subWeekday($weekday, $locale = null)
  3599. {
  3600. return $this->_weekday('sub', $weekday, $locale);
  3601. }
  3602. /**
  3603. * Compares the weekday with the existing date object, ignoring other date parts.
  3604. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3605. * Returns if equal, earlier or later
  3606. *
  3607. * @param string|integer|array|Zend_Date $weekday Weekday to compare
  3608. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3609. * @return integer 0 = equal, 1 = later, -1 = earlier
  3610. * @throws Zend_Date_Exception
  3611. */
  3612. public function compareWeekday($weekday, $locale = null)
  3613. {
  3614. return $this->_weekday('cmp', $weekday, $locale);
  3615. }
  3616. /**
  3617. * Returns the day of year as new date object
  3618. * Example: 02.Feb.1986 10:00:00 -> 02.Feb.1970 00:00:00
  3619. *
  3620. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3621. * @return Zend_Date
  3622. */
  3623. public function getDayOfYear($locale = null)
  3624. {
  3625. if (self::$_options['format_type'] == 'php') {
  3626. $format = 'D';
  3627. } else {
  3628. $format = self::DAY_OF_YEAR;
  3629. }
  3630. return $this->copyPart($format, $locale);
  3631. }
  3632. /**
  3633. * Sets a new day of year
  3634. * The day of year is always a number.
  3635. * Returned is the new date object
  3636. * Example: 04.May.2004 -> setDayOfYear(10) -> 10.Jan.2004
  3637. *
  3638. * @param string|integer|array|Zend_Date $day Day of Year to set
  3639. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3640. * @return Zend_Date Provides a fluent interface
  3641. * @throws Zend_Date_Exception
  3642. */
  3643. public function setDayOfYear($day, $locale = null)
  3644. {
  3645. return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3646. }
  3647. /**
  3648. * Adds a day of year to the existing date object.
  3649. * The day of year is always a number.
  3650. * Returned is the new date object
  3651. * Example: addDayOfYear(10); will add 10 days to the existing date object.
  3652. *
  3653. * @param string|integer|array|Zend_Date $day Day of Year to add
  3654. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3655. * @return Zend_Date Provides a fluent interface
  3656. * @throws Zend_Date_Exception
  3657. */
  3658. public function addDayOfYear($day, $locale = null)
  3659. {
  3660. return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3661. }
  3662. /**
  3663. * Subtracts a day of year from the existing date object.
  3664. * The day of year is always a number.
  3665. * Returned is the new date object
  3666. * Example: subDayOfYear(10); will subtract 10 days from the existing date object.
  3667. *
  3668. * @param string|integer|array|Zend_Date $day Day of Year to sub
  3669. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3670. * @return Zend_Date Provides a fluent interface
  3671. * @throws Zend_Date_Exception
  3672. */
  3673. public function subDayOfYear($day, $locale = null)
  3674. {
  3675. return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3676. }
  3677. /**
  3678. * Compares the day of year with the existing date object.
  3679. * For example: compareDayOfYear(33) -> 02.Feb.2007 -> 0
  3680. * Returns if equal, earlier or later
  3681. *
  3682. * @param string|integer|array|Zend_Date $day Day of Year to compare
  3683. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3684. * @return integer 0 = equal, 1 = later, -1 = earlier
  3685. * @throws Zend_Date_Exception
  3686. */
  3687. public function compareDayOfYear($day, $locale = null)
  3688. {
  3689. return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3690. }
  3691. /**
  3692. * Returns the hour as new date object
  3693. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 10:00:00
  3694. *
  3695. * @param Zend_Locale $locale OPTIONAL Locale for parsing input
  3696. * @return Zend_Date
  3697. */
  3698. public function getHour($locale = null)
  3699. {
  3700. return $this->copyPart(self::HOUR, $locale);
  3701. }
  3702. /**
  3703. * Sets a new hour
  3704. * The hour is always a number.
  3705. * Returned is the new date object
  3706. * Example: 04.May.1993 13:07:25 -> setHour(7); -> 04.May.1993 07:07:25
  3707. *
  3708. * @param string|integer|array|Zend_Date $hour Hour to set
  3709. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3710. * @return Zend_Date Provides a fluent interface
  3711. * @throws Zend_Date_Exception
  3712. */
  3713. public function setHour($hour, $locale = null)
  3714. {
  3715. return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
  3716. }
  3717. /**
  3718. * Adds hours to the existing date object.
  3719. * The hour is always a number.
  3720. * Returned is the new date object
  3721. * Example: 04.May.1993 13:07:25 -> addHour(12); -> 05.May.1993 01:07:25
  3722. *
  3723. * @param string|integer|array|Zend_Date $hour Hour to add
  3724. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3725. * @return Zend_Date Provides a fluent interface
  3726. * @throws Zend_Date_Exception
  3727. */
  3728. public function addHour($hour, $locale = null)
  3729. {
  3730. return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
  3731. }
  3732. /**
  3733. * Subtracts hours from the existing date object.
  3734. * The hour is always a number.
  3735. * Returned is the new date object
  3736. * Example: 04.May.1993 13:07:25 -> subHour(6); -> 05.May.1993 07:07:25
  3737. *
  3738. * @param string|integer|array|Zend_Date $hour Hour to sub
  3739. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3740. * @return Zend_Date Provides a fluent interface
  3741. * @throws Zend_Date_Exception
  3742. */
  3743. public function subHour($hour, $locale = null)
  3744. {
  3745. return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
  3746. }
  3747. /**
  3748. * Compares the hour with the existing date object.
  3749. * For example: 10:30:25 -> compareHour(10) -> 0
  3750. * Returns if equal, earlier or later
  3751. *
  3752. * @param string|integer|array|Zend_Date $hour Hour to compare
  3753. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3754. * @return integer 0 = equal, 1 = later, -1 = earlier
  3755. * @throws Zend_Date_Exception
  3756. */
  3757. public function compareHour($hour, $locale = null)
  3758. {
  3759. return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
  3760. }
  3761. /**
  3762. * Returns the minute as new date object
  3763. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:30:00
  3764. *
  3765. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3766. * @return Zend_Date
  3767. */
  3768. public function getMinute($locale = null)
  3769. {
  3770. if (self::$_options['format_type'] == 'php') {
  3771. $format = 'i';
  3772. } else {
  3773. $format = self::MINUTE;
  3774. }
  3775. return $this->copyPart($format, $locale);
  3776. }
  3777. /**
  3778. * Sets a new minute
  3779. * The minute is always a number.
  3780. * Returned is the new date object
  3781. * Example: 04.May.1993 13:07:25 -> setMinute(29); -> 04.May.1993 13:29:25
  3782. *
  3783. * @param string|integer|array|Zend_Date $minute Minute to set
  3784. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3785. * @return Zend_Date Provides a fluent interface
  3786. * @throws Zend_Date_Exception
  3787. */
  3788. public function setMinute($minute, $locale = null)
  3789. {
  3790. return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3791. }
  3792. /**
  3793. * Adds minutes to the existing date object.
  3794. * The minute is always a number.
  3795. * Returned is the new date object
  3796. * Example: 04.May.1993 13:07:25 -> addMinute(65); -> 04.May.1993 13:12:25
  3797. *
  3798. * @param string|integer|array|Zend_Date $minute Minute to add
  3799. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3800. * @return Zend_Date Provides a fluent interface
  3801. * @throws Zend_Date_Exception
  3802. */
  3803. public function addMinute($minute, $locale = null)
  3804. {
  3805. return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3806. }
  3807. /**
  3808. * Subtracts minutes from the existing date object.
  3809. * The minute is always a number.
  3810. * Returned is the new date object
  3811. * Example: 04.May.1993 13:07:25 -> subMinute(9); -> 04.May.1993 12:58:25
  3812. *
  3813. * @param string|integer|array|Zend_Date $minute Minute to sub
  3814. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3815. * @return Zend_Date Provides a fluent interface
  3816. * @throws Zend_Date_Exception
  3817. */
  3818. public function subMinute($minute, $locale = null)
  3819. {
  3820. return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3821. }
  3822. /**
  3823. * Compares the minute with the existing date object.
  3824. * For example: 10:30:25 -> compareMinute(30) -> 0
  3825. * Returns if equal, earlier or later
  3826. *
  3827. * @param string|integer|array|Zend_Date $minute Hour to compare
  3828. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3829. * @return integer 0 = equal, 1 = later, -1 = earlier
  3830. * @throws Zend_Date_Exception
  3831. */
  3832. public function compareMinute($minute, $locale = null)
  3833. {
  3834. return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3835. }
  3836. /**
  3837. * Returns the second as new date object
  3838. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:00:25
  3839. *
  3840. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3841. * @return Zend_Date
  3842. */
  3843. public function getSecond($locale = null)
  3844. {
  3845. if (self::$_options['format_type'] == 'php') {
  3846. $format = 's';
  3847. } else {
  3848. $format = self::SECOND;
  3849. }
  3850. return $this->copyPart($format, $locale);
  3851. }
  3852. /**
  3853. * Sets new seconds to the existing date object.
  3854. * The second is always a number.
  3855. * Returned is the new date object
  3856. * Example: 04.May.1993 13:07:25 -> setSecond(100); -> 04.May.1993 13:08:40
  3857. *
  3858. * @param string|integer|array|Zend_Date $second Second to set
  3859. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3860. * @return Zend_Date Provides a fluent interface
  3861. * @throws Zend_Date_Exception
  3862. */
  3863. public function setSecond($second, $locale = null)
  3864. {
  3865. return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
  3866. }
  3867. /**
  3868. * Adds seconds to the existing date object.
  3869. * The second is always a number.
  3870. * Returned is the new date object
  3871. * Example: 04.May.1993 13:07:25 -> addSecond(65); -> 04.May.1993 13:08:30
  3872. *
  3873. * @param string|integer|array|Zend_Date $second Second to add
  3874. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3875. * @return Zend_Date Provides a fluent interface
  3876. * @throws Zend_Date_Exception
  3877. */
  3878. public function addSecond($second, $locale = null)
  3879. {
  3880. return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
  3881. }
  3882. /**
  3883. * Subtracts seconds from the existing date object.
  3884. * The second is always a number.
  3885. * Returned is the new date object
  3886. * Example: 04.May.1993 13:07:25 -> subSecond(10); -> 04.May.1993 13:07:15
  3887. *
  3888. * @param string|integer|array|Zend_Date $second Second to sub
  3889. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3890. * @return Zend_Date Provides a fluent interface
  3891. * @throws Zend_Date_Exception
  3892. */
  3893. public function subSecond($second, $locale = null)
  3894. {
  3895. return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
  3896. }
  3897. /**
  3898. * Compares the second with the existing date object.
  3899. * For example: 10:30:25 -> compareSecond(25) -> 0
  3900. * Returns if equal, earlier or later
  3901. *
  3902. * @param string|integer|array|Zend_Date $second Second to compare
  3903. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3904. * @return integer 0 = equal, 1 = later, -1 = earlier
  3905. * @throws Zend_Date_Exception
  3906. */
  3907. public function compareSecond($second, $locale = null)
  3908. {
  3909. return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
  3910. }
  3911. /**
  3912. * Returns the precision for fractional seconds
  3913. *
  3914. * @return integer
  3915. */
  3916. public function getFractionalPrecision()
  3917. {
  3918. return $this->_precision;
  3919. }
  3920. /**
  3921. * Sets a new precision for fractional seconds
  3922. *
  3923. * @param integer $precision Precision for the fractional datepart 3 = milliseconds
  3924. * @throws Zend_Date_Exception
  3925. * @return Zend_Date Provides a fluent interface
  3926. */
  3927. public function setFractionalPrecision($precision)
  3928. {
  3929. if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
  3930. #require_once 'Zend/Date/Exception.php';
  3931. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3932. }
  3933. $this->_precision = (int) $precision;
  3934. if ($this->_precision < strlen($this->_fractional)) {
  3935. $this->_fractional = substr($this->_fractional, 0, $this->_precision);
  3936. } else {
  3937. $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_RIGHT);
  3938. }
  3939. return $this;
  3940. }
  3941. /**
  3942. * Returns the milliseconds of the date object
  3943. *
  3944. * @return string
  3945. */
  3946. public function getMilliSecond()
  3947. {
  3948. return $this->_fractional;
  3949. }
  3950. /**
  3951. * Sets new milliseconds for the date object
  3952. * Example: setMilliSecond(550, 2) -> equals +5 Sec +50 MilliSec
  3953. *
  3954. * @param integer|Zend_Date $milli (Optional) Millisecond to set, when null the actual millisecond is set
  3955. * @param integer $precision (Optional) Fraction precision of the given milliseconds
  3956. * @throws Zend_Date_Exception
  3957. * @return Zend_Date Provides a fluent interface
  3958. */
  3959. public function setMilliSecond($milli = null, $precision = null)
  3960. {
  3961. if ($milli === null) {
  3962. list($milli, $time) = explode(" ", microtime());
  3963. $milli = intval($milli);
  3964. $precision = 6;
  3965. } else if (!is_numeric($milli)) {
  3966. #require_once 'Zend/Date/Exception.php';
  3967. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  3968. }
  3969. if ($precision === null) {
  3970. $precision = $this->_precision;
  3971. }
  3972. if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3973. #require_once 'Zend/Date/Exception.php';
  3974. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3975. }
  3976. $this->_fractional = 0;
  3977. $this->addMilliSecond($milli, $precision);
  3978. return $this;
  3979. }
  3980. /**
  3981. * Adds milliseconds to the date object
  3982. *
  3983. * @param integer|Zend_Date $milli (Optional) Millisecond to add, when null the actual millisecond is added
  3984. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3985. * @throws Zend_Date_Exception
  3986. * @return Zend_Date Provides a fluent interface
  3987. */
  3988. public function addMilliSecond($milli = null, $precision = null)
  3989. {
  3990. if ($milli === null) {
  3991. list($milli, $time) = explode(" ", microtime());
  3992. $milli = intval($milli);
  3993. } else if (!is_numeric($milli)) {
  3994. #require_once 'Zend/Date/Exception.php';
  3995. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  3996. }
  3997. if ($precision === null) {
  3998. // Use internal default precision
  3999. // Is not as logic as using the length of the input. But this would break tests and maybe other things
  4000. // as an input value of integer 10, which is used in tests, must be parsed as 10 milliseconds (real milliseconds, precision 3)
  4001. // but with auto-detect of precision, 100 milliseconds would be added.
  4002. $precision = $this->_precision;
  4003. }
  4004. if (!is_int($precision) || $precision < 1 || $precision > 9) {
  4005. #require_once 'Zend/Date/Exception.php';
  4006. throw new Zend_Date_Exception(
  4007. "precision ($precision) must be a positive integer less than 10", 0, null, $precision
  4008. );
  4009. }
  4010. if ($this->_precision > $precision) {
  4011. $milli = $milli * pow(10, $this->_precision - $precision);
  4012. } elseif ($this->_precision < $precision) {
  4013. $milli = round($milli / pow(10, $precision - $this->_precision));
  4014. }
  4015. $this->_fractional += $milli;
  4016. // Add/sub milliseconds + add/sub seconds
  4017. $max = pow(10, $this->_precision);
  4018. // Milli includes seconds
  4019. if ($this->_fractional >= $max) {
  4020. while ($this->_fractional >= $max) {
  4021. $this->addSecond(1);
  4022. $this->_fractional -= $max;
  4023. }
  4024. }
  4025. if ($this->_fractional < 0) {
  4026. while ($this->_fractional < 0) {
  4027. $this->subSecond(1);
  4028. $this->_fractional += $max;
  4029. }
  4030. }
  4031. if ($this->_precision > strlen($this->_fractional)) {
  4032. $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_LEFT);
  4033. }
  4034. return $this;
  4035. }
  4036. /**
  4037. * Subtracts a millisecond
  4038. *
  4039. * @param integer|Zend_Date $milli (Optional) Millisecond to sub, when null the actual millisecond is subtracted
  4040. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  4041. * @return Zend_Date Provides a fluent interface
  4042. */
  4043. public function subMilliSecond($milli = null, $precision = null)
  4044. {
  4045. $this->addMilliSecond(0 - $milli, $precision);
  4046. return $this;
  4047. }
  4048. /**
  4049. * Compares only the millisecond part, returning the difference
  4050. *
  4051. * @param integer|Zend_Date $milli OPTIONAL Millisecond to compare, when null the actual millisecond is compared
  4052. * @param integer $precision OPTIONAL Fractional precision for the given milliseconds
  4053. * @throws Zend_Date_Exception On invalid input
  4054. * @return integer 0 = equal, 1 = later, -1 = earlier
  4055. */
  4056. public function compareMilliSecond($milli = null, $precision = null)
  4057. {
  4058. if ($milli === null) {
  4059. list($milli, $time) = explode(" ", microtime());
  4060. $milli = intval($milli);
  4061. } else if (is_numeric($milli) === false) {
  4062. #require_once 'Zend/Date/Exception.php';
  4063. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  4064. }
  4065. if ($precision === null) {
  4066. $precision = strlen($milli);
  4067. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  4068. #require_once 'Zend/Date/Exception.php';
  4069. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  4070. }
  4071. if ($precision === 0) {
  4072. #require_once 'Zend/Date/Exception.php';
  4073. throw new Zend_Date_Exception('precision is 0');
  4074. }
  4075. if ($precision != $this->_precision) {
  4076. if ($precision > $this->_precision) {
  4077. $diff = $precision - $this->_precision;
  4078. $milli = (int) ($milli / (10 * $diff));
  4079. } else {
  4080. $diff = $this->_precision - $precision;
  4081. $milli = (int) ($milli * (10 * $diff));
  4082. }
  4083. }
  4084. $comp = $this->_fractional - $milli;
  4085. if ($comp < 0) {
  4086. return -1;
  4087. } else if ($comp > 0) {
  4088. return 1;
  4089. }
  4090. return 0;
  4091. }
  4092. /**
  4093. * Returns the week as new date object using monday as begining of the week
  4094. * Example: 12.Jan.2007 -> 08.Jan.1970 00:00:00
  4095. *
  4096. * @param Zend_Locale $locale OPTIONAL Locale for parsing input
  4097. * @return Zend_Date
  4098. */
  4099. public function getWeek($locale = null)
  4100. {
  4101. if (self::$_options['format_type'] == 'php') {
  4102. $format = 'W';
  4103. } else {
  4104. $format = self::WEEK;
  4105. }
  4106. return $this->copyPart($format, $locale);
  4107. }
  4108. /**
  4109. * Sets a new week. The week is always a number. The day of week is not changed.
  4110. * Returned is the new date object
  4111. * Example: 09.Jan.2007 13:07:25 -> setWeek(1); -> 02.Jan.2007 13:07:25
  4112. *
  4113. * @param string|integer|array|Zend_Date $week Week to set
  4114. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4115. * @return Zend_Date Provides a fluent interface
  4116. * @throws Zend_Date_Exception
  4117. */
  4118. public function setWeek($week, $locale = null)
  4119. {
  4120. return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
  4121. }
  4122. /**
  4123. * Adds a week. The week is always a number. The day of week is not changed.
  4124. * Returned is the new date object
  4125. * Example: 09.Jan.2007 13:07:25 -> addWeek(1); -> 16.Jan.2007 13:07:25
  4126. *
  4127. * @param string|integer|array|Zend_Date $week Week to add
  4128. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4129. * @return Zend_Date Provides a fluent interface
  4130. * @throws Zend_Date_Exception
  4131. */
  4132. public function addWeek($week, $locale = null)
  4133. {
  4134. return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
  4135. }
  4136. /**
  4137. * Subtracts a week. The week is always a number. The day of week is not changed.
  4138. * Returned is the new date object
  4139. * Example: 09.Jan.2007 13:07:25 -> subWeek(1); -> 02.Jan.2007 13:07:25
  4140. *
  4141. * @param string|integer|array|Zend_Date $week Week to sub
  4142. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4143. * @return Zend_Date Provides a fluent interface
  4144. * @throws Zend_Date_Exception
  4145. */
  4146. public function subWeek($week, $locale = null)
  4147. {
  4148. return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
  4149. }
  4150. /**
  4151. * Compares only the week part, returning the difference
  4152. * Returned is the new date object
  4153. * Returns if equal, earlier or later
  4154. * Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
  4155. *
  4156. * @param string|integer|array|Zend_Date $week Week to compare
  4157. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4158. * @return integer 0 = equal, 1 = later, -1 = earlier
  4159. */
  4160. public function compareWeek($week, $locale = null)
  4161. {
  4162. return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
  4163. }
  4164. /**
  4165. * Sets a new standard locale for the date object.
  4166. * This locale will be used for all functions
  4167. * Returned is the really set locale.
  4168. * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
  4169. * 'xx_YY' will be set to 'root' because 'xx' does not exist
  4170. *
  4171. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  4172. * @throws Zend_Date_Exception When the given locale does not exist
  4173. * @return Zend_Date Provides fluent interface
  4174. */
  4175. public function setLocale($locale = null)
  4176. {
  4177. try {
  4178. $this->_locale = Zend_Locale::findLocale($locale);
  4179. } catch (Zend_Locale_Exception $e) {
  4180. #require_once 'Zend/Date/Exception.php';
  4181. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  4182. }
  4183. return $this;
  4184. }
  4185. /**
  4186. * Returns the actual set locale
  4187. *
  4188. * @return string
  4189. */
  4190. public function getLocale()
  4191. {
  4192. return $this->_locale;
  4193. }
  4194. /**
  4195. * Checks if the given date is a real date or datepart.
  4196. * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  4197. * But the check will only be done for the expected dateparts which are given by format.
  4198. * If no format is given the standard dateformat for the actual locale is used.
  4199. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  4200. *
  4201. * @param string|array|Zend_Date $date Date to parse for correctness
  4202. * @param string $format (Optional) Format for parsing the date string
  4203. * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts
  4204. * @return boolean True when all date parts are correct
  4205. */
  4206. public static function isDate($date, $format = null, $locale = null)
  4207. {
  4208. if (!is_string($date) && !is_numeric($date) && !($date instanceof Zend_Date) &&
  4209. !is_array($date)) {
  4210. return false;
  4211. }
  4212. if (($format !== null) && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
  4213. && (Zend_Locale::isLocale($format, null, false))) {
  4214. $locale = $format;
  4215. $format = null;
  4216. }
  4217. $locale = Zend_Locale::findLocale($locale);
  4218. if ($format === null) {
  4219. $format = Zend_Locale_Format::getDateFormat($locale);
  4220. } else if ((self::$_options['format_type'] == 'php') && !defined($format)) {
  4221. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  4222. }
  4223. $format = self::_getLocalizedToken($format, $locale);
  4224. if (!is_array($date)) {
  4225. try {
  4226. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
  4227. 'date_format' => $format, 'format_type' => 'iso',
  4228. 'fix_date' => false));
  4229. } catch (Zend_Locale_Exception $e) {
  4230. // Date can not be parsed
  4231. return false;
  4232. }
  4233. } else {
  4234. $parsed = $date;
  4235. }
  4236. if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
  4237. (!isset($parsed['year']))) {
  4238. // Year expected but not found
  4239. return false;
  4240. }
  4241. if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
  4242. // Month expected but not found
  4243. return false;
  4244. }
  4245. if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
  4246. // Day expected but not found
  4247. return false;
  4248. }
  4249. if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
  4250. (!isset($parsed['hour']))) {
  4251. // Hour expected but not found
  4252. return false;
  4253. }
  4254. if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
  4255. // Minute expected but not found
  4256. return false;
  4257. }
  4258. if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
  4259. // Second expected but not found
  4260. return false;
  4261. }
  4262. // Set not given dateparts
  4263. if (isset($parsed['hour']) === false) {
  4264. $parsed['hour'] = 12;
  4265. }
  4266. if (isset($parsed['minute']) === false) {
  4267. $parsed['minute'] = 0;
  4268. }
  4269. if (isset($parsed['second']) === false) {
  4270. $parsed['second'] = 0;
  4271. }
  4272. if (isset($parsed['month']) === false) {
  4273. $parsed['month'] = 1;
  4274. }
  4275. if (isset($parsed['day']) === false) {
  4276. $parsed['day'] = 1;
  4277. }
  4278. if (isset($parsed['year']) === false) {
  4279. $parsed['year'] = 1970;
  4280. }
  4281. if (self::isYearLeapYear($parsed['year'])) {
  4282. $parsed['year'] = 1972;
  4283. } else {
  4284. $parsed['year'] = 1971;
  4285. }
  4286. $date = new self($parsed, null, $locale);
  4287. $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
  4288. $parsed['month'], $parsed['day'], $parsed['year']);
  4289. if ($parsed['year'] != $date->date('Y', $timestamp)) {
  4290. // Given year differs from parsed year
  4291. return false;
  4292. }
  4293. if ($parsed['month'] != $date->date('n', $timestamp)) {
  4294. // Given month differs from parsed month
  4295. return false;
  4296. }
  4297. if ($parsed['day'] != $date->date('j', $timestamp)) {
  4298. // Given day differs from parsed day
  4299. return false;
  4300. }
  4301. if ($parsed['hour'] != $date->date('G', $timestamp)) {
  4302. // Given hour differs from parsed hour
  4303. return false;
  4304. }
  4305. if ($parsed['minute'] != $date->date('i', $timestamp)) {
  4306. // Given minute differs from parsed minute
  4307. return false;
  4308. }
  4309. if ($parsed['second'] != $date->date('s', $timestamp)) {
  4310. // Given second differs from parsed second
  4311. return false;
  4312. }
  4313. return true;
  4314. }
  4315. /**
  4316. * Returns the ISO Token for all localized constants
  4317. *
  4318. * @param string $token Token to normalize
  4319. * @param string $locale Locale to search
  4320. * @return string
  4321. */
  4322. protected static function _getLocalizedToken($token, $locale)
  4323. {
  4324. switch($token) {
  4325. case self::ISO_8601 :
  4326. return "yyyy-MM-ddThh:mm:ss";
  4327. break;
  4328. case self::RFC_2822 :
  4329. return "EEE, dd MMM yyyy HH:mm:ss";
  4330. break;
  4331. case self::DATES :
  4332. return Zend_Locale_Data::getContent($locale, 'date');
  4333. break;
  4334. case self::DATE_FULL :
  4335. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  4336. break;
  4337. case self::DATE_LONG :
  4338. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  4339. break;
  4340. case self::DATE_MEDIUM :
  4341. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  4342. break;
  4343. case self::DATE_SHORT :
  4344. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  4345. break;
  4346. case self::TIMES :
  4347. return Zend_Locale_Data::getContent($locale, 'time');
  4348. break;
  4349. case self::TIME_FULL :
  4350. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  4351. break;
  4352. case self::TIME_LONG :
  4353. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
  4354. break;
  4355. case self::TIME_MEDIUM :
  4356. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
  4357. break;
  4358. case self::TIME_SHORT :
  4359. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
  4360. break;
  4361. case self::DATETIME :
  4362. return Zend_Locale_Data::getContent($locale, 'datetime');
  4363. break;
  4364. case self::DATETIME_FULL :
  4365. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
  4366. break;
  4367. case self::DATETIME_LONG :
  4368. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
  4369. break;
  4370. case self::DATETIME_MEDIUM :
  4371. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
  4372. break;
  4373. case self::DATETIME_SHORT :
  4374. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
  4375. break;
  4376. case self::ATOM :
  4377. case self::RFC_3339 :
  4378. case self::W3C :
  4379. return "yyyy-MM-DD HH:mm:ss";
  4380. break;
  4381. case self::COOKIE :
  4382. case self::RFC_850 :
  4383. return "EEEE, dd-MM-yyyy HH:mm:ss";
  4384. break;
  4385. case self::RFC_822 :
  4386. case self::RFC_1036 :
  4387. case self::RFC_1123 :
  4388. case self::RSS :
  4389. return "EEE, dd MM yyyy HH:mm:ss";
  4390. break;
  4391. }
  4392. return $token;
  4393. }
  4394. /**
  4395. * Get unix timestamp.
  4396. * Added limitation: $year value must be between -10 000 and 10 000
  4397. * Parent method implementation causes 504 error if it gets too big(small) year value
  4398. *
  4399. * @see Zend_Date_DateObject::mktime
  4400. * @throws Zend_Date_Exception
  4401. * @param $hour
  4402. * @param $minute
  4403. * @param $second
  4404. * @param $month
  4405. * @param $day
  4406. * @param $year
  4407. * @param bool $gmt
  4408. * @return float|int
  4409. */
  4410. protected function mktime($hour, $minute, $second, $month, $day, $year, $gmt = false)
  4411. {
  4412. $day = intval($day);
  4413. $month = intval($month);
  4414. $year = intval($year);
  4415. // correct months > 12 and months < 1
  4416. if ($month > 12) {
  4417. $overlap = floor($month / 12);
  4418. $year += $overlap;
  4419. $month -= $overlap * 12;
  4420. } else {
  4421. $overlap = ceil((1 - $month) / 12);
  4422. $year -= $overlap;
  4423. $month += $overlap * 12;
  4424. }
  4425. if ($year > self::YEAR_MAX_VALUE || $year < self::YEAR_MIN_VALUE) {
  4426. throw new Zend_Date_Exception('Invalid year, it must be between ' . self::YEAR_MIN_VALUE . ' and '
  4427. . self::YEAR_MAX_VALUE);
  4428. }
  4429. return parent::mktime($hour, $minute, $second, $month, $day, $year, $gmt);
  4430. }
  4431. }