PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/library/Zend/Date.php

https://bitbucket.org/mayorbrain/precurio-v2
PHP | 4923 lines | 3154 code | 494 blank | 1275 comment | 600 complexity | a6297af7416e75ace2e27b6203ea1fe5 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, MIT

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 20936 2010-02-05 22:36:44Z 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) and !defined($format) and
  412. ($format != 'ee') and ($format != 'ss') and Zend_Locale::isLocale($format, null, false)) {
  413. $locale = $format;
  414. $format = null;
  415. }
  416. if (($type !== null) and ($type != 'php') and ($type != 'iso') and
  417. Zend_Locale::isLocale($type, null, false)) {
  418. $locale = $type;
  419. $type = null;
  420. }
  421. if ($locale === null) {
  422. $locale = $this->getLocale();
  423. }
  424. if ($format === null) {
  425. $format = Zend_Locale_Format::getDateFormat($locale) . ' ' . Zend_Locale_Format::getTimeFormat($locale);
  426. } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
  427. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  428. }
  429. return $this->date($this->_toToken($format, $locale), $this->getUnixTimestamp(), false);
  430. }
  431. /**
  432. * Returns a string representation of the date which is equal with the timestamp
  433. *
  434. * @return string
  435. */
  436. public function __toString()
  437. {
  438. return $this->toString(null, $this->_locale);
  439. }
  440. /**
  441. * Returns a integer representation of the object
  442. * But returns false when the given part is no value f.e. Month-Name
  443. *
  444. * @param string|integer|Zend_Date $part OPTIONAL Defines the date or datepart to return as integer
  445. * @return integer|false
  446. */
  447. public function toValue($part = null)
  448. {
  449. $result = $this->get($part);
  450. if (is_numeric($result)) {
  451. return intval("$result");
  452. } else {
  453. return false;
  454. }
  455. }
  456. /**
  457. * Returns an array representation of the object
  458. *
  459. * @return array
  460. */
  461. public function toArray()
  462. {
  463. return array('day' => $this->toString(self::DAY_SHORT, 'iso'),
  464. 'month' => $this->toString(self::MONTH_SHORT, 'iso'),
  465. 'year' => $this->toString(self::YEAR, 'iso'),
  466. 'hour' => $this->toString(self::HOUR_SHORT, 'iso'),
  467. 'minute' => $this->toString(self::MINUTE_SHORT, 'iso'),
  468. 'second' => $this->toString(self::SECOND_SHORT, 'iso'),
  469. 'timezone' => $this->toString(self::TIMEZONE, 'iso'),
  470. 'timestamp' => $this->toString(self::TIMESTAMP, 'iso'),
  471. 'weekday' => $this->toString(self::WEEKDAY_8601, 'iso'),
  472. 'dayofyear' => $this->toString(self::DAY_OF_YEAR, 'iso'),
  473. 'week' => $this->toString(self::WEEK, 'iso'),
  474. 'gmtsecs' => $this->toString(self::TIMEZONE_SECS, 'iso'));
  475. }
  476. /**
  477. * Returns a representation of a date or datepart
  478. * This could be for example a localized monthname, the time without date,
  479. * the era or only the fractional seconds. There are about 50 different supported date parts.
  480. * For a complete list of supported datepart values look into the docu
  481. *
  482. * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned
  483. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  484. * @return string date or datepart
  485. */
  486. public function get($part = null, $locale = null)
  487. {
  488. if ($locale === null) {
  489. $locale = $this->getLocale();
  490. }
  491. if (($part !== null) and !defined($part) and
  492. ($part != 'ee') and ($part != 'ss') and Zend_Locale::isLocale($part, null, false)) {
  493. $locale = $part;
  494. $part = null;
  495. }
  496. if ($part === null) {
  497. $part = self::TIMESTAMP;
  498. } else if (self::$_options['format_type'] == 'php') {
  499. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  500. }
  501. return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);
  502. }
  503. /**
  504. * Internal method to apply tokens
  505. *
  506. * @param string $part
  507. * @param string $locale
  508. * @return string
  509. */
  510. private function _toToken($part, $locale) {
  511. // get format tokens
  512. $comment = false;
  513. $format = '';
  514. $orig = '';
  515. for ($i = 0; $i < strlen($part); ++$i) {
  516. if ($part[$i] == "'") {
  517. $comment = $comment ? false : true;
  518. if (isset($part[$i+1]) && ($part[$i+1] == "'")) {
  519. $comment = $comment ? false : true;
  520. $format .= "\\'";
  521. ++$i;
  522. }
  523. $orig = '';
  524. continue;
  525. }
  526. if ($comment) {
  527. $format .= '\\' . $part[$i];
  528. $orig = '';
  529. } else {
  530. $orig .= $part[$i];
  531. if (!isset($part[$i+1]) || (isset($orig[0]) && ($orig[0] != $part[$i+1]))) {
  532. $format .= $this->_parseIsoToDate($orig, $locale);
  533. $orig = '';
  534. }
  535. }
  536. }
  537. return $format;
  538. }
  539. /**
  540. * Internal parsing method
  541. *
  542. * @param string $token
  543. * @param string $locale
  544. * @return string
  545. */
  546. private function _parseIsoToDate($token, $locale) {
  547. switch($token) {
  548. case self::DAY :
  549. return 'd';
  550. break;
  551. case self::WEEKDAY_SHORT :
  552. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  553. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
  554. return $this->_toComment(iconv_substr($day, 0, 3, 'UTF-8'));
  555. break;
  556. case self::DAY_SHORT :
  557. return 'j';
  558. break;
  559. case self::WEEKDAY :
  560. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  561. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)));
  562. break;
  563. case self::WEEKDAY_8601 :
  564. return 'N';
  565. break;
  566. case 'ee' :
  567. return $this->_toComment(str_pad($this->date('N', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  568. break;
  569. case self::DAY_SUFFIX :
  570. return 'S';
  571. break;
  572. case self::WEEKDAY_DIGIT :
  573. return 'w';
  574. break;
  575. case self::DAY_OF_YEAR :
  576. return 'z';
  577. break;
  578. case 'DDD' :
  579. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 3, '0', STR_PAD_LEFT));
  580. break;
  581. case 'DD' :
  582. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  583. break;
  584. case self::WEEKDAY_NARROW :
  585. case 'EEEEE' :
  586. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  587. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
  588. return $this->_toComment(iconv_substr($day, 0, 1, 'UTF-8'));
  589. break;
  590. case self::WEEKDAY_NAME :
  591. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  592. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)));
  593. break;
  594. case 'w' :
  595. $week = $this->date('W', $this->getUnixTimestamp(), false);
  596. return $this->_toComment(($week[0] == '0') ? $week[1] : $week);
  597. break;
  598. case self::WEEK :
  599. return 'W';
  600. break;
  601. case self::MONTH_NAME :
  602. $month = $this->date('n', $this->getUnixTimestamp(), false);
  603. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)));
  604. break;
  605. case self::MONTH :
  606. return 'm';
  607. break;
  608. case self::MONTH_NAME_SHORT :
  609. $month = $this->date('n', $this->getUnixTimestamp(), false);
  610. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)));
  611. break;
  612. case self::MONTH_SHORT :
  613. return 'n';
  614. break;
  615. case self::MONTH_DAYS :
  616. return 't';
  617. break;
  618. case self::MONTH_NAME_NARROW :
  619. $month = $this->date('n', $this->getUnixTimestamp(), false);
  620. $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
  621. return $this->_toComment(iconv_substr($mon, 0, 1, 'UTF-8'));
  622. break;
  623. case self::LEAPYEAR :
  624. return 'L';
  625. break;
  626. case self::YEAR_8601 :
  627. return 'o';
  628. break;
  629. case self::YEAR :
  630. return 'Y';
  631. break;
  632. case self::YEAR_SHORT :
  633. return 'y';
  634. break;
  635. case self::YEAR_SHORT_8601 :
  636. return $this->_toComment(substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2));
  637. break;
  638. case self::MERIDIEM :
  639. $am = $this->date('a', $this->getUnixTimestamp(), false);
  640. if ($am == 'am') {
  641. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'am'));
  642. }
  643. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'pm'));
  644. break;
  645. case self::SWATCH :
  646. return 'B';
  647. break;
  648. case self::HOUR_SHORT_AM :
  649. return 'g';
  650. break;
  651. case self::HOUR_SHORT :
  652. return 'G';
  653. break;
  654. case self::HOUR_AM :
  655. return 'h';
  656. break;
  657. case self::HOUR :
  658. return 'H';
  659. break;
  660. case self::MINUTE :
  661. return $this->_toComment(str_pad($this->date('i', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  662. break;
  663. case self::SECOND :
  664. return $this->_toComment(str_pad($this->date('s', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  665. break;
  666. case self::MINUTE_SHORT :
  667. return 'i';
  668. break;
  669. case self::SECOND_SHORT :
  670. return 's';
  671. break;
  672. case self::MILLISECOND :
  673. return $this->_toComment($this->getMilliSecond());
  674. break;
  675. case self::TIMEZONE_NAME :
  676. case 'vvvv' :
  677. return 'e';
  678. break;
  679. case self::DAYLIGHT :
  680. return 'I';
  681. break;
  682. case self::GMT_DIFF :
  683. case 'ZZ' :
  684. case 'ZZZ' :
  685. return 'O';
  686. break;
  687. case self::GMT_DIFF_SEP :
  688. return 'P';
  689. break;
  690. case self::TIMEZONE :
  691. case 'v' :
  692. case 'zz' :
  693. case 'zzz' :
  694. return 'T';
  695. break;
  696. case self::TIMEZONE_SECS :
  697. return 'Z';
  698. break;
  699. case self::ISO_8601 :
  700. return 'c';
  701. break;
  702. case self::RFC_2822 :
  703. return 'r';
  704. break;
  705. case self::TIMESTAMP :
  706. return 'U';
  707. break;
  708. case self::ERA :
  709. case 'GG' :
  710. case 'GGG' :
  711. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  712. if ($year < 0) {
  713. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')));
  714. }
  715. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')));
  716. break;
  717. case self::ERA_NARROW :
  718. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  719. if ($year < 0) {
  720. return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')), 0, 1, 'UTF-8')) . '.';
  721. }
  722. return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')), 0, 1, 'UTF-8')) . '.';
  723. break;
  724. case self::ERA_NAME :
  725. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  726. if ($year < 0) {
  727. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0')));
  728. }
  729. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1')));
  730. break;
  731. case self::DATES :
  732. return $this->_toToken(Zend_Locale_Format::getDateFormat($locale), $locale);
  733. break;
  734. case self::DATE_FULL :
  735. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full')), $locale);
  736. break;
  737. case self::DATE_LONG :
  738. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long')), $locale);
  739. break;
  740. case self::DATE_MEDIUM :
  741. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium')), $locale);
  742. break;
  743. case self::DATE_SHORT :
  744. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short')), $locale);
  745. break;
  746. case self::TIMES :
  747. return $this->_toToken(Zend_Locale_Format::getTimeFormat($locale), $locale);
  748. break;
  749. case self::TIME_FULL :
  750. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'full'), $locale);
  751. break;
  752. case self::TIME_LONG :
  753. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'long'), $locale);
  754. break;
  755. case self::TIME_MEDIUM :
  756. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'medium'), $locale);
  757. break;
  758. case self::TIME_SHORT :
  759. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'short'), $locale);
  760. break;
  761. case self::DATETIME :
  762. return $this->_toToken(Zend_Locale_Format::getDateTimeFormat($locale), $locale);
  763. break;
  764. case self::DATETIME_FULL :
  765. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full')), $locale);
  766. break;
  767. case self::DATETIME_LONG :
  768. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long')), $locale);
  769. break;
  770. case self::DATETIME_MEDIUM :
  771. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium')), $locale);
  772. break;
  773. case self::DATETIME_SHORT :
  774. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short')), $locale);
  775. break;
  776. case self::ATOM :
  777. return 'Y\-m\-d\TH\:i\:sP';
  778. break;
  779. case self::COOKIE :
  780. return 'l\, d\-M\-y H\:i\:s e';
  781. break;
  782. case self::RFC_822 :
  783. return 'D\, d M y H\:i\:s O';
  784. break;
  785. case self::RFC_850 :
  786. return 'l\, d\-M\-y H\:i\:s e';
  787. break;
  788. case self::RFC_1036 :
  789. return 'D\, d M y H\:i\:s O';
  790. break;
  791. case self::RFC_1123 :
  792. return 'D\, d M Y H\:i\:s O';
  793. break;
  794. case self::RFC_3339 :
  795. return 'Y\-m\-d\TH\:i\:sP';
  796. break;
  797. case self::RSS :
  798. return 'D\, d M Y H\:i\:s O';
  799. break;
  800. case self::W3C :
  801. return 'Y\-m\-d\TH\:i\:sP';
  802. break;
  803. }
  804. if ($token == '') {
  805. return '';
  806. }
  807. switch ($token[0]) {
  808. case 'y' :
  809. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  810. return 'Y';
  811. }
  812. $length = iconv_strlen($token, 'UTF-8');
  813. return $this->_toComment(str_pad($this->date('Y', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  814. break;
  815. case 'Y' :
  816. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  817. return 'o';
  818. }
  819. $length = iconv_strlen($token, 'UTF-8');
  820. return $this->_toComment(str_pad($this->date('o', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  821. break;
  822. case 'A' :
  823. $length = iconv_strlen($token, 'UTF-8');
  824. $result = substr($this->getMilliSecond(), 0, 3);
  825. $result += $this->date('s', $this->getUnixTimestamp(), false) * 1000;
  826. $result += $this->date('i', $this->getUnixTimestamp(), false) * 60000;
  827. $result += $this->date('H', $this->getUnixTimestamp(), false) * 3600000;
  828. return $this->_toComment(str_pad($result, $length, '0', STR_PAD_LEFT));
  829. break;
  830. }
  831. return $this->_toComment($token);
  832. }
  833. /**
  834. * Private function to make a comment of a token
  835. *
  836. * @param string $token
  837. * @return string
  838. */
  839. private function _toComment($token)
  840. {
  841. $token = str_split($token);
  842. $result = '';
  843. foreach ($token as $tok) {
  844. $result .= '\\' . $tok;
  845. }
  846. return $result;
  847. }
  848. /**
  849. * Return digit from standard names (english)
  850. * Faster implementation than locale aware searching
  851. *
  852. * @param string $name
  853. * @return integer Number of this month
  854. * @throws Zend_Date_Exception
  855. */
  856. private function _getDigitFromName($name)
  857. {
  858. switch($name) {
  859. case "Jan":
  860. return 1;
  861. case "Feb":
  862. return 2;
  863. case "Mar":
  864. return 3;
  865. case "Apr":
  866. return 4;
  867. case "May":
  868. return 5;
  869. case "Jun":
  870. return 6;
  871. case "Jul":
  872. return 7;
  873. case "Aug":
  874. return 8;
  875. case "Sep":
  876. return 9;
  877. case "Oct":
  878. return 10;
  879. case "Nov":
  880. return 11;
  881. case "Dec":
  882. return 12;
  883. default:
  884. require_once 'Zend/Date/Exception.php';
  885. throw new Zend_Date_Exception('Month ($name) is not a known month');
  886. }
  887. }
  888. /**
  889. * Counts the exact year number
  890. * < 70 - 2000 added, >70 < 100 - 1900, others just returned
  891. *
  892. * @param integer $value year number
  893. * @return integer Number of year
  894. */
  895. public static function getFullYear($value)
  896. {
  897. if ($value >= 0) {
  898. if ($value < 70) {
  899. $value += 2000;
  900. } else if ($value < 100) {
  901. $value += 1900;
  902. }
  903. }
  904. return $value;
  905. }
  906. /**
  907. * Sets the given date as new date or a given datepart as new datepart returning the new datepart
  908. * This could be for example a localized dayname, the date without time,
  909. * the month or only the seconds. There are about 50 different supported date parts.
  910. * For a complete list of supported datepart values look into the docu
  911. *
  912. * @param string|integer|array|Zend_Date $date Date or datepart to set
  913. * @param string $part OPTIONAL Part of the date to set, if null the timestamp is set
  914. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  915. * @return Zend_Date Provides fluid interface
  916. * @throws Zend_Date_Exception
  917. */
  918. public function set($date, $part = null, $locale = null)
  919. {
  920. if (self::$_options['format_type'] == 'php') {
  921. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  922. }
  923. $zone = $this->getTimezoneFromString($date);
  924. $this->setTimezone($zone);
  925. $this->_calculate('set', $date, $part, $locale);
  926. return $this;
  927. }
  928. /**
  929. * Adds a date or datepart to the existing date, by extracting $part from $date,
  930. * and modifying this object by adding that part. The $part is then extracted from
  931. * this object and returned as an integer or numeric string (for large values, or $part's
  932. * corresponding to pre-defined formatted date strings).
  933. * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
  934. * There are about 50 different supported date parts.
  935. * For a complete list of supported datepart values look into the docu.
  936. *
  937. * @param string|integer|array|Zend_Date $date Date or datepart to add
  938. * @param string $part OPTIONAL Part of the date to add, if null the timestamp is added
  939. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  940. * @return Zend_Date Provides fluid interface
  941. * @throws Zend_Date_Exception
  942. */
  943. public function add($date, $part = self::TIMESTAMP, $locale = null)
  944. {
  945. if (self::$_options['format_type'] == 'php') {
  946. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  947. }
  948. $this->_calculate('add', $date, $part, $locale);
  949. return $this;
  950. }
  951. /**
  952. * Subtracts a date from another date.
  953. * This could be for example a RFC2822 date, the time,
  954. * the year or only the timestamp. There are about 50 different supported date parts.
  955. * For a complete list of supported datepart values look into the docu
  956. * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
  957. *
  958. * @param string|integer|array|Zend_Date $date Date or datepart to subtract
  959. * @param string $part OPTIONAL Part of the date to sub, if null the timestamp is subtracted
  960. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  961. * @return Zend_Date Provides fluid interface
  962. * @throws Zend_Date_Exception
  963. */
  964. public function sub($date, $part = self::TIMESTAMP, $locale = null)
  965. {
  966. if (self::$_options['format_type'] == 'php') {
  967. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  968. }
  969. $this->_calculate('sub', $date, $part, $locale);
  970. return $this;
  971. }
  972. /**
  973. * Compares a date or datepart with the existing one.
  974. * Returns -1 if earlier, 0 if equal and 1 if later.
  975. *
  976. * @param string|integer|array|Zend_Date $date Date or datepart to compare with the date object
  977. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is subtracted
  978. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  979. * @return integer 0 = equal, 1 = later, -1 = earlier
  980. * @throws Zend_Date_Exception
  981. */
  982. public function compare($date, $part = null, $locale = null)
  983. {
  984. if (self::$_options['format_type'] == 'php') {
  985. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  986. }
  987. $compare = $this->_calculate('cmp', $date, $part, $locale);
  988. if ($compare > 0) {
  989. return 1;
  990. } else if ($compare < 0) {
  991. return -1;
  992. }
  993. return 0;
  994. }
  995. /**
  996. * Returns a new instance of Zend_Date with the selected part copied.
  997. * To make an exact copy, use PHP's clone keyword.
  998. * For a complete list of supported date part values look into the docu.
  999. * If a date part is copied, all other date parts are set to standard values.
  1000. * For example: If only YEAR is copied, the returned date object is equal to
  1001. * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
  1002. * If only HOUR is copied, the returned date object is equal to
  1003. * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
  1004. *
  1005. * @param string $part Part of the date to compare, if null the timestamp is subtracted
  1006. * @param string|Zend_Locale $locale OPTIONAL New object's locale. No adjustments to timezone are made.
  1007. * @return Zend_Date New clone with requested part
  1008. */
  1009. public function copyPart($part, $locale = null)
  1010. {
  1011. $clone = clone $this; // copy all instance variables
  1012. $clone->setUnixTimestamp(0); // except the timestamp
  1013. if ($locale != null) {
  1014. $clone->setLocale($locale); // set an other locale if selected
  1015. }
  1016. $clone->set($this, $part);
  1017. return $clone;
  1018. }
  1019. /**
  1020. * Internal function, returns the offset of a given timezone
  1021. *
  1022. * @param string $zone
  1023. * @return integer
  1024. */
  1025. public function getTimezoneFromString($zone)
  1026. {
  1027. if (is_array($zone)) {
  1028. return $this->getTimezone();
  1029. }
  1030. if ($zone instanceof Zend_Date) {
  1031. return $zone->getTimezone();
  1032. }
  1033. $match = array();
  1034. preg_match('/\dZ$/', $zone, $match);
  1035. if (!empty($match)) {
  1036. return "Etc/UTC";
  1037. }
  1038. preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
  1039. if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
  1040. $zone = "Etc/GMT";
  1041. $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
  1042. $zone .= (int) abs($match[count($match) - 1]);
  1043. return $zone;
  1044. }
  1045. preg_match('/([[:alpha:]\/]{3,30})(?!.*([[:alpha:]\/]{3,30}))/', $zone, $match);
  1046. try {
  1047. if (!empty($match) and (!is_int($match[count($match) - 1]))) {
  1048. $oldzone = $this->getTimezone();
  1049. $this->setTimezone($match[count($match) - 1]);
  1050. $result = $this->getTimezone();
  1051. $this->setTimezone($oldzone);
  1052. if ($result !== $oldzone) {
  1053. return $match[count($match) - 1];
  1054. }
  1055. }
  1056. } catch (Exception $e) {
  1057. // fall through
  1058. }
  1059. return $this->getTimezone();
  1060. }
  1061. /**
  1062. * Calculates the date or object
  1063. *
  1064. * @param string $calc Calculation to make
  1065. * @param string|integer $date Date for calculation
  1066. * @param string|integer $comp Second date for calculation
  1067. * @param boolean|integer $dst Use dst correction if option is set
  1068. * @return integer|string|Zend_Date new timestamp or Zend_Date depending on calculation
  1069. */
  1070. private function _assign($calc, $date, $comp = 0, $dst = false)
  1071. {
  1072. switch ($calc) {
  1073. case 'set' :
  1074. if (!empty($comp)) {
  1075. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $comp));
  1076. }
  1077. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1078. $value = $this->getUnixTimestamp();
  1079. break;
  1080. case 'add' :
  1081. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1082. $value = $this->getUnixTimestamp();
  1083. break;
  1084. case 'sub' :
  1085. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $date));
  1086. $value = $this->getUnixTimestamp();
  1087. break;
  1088. default :
  1089. // cmp - compare
  1090. return call_user_func(Zend_Locale_Math::$comp, $comp, $date);
  1091. break;
  1092. }
  1093. // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
  1094. if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
  1095. $hour = $this->toString(self::HOUR, 'iso');
  1096. if ($hour != $dst) {
  1097. if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
  1098. $value += 3600;
  1099. } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
  1100. $value -= 3600;
  1101. }
  1102. $this->setUnixTimestamp($value);
  1103. }
  1104. }
  1105. return $this->getUnixTimestamp();
  1106. }
  1107. /**
  1108. * Calculates the date or object
  1109. *
  1110. * @param string $calc Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set'
  1111. * @param string|integer|array|Zend_Date $date Date or datepart to calculate with
  1112. * @param string $part Part of the date to calculate, if null the timestamp is used
  1113. * @param string|Zend_Locale $locale Locale for parsing input
  1114. * @return integer|string|Zend_Date new timestamp
  1115. * @throws Zend_Date_Exception
  1116. */
  1117. private function _calculate($calc, $date, $part, $locale)
  1118. {
  1119. if ($date === null) {
  1120. require_once 'Zend/Date/Exception.php';
  1121. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  1122. }
  1123. if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale::isLocale($part, null, false))) {
  1124. $locale = $part;
  1125. $part = null;
  1126. }
  1127. if ($locale === null) {
  1128. $locale = $this->getLocale();
  1129. }
  1130. $locale = (string) $locale;
  1131. // Create date parts
  1132. $year = $this->toString(self::YEAR, 'iso');
  1133. $month = $this->toString(self::MONTH_SHORT, 'iso');
  1134. $day = $this->toString(self::DAY_SHORT, 'iso');
  1135. $hour = $this->toString(self::HOUR_SHORT, 'iso');
  1136. $minute = $this->toString(self::MINUTE_SHORT, 'iso');
  1137. $second = $this->toString(self::SECOND_SHORT, 'iso');
  1138. // If object extract value
  1139. if ($date instanceof Zend_Date) {
  1140. $date = $date->toString($part, 'iso', $locale);
  1141. }
  1142. if (is_array($date) === true) {
  1143. if (empty($part) === false) {
  1144. switch($part) {
  1145. // Fall through
  1146. case self::DAY:
  1147. case self::DAY_SHORT:
  1148. if (isset($date['day']) === true) {
  1149. $date = $date['day'];
  1150. }
  1151. break;
  1152. // Fall through
  1153. case self::WEEKDAY_SHORT:
  1154. case self::WEEKDAY:
  1155. case self::WEEKDAY_8601:
  1156. case self::WEEKDAY_DIGIT:
  1157. case self::WEEKDAY_NARROW:
  1158. case self::WEEKDAY_NAME:
  1159. if (isset($date['weekday']) === true) {
  1160. $date = $date['weekday'];
  1161. $part = self::WEEKDAY_DIGIT;
  1162. }
  1163. break;
  1164. case self::DAY_OF_YEAR:
  1165. if (isset($date['day_of_year']) === true) {
  1166. $date = $date['day_of_year'];
  1167. }
  1168. break;
  1169. // Fall through
  1170. case self::MONTH:
  1171. case self::MONTH_SHORT:
  1172. case self::MONTH_NAME:
  1173. case self::MONTH_NAME_SHORT:
  1174. case self::MONTH_NAME_NARROW:
  1175. if (isset($date['month']) === true) {
  1176. $date = $date['month'];
  1177. }
  1178. break;
  1179. // Fall through
  1180. case self::YEAR:
  1181. case self::YEAR_SHORT:
  1182. case self::YEAR_8601:
  1183. case self::YEAR_SHORT_8601:
  1184. if (isset($date['year']) === true) {
  1185. $date = $date['year'];
  1186. }
  1187. break;
  1188. // Fall through
  1189. case self::HOUR:
  1190. case self::HOUR_AM:
  1191. case self::HOUR_SHORT:
  1192. case self::HOUR_SHORT_AM:
  1193. if (isset($date['hour']

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