PageRenderTime 91ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 1ms

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

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