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

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

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

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