PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Date.php

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

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