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

/library/Zend/Date/Date.php

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