PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Date/Date.php

https://github.com/selfchief/zf2
PHP | 4860 lines | 3078 code | 503 blank | 1279 comment | 644 complexity | 46941e130612c69c6fed78c8885c406b MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Date;
  24. use Zend\TimeSync;
  25. use Zend\Locale\Locale;
  26. use Zend\Locale\Format;
  27. use Zend\Locale\Data\Cldr;
  28. use Zend\Locale\Math;
  29. /**
  30. * @uses \Zend\Date\DateObject
  31. * @uses \Zend\Date\Exception
  32. * @uses \Zend\Locale\Locale
  33. * @uses \Zend\Locale\Format
  34. * @uses \Zend\Locale\Math
  35. * @category Zend
  36. * @package Zend_Date
  37. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Date extends DateObject
  41. {
  42. private $_locale = null;
  43. // Fractional second variables
  44. private $_fractional = 0;
  45. private $_precision = 3;
  46. private static $_options = array(
  47. 'format_type' => 'iso', // format for date strings 'iso' or 'php'
  48. 'fix_dst' => true, // fix dst on summer/winter time change
  49. 'extend_month' => false, // false - addMonth like SQL, true like excel
  50. 'cache' => null, // cache to set
  51. 'timesync' => null // timesync server to set
  52. );
  53. // Class wide Date Constants
  54. const DAY = 'dd';
  55. const DAY_SHORT = 'd';
  56. const DAY_SUFFIX = 'SS';
  57. const DAY_OF_YEAR = 'D';
  58. const WEEKDAY = 'EEEE';
  59. const WEEKDAY_SHORT = 'EEE';
  60. const WEEKDAY_NARROW = 'E';
  61. const WEEKDAY_NAME = 'EE';
  62. const WEEKDAY_8601 = 'eee';
  63. const WEEKDAY_DIGIT = 'e';
  64. const WEEK = 'ww';
  65. const MONTH = 'MM';
  66. const MONTH_SHORT = 'M';
  67. const MONTH_DAYS = 'ddd';
  68. const MONTH_NAME = 'MMMM';
  69. const MONTH_NAME_SHORT = 'MMM';
  70. const MONTH_NAME_NARROW = 'MMMMM';
  71. const YEAR = 'y';
  72. const YEAR_SHORT = 'yy';
  73. const YEAR_8601 = 'Y';
  74. const YEAR_SHORT_8601 = 'YY';
  75. const LEAPYEAR = 'l';
  76. const MERIDIEM = 'a';
  77. const SWATCH = 'B';
  78. const HOUR = 'HH';
  79. const HOUR_SHORT = 'H';
  80. const HOUR_AM = 'hh';
  81. const HOUR_SHORT_AM = 'h';
  82. const MINUTE = 'mm';
  83. const MINUTE_SHORT = 'm';
  84. const SECOND = 'ss';
  85. const SECOND_SHORT = 's';
  86. const MILLISECOND = 'S';
  87. const TIMEZONE_NAME = 'zzzz';
  88. const DAYLIGHT = 'I';
  89. const GMT_DIFF = 'Z';
  90. const GMT_DIFF_SEP = 'ZZZZ';
  91. const TIMEZONE = 'z';
  92. const TIMEZONE_SECS = 'X';
  93. const ISO_8601 = 'c';
  94. const RFC_2822 = 'r';
  95. const TIMESTAMP = 'U';
  96. const ERA = 'G';
  97. const ERA_NAME = 'GGGG';
  98. const ERA_NARROW = 'GGGGG';
  99. const DATES = 'F';
  100. const DATE_FULL = 'FFFFF';
  101. const DATE_LONG = 'FFFF';
  102. const DATE_MEDIUM = 'FFF';
  103. const DATE_SHORT = 'FF';
  104. const TIMES = 'WW';
  105. const TIME_FULL = 'TTTTT';
  106. const TIME_LONG = 'TTTT';
  107. const TIME_MEDIUM = 'TTT';
  108. const TIME_SHORT = 'TT';
  109. const DATETIME = 'K';
  110. const DATETIME_FULL = 'KKKKK';
  111. const DATETIME_LONG = 'KKKK';
  112. const DATETIME_MEDIUM = 'KKK';
  113. const DATETIME_SHORT = 'KK';
  114. const ATOM = 'OOO';
  115. const COOKIE = 'CCC';
  116. const RFC_822 = 'R';
  117. const RFC_850 = 'RR';
  118. const RFC_1036 = 'RRR';
  119. const RFC_1123 = 'RRRR';
  120. const RFC_3339 = 'RRRRR';
  121. const RSS = 'SSS';
  122. const W3C = 'WWW';
  123. /**
  124. * Generates the standard date object, could be a unix timestamp, localized date,
  125. * string, integer, array and so on. Also parts of dates or time are supported
  126. * Always set the default timezone: http://php.net/date_default_timezone_set
  127. * For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
  128. * For detailed instructions please look in the docu.
  129. *
  130. * @param string|integer|\Zend\Date\Date|array $date OPTIONAL Date value or value of date part to set
  131. * ,depending on $part. If null the actual time is set
  132. * @param string $part OPTIONAL Defines the input format of $date
  133. * @param string|\Zend\Locale\Locale $locale OPTIONAL Locale for parsing input
  134. * @return \Zend\Date\Date
  135. * @throws \Zend\Date\Exception
  136. */
  137. public function __construct($date = null, $part = null, $locale = null)
  138. {
  139. if (is_object($date) and !($date instanceof TimeSync\Protocol) and
  140. !($date instanceof Date)) {
  141. if ($locale instanceof Locale) {
  142. $locale = $date;
  143. $date = null;
  144. $part = null;
  145. } else {
  146. $date = (string) $date;
  147. }
  148. }
  149. if (($date !== null) and !is_array($date) and !($date instanceof TimeSync\Protocol) and
  150. !($date instanceof Date) and !defined($date) and Locale::isLocale($date, true)) {
  151. $locale = $date;
  152. $date = null;
  153. $part = null;
  154. } else if (($part !== null) and !defined($part) and Locale::isLocale($part, true)) {
  155. $locale = $part;
  156. $part = null;
  157. }
  158. $this->setLocale($locale);
  159. if (is_string($date) && ($part === null) && (strlen($date) <= 5)) {
  160. $part = $date;
  161. $date = null;
  162. }
  163. if ($date === null) {
  164. if ($part === null) {
  165. $date = time();
  166. } else if ($part !== self::TIMESTAMP) {
  167. $date = self::now($locale);
  168. $date = $date->get($part);
  169. }
  170. }
  171. if ($date instanceof TimeSync\Protocol) {
  172. $date = $date->getInfo();
  173. $date = $this->_getTime($date['offset']);
  174. $part = null;
  175. } else if (parent::$_defaultOffset != 0) {
  176. $date = $this->_getTime(parent::$_defaultOffset);
  177. }
  178. // set the timezone and offset for $this
  179. $zone = @date_default_timezone_get();
  180. $this->setTimezone($zone);
  181. // try to get timezone from date-string
  182. if (!is_int($date)) {
  183. $zone = $this->getTimezoneFromString($date);
  184. $this->setTimezone($zone);
  185. }
  186. // set datepart
  187. if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
  188. // switch off dst handling for value setting
  189. $this->setUnixTimestamp($this->getGmtOffset());
  190. $this->set($date, $part, $this->_locale);
  191. // DST fix
  192. if (is_array($date) === true) {
  193. if (!isset($date['hour'])) {
  194. $date['hour'] = 0;
  195. }
  196. $hour = $this->toString('H', 'iso', true);
  197. $hour = $date['hour'] - $hour;
  198. switch ($hour) {
  199. case 1 :
  200. case -23 :
  201. $this->addTimestamp(3600);
  202. break;
  203. case -1 :
  204. case 23 :
  205. $this->subTimestamp(3600);
  206. break;
  207. case 2 :
  208. case -22 :
  209. $this->addTimestamp(7200);
  210. break;
  211. case -2 :
  212. case 22 :
  213. $this->subTimestamp(7200);
  214. break;
  215. }
  216. }
  217. } else {
  218. $this->setUnixTimestamp($date);
  219. }
  220. }
  221. /**
  222. * Sets class wide options, if no option was given, the actual set options will be returned
  223. *
  224. * @param array $options Options to set
  225. * @throws \Zend\Date\Exception
  226. * @return Options array if no option was given
  227. */
  228. public static function setOptions(array $options = array())
  229. {
  230. if (empty($options)) {
  231. return self::$_options;
  232. }
  233. foreach ($options as $name => $value) {
  234. $name = strtolower($name);
  235. if (array_key_exists($name, self::$_options)) {
  236. switch($name) {
  237. case 'format_type' :
  238. if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
  239. throw new Exception\InvalidArgumentException("Unknown format type ($value) for dates, only 'iso' and 'php' supported"); /*, 0, null, $value */
  240. }
  241. break;
  242. case 'fix_dst' :
  243. if (!is_bool($value)) {
  244. throw new Exception\InvalidArgumentException("'fix_dst' has to be boolean"); /* , 0, null, $value */
  245. }
  246. break;
  247. case 'extend_month' :
  248. if (!is_bool($value)) {
  249. throw new Exception\InvalidArgumentException("'extend_month' has to be boolean"); /* ); */
  250. }
  251. break;
  252. case 'cache' :
  253. if ($value === null) {
  254. parent::$_cache = null;
  255. } else {
  256. if (!$value instanceof \Zend\Cache\Frontend) {
  257. throw new Exception\InvalidArgumentException("Instance of Zend_Cache expected");
  258. }
  259. parent::$_cache = $value;
  260. Cldr::setCache($value);
  261. }
  262. break;
  263. case 'timesync' :
  264. if ($value === null) {
  265. parent::$_defaultOffset = 0;
  266. } else {
  267. if (!$value instanceof TimeSync\Protocol) {
  268. throw new Exception\InvalidArgumentException("Instance of Zend_TimeSync expected for option timesync");
  269. }
  270. $date = $value->getInfo();
  271. parent::$_defaultOffset = $date['offset'];
  272. }
  273. break;
  274. }
  275. self::$_options[$name] = $value;
  276. }
  277. else {
  278. throw new Exception\InvalidArgumentException("Unknown option: $name = $value");
  279. }
  280. }
  281. }
  282. /**
  283. * Returns this object's internal UNIX timestamp (equivalent to Zend_Date::TIMESTAMP).
  284. * If the timestamp is too large for integers, then the return value will be a string.
  285. * This function does not return the timestamp as an object.
  286. * Use clone() or copyPart() instead.
  287. *
  288. * @return integer|string UNIX timestamp
  289. */
  290. public function getTimestamp()
  291. {
  292. return $this->getUnixTimestamp();
  293. }
  294. /**
  295. * Returns the calculated timestamp
  296. * HINT: timestamps are always GMT
  297. *
  298. * @param string $calc Type of calculation to make
  299. * @param string|integer|array|\Zend\Date\Date $stamp Timestamp to calculate, when null the actual timestamp is calculated
  300. * @return \Zend\Date\Date|integer
  301. * @throws \Zend\Date\Exception
  302. */
  303. private function _timestamp($calc, $stamp)
  304. {
  305. if ($stamp instanceof Date) {
  306. // extract timestamp from object
  307. $stamp = $stamp->getTimestamp();
  308. }
  309. if (is_array($stamp)) {
  310. if (isset($stamp['timestamp']) === true) {
  311. $stamp = $stamp['timestamp'];
  312. } else {
  313. throw new Exception\RuntimeException('no timestamp given in array');
  314. }
  315. }
  316. if ($calc === 'set') {
  317. $return = $this->setUnixTimestamp($stamp);
  318. } else {
  319. $return = $this->_calcdetail($calc, $stamp, self::TIMESTAMP, null);
  320. }
  321. if ($calc != 'cmp') {
  322. return $this;
  323. }
  324. return $return;
  325. }
  326. /**
  327. * Sets a new timestamp
  328. *
  329. * @param integer|string|array|\Zend\Date\Date $timestamp Timestamp to set
  330. * @return \Zend\Date\Date Provides fluid interface
  331. * @throws \Zend\Date\Exception
  332. */
  333. public function setTimestamp($timestamp)
  334. {
  335. return $this->_timestamp('set', $timestamp);
  336. }
  337. /**
  338. * Adds a timestamp
  339. *
  340. * @param integer|string|array|\Zend\Date\Date $timestamp Timestamp to add
  341. * @return \Zend\Date\Date Provides fluid interface
  342. * @throws \Zend\Date\Exception
  343. */
  344. public function addTimestamp($timestamp)
  345. {
  346. return $this->_timestamp('add', $timestamp);
  347. }
  348. /**
  349. * Subtracts a timestamp
  350. *
  351. * @param integer|string|array|\Zend\Date\Date $timestamp Timestamp to sub
  352. * @return \Zend\Date\Date Provides fluid interface
  353. * @throws \Zend\Date\Exception
  354. */
  355. public function subTimestamp($timestamp)
  356. {
  357. return $this->_timestamp('sub', $timestamp);
  358. }
  359. /**
  360. * Compares two timestamps, returning the difference as integer
  361. *
  362. * @param integer|string|array|\Zend\Date\Date $timestamp Timestamp to compare
  363. * @return integer 0 = equal, 1 = later, -1 = earlier
  364. * @throws \Zend\Date\Exception
  365. */
  366. public function compareTimestamp($timestamp)
  367. {
  368. return $this->_timestamp('cmp', $timestamp);
  369. }
  370. /**
  371. * Returns a string representation of the object
  372. * Supported format tokens are:
  373. * G - era, y - year, Y - ISO year, M - month, w - week of year, D - day of year, d - day of month
  374. * E - day of week, e - number of weekday (1-7), h - hour 1-12, H - hour 0-23, m - minute, s - second
  375. * A - milliseconds of day, z - timezone, Z - timezone offset, S - fractional second, a - period of day
  376. *
  377. * Additionally format tokens but non ISO conform are:
  378. * SS - day suffix, eee - php number of weekday(0-6), ddd - number of days per month
  379. * l - Leap year, B - swatch internet time, I - daylight saving time, X - timezone offset in seconds
  380. * r - RFC2822 format, U - unix timestamp
  381. *
  382. * Not supported ISO tokens are
  383. * u - extended year, Q - quarter, q - quarter, L - stand alone month, W - week of month
  384. * F - day of week of month, g - modified julian, c - stand alone weekday, k - hour 0-11, K - hour 1-24
  385. * v - wall zone
  386. *
  387. * @param string $format OPTIONAL Rule for formatting output. If null the default date format is used
  388. * @param string $type OPTIONAL Type for the format string which overrides the standard setting
  389. * @param string|\Zend\Locale\Locale $locale OPTIONAL Locale for parsing input
  390. * @return string
  391. */
  392. public function toString($format = null, $type = null, $locale = null)
  393. {
  394. if (is_object($format)) {
  395. if ($format instanceof Locale) {
  396. $locale = $format;
  397. $format = null;
  398. } else {
  399. $format = (string) $format;
  400. }
  401. }
  402. if (is_object($type)) {
  403. if ($type instanceof Locale) {
  404. $locale = $type;
  405. $type = null;
  406. } else {
  407. $type = (string) $type;
  408. }
  409. }
  410. if (($format !== null) && !defined($format)
  411. && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
  412. && Locale::isLocale($format)
  413. ) {
  414. $locale = $format;
  415. $format = null;
  416. }
  417. if (($type !== null) and ($type != 'php') and ($type != 'iso') and
  418. Locale::isLocale($type)) {
  419. $locale = $type;
  420. $type = null;
  421. }
  422. if ($locale === null) {
  423. $locale = $this->getLocale();
  424. }
  425. if ($format === null) {
  426. $format = Format::getDateFormat($locale) . ' ' . Format::getTimeFormat($locale);
  427. } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
  428. $format = Format::convertPhpToIsoFormat($format);
  429. }
  430. return $this->date($this->_toToken($format, $locale), $this->getUnixTimestamp(), false);
  431. }
  432. /**
  433. * Returns a string representation of the date which is equal with the timestamp
  434. *
  435. * @return string
  436. */
  437. public function __toString()
  438. {
  439. return $this->toString(null, $this->_locale);
  440. }
  441. /**
  442. * Returns a integer representation of the object
  443. * But returns false when the given part is no value f.e. Month-Name
  444. *
  445. * @param string|integer|\Zend\Date\Date $part OPTIONAL Defines the date or datepart to return as integer
  446. * @return integer|false
  447. */
  448. public function toValue($part = null)
  449. {
  450. $result = $this->get($part);
  451. if (is_numeric($result)) {
  452. return intval("$result");
  453. } else {
  454. return false;
  455. }
  456. }
  457. /**
  458. * Returns an array representation of the object
  459. *
  460. * @return array
  461. */
  462. public function toArray()
  463. {
  464. return array('day' => $this->toString(self::DAY_SHORT, 'iso'),
  465. 'month' => $this->toString(self::MONTH_SHORT, 'iso'),
  466. 'year' => $this->toString(self::YEAR, 'iso'),
  467. 'hour' => $this->toString(self::HOUR_SHORT, 'iso'),
  468. 'minute' => $this->toString(self::MINUTE_SHORT, 'iso'),
  469. 'second' => $this->toString(self::SECOND_SHORT, 'iso'),
  470. 'timezone' => $this->toString(self::TIMEZONE, 'iso'),
  471. 'timestamp' => $this->toString(self::TIMESTAMP, 'iso'),
  472. 'weekday' => $this->toString(self::WEEKDAY_8601, 'iso'),
  473. 'dayofyear' => $this->toString(self::DAY_OF_YEAR, 'iso'),
  474. 'week' => $this->toString(self::WEEK, 'iso'),
  475. 'gmtsecs' => $this->toString(self::TIMEZONE_SECS, 'iso'));
  476. }
  477. /**
  478. * Returns a representation of a date or datepart
  479. * This could be for example a localized monthname, the time without date,
  480. * the era or only the fractional seconds. There are about 50 different supported date parts.
  481. * For a complete list of supported datepart values look into the docu
  482. *
  483. * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned
  484. * @param string|\Zend\Locale\Locale $locale OPTIONAL Locale for parsing input
  485. * @return string date or datepart
  486. */
  487. public function get($part = null, $locale = null)
  488. {
  489. if ($locale === null) {
  490. $locale = $this->getLocale();
  491. }
  492. if (($part !== null) && !defined($part)
  493. && ($part != 'ee') && ($part != 'ss') && ($part != 'GG') && ($part != 'MM') && ($part != 'EE') && ($part != 'TT')
  494. && Locale::isLocale($part)
  495. ) {
  496. $locale = $part;
  497. $part = null;
  498. }
  499. if ($part === null) {
  500. $part = self::TIMESTAMP;
  501. } else if (self::$_options['format_type'] == 'php') {
  502. $part = Format::convertPhpToIsoFormat($part);
  503. }
  504. return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);
  505. }
  506. /**
  507. * Internal method to apply tokens
  508. *
  509. * @param string $part
  510. * @param string $locale
  511. * @return string
  512. */
  513. private function _toToken($part, $locale) {
  514. // get format tokens
  515. $comment = false;
  516. $format = '';
  517. $orig = '';
  518. for ($i = 0; isset($part[$i]); ++$i) {
  519. if ($part[$i] == "'") {
  520. $comment = $comment ? false : true;
  521. if (isset($part[$i+1]) && ($part[$i+1] == "'")) {
  522. $comment = $comment ? false : true;
  523. $format .= "\\'";
  524. ++$i;
  525. }
  526. $orig = '';
  527. continue;
  528. }
  529. if ($comment) {
  530. $format .= '\\' . $part[$i];
  531. $orig = '';
  532. } else {
  533. $orig .= $part[$i];
  534. if (!isset($part[$i+1]) || (isset($orig[0]) && ($orig[0] != $part[$i+1]))) {
  535. $format .= $this->_parseIsoToDate($orig, $locale);
  536. $orig = '';
  537. }
  538. }
  539. }
  540. return $format;
  541. }
  542. /**
  543. * Internal parsing method
  544. *
  545. * @param string $token
  546. * @param string $locale
  547. * @return string
  548. */
  549. private function _parseIsoToDate($token, $locale) {
  550. switch($token) {
  551. case self::DAY :
  552. return 'd';
  553. break;
  554. case self::WEEKDAY_SHORT :
  555. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  556. $day = Cldr::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
  557. return $this->_toComment(iconv_substr($day, 0, 3, 'UTF-8'));
  558. break;
  559. case self::DAY_SHORT :
  560. return 'j';
  561. break;
  562. case self::WEEKDAY :
  563. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  564. return $this->_toComment(Cldr::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)));
  565. break;
  566. case self::WEEKDAY_8601 :
  567. return 'N';
  568. break;
  569. case 'ee' :
  570. return $this->_toComment(str_pad($this->date('N', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  571. break;
  572. case self::DAY_SUFFIX :
  573. return 'S';
  574. break;
  575. case self::WEEKDAY_DIGIT :
  576. return 'w';
  577. break;
  578. case self::DAY_OF_YEAR :
  579. return 'z';
  580. break;
  581. case 'DDD' :
  582. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 3, '0', STR_PAD_LEFT));
  583. break;
  584. case 'DD' :
  585. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  586. break;
  587. case self::WEEKDAY_NARROW :
  588. case 'EEEEE' :
  589. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  590. $day = Cldr::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
  591. return $this->_toComment(iconv_substr($day, 0, 1, 'UTF-8'));
  592. break;
  593. case self::WEEKDAY_NAME :
  594. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  595. return $this->_toComment(Cldr::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)));
  596. break;
  597. case 'w' :
  598. $week = $this->date('W', $this->getUnixTimestamp(), false);
  599. return $this->_toComment(($week[0] == '0') ? $week[1] : $week);
  600. break;
  601. case self::WEEK :
  602. return 'W';
  603. break;
  604. case self::MONTH_NAME :
  605. $month = $this->date('n', $this->getUnixTimestamp(), false);
  606. return $this->_toComment(Cldr::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)));
  607. break;
  608. case self::MONTH :
  609. return 'm';
  610. break;
  611. case self::MONTH_NAME_SHORT :
  612. $month = $this->date('n', $this->getUnixTimestamp(), false);
  613. return $this->_toComment(Cldr::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)));
  614. break;
  615. case self::MONTH_SHORT :
  616. return 'n';
  617. break;
  618. case self::MONTH_DAYS :
  619. return 't';
  620. break;
  621. case self::MONTH_NAME_NARROW :
  622. $month = $this->date('n', $this->getUnixTimestamp(), false);
  623. $mon = Cldr::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
  624. return $this->_toComment(iconv_substr($mon, 0, 1, 'UTF-8'));
  625. break;
  626. case self::LEAPYEAR :
  627. return 'L';
  628. break;
  629. case self::YEAR_8601 :
  630. return 'o';
  631. break;
  632. case self::YEAR :
  633. return 'Y';
  634. break;
  635. case self::YEAR_SHORT :
  636. return 'y';
  637. break;
  638. case self::YEAR_SHORT_8601 :
  639. return $this->_toComment(substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2));
  640. break;
  641. case self::MERIDIEM :
  642. $am = $this->date('a', $this->getUnixTimestamp(), false);
  643. if ($am == 'am') {
  644. return $this->_toComment(Cldr::getContent($locale, 'am'));
  645. }
  646. return $this->_toComment(Cldr::getContent($locale, 'pm'));
  647. break;
  648. case self::SWATCH :
  649. return 'B';
  650. break;
  651. case self::HOUR_SHORT_AM :
  652. return 'g';
  653. break;
  654. case self::HOUR_SHORT :
  655. return 'G';
  656. break;
  657. case self::HOUR_AM :
  658. return 'h';
  659. break;
  660. case self::HOUR :
  661. return 'H';
  662. break;
  663. case self::MINUTE :
  664. return $this->_toComment(str_pad($this->date('i', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  665. break;
  666. case self::SECOND :
  667. return $this->_toComment(str_pad($this->date('s', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  668. break;
  669. case self::MINUTE_SHORT :
  670. return 'i';
  671. break;
  672. case self::SECOND_SHORT :
  673. return 's';
  674. break;
  675. case self::MILLISECOND :
  676. return $this->_toComment($this->getMilliSecond());
  677. break;
  678. case self::TIMEZONE_NAME :
  679. case 'vvvv' :
  680. return 'e';
  681. break;
  682. case self::DAYLIGHT :
  683. return 'I';
  684. break;
  685. case self::GMT_DIFF :
  686. case 'ZZ' :
  687. case 'ZZZ' :
  688. return 'O';
  689. break;
  690. case self::GMT_DIFF_SEP :
  691. return 'P';
  692. break;
  693. case self::TIMEZONE :
  694. case 'v' :
  695. case 'zz' :
  696. case 'zzz' :
  697. return 'T';
  698. break;
  699. case self::TIMEZONE_SECS :
  700. return 'Z';
  701. break;
  702. case self::ISO_8601 :
  703. return 'c';
  704. break;
  705. case self::RFC_2822 :
  706. return 'r';
  707. break;
  708. case self::TIMESTAMP :
  709. return 'U';
  710. break;
  711. case self::ERA :
  712. case 'GG' :
  713. case 'GGG' :
  714. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  715. if ($year < 0) {
  716. return $this->_toComment(Cldr::getContent($locale, 'era', array('gregorian', 'Abbr', '0')));
  717. }
  718. return $this->_toComment(Cldr::getContent($locale, 'era', array('gregorian', 'Abbr', '1')));
  719. break;
  720. case self::ERA_NARROW :
  721. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  722. if ($year < 0) {
  723. return $this->_toComment(iconv_substr(Cldr::getContent($locale, 'era', array('gregorian', 'Abbr', '0')), 0, 1, 'UTF-8')) . '.';
  724. }
  725. return $this->_toComment(iconv_substr(Cldr::getContent($locale, 'era', array('gregorian', 'Abbr', '1')), 0, 1, 'UTF-8')) . '.';
  726. break;
  727. case self::ERA_NAME :
  728. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  729. if ($year < 0) {
  730. return $this->_toComment(Cldr::getContent($locale, 'era', array('gregorian', 'Names', '0')));
  731. }
  732. return $this->_toComment(Cldr::getContent($locale, 'era', array('gregorian', 'Names', '1')));
  733. break;
  734. case self::DATES :
  735. return $this->_toToken(Format::getDateFormat($locale), $locale);
  736. break;
  737. case self::DATE_FULL :
  738. return $this->_toToken(Cldr::getContent($locale, 'date', array('gregorian', 'full')), $locale);
  739. break;
  740. case self::DATE_LONG :
  741. return $this->_toToken(Cldr::getContent($locale, 'date', array('gregorian', 'long')), $locale);
  742. break;
  743. case self::DATE_MEDIUM :
  744. return $this->_toToken(Cldr::getContent($locale, 'date', array('gregorian', 'medium')), $locale);
  745. break;
  746. case self::DATE_SHORT :
  747. return $this->_toToken(Cldr::getContent($locale, 'date', array('gregorian', 'short')), $locale);
  748. break;
  749. case self::TIMES :
  750. return $this->_toToken(Format::getTimeFormat($locale), $locale);
  751. break;
  752. case self::TIME_FULL :
  753. return $this->_toToken(Cldr::getContent($locale, 'time', 'full'), $locale);
  754. break;
  755. case self::TIME_LONG :
  756. return $this->_toToken(Cldr::getContent($locale, 'time', 'long'), $locale);
  757. break;
  758. case self::TIME_MEDIUM :
  759. return $this->_toToken(Cldr::getContent($locale, 'time', 'medium'), $locale);
  760. break;
  761. case self::TIME_SHORT :
  762. return $this->_toToken(Cldr::getContent($locale, 'time', 'short'), $locale);
  763. break;
  764. case self::DATETIME :
  765. return $this->_toToken(Format::getDateTimeFormat($locale), $locale);
  766. break;
  767. case self::DATETIME_FULL :
  768. return $this->_toToken(Cldr::getContent($locale, 'datetime', array('gregorian', 'full')), $locale);
  769. break;
  770. case self::DATETIME_LONG :
  771. return $this->_toToken(Cldr::getContent($locale, 'datetime', array('gregorian', 'long')), $locale);
  772. break;
  773. case self::DATETIME_MEDIUM :
  774. return $this->_toToken(Cldr::getContent($locale, 'datetime', array('gregorian', 'medium')), $locale);
  775. break;
  776. case self::DATETIME_SHORT :
  777. return $this->_toToken(Cldr::getContent($locale, 'datetime', array('gregorian', 'short')), $locale);
  778. break;
  779. case self::ATOM :
  780. return 'Y\-m\-d\TH\:i\:sP';
  781. break;
  782. case self::COOKIE :
  783. return 'l\, d\-M\-y H\:i\:s e';
  784. break;
  785. case self::RFC_822 :
  786. return 'D\, d M y H\:i\:s O';
  787. break;
  788. case self::RFC_850 :
  789. return 'l\, d\-M\-y H\:i\:s e';
  790. break;
  791. case self::RFC_1036 :
  792. return 'D\, d M y H\:i\:s O';
  793. break;
  794. case self::RFC_1123 :
  795. return 'D\, d M Y H\:i\:s O';
  796. break;
  797. case self::RFC_3339 :
  798. return 'Y\-m\-d\TH\:i\:sP';
  799. break;
  800. case self::RSS :
  801. return 'D\, d M Y H\:i\:s O';
  802. break;
  803. case self::W3C :
  804. return 'Y\-m\-d\TH\:i\:sP';
  805. break;
  806. }
  807. if ($token == '') {
  808. return '';
  809. }
  810. switch ($token[0]) {
  811. case 'y' :
  812. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  813. return 'Y';
  814. }
  815. $length = iconv_strlen($token, 'UTF-8');
  816. return $this->_toComment(str_pad($this->date('Y', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  817. break;
  818. case 'Y' :
  819. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  820. return 'o';
  821. }
  822. $length = iconv_strlen($token, 'UTF-8');
  823. return $this->_toComment(str_pad($this->date('o', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  824. break;
  825. case 'A' :
  826. $length = iconv_strlen($token, 'UTF-8');
  827. $result = substr($this->getMilliSecond(), 0, 3);
  828. $result += $this->date('s', $this->getUnixTimestamp(), false) * 1000;
  829. $result += $this->date('i', $this->getUnixTimestamp(), false) * 60000;
  830. $result += $this->date('H', $this->getUnixTimestamp(), false) * 3600000;
  831. return $this->_toComment(str_pad($result, $length, '0', STR_PAD_LEFT));
  832. break;
  833. }
  834. return $this->_toComment($token);
  835. }
  836. /**
  837. * Private function to make a comment of a token
  838. *
  839. * @param string $token
  840. * @return string
  841. */
  842. private function _toComment($token)
  843. {
  844. $token = str_split($token);
  845. $result = '';
  846. foreach ($token as $tok) {
  847. $result .= '\\' . $tok;
  848. }
  849. return $result;
  850. }
  851. /**
  852. * Return digit from standard names (english)
  853. * Faster implementation than locale aware searching
  854. *
  855. * @param string $name
  856. * @return integer Number of this month
  857. * @throws \Zend\Date\Exception
  858. */
  859. private function _getDigitFromName($name)
  860. {
  861. switch($name) {
  862. case "Jan":
  863. return 1;
  864. case "Feb":
  865. return 2;
  866. case "Mar":
  867. return 3;
  868. case "Apr":
  869. return 4;
  870. case "May":
  871. return 5;
  872. case "Jun":
  873. return 6;
  874. case "Jul":
  875. return 7;
  876. case "Aug":
  877. return 8;
  878. case "Sep":
  879. return 9;
  880. case "Oct":
  881. return 10;
  882. case "Nov":
  883. return 11;
  884. case "Dec":
  885. return 12;
  886. default:
  887. throw new Exception\InvalidArgumentException('Month ($name) is not a known month');
  888. }
  889. }
  890. /**
  891. * Counts the exact year number
  892. * < 70 - 2000 added, >70 < 100 - 1900, others just returned
  893. *
  894. * @param integer $value year number
  895. * @return integer Number of year
  896. */
  897. public static function getFullYear($value)
  898. {
  899. if ($value >= 0) {
  900. if ($value < 70) {
  901. $value += 2000;
  902. } else if ($value < 100) {
  903. $value += 1900;
  904. }
  905. }
  906. return $value;
  907. }
  908. /**
  909. * Sets the given date as new date or a given datepart as new datepart returning the new datepart
  910. * This could be for example a localized dayname, the date without time,
  911. * the month or only the seconds. There are about 50 different supported date parts.
  912. * For a complete list of supported datepart values look into the docu
  913. *
  914. * @param string|integer|array|\Zend\Date\Date $date Date or datepart to set
  915. * @param string $part OPTIONAL Part of the date to set, if null the timestamp is set
  916. * @param string|\Zend\Locale\Locale $locale OPTIONAL Locale for parsing input
  917. * @return \Zend\Date\Date Provides fluid interface
  918. * @throws \Zend\Date\Exception
  919. */
  920. public function set($date, $part = null, $locale = null)
  921. {
  922. if (self::$_options['format_type'] == 'php') {
  923. $part = Format::convertPhpToIsoFormat($part);
  924. }
  925. $zone = $this->getTimezoneFromString($date);
  926. $this->setTimezone($zone);
  927. $this->_calculate('set', $date, $part, $locale);
  928. return $this;
  929. }
  930. /**
  931. * Adds a date or datepart to the existing date, by extracting $part from $date,
  932. * and modifying this object by adding that part. The $part is then extracted from
  933. * this object and returned as an integer or numeric string (for large values, or $part's
  934. * corresponding to pre-defined formatted date strings).
  935. * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
  936. * There are about 50 different supported date parts.
  937. * For a complete list of supported datepart values look into the docu.
  938. *
  939. * @param string|integer|array|\Zend\Date\Date $date Date or datepart to add
  940. * @param string $part OPTIONAL Part of the date to add, if null the timestamp is added
  941. * @param string|\Zend\Locale\Locale $locale OPTIONAL Locale for parsing input
  942. * @return \Zend\Date\Date Provides fluid interface
  943. * @throws \Zend\Date\Exception
  944. */
  945. public function add($date, $part = self::TIMESTAMP, $locale = null)
  946. {
  947. if (self::$_options['format_type'] == 'php') {
  948. $part = Format::convertPhpToIsoFormat($part);
  949. }
  950. $this->_calculate('add', $date, $part, $locale);
  951. return $this;
  952. }
  953. /**
  954. * Subtracts a date from another date.
  955. * This could be for example a RFC2822 date, the time,
  956. * the year or only the timestamp. There are about 50 different supported date parts.
  957. * For a complete list of supported datepart values look into the docu
  958. * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
  959. *
  960. * @param string|integer|array|\Zend\Date\Date $date Date or datepart to subtract
  961. * @param string $part OPTIONAL Part of the date to sub, if null the timestamp is subtracted
  962. * @param string|\Zend\Locale\Locale $locale OPTIONAL Locale for parsing input
  963. * @return \Zend\Date\Date Provides fluid interface
  964. * @throws \Zend\Date\Exception
  965. */
  966. public function sub($date, $part = self::TIMESTAMP, $locale = null)
  967. {
  968. if (self::$_options['format_type'] == 'php') {
  969. $part = Format::convertPhpToIsoFormat($part);
  970. }
  971. $this->_calculate('sub', $date, $part, $locale);
  972. return $this;
  973. }
  974. /**
  975. * Compares a date or datepart with the existing one.
  976. * Returns -1 if earlier, 0 if equal and 1 if later.
  977. *
  978. * @param string|integer|array|\Zend\Date\Date $date Date or datepart to compare with the date object
  979. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is subtracted
  980. * @param string|\Zend\Locale\Locale $locale OPTIONAL Locale for parsing input
  981. * @return integer 0 = equal, 1 = later, -1 = earlier
  982. * @throws \Zend\Date\Exception
  983. */
  984. public function compare($date, $part = self::TIMESTAMP, $locale = null)
  985. {
  986. if (self::$_options['format_type'] == 'php') {
  987. $part = Format::convertPhpToIsoFormat($part);
  988. }
  989. $compare = $this->_calculate('cmp', $date, $part, $locale);
  990. if ($compare > 0) {
  991. return 1;
  992. } else if ($compare < 0) {
  993. return -1;
  994. }
  995. return 0;
  996. }
  997. /**
  998. * Returns a new instance of Zend_Date with the selected part copied.
  999. * To make an exact copy, use PHP's clone keyword.
  1000. * For a complete list of supported date part values look into the docu.
  1001. * If a date part is copied, all other date parts are set to standard values.
  1002. * For example: If only YEAR is copied, the returned date object is equal to
  1003. * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
  1004. * If only HOUR is copied, the returned date object is equal to
  1005. * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
  1006. *
  1007. * @param string $part Part of the date to compare, if null the timestamp is subtracted
  1008. * @param string|\Zend\Locale\Locale $locale OPTIONAL New object's locale. No adjustments to timezone are made.
  1009. * @return \Zend\Date\Date New clone with requested part
  1010. */
  1011. public function copyPart($part, $locale = null)
  1012. {
  1013. $clone = clone $this; // copy all instance variables
  1014. $clone->setUnixTimestamp(0); // except the timestamp
  1015. if ($locale != null) {
  1016. $clone->setLocale($locale); // set an other locale if selected
  1017. }
  1018. $clone->set($this, $part);
  1019. return $clone;
  1020. }
  1021. /**
  1022. * Internal function, returns the offset of a given timezone
  1023. *
  1024. * @param string $zone
  1025. * @return integer
  1026. */
  1027. public function getTimezoneFromString($zone)
  1028. {
  1029. if (is_array($zone)) {
  1030. return $this->getTimezone();
  1031. }
  1032. if ($zone instanceof Date) {
  1033. return $zone->getTimezone();
  1034. }
  1035. $match = array();
  1036. preg_match('/\dZ$/', $zone, $match);
  1037. if (!empty($match)) {
  1038. return "Etc/UTC";
  1039. }
  1040. preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
  1041. if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
  1042. $zone = "Etc/GMT";
  1043. $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
  1044. $zone .= (int) abs($match[count($match) - 1]);
  1045. return $zone;
  1046. }
  1047. preg_match('/([[:alpha:]\/]{3,30})(?!.*([[:alpha:]\/]{3,30}))/', $zone, $match);
  1048. try {
  1049. if (!empty($match) and (!is_int($match[count($match) - 1]))) {
  1050. $oldzone = $this->getTimezone();
  1051. $this->setTimezone($match[count($match) - 1]);
  1052. $result = $this->getTimezone();
  1053. $this->setTimezone($oldzone);
  1054. if ($result !== $oldzone) {
  1055. return $match[count($match) - 1];
  1056. }
  1057. }
  1058. } catch (\Exception $e) {
  1059. // fall through
  1060. }
  1061. return $this->getTimezone();
  1062. }
  1063. /**
  1064. * Calculates the date or object
  1065. *
  1066. * @param string $calc Calculation to make
  1067. * @param string|integer $date Date for calculation
  1068. * @param string|integer $comp Second date for calculation
  1069. * @param boolean|integer $dst Use dst correction if option is set
  1070. * @return integer|string|\Zend\Date\Date new timestamp or \Zend\Date\Date depending on calculation
  1071. */
  1072. private function _assign($calc, $date, $comp = 0, $dst = false)
  1073. {
  1074. switch ($calc) {
  1075. case 'set' :
  1076. if (!empty($comp)) {
  1077. $this->setUnixTimestamp(call_user_func(Math::$sub, $this->getUnixTimestamp(), $comp));
  1078. }
  1079. $this->setUnixTimestamp(call_user_func(Math::$add, $this->getUnixTimestamp(), $date));
  1080. $value = $this->getUnixTimestamp();
  1081. break;
  1082. case 'add' :
  1083. $this->setUnixTimestamp(call_user_func(Math::$add, $this->getUnixTimestamp(), $date));
  1084. $value = $this->getUnixTimestamp();
  1085. break;
  1086. case 'sub' :
  1087. $this->setUnixTimestamp(call_user_func(Math::$sub, $this->getUnixTimestamp(), $date));
  1088. $value = $this->getUnixTimestamp();
  1089. break;
  1090. default :
  1091. // cmp - compare
  1092. return call_user_func(Math::$comp, $comp, $date);
  1093. break;
  1094. }
  1095. // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
  1096. if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
  1097. $hour = $this->toString(self::HOUR, 'iso');
  1098. if ($hour != $dst) {
  1099. if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
  1100. $value += 3600;
  1101. } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
  1102. $value -= 3600;
  1103. }
  1104. $this->setUnixTimestamp($value);
  1105. }
  1106. }
  1107. return $this->getUnixTimestamp();
  1108. }
  1109. /**
  1110. * Calculates the date or object
  1111. *
  1112. * @param string $calc Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set'
  1113. * @param string|integer|array|\Zend\Date\Date $date Date or datepart to calculate with
  1114. * @param string $part Part of the date to calculate, if null the timestamp is used
  1115. * @param string|\Zend\Locale\Locale $locale Locale for parsing input
  1116. * @return integer|string|\Zend\Date\Date new timestamp
  1117. * @throws \Zend\Date\Exception
  1118. */
  1119. private function _calculate($calc, $date, $part, $locale)
  1120. {
  1121. if ($date === null) {
  1122. throw new Exception\RuntimeException('parameter $date must be set, null is not allowed');
  1123. }
  1124. if (($part !== null) && (strlen($part) !== 2) && (Locale::isLocale($part))) {
  1125. $locale = $part;
  1126. $part = null;
  1127. }
  1128. if ($locale === null) {
  1129. $locale = $this->getLocale();
  1130. }
  1131. $locale = (string) $locale;
  1132. // Create date parts
  1133. $year = $this->toString(self::YEAR, 'iso');
  1134. $month = $this->toString(self::MONTH_SHORT, 'iso');
  1135. $day = $this->toString(self::DAY_SHORT, 'iso');
  1136. $hour = $this->toString(self::HOUR_SHORT, 'iso');
  1137. $minute = $this->toString(self::MINUTE_SHORT, 'iso');
  1138. $second = $this->toString(self::SECOND_SHORT, 'iso');
  1139. // If object extract value
  1140. if ($date instanceof Date) {
  1141. $date = $date->toString($part, 'iso', $locale);
  1142. }
  1143. if (is_array($date) === true) {
  1144. if (empty($part) === false) {
  1145. switch($part) {
  1146. // Fall through
  1147. case self::DAY:
  1148. case self::DAY_SHORT:
  1149. if (isset($date['day']) === true) {
  1150. $date = $date['day'];
  1151. }
  1152. break;
  1153. // Fall through
  1154. case self::WEEKDAY_SHORT:
  1155. case self::WEEKDAY:
  1156. case self::WEEKDAY_8601:
  1157. case self::WEEKDAY_DIGIT:
  1158. case self::WEEKDAY_NARROW:
  1159. case self::WEEKDAY_NAME:
  1160. if (isset($date['weekday']) === true) {
  1161. $date = $date['weekday'];
  1162. $part = self::WEEKDAY_DIGIT;
  1163. }
  1164. break;
  1165. case self::DAY_OF_YEAR:
  1166. if (isset($date['day_of_year']) === true) {
  1167. $date = $date['day_of_year'];
  1168. }
  1169. break;
  1170. // Fall through
  1171. case self::MONTH:
  1172. case self::MONTH_SHORT:
  1173. case self::MONTH_NAME:
  1174. case self::MONTH_NAME_SHORT:
  1175. case self::MONTH_NAME_NARROW:
  1176. if (isset($date['month']) === true) {
  1177. $date = $date['month'];
  1178. }
  1179. break;
  1180. // Fall through
  1181. case self::YEAR:
  1182. case self::YEAR_SHORT:
  1183. case self::YEAR_8601:
  1184. case self::YEAR_SHORT_8601:
  1185. if (isset($date['year']) === true) {
  1186. $date = $date['year'];
  1187. }
  1188. break;
  1189. // Fall through
  1190. case self::HOUR:
  1191. case self::HOUR_AM:
  1192. case self::HOUR_SHORT:
  1193. case self::HOUR_SHORT_AM:
  1194. if (isset($date['hour']) === true) {
  1195. $date = $date['hour'];
  1196. }
  1197. break;
  1198. // Fall through
  1199. case self::MINUTE:
  1200. case self::MINUTE_SHORT:
  1201. if (isset($date['minute']) === true) {
  1202. $date = $date['minute'];
  1203. }
  1204. break;
  1205. // Fall through
  1206. case self::SECOND:
  1207. case self::SECOND_SHORT:
  1208. if (isset($date['second']) === true) {

Large files files are truncated, but you can click here to view the full file