PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Date.php

https://bitbucket.org/imaweda/jelly2
PHP | 4954 lines | 3176 code | 503 blank | 1275 comment | 643 complexity | cbe6534559dd8f79b8ba920d9e9ec9d6 MD5 | raw file
Possible License(s): BSD-3-Clause

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

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

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