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

/ railoapacheportable/php/PEAR/Zend/Date.php

http://railoapacheportable.googlecode.com/
PHP | 1683 lines | 1279 code | 110 blank | 294 comment | 188 complexity | d3e71ae6b22c2ce2017587330d81c0f6 MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, EPL-1.0, LGPL-3.0, GPL-3.0, AGPL-1.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, GPL-2.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, CC-BY-SA-3.0

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

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