PageRenderTime 72ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Zend/Date.php

https://github.com/ceefour/magento-mirror
PHP | 4665 lines | 2721 code | 542 blank | 1402 comment | 520 complexity | c7bd4cfd3969f020b1023d229c98ffa1 MD5 | raw file

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

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

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