PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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

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. 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;

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