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

/library/Zend/Date.php

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

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