PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/website/library/Zend/Date.php

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

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