PageRenderTime 81ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Zend/Date.php

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