PageRenderTime 300ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 1ms

/library/Zend/Date.php

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