PageRenderTime 64ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Date/Date.php

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

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