PageRenderTime 76ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/www/system/library/Zend/Date.php

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