PageRenderTime 89ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Date/Date.php

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