PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Date.php

https://github.com/o2sources/nicknames
PHP | 4704 lines | 2857 code | 546 blank | 1301 comment | 528 complexity | a8eca2e3716d200ab1772bc361db624f 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 13676 2009-01-17 19:36:30Z alexander $
  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. 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, null, false))) {
  365. $locale = $format;
  366. $format = null;
  367. }
  368. if (($type !== null) and (Zend_Locale::isLocale($type, null, false))) {
  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] = iconv_substr($this->get(self::ERA, $locale), 0, 1, 'UTF-8') . ".";
  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] = iconv_substr($this->get(self::MONTH_NAME_NARROW, $locale), 0, 1, 'UTF-8');
  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 = iconv_strlen($output[$i], 'UTF-8');
  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 = iconv_strlen($output[$i], 'UTF-8');
  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 = iconv_strlen($output[$i], 'UTF-8');
  596. $hour = $this->get(self::HOUR, $locale);
  597. $minute = $this->get(self::MINUTE, $locale);
  598. $second = $this->get(self::SECOND, $locale);
  599. $milli = $this->get(self::MILLISECOND, $locale);
  600. $seconds = $milli + ($second * 1000) + ($minute * 60000) + ($hour * 3600000);
  601. $output[$i] = str_pad($seconds, $length, '0', STR_PAD_LEFT);
  602. }
  603. if ($output[$i][0] === "'") {
  604. $output[$i] = iconv_substr($output[$i],
  605. 1,
  606. iconv_strlen($output[$i], 'UTF-8') - 1,
  607. 'UTF-8');
  608. }
  609. }
  610. $notset = false;
  611. }
  612. return implode('', $output);
  613. }
  614. /**
  615. * Returns a string representation of the date which is equal with the timestamp
  616. *
  617. * @return string
  618. */
  619. public function __toString()
  620. {
  621. return $this->toString(null, $this->_locale);
  622. }
  623. /**
  624. * Returns a integer representation of the object
  625. * But returns false when the given part is no value f.e. Month-Name
  626. *
  627. * @param string|integer|Zend_Date $part OPTIONAL Defines the date or datepart to return as integer
  628. * @return integer|false
  629. */
  630. public function toValue($part = null)
  631. {
  632. $result = $this->get($part);
  633. if (is_numeric($result)) {
  634. return intval("$result");
  635. } else {
  636. return false;
  637. }
  638. }
  639. /**
  640. * Returns an array representation of the object
  641. *
  642. * @return array
  643. */
  644. public function toArray()
  645. {
  646. return array('day' => $this->get(self::DAY_SHORT),
  647. 'month' => $this->get(self::MONTH_SHORT),
  648. 'year' => $this->get(self::YEAR),
  649. 'hour' => $this->get(self::HOUR_SHORT),
  650. 'minute' => $this->get(self::MINUTE_SHORT),
  651. 'second' => $this->get(self::SECOND_SHORT),
  652. 'timezone' => $this->get(self::TIMEZONE),
  653. 'timestamp' => $this->get(self::TIMESTAMP),
  654. 'weekday' => $this->get(self::WEEKDAY_8601),
  655. 'dayofyear' => $this->get(self::DAY_OF_YEAR),
  656. 'week' => $this->get(self::WEEK),
  657. 'gmtsecs' => $this->get(self::TIMEZONE_SECS));
  658. }
  659. /**
  660. * Returns a representation of a date or datepart
  661. * This could be for example a localized monthname, the time without date,
  662. * the era or only the fractional seconds. There are about 50 different supported date parts.
  663. * For a complete list of supported datepart values look into the docu
  664. *
  665. * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned
  666. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  667. * @return integer|string date or datepart
  668. */
  669. public function get($part = null, $locale = null)
  670. {
  671. if ($locale === null) {
  672. $locale = $this->getLocale();
  673. }
  674. if (($part !== null) and (Zend_Locale::isLocale($part, null, false))) {
  675. $locale = $part;
  676. $part = null;
  677. }
  678. if ($part === null) {
  679. $part = self::TIMESTAMP;
  680. }
  681. if (!defined("self::".$part)) {
  682. return $this->toString($part, $locale);
  683. }
  684. switch($part) {
  685. // day formats
  686. case self::DAY :
  687. return $this->date('d', $this->getUnixTimestamp(), false);
  688. break;
  689. case self::WEEKDAY_SHORT :
  690. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  691. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
  692. return iconv_substr($day, 0, 3, 'UTF-8');
  693. break;
  694. case self::DAY_SHORT :
  695. return $this->date('j', $this->getUnixTimestamp(), false);
  696. break;
  697. case self::WEEKDAY :
  698. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  699. return Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
  700. break;
  701. case self::WEEKDAY_8601 :
  702. return $this->date('N', $this->getUnixTimestamp(), false);
  703. break;
  704. case self::DAY_SUFFIX :
  705. return $this->date('S', $this->getUnixTimestamp(), false);
  706. break;
  707. case self::WEEKDAY_DIGIT :
  708. return $this->date('w', $this->getUnixTimestamp(), false);
  709. break;
  710. case self::DAY_OF_YEAR :
  711. return $this->date('z', $this->getUnixTimestamp(), false);
  712. break;
  713. case self::WEEKDAY_NARROW :
  714. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  715. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
  716. return iconv_substr($day, 0, 1, 'UTF-8');
  717. break;
  718. case self::WEEKDAY_NAME :
  719. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  720. return Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
  721. break;
  722. // week formats
  723. case self::WEEK :
  724. return $this->date('W', $this->getUnixTimestamp(), false);
  725. break;
  726. // month formats
  727. case self::MONTH_NAME :
  728. $month = $this->date('n', $this->getUnixTimestamp(), false);
  729. return Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month));
  730. break;
  731. case self::MONTH :
  732. return $this->date('m', $this->getUnixTimestamp(), false);
  733. break;
  734. case self::MONTH_NAME_SHORT :
  735. $month = $this->date('n', $this->getUnixTimestamp(), false);
  736. return Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
  737. break;
  738. case self::MONTH_SHORT :
  739. return $this->date('n', $this->getUnixTimestamp(), false);
  740. break;
  741. case self::MONTH_DAYS :
  742. return $this->date('t', $this->getUnixTimestamp(), false);
  743. break;
  744. case self::MONTH_NAME_NARROW :
  745. $month = $this->date('n', $this->getUnixTimestamp(), false);
  746. $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
  747. return iconv_substr($mon, 0, 1, 'UTF-8');
  748. break;
  749. // year formats
  750. case self::LEAPYEAR :
  751. return $this->date('L', $this->getUnixTimestamp(), false);
  752. break;
  753. case self::YEAR_8601 :
  754. return $this->date('o', $this->getUnixTimestamp(), false);
  755. break;
  756. case self::YEAR :
  757. return $this->date('Y', $this->getUnixTimestamp(), false);
  758. break;
  759. case self::YEAR_SHORT :
  760. return $this->date('y', $this->getUnixTimestamp(), false);
  761. break;
  762. case self::YEAR_SHORT_8601 :
  763. return substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2);
  764. break;
  765. // time formats
  766. case self::MERIDIEM :
  767. $am = $this->date('a', $this->getUnixTimestamp(), false);
  768. if ($am == 'am') {
  769. return Zend_Locale_Data::getContent($locale, 'am');
  770. }
  771. return Zend_Locale_Data::getContent($locale, 'pm');
  772. break;
  773. case self::SWATCH :
  774. return $this->date('B', $this->getUnixTimestamp(), false);
  775. break;
  776. case self::HOUR_SHORT_AM :
  777. return $this->date('g', $this->getUnixTimestamp(), false);
  778. break;
  779. case self::HOUR_SHORT :
  780. return $this->date('G', $this->getUnixTimestamp(), false);
  781. break;
  782. case self::HOUR_AM :
  783. return $this->date('h', $this->getUnixTimestamp(), false);
  784. break;
  785. case self::HOUR :
  786. return $this->date('H', $this->getUnixTimestamp(), false);
  787. break;
  788. case self::MINUTE :
  789. return $this->date('i', $this->getUnixTimestamp(), false);
  790. break;
  791. case self::SECOND :
  792. return $this->date('s', $this->getUnixTimestamp(), false);
  793. break;
  794. case self::MINUTE_SHORT :
  795. return $this->date('i', $this->getUnixTimestamp(), false);
  796. break;
  797. case self::SECOND_SHORT :
  798. return $this->date('s', $this->getUnixTimestamp(), false);
  799. break;
  800. case self::MILLISECOND :
  801. return $this->_fractional;
  802. break;
  803. // timezone formats
  804. case self::TIMEZONE_NAME :
  805. return $this->date('e', $this->getUnixTimestamp(), false);
  806. break;
  807. case self::DAYLIGHT :
  808. return $this->date('I', $this->getUnixTimestamp(), false);
  809. break;
  810. case self::GMT_DIFF :
  811. return $this->date('O', $this->getUnixTimestamp(), false);
  812. break;
  813. case self::GMT_DIFF_SEP :
  814. return $this->date('P', $this->getUnixTimestamp(), false);
  815. break;
  816. case self::TIMEZONE :
  817. return $this->date('T', $this->getUnixTimestamp(), false);
  818. break;
  819. case self::TIMEZONE_SECS :
  820. return $this->date('Z', $this->getUnixTimestamp(), false);
  821. break;
  822. // date strings
  823. case self::ISO_8601 :
  824. return $this->date('c', $this->getUnixTimestamp(), false);
  825. break;
  826. case self::RFC_2822 :
  827. return $this->date('r', $this->getUnixTimestamp(), false);
  828. break;
  829. case self::TIMESTAMP :
  830. return $this->getUnixTimestamp();
  831. break;
  832. // additional formats
  833. case self::ERA :
  834. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  835. if ($year < 0) {
  836. return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0'));
  837. }
  838. return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1'));
  839. break;
  840. case self::ERA_NAME :
  841. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  842. if ($year < 0) {
  843. return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0'));
  844. }
  845. return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1'));
  846. break;
  847. case self::DATES :
  848. return $this->toString(Zend_Locale_Format::getDateFormat($locale), 'iso', $locale);
  849. break;
  850. case self::DATE_FULL :
  851. $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  852. return $this->toString($date, 'iso', $locale);
  853. break;
  854. case self::DATE_LONG :
  855. $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  856. return $this->toString($date, 'iso', $locale);
  857. break;
  858. case self::DATE_MEDIUM :
  859. $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  860. return $this->toString($date, 'iso', $locale);
  861. break;
  862. case self::DATE_SHORT :
  863. $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  864. return $this->toString($date, 'iso', $locale);
  865. break;
  866. case self::TIMES :
  867. return $this->toString(Zend_Locale_Format::getTimeFormat($locale), 'iso', $locale);
  868. break;
  869. case self::TIME_FULL :
  870. $time = Zend_Locale_Data::getContent($locale, 'time', 'full');
  871. return $this->toString($time, 'iso', $locale);
  872. break;
  873. case self::TIME_LONG :
  874. $time = Zend_Locale_Data::getContent($locale, 'time', 'long');
  875. return $this->toString($time, 'iso', $locale);
  876. break;
  877. case self::TIME_MEDIUM :
  878. $time = Zend_Locale_Data::getContent($locale, 'time', 'medium');
  879. return $this->toString($time, 'iso', $locale);
  880. break;
  881. case self::TIME_SHORT :
  882. $time = Zend_Locale_Data::getContent($locale, 'time', 'short');
  883. return $this->toString($time, 'iso', $locale);
  884. break;
  885. case self::ATOM :
  886. return $this->date('Y\-m\-d\TH\:i\:sP', $this->getUnixTimestamp(), false);
  887. break;
  888. case self::COOKIE :
  889. return $this->date('l\, d\-M\-y H\:i\:s e', $this->getUnixTimestamp(), false);
  890. break;
  891. case self::RFC_822 :
  892. return $this->date('D\, d M y H\:i\:s O', $this->getUnixTimestamp(), false);
  893. break;
  894. case self::RFC_850 :
  895. return $this->date('l\, d\-M\-y H\:i\:s e', $this->getUnixTimestamp(), false);
  896. break;
  897. case self::RFC_1036 :
  898. return $this->date('D\, d M y H\:i\:s O', $this->getUnixTimestamp(), false);
  899. break;
  900. case self::RFC_1123 :
  901. return $this->date('D\, d M Y H\:i\:s O', $this->getUnixTimestamp(), false);
  902. break;
  903. case self::RFC_3339 :
  904. return $this->date('Y\-m\-d\TH\:i\:sP', $this->getUnixTimestamp(), false);
  905. break;
  906. case self::RSS :
  907. return $this->date('D\, d M Y H\:i\:s O', $this->getUnixTimestamp(), false);
  908. break;
  909. case self::W3C :
  910. return $this->date('Y\-m\-d\TH\:i\:sP', $this->getUnixTimestamp(), false);
  911. break;
  912. }
  913. }
  914. /**
  915. * Return digit from standard names (english)
  916. * Faster implementation than locale aware searching
  917. *
  918. * @param string $name
  919. * @return integer Number of this month
  920. * @throws Zend_Date_Exception
  921. */
  922. private function _getDigitFromName($name)
  923. {
  924. switch($name) {
  925. case "Jan":
  926. return 1;
  927. case "Feb":
  928. return 2;
  929. case "Mar":
  930. return 3;
  931. case "Apr":
  932. return 4;
  933. case "May":
  934. return 5;
  935. case "Jun":
  936. return 6;
  937. case "Jul":
  938. return 7;
  939. case "Aug":
  940. return 8;
  941. case "Sep":
  942. return 9;
  943. case "Oct":
  944. return 10;
  945. case "Nov":
  946. return 11;
  947. case "Dec":
  948. return 12;
  949. default:
  950. require_once 'Zend/Date/Exception.php';
  951. throw new Zend_Date_Exception('Month ($name) is not a known month');
  952. }
  953. }
  954. /**
  955. * Counts the exact year number
  956. * < 70 - 2000 added, >70 < 100 - 1900, others just returned
  957. *
  958. * @param integer $value year number
  959. * @return integer Number of year
  960. */
  961. public static function getFullYear($value)
  962. {
  963. if ($value >= 0) {
  964. if ($value < 70) {
  965. $value += 2000;
  966. } else if ($value < 100) {
  967. $value += 1900;
  968. }
  969. }
  970. return $value;
  971. }
  972. /**
  973. * Sets the given date as new date or a given datepart as new datepart returning the new datepart
  974. * This could be for example a localized dayname, the date without time,
  975. * the month or only the seconds. There are about 50 different supported date parts.
  976. * For a complete list of supported datepart values look into the docu
  977. *
  978. * @param string|integer|array|Zend_Date $date Date or datepart to set
  979. * @param string $part OPTIONAL Part of the date to set, if null the timestamp is set
  980. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  981. * @return integer|string new datepart
  982. * @throws Zend_Date_Exception
  983. */
  984. public function set($date, $part = null, $locale = null)
  985. {
  986. $zone = $this->getTimezoneFromString($date);
  987. $this->setTimezone($zone);
  988. $result = $this->_calculate('set', $date, $part, $locale);
  989. return $result;
  990. }
  991. /**
  992. * Adds a date or datepart to the existing date, by extracting $part from $date,
  993. * and modifying this object by adding that part. The $part is then extracted from
  994. * this object and returned as an integer or numeric string (for large values, or $part's
  995. * corresponding to pre-defined formatted date strings).
  996. * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
  997. * There are about 50 different supported date parts.
  998. * For a complete list of supported datepart values look into the docu.
  999. *
  1000. * @param string|integer|array|Zend_Date $date Date or datepart to add
  1001. * @param string $part OPTIONAL Part of the date to add, if null the timestamp is added
  1002. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  1003. * @return integer|string new datepart
  1004. * @throws Zend_Date_Exception
  1005. */
  1006. public function add($date, $part = null, $locale = null)
  1007. {
  1008. $this->_calculate('add', $date, $part, $locale);
  1009. $result = $this->get($part, $locale);
  1010. return $result;
  1011. }
  1012. /**
  1013. * Subtracts a date from another date.
  1014. * This could be for example a RFC2822 date, the time,
  1015. * the year or only the timestamp. There are about 50 different supported date parts.
  1016. * For a complete list of supported datepart values look into the docu
  1017. * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
  1018. *
  1019. * @param string|integer|array|Zend_Date $date Date or datepart to subtract
  1020. * @param string $part OPTIONAL Part of the date to sub, if null the timestamp is subtracted
  1021. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  1022. * @return integer|string new datepart
  1023. * @throws Zend_Date_Exception
  1024. */
  1025. public function sub($date, $part = null, $locale = null)
  1026. {
  1027. $this->_calculate('sub', $date, $part, $locale);
  1028. $result = $this->get($part, $locale);
  1029. return $result;
  1030. }
  1031. /**
  1032. * Compares a date or datepart with the existing one.
  1033. * Returns -1 if earlier, 0 if equal and 1 if later.
  1034. *
  1035. * @param string|integer|array|Zend_Date $date Date or datepart to compare with the date object
  1036. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is subtracted
  1037. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  1038. * @return integer 0 = equal, 1 = later, -1 = earlier
  1039. * @throws Zend_Date_Exception
  1040. */
  1041. public function compare($date, $part = null, $locale = null)
  1042. {
  1043. $compare = $this->_calculate('cmp', $date, $part, $locale);
  1044. if ($compare > 0) {
  1045. return 1;
  1046. } else if ($compare < 0) {
  1047. return -1;
  1048. }
  1049. return 0;
  1050. }
  1051. /**
  1052. * Returns a new instance of Zend_Date with the selected part copied.
  1053. * To make an exact copy, use PHP's clone keyword.
  1054. * For a complete list of supported date part values look into the docu.
  1055. * If a date part is copied, all other date parts are set to standard values.
  1056. * For example: If only YEAR is copied, the returned date object is equal to
  1057. * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
  1058. * If only HOUR is copied, the returned date object is equal to
  1059. * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
  1060. *
  1061. * @param string $part Part of the date to compare, if null the timestamp is subtracted
  1062. * @param string|Zend_Locale $locale OPTIONAL New object's locale. No adjustments to timezone are made.
  1063. * @return Zend_Date
  1064. */
  1065. public function copyPart($part, $locale = null)
  1066. {
  1067. $clone = clone $this; // copy all instance variables
  1068. $clone->setUnixTimestamp(0); // except the timestamp
  1069. if ($locale != null) {
  1070. $clone->setLocale($locale); // set an other locale if selected
  1071. }
  1072. $clone->set($this, $part);
  1073. return $clone;
  1074. }
  1075. /**
  1076. * Internal function, returns the offset of a given timezone
  1077. *
  1078. * @param string $zone
  1079. * @return integer
  1080. */
  1081. public function getTimezoneFromString($zone)
  1082. {
  1083. if (is_array($zone)) {
  1084. return $this->getTimezone();
  1085. }
  1086. if ($zone instanceof Zend_Date) {
  1087. return $zone->getTimezone();
  1088. }
  1089. preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
  1090. if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
  1091. $zone = "Etc/GMT";
  1092. $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
  1093. $zone .= (int) abs($match[count($match) - 1]);
  1094. return $zone;
  1095. }
  1096. preg_match('/([[:alpha:]\/]{3,30})/', $zone, $match);
  1097. try {
  1098. if (!empty($match) and (!is_int($match[count($match) - 1]))) {
  1099. $oldzone = $this->getTimezone();
  1100. $this->setTime

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