PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 1ms

/www/system/library/Zend/Date.php

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

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