PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/_workbench/tokenizer/test3.php

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

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