PageRenderTime 46ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/cubi/openbiz/others/Zend/Date.php

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

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