PageRenderTime 78ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/cubi/openbiz/others/Zend/Date.php

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