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

/workbench/tokenizer/test3.php

https://github.com/beardedlinuxgeek/phpjs
PHP | 4662 lines | 2721 code | 543 blank | 1398 comment | 518 complexity | 668f34d12a6e41e4348c5123d488a0b9 MD5 | raw 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 $comp Second date for calculation
  1106. * @param boolean|integer $dst Use dst correction if option is set
  1107. * @return integer|string|Zend_Date new timestamp or Zend_Date depending on calculation
  1108. */
  1109. private function _assign($calc, $date, $comp = 0, $dst = false)
  1110. {
  1111. switch ($calc) {
  1112. case 'set' :
  1113. if (!empty($comp)) {
  1114. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $comp));
  1115. }
  1116. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1117. $value = $this->getUnixTimestamp();
  1118. break;
  1119. case 'add' :
  1120. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1121. $value = $this->getUnixTimestamp();
  1122. break;
  1123. case 'sub' :
  1124. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $date));
  1125. $value = $this->getUnixTimestamp();
  1126. break;
  1127. default :
  1128. // cmp - compare
  1129. return call_user_func(Zend_Locale_Math::$comp, $comp, $date);
  1130. break;
  1131. }
  1132. // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
  1133. if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
  1134. $hour = $this->get(self::HOUR);
  1135. if ($hour != $dst) {
  1136. if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
  1137. $value += 3600;
  1138. } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
  1139. $value -= 3600;
  1140. }
  1141. $this->setUnixTimestamp($value);
  1142. }
  1143. }
  1144. return $this->getUnixTimestamp();
  1145. }
  1146. /**
  1147. * Calculates the date or object
  1148. *
  1149. * @param string $calc Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set'
  1150. * @param string|integer|array|Zend_Date $date Date or datepart to calculate with
  1151. * @param string $part Part of the date to calculate, if null the timestamp is used
  1152. * @param string|Zend_Locale $locale Locale for parsing input
  1153. * @return integer|string|Zend_Date new timestamp
  1154. * @throws Zend_Date_Exception
  1155. */
  1156. private function _calculate($calc, $date, $part, $locale)
  1157. {
  1158. if (is_null($date) === true) {
  1159. #require_once 'Zend/Date/Exception.php';
  1160. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  1161. }
  1162. if (($part !== null) and (Zend_Locale::isLocale($part, null, false))) {
  1163. $locale = $part;
  1164. $part = null;
  1165. }
  1166. if ($locale === null) {
  1167. $locale = $this->getLocale();
  1168. }
  1169. $locale = (string) $locale;
  1170. // Create date parts
  1171. $year = $this->get(self::YEAR);
  1172. $month = $this->get(self::MONTH_SHORT);
  1173. $day = $this->get(self::DAY_SHORT);
  1174. $hour = $this->get(self::HOUR_SHORT);
  1175. $minute = $this->get(self::MINUTE_SHORT);
  1176. $second = $this->get(self::SECOND_SHORT);
  1177. // If object extract value
  1178. if ($date instanceof Zend_Date) {
  1179. $date = $date->get($part, $locale);
  1180. }
  1181. if (is_array($date) === true) {
  1182. if (empty($part) === false) {
  1183. switch($part) {
  1184. // Fall through
  1185. case self::DAY:
  1186. case self::DAY_SHORT:
  1187. if (isset($date['day']) === true) {
  1188. $date = $date['day'];
  1189. }
  1190. break;
  1191. // Fall through
  1192. case self::WEEKDAY_SHORT:
  1193. case self::WEEKDAY:
  1194. case self::WEEKDAY_8601:
  1195. case self::WEEKDAY_DIGIT:
  1196. case self::WEEKDAY_NARROW:
  1197. case self::WEEKDAY_NAME:
  1198. if (isset($date['weekday']) === true) {
  1199. $date = $date['weekday'];
  1200. $part = self::WEEKDAY_DIGIT;
  1201. }
  1202. break;
  1203. case self::DAY_OF_YEAR:
  1204. if (isset($date['day_of_year']) === true) {
  1205. $date = $date['day_of_year'];
  1206. }
  1207. break;
  1208. // Fall through
  1209. case self::MONTH:
  1210. case self::MONTH_SHORT:
  1211. case self::MONTH_NAME:
  1212. case self::MONTH_NAME_SHORT:
  1213. case self::MONTH_NAME_NARROW:
  1214. if (isset($date['month']) === true) {
  1215. $date = $date['month'];
  1216. }
  1217. break;
  1218. // Fall through
  1219. case self::YEAR:
  1220. case self::YEAR_SHORT:
  1221. case self::YEAR_8601:
  1222. case self::YEAR_SHORT_8601:
  1223. if (isset($date['year']) === true) {
  1224. $date = $date['year'];
  1225. }
  1226. break;
  1227. // Fall through
  1228. case self::HOUR:
  1229. case self::HOUR_AM:
  1230. case self::HOUR_SHORT:
  1231. case self::HOUR_SHORT_AM:
  1232. if (isset($date['hour']) === true) {
  1233. $date = $date['hour'];
  1234. }
  1235. break;
  1236. // Fall through
  1237. case self::MINUTE:
  1238. case self::MINUTE_SHORT:
  1239. if (isset($date['minute']) === true) {
  1240. $date = $date['minute'];
  1241. }
  1242. break;
  1243. // Fall through
  1244. case self::SECOND:
  1245. case self::SECOND_SHORT:
  1246. if (isset($date['second']) === true) {
  1247. $date = $date['second'];
  1248. }
  1249. break;
  1250. // Fall through
  1251. case self::TIMEZONE:
  1252. case self::TIMEZONE_NAME:
  1253. if (isset($date['timezone']) === true) {
  1254. $date = $date['timezone'];
  1255. }
  1256. break;
  1257. case self::TIMESTAMP:
  1258. if (isset($date['timestamp']) === true) {
  1259. $date = $date['timestamp'];
  1260. }
  1261. break;
  1262. case self::WEEK:
  1263. if (isset($date['week']) === true) {
  1264. $date = $date['week'];
  1265. }
  1266. break;
  1267. case self::TIMEZONE_SECS:
  1268. if (isset($date['gmtsecs']) === true) {
  1269. $date = $date['gmtsecs'];
  1270. }
  1271. break;
  1272. default:
  1273. #require_once 'Zend/Date/Exception.php';
  1274. throw new Zend_Date_Exception("datepart for part ($part) not found in array");
  1275. break;
  1276. }
  1277. } else {
  1278. $hours = 0;
  1279. if (isset($date['hour']) === true) {
  1280. $hours = $date['hour'];
  1281. }
  1282. $minutes = 0;
  1283. if (isset($date['minute']) === true) {
  1284. $minutes = $date['minute'];
  1285. }
  1286. $seconds = 0;
  1287. if (isset($date['second']) === true) {
  1288. $seconds = $date['second'];
  1289. }
  1290. $months = 0;
  1291. if (isset($date['month']) === true) {
  1292. $months = $date['month'];
  1293. }
  1294. $days = 0;
  1295. if (isset($date['day']) === true) {
  1296. $days = $date['day'];
  1297. }
  1298. $years = 0;
  1299. if (isset($date['year']) === true) {
  1300. $years = $date['year'];
  1301. }
  1302. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true),
  1303. $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour);
  1304. }
  1305. }
  1306. // $date as object, part of foreign date as own date
  1307. switch($part) {
  1308. // day formats
  1309. case self::DAY:
  1310. if (is_numeric($date)) {
  1311. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1312. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1313. }
  1314. #require_once 'Zend/Date/Exception.php';
  1315. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
  1316. break;
  1317. case self::WEEKDAY_SHORT:
  1318. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1319. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1320. $cnt = 0;
  1321. foreach ($daylist as $key => $value) {
  1322. if (strtoupper(iconv_substr($value, 0, 3)) == strtoupper($date)) {
  1323. $found = $cnt;
  1324. break;
  1325. }
  1326. ++$cnt;
  1327. }
  1328. // Weekday found
  1329. if ($cnt < 7) {
  1330. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1331. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1332. }
  1333. // Weekday not found
  1334. #require_once 'Zend/Date/Exception.php';
  1335. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1336. break;
  1337. case self::DAY_SHORT:
  1338. if (is_numeric($date)) {
  1339. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1340. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1341. }
  1342. #require_once 'Zend/Date/Exception.php';
  1343. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
  1344. break;
  1345. case self::WEEKDAY:
  1346. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1347. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1348. $cnt = 0;
  1349. foreach ($daylist as $key => $value) {
  1350. if (strtoupper($value) == strtoupper($date)) {
  1351. $found = $cnt;
  1352. break;
  1353. }
  1354. ++$cnt;
  1355. }
  1356. // Weekday found
  1357. if ($cnt < 7) {
  1358. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1359. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1360. }
  1361. // Weekday not found
  1362. #require_once 'Zend/Date/Exception.php';
  1363. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1364. break;
  1365. case self::WEEKDAY_8601:
  1366. $weekday = (int) $this->get(self::WEEKDAY_8601, $locale);
  1367. if ((intval($date) > 0) and (intval($date) < 8)) {
  1368. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1369. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1370. }
  1371. // Weekday not found
  1372. #require_once 'Zend/Date/Exception.php';
  1373. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1374. break;
  1375. case self::DAY_SUFFIX:
  1376. #require_once 'Zend/Date/Exception.php';
  1377. throw new Zend_Date_Exception('day suffix not supported', $date);
  1378. break;
  1379. case self::WEEKDAY_DIGIT:
  1380. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1381. if (is_numeric($date) and (intval($date) >= 0) and (intval($date) < 7)) {
  1382. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
  1383. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1384. }
  1385. // Weekday not found
  1386. #require_once 'Zend/Date/Exception.php';
  1387. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1388. break;
  1389. case self::DAY_OF_YEAR:
  1390. if (is_numeric($date)) {
  1391. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
  1392. $this->mktime(0, 0, 0, $month, 1 + $day, 1970, true), $hour);
  1393. }
  1394. #require_once 'Zend/Date/Exception.php';
  1395. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
  1396. break;
  1397. case self::WEEKDAY_NARROW:
  1398. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1399. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1400. $cnt = 0;
  1401. foreach ($daylist as $key => $value) {
  1402. if (strtoupper(iconv_substr($value, 0, 1)) == strtoupper($date)) {
  1403. $found = $cnt;
  1404. break;
  1405. }
  1406. ++$cnt;
  1407. }
  1408. // Weekday found
  1409. if ($cnt < 7) {
  1410. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1411. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1412. }
  1413. // Weekday not found
  1414. #require_once 'Zend/Date/Exception.php';
  1415. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1416. break;
  1417. case self::WEEKDAY_NAME:
  1418. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1419. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1420. $cnt = 0;
  1421. foreach ($daylist as $key => $value) {
  1422. if (strtoupper($value) == strtoupper($date)) {
  1423. $found = $cnt;
  1424. break;
  1425. }
  1426. ++$cnt;
  1427. }
  1428. // Weekday found
  1429. if ($cnt < 7) {
  1430. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1431. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1432. }
  1433. // Weekday not found
  1434. #require_once 'Zend/Date/Exception.php';
  1435. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1436. break;
  1437. // week formats
  1438. case self::WEEK:
  1439. if (is_numeric($date)) {
  1440. $week = (int) $this->get(self::WEEK, $locale);
  1441. return $this->_assign($calc, parent::mktime(0, 0, 0, 1, 1 + ($date * 7), 1970, true),
  1442. parent::mktime(0, 0, 0, 1, 1 + ($week * 7), 1970, true), $hour);
  1443. }
  1444. #require_once 'Zend/Date/Exception.php';
  1445. throw new Zend_Date_Exception("invalid date ($date) operand, week expected", $date);
  1446. break;
  1447. // month formats
  1448. case self::MONTH_NAME:
  1449. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  1450. $cnt = 0;
  1451. foreach ($monthlist as $key => $value) {
  1452. if (strtoupper($value) == strtoupper($date)) {
  1453. $found = $key;
  1454. break;
  1455. }
  1456. ++$cnt;
  1457. }
  1458. $date = array_search($date, $monthlist);
  1459. // Monthname found
  1460. if ($cnt < 12) {
  1461. $fixday = 0;
  1462. if ($calc == 'add') {
  1463. $date += $found;
  1464. $calc = 'set';
  1465. if (self::$_options['extend_month'] == false) {
  1466. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1467. if ($parts['mday'] != $day) {
  1468. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1469. }
  1470. }
  1471. } else if ($calc == 'sub') {
  1472. $date = $month - $found;
  1473. $calc = 'set';
  1474. if (self::$_options['extend_month'] == false) {
  1475. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1476. if ($parts['mday'] != $day) {
  1477. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1478. }
  1479. }
  1480. }
  1481. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1482. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1483. }
  1484. // Monthname not found
  1485. #require_once 'Zend/Date/Exception.php';
  1486. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1487. break;
  1488. case self::MONTH:
  1489. if (is_numeric($date)) {
  1490. $fixday = 0;
  1491. if ($calc == 'add') {
  1492. $date += $month;
  1493. $calc = 'set';
  1494. if (self::$_options['extend_month'] == false) {
  1495. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1496. if ($parts['mday'] != $day) {
  1497. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1498. }
  1499. }
  1500. } else if ($calc == 'sub') {
  1501. $date = $month - $date;
  1502. $calc = 'set';
  1503. if (self::$_options['extend_month'] == false) {
  1504. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1505. if ($parts['mday'] != $day) {
  1506. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1507. }
  1508. }
  1509. }
  1510. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1511. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1512. }
  1513. #require_once 'Zend/Date/Exception.php';
  1514. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1515. break;
  1516. case self::MONTH_NAME_SHORT:
  1517. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  1518. $cnt = 0;
  1519. foreach ($monthlist as $key => $value) {
  1520. if (strtoupper($value) == strtoupper($date)) {
  1521. $found = $key;
  1522. break;
  1523. }
  1524. ++$cnt;
  1525. }
  1526. $date = array_search($date, $monthlist);
  1527. // Monthname found
  1528. if ($cnt < 12) {
  1529. $fixday = 0;
  1530. if ($calc == 'add') {
  1531. $date += $found;
  1532. $calc = 'set';
  1533. if (self::$_options['extend_month'] === false) {
  1534. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1535. if ($parts['mday'] != $day) {
  1536. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1537. }
  1538. }
  1539. } else if ($calc == 'sub') {
  1540. $date = $month - $found;
  1541. $calc = 'set';
  1542. if (self::$_options['extend_month'] === false) {
  1543. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1544. if ($parts['mday'] != $day) {
  1545. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1546. }
  1547. }
  1548. }
  1549. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1550. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1551. }
  1552. // Monthname not found
  1553. #require_once 'Zend/Date/Exception.php';
  1554. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1555. break;
  1556. case self::MONTH_SHORT:
  1557. if (is_numeric($date) === true) {
  1558. $fixday = 0;
  1559. if ($calc === 'add') {
  1560. $date += $month;
  1561. $calc = 'set';
  1562. if (self::$_options['extend_month'] === false) {
  1563. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1564. if ($parts['mday'] != $day) {
  1565. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1566. }
  1567. }
  1568. } else if ($calc === 'sub') {
  1569. $date = $month - $date;
  1570. $calc = 'set';
  1571. if (self::$_options['extend_month'] === false) {
  1572. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1573. if ($parts['mday'] != $day) {
  1574. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1575. }
  1576. }
  1577. }
  1578. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1579. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1580. }
  1581. #require_once 'Zend/Date/Exception.php';
  1582. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1583. break;
  1584. case self::MONTH_DAYS:
  1585. #require_once 'Zend/Date/Exception.php';
  1586. throw new Zend_Date_Exception('month days not supported', $date);
  1587. break;
  1588. case self::MONTH_NAME_NARROW:
  1589. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow'));
  1590. $cnt = 0;
  1591. foreach ($monthlist as $key => $value) {
  1592. if (strtoupper($value) === strtoupper($date)) {
  1593. $found = $key;
  1594. break;
  1595. }
  1596. ++$cnt;
  1597. }
  1598. $date = array_search($date, $monthlist);
  1599. // Monthname found
  1600. if ($cnt < 12) {
  1601. $fixday = 0;
  1602. if ($calc === 'add') {
  1603. $date += $found;
  1604. $calc = 'set';
  1605. if (self::$_options['extend_month'] === false) {
  1606. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1607. if ($parts['mday'] != $day) {
  1608. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1609. }
  1610. }
  1611. } else if ($calc === 'sub') {
  1612. $date = $month - $found;
  1613. $calc = 'set';
  1614. if (self::$_options['extend_month'] === false) {
  1615. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1616. if ($parts['mday'] != $day) {
  1617. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1618. }
  1619. }
  1620. }
  1621. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1622. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1623. }
  1624. // Monthname not found
  1625. #require_once 'Zend/Date/Exception.php';
  1626. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1627. break;
  1628. // year formats
  1629. case self::LEAPYEAR:
  1630. #require_once 'Zend/Date/Exception.php';
  1631. throw new Zend_Date_Exception('leap year not supported', $date);
  1632. break;
  1633. case self::YEAR_8601:
  1634. if (is_numeric($date)) {
  1635. if ($calc === 'add') {
  1636. $date += $year;
  1637. $calc = 'set';
  1638. } else if ($calc === 'sub') {
  1639. $date = $year - $date;
  1640. $calc = 'set';
  1641. }
  1642. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1643. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1644. }
  1645. #require_once 'Zend/Date/Exception.php';
  1646. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1647. break;
  1648. case self::YEAR:
  1649. if (is_numeric($date)) {
  1650. if ($calc === 'add') {
  1651. $date += $year;
  1652. $calc = 'set';
  1653. } else if ($calc === 'sub') {
  1654. $date = $year - $date;
  1655. $calc = 'set';
  1656. }
  1657. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1658. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1659. }
  1660. #require_once 'Zend/Date/Exception.php';
  1661. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1662. break;
  1663. case self::YEAR_SHORT:
  1664. if (is_numeric($date)) {
  1665. $date = intval($date);
  1666. if (($calc == 'set') || ($calc == 'cmp')) {
  1667. $date = self::getFullYear($date);
  1668. }
  1669. if ($calc === 'add') {
  1670. $date += $year;
  1671. $calc = 'set';
  1672. } else if ($calc === 'sub') {
  1673. $date = $year - $date;
  1674. $calc = 'set';
  1675. }
  1676. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1677. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1678. }
  1679. #require_once 'Zend/Date/Exception.php';
  1680. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1681. break;
  1682. case self::YEAR_SHORT_8601:
  1683. if (is_numeric($date)) {
  1684. $date = intval($date);
  1685. if (($calc === 'set') || ($calc === 'cmp')) {
  1686. $date = self::getFullYear($date);
  1687. }
  1688. if ($calc === 'add') {
  1689. $date += $year;
  1690. $calc = 'set';
  1691. } else if ($calc === 'sub') {
  1692. $date = $year - $date;
  1693. $calc = 'set';
  1694. }
  1695. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1696. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1697. }
  1698. #require_once 'Zend/Date/Exception.php';
  1699. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1700. break;
  1701. // time formats
  1702. case self::MERIDIEM:
  1703. #require_once 'Zend/Date/Exception.php';
  1704. throw new Zend_Date_Exception('meridiem not supported', $date);
  1705. break;
  1706. case self::SWATCH:
  1707. if (is_numeric($date)) {
  1708. $rest = intval($date);
  1709. $hours = floor($rest * 24 / 1000);
  1710. $rest = $rest - ($hours * 1000 / 24);
  1711. $minutes = floor($rest * 1440 / 1000);
  1712. $rest = $rest - ($minutes * 1000 / 1440);
  1713. $seconds = floor($rest * 86400 / 1000);
  1714. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true),
  1715. $this->mktime($hour, $minute, $second, 1, 1, 1970, true), false);
  1716. }
  1717. #require_once 'Zend/Date/Exception.php';
  1718. throw new Zend_Date_Exception("invalid date ($date) operand, swatchstamp expected", $date);
  1719. break;
  1720. case self::HOUR_SHORT_AM:
  1721. if (is_numeric($date)) {
  1722. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1723. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1724. }
  1725. #require_once 'Zend/Date/Exception.php';
  1726. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1727. break;
  1728. case self::HOUR_SHORT:
  1729. if (is_numeric($date)) {
  1730. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1731. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1732. }
  1733. #require_once 'Zend/Date/Exception.php';
  1734. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1735. break;
  1736. case self::HOUR_AM:
  1737. if (is_numeric($date)) {
  1738. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1739. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1740. }
  1741. #require_once 'Zend/Date/Exception.php';
  1742. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1743. break;
  1744. case self::HOUR:
  1745. if (is_numeric($date)) {
  1746. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1747. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1748. }
  1749. #require_once 'Zend/Date/Exception.php';
  1750. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1751. break;
  1752. case self::MINUTE:
  1753. if (is_numeric($date)) {
  1754. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1755. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1756. }
  1757. #require_once 'Zend/Date/Exception.php';
  1758. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", $date);
  1759. break;
  1760. case self::SECOND:
  1761. if (is_numeric($date)) {
  1762. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1763. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1764. }
  1765. #require_once 'Zend/Date/Exception.php';
  1766. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", $date);
  1767. break;
  1768. case self::MILLISECOND:
  1769. if (is_numeric($date)) {
  1770. switch($calc) {
  1771. case 'set' :
  1772. return $this->setMillisecond($date);
  1773. break;
  1774. case 'add' :
  1775. return $this->addMillisecond($date);
  1776. break;
  1777. case 'sub' :
  1778. return $this->subMillisecond($date);
  1779. break;
  1780. }
  1781. return $this->compareMillisecond($date);
  1782. }
  1783. #require_once 'Zend/Date/Exception.php';
  1784. throw new Zend_Date_Exception("invalid date ($date) operand, milliseconds expected", $date);
  1785. break;
  1786. case self::MINUTE_SHORT:
  1787. if (is_numeric($date)) {
  1788. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1789. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1790. }
  1791. #require_once 'Zend/Date/Exception.php';
  1792. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", $date);
  1793. break;
  1794. case self::SECOND_SHORT:
  1795. if (is_numeric($date)) {
  1796. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1797. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1798. }
  1799. #require_once 'Zend/Date/Exception.php';
  1800. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", $date);
  1801. break;
  1802. // timezone formats
  1803. // break intentionally omitted
  1804. case self::TIMEZONE_NAME:
  1805. case self::TIMEZONE:
  1806. case self::TIMEZONE_SECS:
  1807. #require_once 'Zend/Date/Exception.php';
  1808. throw new Zend_Date_Exception('timezone not supported', $date);
  1809. break;
  1810. case self::DAYLIGHT:
  1811. #require_once 'Zend/Date/Exception.php';
  1812. throw new Zend_Date_Exception('daylight not supported', $date);
  1813. break;
  1814. case self::GMT_DIFF:
  1815. case self::GMT_DIFF_SEP:
  1816. #require_once 'Zend/Date/Exception.php';
  1817. throw new Zend_Date_Exception('gmtdiff not supported', $date);
  1818. break;
  1819. // date strings
  1820. case self::ISO_8601:
  1821. // (-)YYYY-MM-dd
  1822. preg_match('/^(-{0,1}\d{4})-(\d{2})-(\d{2})/', $date, $datematch);
  1823. // (-)YY-MM-dd
  1824. if (empty($datematch)) {
  1825. preg_match('/^(-{0,1}\d{2})-(\d{2})-(\d{2})/', $date, $datematch);
  1826. }
  1827. // (-)YYYYMMdd
  1828. if (empty($datematch)) {
  1829. preg_match('/^(-{0,1}\d{4})(\d{2})(\d{2})/', $date, $datematch);
  1830. }
  1831. // (-)YYMMdd
  1832. if (empty($datematch)) {
  1833. preg_match('/^(-{0,1}\d{2})(\d{2})(\d{2})/', $date, $datematch);
  1834. }
  1835. $tmpdate = $date;
  1836. if (!empty($datematch)) {
  1837. $tmpdate = iconv_substr($date, iconv_strlen($datematch[0]));
  1838. }
  1839. // (T)hh:mm:ss
  1840. preg_match('/[T,\s]{0,1}(\d{2}):(\d{2}):(\d{2})/', $tmpdate, $timematch);
  1841. if (empty($timematch)) {
  1842. preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
  1843. }
  1844. if (empty($datematch) and empty($timematch)) {
  1845. #require_once 'Zend/Date/Exception.php';
  1846. throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", $date);
  1847. }
  1848. if (!empty($timematch)) {
  1849. $tmpdate = iconv_substr($tmpdate, iconv_strlen($timematch[0]));
  1850. }
  1851. if (empty($datematch)) {
  1852. $datematch[1] = 1970;
  1853. $datematch[2] = 1;
  1854. $datematch[3] = 1;
  1855. } else if (iconv_strlen($datematch[1]) == 2) {
  1856. $datematch[1] = self::getFullYear($datematch[1]);
  1857. }
  1858. if (empty($timematch)) {
  1859. $timematch[1] = 0;
  1860. $timematch[2] = 0;
  1861. $timematch[3] = 0;
  1862. }
  1863. if (($calc == 'set') || ($calc == 'cmp')) {
  1864. --$datematch[2];
  1865. --$month;
  1866. --$datematch[3];
  1867. --$day;
  1868. $datematch[1] -= 1970;
  1869. $year -= 1970;
  1870. }
  1871. return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false),
  1872. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1873. break;
  1874. case self::RFC_2822:
  1875. $result = preg_match('/^\w{3},\s(\d{1,2})\s(\w{3})\s(\d{4})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4})$/', $date, $match);
  1876. if (!$result) {
  1877. #require_once 'Zend/Date/Exception.php';
  1878. throw new Zend_Date_Exception("no RFC 2822 format ($date)", $date);
  1879. }
  1880. $months = $this->_getDigitFromName($match[2]);
  1881. if (($calc == 'set') || ($calc == 'cmp')) {
  1882. --$months;
  1883. --$month;
  1884. --$match[1];
  1885. --$day;
  1886. $match[3] -= 1970;
  1887. $year -= 1970;
  1888. }
  1889. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  1890. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1891. break;
  1892. case self::TIMESTAMP:
  1893. if (is_numeric($date)) {
  1894. return $this->_assign($calc, $date, $this->getUnixTimestamp());
  1895. }
  1896. #require_once 'Zend/Date/Exception.php';
  1897. throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", $date);
  1898. break;
  1899. // additional formats
  1900. // break intentionally omitted
  1901. case self::ERA:
  1902. case self::ERA_NAME:
  1903. #require_once 'Zend/Date/Exception.php';
  1904. throw new Zend_Date_Exception('era not supported', $date);
  1905. break;
  1906. case self::DATES:
  1907. try {
  1908. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  1909. if (($calc == 'set') || ($calc == 'cmp')) {
  1910. --$parsed['month'];
  1911. --$month;
  1912. --$parsed['day'];
  1913. --$day;
  1914. $parsed['year'] -= 1970;
  1915. $year -= 1970;
  1916. }
  1917. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1918. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1919. } catch (Zend_Locale_Exception $e) {
  1920. #require_once 'Zend/Date/Exception.php';
  1921. throw new Zend_Date_Exception($e->getMessage(), $date);
  1922. }
  1923. break;
  1924. case self::DATE_FULL:
  1925. try {
  1926. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  1927. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1928. if (($calc == 'set') || ($calc == 'cmp')) {
  1929. --$parsed['month'];
  1930. --$month;
  1931. --$parsed['day'];
  1932. --$day;
  1933. $parsed['year'] -= 1970;
  1934. $year -= 1970;
  1935. }
  1936. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1937. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1938. } catch (Zend_Locale_Exception $e) {
  1939. #require_once 'Zend/Date/Exception.php';
  1940. throw new Zend_Date_Exception($e->getMessage(), $date);
  1941. }
  1942. break;
  1943. case self::DATE_LONG:
  1944. try {
  1945. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  1946. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1947. if (($calc == 'set') || ($calc == 'cmp')){
  1948. --$parsed['month'];
  1949. --$month;
  1950. --$parsed['day'];
  1951. --$day;
  1952. $parsed['year'] -= 1970;
  1953. $year -= 1970;
  1954. }
  1955. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1956. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1957. } catch (Zend_Locale_Exception $e) {
  1958. #require_once 'Zend/Date/Exception.php';
  1959. throw new Zend_Date_Exception($e->getMessage(), $date);
  1960. }
  1961. break;
  1962. case self::DATE_MEDIUM:
  1963. try {
  1964. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  1965. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1966. if (($calc == 'set') || ($calc == 'cmp')) {
  1967. --$parsed['month'];
  1968. --$month;
  1969. --$parsed['day'];
  1970. --$day;
  1971. $parsed['year'] -= 1970;
  1972. $year -= 1970;
  1973. }
  1974. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1975. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1976. } catch (Zend_Locale_Exception $e) {
  1977. #require_once 'Zend/Date/Exception.php';
  1978. throw new Zend_Date_Exception($e->getMessage(), $date);
  1979. }
  1980. break;
  1981. case self::DATE_SHORT:
  1982. try {
  1983. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  1984. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1985. $parsed['year'] = self::getFullYear($parsed['year']);
  1986. if (($calc == 'set') || ($calc == 'cmp')) {
  1987. --$parsed['month'];
  1988. --$month;
  1989. --$parsed['day'];
  1990. --$day;
  1991. $parsed['year'] -= 1970;
  1992. $year -= 1970;
  1993. }
  1994. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1995. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1996. } catch (Zend_Locale_Exception $e) {
  1997. #require_once 'Zend/Date/Exception.php';
  1998. throw new Zend_Date_Exception($e->getMessage(), $date);
  1999. }
  2000. break;
  2001. case self::TIMES:
  2002. try {
  2003. if ($calc != 'set') {
  2004. $month = 1;
  2005. $day = 1;
  2006. $year = 1970;
  2007. }
  2008. $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  2009. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2010. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2011. } catch (Zend_Locale_Exception $e) {
  2012. #require_once 'Zend/Date/Exception.php';
  2013. throw new Zend_Date_Exception($e->getMessage(), $date);
  2014. }
  2015. break;
  2016. case self::TIME_FULL:
  2017. try {
  2018. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  2019. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2020. if ($calc != 'set') {
  2021. $month = 1;
  2022. $day = 1;
  2023. $year = 1970;
  2024. }
  2025. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], 0, $month, $day, $year, true),
  2026. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2027. } catch (Zend_Locale_Exception $e) {
  2028. #require_once 'Zend/Date/Exception.php';
  2029. throw new Zend_Date_Exception($e->getMessage(), $date);
  2030. }
  2031. break;
  2032. case self::TIME_LONG:
  2033. try {
  2034. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
  2035. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2036. if ($calc != 'set') {
  2037. $month = 1;
  2038. $day = 1;
  2039. $year = 1970;
  2040. }
  2041. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2042. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2043. } catch (Zend_Locale_Exception $e) {
  2044. #require_once 'Zend/Date/Exception.php';
  2045. throw new Zend_Date_Exception($e->getMessage(), $date);
  2046. }
  2047. break;
  2048. case self::TIME_MEDIUM:
  2049. try {
  2050. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
  2051. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2052. if ($calc != 'set') {
  2053. $month = 1;
  2054. $day = 1;
  2055. $year = 1970;
  2056. }
  2057. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2058. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2059. } catch (Zend_Locale_Exception $e) {
  2060. #require_once 'Zend/Date/Exception.php';
  2061. throw new Zend_Date_Exception($e->getMessage(), $date);
  2062. }
  2063. break;
  2064. case self::TIME_SHORT:
  2065. try {
  2066. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
  2067. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2068. if ($calc != 'set') {
  2069. $month = 1;
  2070. $day = 1;
  2071. $year = 1970;
  2072. }
  2073. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], 0, $month, $day, $year, true),
  2074. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2075. } catch (Zend_Locale_Exception $e) {
  2076. #require_once 'Zend/Date/Exception.php';
  2077. throw new Zend_Date_Exception($e->getMessage(), $date);
  2078. }
  2079. break;
  2080. // ATOM and RFC_3339 are identical
  2081. case self::ATOM:
  2082. case self::RFC_3339:
  2083. $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\d{0,4}([+-]{1}\d{2}:\d{2}|Z)$/', $date, $match);
  2084. if (!$result) {
  2085. #require_once 'Zend/Date/Exception.php';
  2086. throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", $date);
  2087. }
  2088. if (($calc == 'set') || ($calc == 'cmp')) {
  2089. --$match[2];
  2090. --$month;
  2091. --$match[3];
  2092. --$day;
  2093. $match[1] -= 1970;
  2094. $year -= 1970;
  2095. }
  2096. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2097. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2098. break;
  2099. case self::COOKIE:
  2100. $result = preg_match("/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,20}$/", $date, $match);
  2101. if (!$result) {
  2102. #require_once 'Zend/Date/Exception.php';
  2103. throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", $date);
  2104. }
  2105. $match[0] = iconv_substr($match[0], iconv_strpos($match[0], ' ')+1);
  2106. $months = $this->_getDigitFromName($match[2]);
  2107. $match[3] = self::getFullYear($match[3]);
  2108. if (($calc == 'set') || ($calc == 'cmp')) {
  2109. --$months;
  2110. --$month;
  2111. --$match[1];
  2112. --$day;
  2113. $match[3] -= 1970;
  2114. $year -= 1970;
  2115. }
  2116. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2117. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2118. break;
  2119. case self::RFC_822:
  2120. case self::RFC_1036:
  2121. // new RFC 822 format, identical to RFC 1036 standard
  2122. $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
  2123. if (!$result) {
  2124. #require_once 'Zend/Date/Exception.php';
  2125. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", $date);
  2126. }
  2127. $months = $this->_getDigitFromName($match[2]);
  2128. $match[3] = self::getFullYear($match[3]);
  2129. if (($calc == 'set') || ($calc == 'cmp')) {
  2130. --$months;
  2131. --$month;
  2132. --$match[1];
  2133. --$day;
  2134. $match[3] -= 1970;
  2135. $year -= 1970;
  2136. }
  2137. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  2138. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  2139. break;
  2140. case self::RFC_850:
  2141. $result = preg_match('/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,21}$/', $date, $match);
  2142. if (!$result) {
  2143. #require_once 'Zend/Date/Exception.php';
  2144. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", $date);
  2145. }
  2146. $months = $this->_getDigitFromName($match[2]);
  2147. $match[3] = self::getFullYear($match[3]);
  2148. if (($calc == 'set') || ($calc == 'cmp')) {
  2149. --$months;
  2150. --$month;
  2151. --$match[1];
  2152. --$day;
  2153. $match[3] -= 1970;
  2154. $year -= 1970;
  2155. }
  2156. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2157. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2158. break;
  2159. case self::RFC_1123:
  2160. $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2,4})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
  2161. if (!$result) {
  2162. #require_once 'Zend/Date/Exception.php';
  2163. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", $date);
  2164. }
  2165. $months = $this->_getDigitFromName($match[2]);
  2166. if (($calc == 'set') || ($calc == 'cmp')) {
  2167. --$months;
  2168. --$month;
  2169. --$match[1];
  2170. --$day;
  2171. $match[3] -= 1970;
  2172. $year -= 1970;
  2173. }
  2174. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2175. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2176. break;
  2177. case self::RSS:
  2178. $result = preg_match('/^\w{3},\s(\d{2})\s(\w{3})\s(\d{2,4})\s(\d{1,2}):(\d{2}):(\d{2})\s.{1,21}$/', $date, $match);
  2179. if (!$result) {
  2180. #require_once 'Zend/Date/Exception.php';
  2181. throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", $date);
  2182. }
  2183. $months = $this->_getDigitFromName($match[2]);
  2184. $match[3] = self::getFullYear($match[3]);
  2185. if (($calc == 'set') || ($calc == 'cmp')) {
  2186. --$months;
  2187. --$month;
  2188. --$match[1];
  2189. --$day;
  2190. $match[3] -= 1970;
  2191. $year -= 1970;
  2192. }
  2193. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2194. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2195. break;
  2196. case self::W3C:
  2197. $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
  2198. if (!$result) {
  2199. #require_once 'Zend/Date/Exception.php';
  2200. throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", $date);
  2201. }
  2202. if (($calc == 'set') || ($calc == 'cmp')) {
  2203. --$match[2];
  2204. --$month;
  2205. --$match[3];
  2206. --$day;
  2207. $match[1] -= 1970;
  2208. $year -= 1970;
  2209. }
  2210. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2211. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2212. break;
  2213. default:
  2214. if (!is_numeric($date) || !empty($part)) {
  2215. try {
  2216. if (self::$_options['format_type'] == 'php') {
  2217. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  2218. }
  2219. if (empty($part)) {
  2220. $part = Zend_Locale_Format::getDateFormat($locale) . " ";
  2221. $part .= Zend_Locale_Format::getTimeFormat($locale);
  2222. }
  2223. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
  2224. if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
  2225. $parsed['year'] = self::getFullYear($parsed['year']);
  2226. }
  2227. if (($calc == 'set') || ($calc == 'cmp')) {
  2228. if (isset($parsed['month'])) {
  2229. --$parsed['month'];
  2230. } else {
  2231. $parsed['month'] = 0;
  2232. }
  2233. if (isset($parsed['day'])) {
  2234. --$parsed['day'];
  2235. } else {
  2236. $parsed['day'] = 0;
  2237. }
  2238. if (isset($parsed['year'])) {
  2239. $parsed['year'] -= 1970;
  2240. } else {
  2241. $parsed['year'] = 0;
  2242. }
  2243. }
  2244. return $this->_assign($calc, $this->mktime(
  2245. isset($parsed['hour']) ? $parsed['hour'] : 0,
  2246. isset($parsed['minute']) ? $parsed['minute'] : 0,
  2247. isset($parsed['second']) ? $parsed['second'] : 0,
  2248. 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'],
  2249. false), $this->getUnixTimestamp(), false);
  2250. } catch (Zend_Locale_Exception $e) {
  2251. if (!is_numeric($date)) {
  2252. #require_once 'Zend/Date/Exception.php';
  2253. throw new Zend_Date_Exception($e->getMessage(), $date);
  2254. }
  2255. }
  2256. }
  2257. return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
  2258. break;
  2259. }
  2260. }
  2261. /**
  2262. * Returns true when both date objects or date parts are equal.
  2263. * For example:
  2264. * 15.May.2000 <-> 15.June.2000 Equals only for Day or Year... all other will return false
  2265. *
  2266. * @param string|integer|array|Zend_Date $date Date or datepart to equal with
  2267. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2268. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2269. * @return boolean
  2270. * @throws Zend_Date_Exception
  2271. */
  2272. public function equals($date, $part = null, $locale = null)
  2273. {
  2274. $result = $this->compare($date, $part, $locale);
  2275. if ($result == 0) {
  2276. return true;
  2277. }
  2278. return false;
  2279. }
  2280. /**
  2281. * Returns if the given date or datepart is earlier
  2282. * For example:
  2283. * 15.May.2000 <-> 13.June.1999 will return true for day, year and date, but not for month
  2284. *
  2285. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2286. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2287. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2288. * @return boolean
  2289. * @throws Zend_Date_Exception
  2290. */
  2291. public function isEarlier($date, $part = null, $locale = null)
  2292. {
  2293. $result = $this->compare($date, $part, $locale);
  2294. if ($result == -1) {
  2295. return true;
  2296. }
  2297. return false;
  2298. }
  2299. /**
  2300. * Returns if the given date or datepart is later
  2301. * For example:
  2302. * 15.May.2000 <-> 13.June.1999 will return true for month but false for day, year and date
  2303. * Returns if the given date is later
  2304. *
  2305. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2306. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2307. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2308. * @return boolean
  2309. * @throws Zend_Date_Exception
  2310. */
  2311. public function isLater($date, $part = null, $locale = null)
  2312. {
  2313. $result = $this->compare($date, $part, $locale);
  2314. if ($result == 1) {
  2315. return true;
  2316. }
  2317. return false;
  2318. }
  2319. /**
  2320. * Returns only the time of the date as new Zend_Date object
  2321. * For example:
  2322. * 15.May.2000 10:11:23 will return a dateobject equal to 01.Jan.1970 10:11:23
  2323. *
  2324. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2325. * @return Zend_Date
  2326. */
  2327. public function getTime($locale = null)
  2328. {
  2329. return $this->copyPart(self::TIME_MEDIUM, $locale);
  2330. }
  2331. /**
  2332. * Returns the calculated time
  2333. *
  2334. * @param string $calc Calculation to make
  2335. * @param string|integer|array|Zend_Date $time Time to calculate with, if null the actual time is taken
  2336. * @param string $format Timeformat for parsing input
  2337. * @param string|Zend_Locale $locale Locale for parsing input
  2338. * @return integer|Zend_Date new time
  2339. * @throws Zend_Date_Exception
  2340. */
  2341. private function _time($calc, $time, $format, $locale)
  2342. {
  2343. if (is_null($time)) {
  2344. #require_once 'Zend/Date/Exception.php';
  2345. throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
  2346. }
  2347. if ($locale === null) {
  2348. $locale = $this->getLocale();
  2349. }
  2350. if ($time instanceof Zend_Date) {
  2351. // extract time from object
  2352. $time = $time->get(self::TIME_MEDIUM, $locale);
  2353. } else {
  2354. if (is_array($time)) {
  2355. if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
  2356. (isset($time['second']) === true)) {
  2357. $parsed = $time;
  2358. } else {
  2359. #require_once 'Zend/Date/Exception.php';
  2360. throw new Zend_Date_Exception("no hour, minute or second given in array");
  2361. }
  2362. } else {
  2363. if (self::$_options['format_type'] == 'php') {
  2364. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2365. }
  2366. try {
  2367. $parsed = Zend_Locale_Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2368. } catch (Zend_Locale_Exception $e) {
  2369. #require_once 'Zend/Date/Exception.php';
  2370. throw new Zend_Date_Exception($e->getMessage());
  2371. }
  2372. }
  2373. $time = new self(0, self::TIMESTAMP, $locale);
  2374. $time->setTimezone('UTC');
  2375. $time->set($parsed['hour'], self::HOUR);
  2376. $time->set($parsed['minute'], self::MINUTE);
  2377. $time->set($parsed['second'], self::SECOND);
  2378. $time = $time->get(self::TIME_MEDIUM, $locale);
  2379. }
  2380. $return = $this->_calcdetail($calc, $time, self::TIME_MEDIUM, $locale);
  2381. if ($calc != 'cmp') {
  2382. return $this;
  2383. }
  2384. return $return;
  2385. }
  2386. /**
  2387. * Sets a new time for the date object. Format defines how to parse the time string.
  2388. * Also a complete date can be given, but only the time is used for setting.
  2389. * For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
  2390. * Returned is the new date object and the existing date is left as it was before
  2391. *
  2392. * @param string|integer|array|Zend_Date $time Time to set
  2393. * @param string $format OPTIONAL Timeformat for parsing input
  2394. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2395. * @return Zend_Date new time
  2396. * @throws Zend_Date_Exception
  2397. */
  2398. public function setTime($time, $format = null, $locale = null)
  2399. {
  2400. return $this->_time('set', $time, $format, $locale);
  2401. }
  2402. /**
  2403. * Adds a time to the existing date. Format defines how to parse the time string.
  2404. * If only parts are given the other parts are set to 0.
  2405. * If no format is given, the standardformat of this locale is used.
  2406. * For example: HH:mm:ss -> 10 -> +10 hours
  2407. *
  2408. * @param string|integer|array|Zend_Date $time Time to add
  2409. * @param string $format OPTIONAL Timeformat for parsing input
  2410. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2411. * @return Zend_Date new time
  2412. * @throws Zend_Date_Exception
  2413. */
  2414. public function addTime($time, $format = null, $locale = null)
  2415. {
  2416. return $this->_time('add', $time, $format, $locale);
  2417. }
  2418. /**
  2419. * Subtracts a time from the existing date. Format defines how to parse the time string.
  2420. * If only parts are given the other parts are set to 0.
  2421. * If no format is given, the standardformat of this locale is used.
  2422. * For example: HH:mm:ss -> 10 -> -10 hours
  2423. *
  2424. * @param string|integer|array|Zend_Date $time Time to sub
  2425. * @param string $format OPTIONAL Timeformat for parsing input
  2426. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2427. * @return Zend_Date new time
  2428. * @throws Zend_Date_Exception
  2429. */
  2430. public function subTime($time, $format = null, $locale = null)
  2431. {
  2432. return $this->_time('sub', $time, $format, $locale);
  2433. }
  2434. /**
  2435. * Compares the time from the existing date. Format defines how to parse the time string.
  2436. * If only parts are given the other parts are set to default.
  2437. * If no format us given, the standardformat of this locale is used.
  2438. * For example: HH:mm:ss -> 10 -> 10 hours
  2439. *
  2440. * @param string|integer|array|Zend_Date $time Time to compare
  2441. * @param string $format OPTIONAL Timeformat for parsing input
  2442. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2443. * @return integer 0 = equal, 1 = later, -1 = earlier
  2444. * @throws Zend_Date_Exception
  2445. */
  2446. public function compareTime($time, $format = null, $locale = null)
  2447. {
  2448. return $this->_time('cmp', $time, $format, $locale);
  2449. }
  2450. /**
  2451. * Returns a clone of $this, with the time part set to 00:00:00.
  2452. *
  2453. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2454. * @return Zend_Date
  2455. */
  2456. public function getDate($locale = null)
  2457. {
  2458. $date = $this->copyPart(self::DATE_FULL, $locale);
  2459. $date->addTimestamp($this->getGmtOffset());
  2460. return $date;
  2461. }
  2462. /**
  2463. * Returns the calculated date
  2464. *
  2465. * @param string $calc Calculation to make
  2466. * @param string|integer|array|Zend_Date $date Date to calculate with, if null the actual date is taken
  2467. * @param string $format Date format for parsing
  2468. * @param string|Zend_Locale $locale Locale for parsing input
  2469. * @return integer|Zend_Date new date
  2470. * @throws Zend_Date_Exception
  2471. */
  2472. private function _date($calc, $date, $format, $locale)
  2473. {
  2474. if (is_null($date)) {
  2475. #require_once 'Zend/Date/Exception.php';
  2476. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  2477. }
  2478. if ($locale === null) {
  2479. $locale = $this->getLocale();
  2480. }
  2481. if ($date instanceof Zend_Date) {
  2482. // extract date from object
  2483. $date = $date->get(self::DATE_FULL, $locale);
  2484. } else {
  2485. if (is_array($date)) {
  2486. if ((isset($time['year']) === true) or (isset($time['month']) === true) or
  2487. (isset($time['day']) === true)) {
  2488. $parsed = $time;
  2489. } else {
  2490. #require_once 'Zend/Date/Exception.php';
  2491. throw new Zend_Date_Exception("no day,month or year given in array");
  2492. }
  2493. } else {
  2494. if (self::$_options['format_type'] == 'php') {
  2495. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2496. }
  2497. try {
  2498. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2499. if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
  2500. $parsed['year'] = self::getFullYear($parsed['year']);
  2501. }
  2502. } catch (Zend_Locale_Exception $e) {
  2503. #require_once 'Zend/Date/Exception.php';
  2504. throw new Zend_Date_Exception($e->getMessage());
  2505. }
  2506. }
  2507. $date = new self(0, self::TIMESTAMP, $locale);
  2508. $date->setTimezone('UTC');
  2509. $date->set($parsed['year'], self::YEAR);
  2510. $date->set($parsed['month'], self::MONTH);
  2511. $date->set($parsed['day'], self::DAY);
  2512. $date = $date->get(self::DATE_FULL, $locale);
  2513. }
  2514. $return = $this->_calcdetail($calc, $date, self::DATE_FULL, $locale);
  2515. if ($calc != 'cmp') {
  2516. return $this;
  2517. }
  2518. return $return;
  2519. }
  2520. /**
  2521. * Sets a new date for the date object. Format defines how to parse the date string.
  2522. * Also a complete date with time can be given, but only the date is used for setting.
  2523. * For example: MMMM.yy HH:mm-> May.07 22:11 => 01.May.07 00:00
  2524. * Returned is the new date object and the existing time is left as it was before
  2525. *
  2526. * @param string|integer|array|Zend_Date $date Date to set
  2527. * @param string $format OPTIONAL Date format for parsing
  2528. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2529. * @return integer|Zend_Date new date
  2530. * @throws Zend_Date_Exception
  2531. */
  2532. public function setDate($date, $format = null, $locale = null)
  2533. {
  2534. return $this->_date('set', $date, $format, $locale);
  2535. }
  2536. /**
  2537. * Adds a date to the existing date object. Format defines how to parse the date string.
  2538. * If only parts are given the other parts are set to 0.
  2539. * If no format is given, the standardformat of this locale is used.
  2540. * For example: MM.dd.YYYY -> 10 -> +10 months
  2541. *
  2542. * @param string|integer|array|Zend_Date $date Date to add
  2543. * @param string $format OPTIONAL Date format for parsing input
  2544. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2545. * @return Zend_Date new date
  2546. * @throws Zend_Date_Exception
  2547. */
  2548. public function addDate($date, $format = null, $locale = null)
  2549. {
  2550. return $this->_date('add', $date, $format, $locale);
  2551. }
  2552. /**
  2553. * Subtracts a date from the existing date object. Format defines how to parse the date string.
  2554. * If only parts are given the other parts are set to 0.
  2555. * If no format is given, the standardformat of this locale is used.
  2556. * For example: MM.dd.YYYY -> 10 -> -10 months
  2557. * Be aware: Subtracting 2 months is not equal to Adding -2 months !!!
  2558. *
  2559. * @param string|integer|array|Zend_Date $date Date to sub
  2560. * @param string $format OPTIONAL Date format for parsing input
  2561. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2562. * @return Zend_Date new date
  2563. * @throws Zend_Date_Exception
  2564. */
  2565. public function subDate($date, $format = null, $locale = null)
  2566. {
  2567. return $this->_date('sub', $date, $format, $locale);
  2568. }
  2569. /**
  2570. * Compares the date from the existing date object, ignoring the time.
  2571. * Format defines how to parse the date string.
  2572. * If only parts are given the other parts are set to 0.
  2573. * If no format is given, the standardformat of this locale is used.
  2574. * For example: 10.01.2000 => 10.02.1999 -> false
  2575. *
  2576. * @param string|integer|array|Zend_Date $date Date to compare
  2577. * @param string $format OPTIONAL Date format for parsing input
  2578. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2579. * @return Zend_Date new date
  2580. * @throws Zend_Date_Exception
  2581. */
  2582. public function compareDate($date, $format = null, $locale = null)
  2583. {
  2584. return $this->_date('cmp', $date, $format, $locale);
  2585. }
  2586. /**
  2587. * Returns the full ISO 8601 date from the date object.
  2588. * Always the complete ISO 8601 specifiction is used. If an other ISO date is needed
  2589. * (ISO 8601 defines several formats) use toString() instead.
  2590. * This function does not return the ISO date as object. Use copy() instead.
  2591. *
  2592. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2593. * @return string
  2594. */
  2595. public function getIso($locale = null)
  2596. {
  2597. return $this->get(self::ISO_8601, $locale);
  2598. }
  2599. /**
  2600. * Sets a new date for the date object. Not given parts are set to default.
  2601. * Only supported ISO 8601 formats are accepted.
  2602. * For example: 050901 -> 01.Sept.2005 00:00:00, 20050201T10:00:30 -> 01.Feb.2005 10h00m30s
  2603. * Returned is the new date object
  2604. *
  2605. * @param string|integer|Zend_Date $date ISO Date to set
  2606. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2607. * @return integer|Zend_Date new date
  2608. * @throws Zend_Date_Exception
  2609. */
  2610. public function setIso($date, $locale = null)
  2611. {
  2612. return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
  2613. }
  2614. /**
  2615. * Adds a ISO date to the date object. Not given parts are set to default.
  2616. * Only supported ISO 8601 formats are accepted.
  2617. * For example: 050901 -> + 01.Sept.2005 00:00:00, 10:00:00 -> +10h
  2618. * Returned is the new date object
  2619. *
  2620. * @param string|integer|Zend_Date $date ISO Date to add
  2621. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2622. * @return integer|Zend_Date new date
  2623. * @throws Zend_Date_Exception
  2624. */
  2625. public function addIso($date, $locale = null)
  2626. {
  2627. return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
  2628. }
  2629. /**
  2630. * Subtracts a ISO date from the date object. Not given parts are set to default.
  2631. * Only supported ISO 8601 formats are accepted.
  2632. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2633. * Returned is the new date object
  2634. *
  2635. * @param string|integer|Zend_Date $date ISO Date to sub
  2636. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2637. * @return integer|Zend_Date new date
  2638. * @throws Zend_Date_Exception
  2639. */
  2640. public function subIso($date, $locale = null)
  2641. {
  2642. return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
  2643. }
  2644. /**
  2645. * Compares a ISO date with the date object. Not given parts are set to default.
  2646. * Only supported ISO 8601 formats are accepted.
  2647. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2648. * Returns if equal, earlier or later
  2649. *
  2650. * @param string|integer|Zend_Date $date ISO Date to sub
  2651. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2652. * @return integer 0 = equal, 1 = later, -1 = earlier
  2653. * @throws Zend_Date_Exception
  2654. */
  2655. public function compareIso($date, $locale = null)
  2656. {
  2657. return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
  2658. }
  2659. /**
  2660. * Returns a RFC 822 compilant datestring from the date object.
  2661. * This function does not return the RFC date as object. Use copy() instead.
  2662. *
  2663. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2664. * @return string
  2665. */
  2666. public function getArpa($locale = null)
  2667. {
  2668. return $this->get(self::RFC_822, $locale);
  2669. }
  2670. /**
  2671. * Sets a RFC 822 date as new date for the date object.
  2672. * Only RFC 822 compilant date strings are accepted.
  2673. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2674. * Returned is the new date object
  2675. *
  2676. * @param string|integer|Zend_Date $date RFC 822 to set
  2677. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2678. * @return integer|Zend_Date new date
  2679. * @throws Zend_Date_Exception
  2680. */
  2681. public function setArpa($date, $locale = null)
  2682. {
  2683. return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
  2684. }
  2685. /**
  2686. * Adds a RFC 822 date to the date object.
  2687. * ARPA messages are used in emails or HTTP Headers.
  2688. * Only RFC 822 compilant date strings are accepted.
  2689. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2690. * Returned is the new date object
  2691. *
  2692. * @param string|integer|Zend_Date $date RFC 822 Date to add
  2693. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2694. * @return integer|Zend_Date new date
  2695. * @throws Zend_Date_Exception
  2696. */
  2697. public function addArpa($date, $locale = null)
  2698. {
  2699. return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
  2700. }
  2701. /**
  2702. * Subtracts a RFC 822 date from the date object.
  2703. * ARPA messages are used in emails or HTTP Headers.
  2704. * Only RFC 822 compilant date strings are accepted.
  2705. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2706. * Returned is the new date object
  2707. *
  2708. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2709. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2710. * @return integer|Zend_Date new date
  2711. * @throws Zend_Date_Exception
  2712. */
  2713. public function subArpa($date, $locale = null)
  2714. {
  2715. return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
  2716. }
  2717. /**
  2718. * Compares a RFC 822 compilant date with the date object.
  2719. * ARPA messages are used in emails or HTTP Headers.
  2720. * Only RFC 822 compilant date strings are accepted.
  2721. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2722. * Returns if equal, earlier or later
  2723. *
  2724. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2725. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2726. * @return integer 0 = equal, 1 = later, -1 = earlier
  2727. * @throws Zend_Date_Exception
  2728. */
  2729. public function compareArpa($date, $locale = null)
  2730. {
  2731. return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
  2732. }
  2733. /**
  2734. * Check if location is supported
  2735. *
  2736. * @param $location array - locations array
  2737. * @return $horizon float
  2738. */
  2739. private function _checkLocation($location)
  2740. {
  2741. if (!isset($location['longitude']) or !isset($location['latitude'])) {
  2742. #require_once 'Zend/Date/Exception.php';
  2743. throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', $location);
  2744. }
  2745. if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
  2746. #require_once 'Zend/Date/Exception.php';
  2747. throw new Zend_Date_Exception('Longitude must be between -180 and 180', $location);
  2748. }
  2749. if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
  2750. #require_once 'Zend/Date/Exception.php';
  2751. throw new Zend_Date_Exception('Latitude must be between -90 and 90', $location);
  2752. }
  2753. if (!isset($location['horizon'])){
  2754. $location['horizon'] = 'effective';
  2755. }
  2756. switch ($location['horizon']) {
  2757. case 'civil' :
  2758. return -0.104528;
  2759. break;
  2760. case 'nautic' :
  2761. return -0.207912;
  2762. break;
  2763. case 'astronomic' :
  2764. return -0.309017;
  2765. break;
  2766. default :
  2767. return -0.0145439;
  2768. break;
  2769. }
  2770. }
  2771. /**
  2772. * Returns the time of sunrise for this date and a given location as new date object
  2773. * For a list of cities and correct locations use the class Zend_Date_Cities
  2774. *
  2775. * @param $location array - location of sunrise
  2776. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2777. * ['longitude'] -> longitude of location
  2778. * ['latitude'] -> latitude of location
  2779. * @return Zend_Date
  2780. * @throws Zend_Date_Exception
  2781. */
  2782. public function getSunrise($location)
  2783. {
  2784. $horizon = $this->_checkLocation($location);
  2785. $result = clone $this;
  2786. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2787. return $result;
  2788. }
  2789. /**
  2790. * Returns the time of sunset for this date and a given location as new date object
  2791. * For a list of cities and correct locations use the class Zend_Date_Cities
  2792. *
  2793. * @param $location array - location of sunset
  2794. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2795. * ['longitude'] -> longitude of location
  2796. * ['latitude'] -> latitude of location
  2797. * @return Zend_Date
  2798. * @throws Zend_Date_Exception
  2799. */
  2800. public function getSunset($location)
  2801. {
  2802. $horizon = $this->_checkLocation($location);
  2803. $result = clone $this;
  2804. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2805. return $result;
  2806. }
  2807. /**
  2808. * Returns an array with the sunset and sunrise dates for all horizon types
  2809. * For a list of cities and correct locations use the class Zend_Date_Cities
  2810. *
  2811. * @param $location array - location of suninfo
  2812. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2813. * ['longitude'] -> longitude of location
  2814. * ['latitude'] -> latitude of location
  2815. * @return array - [sunset|sunrise][effective|civil|nautic|astronomic]
  2816. * @throws Zend_Date_Exception
  2817. */
  2818. public function getSunInfo($location)
  2819. {
  2820. $suninfo = array();
  2821. for ($i = 0; $i < 4; ++$i) {
  2822. switch ($i) {
  2823. case 0 :
  2824. $location['horizon'] = 'effective';
  2825. break;
  2826. case 1 :
  2827. $location['horizon'] = 'civil';
  2828. break;
  2829. case 2 :
  2830. $location['horizon'] = 'nautic';
  2831. break;
  2832. case 3 :
  2833. $location['horizon'] = 'astronomic';
  2834. break;
  2835. }
  2836. $horizon = $this->_checkLocation($location);
  2837. $result = clone $this;
  2838. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2839. $suninfo['sunrise'][$location['horizon']] = $result;
  2840. $result = clone $this;
  2841. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2842. $suninfo['sunset'][$location['horizon']] = $result;
  2843. }
  2844. return $suninfo;
  2845. }
  2846. /**
  2847. * Check a given year for leap year.
  2848. *
  2849. * @param integer|array|Zend_Date $year Year to check
  2850. * @return boolean
  2851. */
  2852. public static function checkLeapYear($year)
  2853. {
  2854. if ($year instanceof Zend_Date) {
  2855. $year = (int) $year->get(self::YEAR);
  2856. }
  2857. if (is_array($year)) {
  2858. if (isset($year['year']) === true) {
  2859. $year = $year['year'];
  2860. } else {
  2861. #require_once 'Zend/Date/Exception.php';
  2862. throw new Zend_Date_Exception("no year given in array");
  2863. }
  2864. }
  2865. if (!is_numeric($year)) {
  2866. #require_once 'Zend/Date/Exception.php';
  2867. throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", $year);
  2868. }
  2869. return (bool) parent::isYearLeapYear($year);
  2870. }
  2871. /**
  2872. * Returns true, if the year is a leap year.
  2873. *
  2874. * @return boolean
  2875. */
  2876. public function isLeapYear()
  2877. {
  2878. return self::checkLeapYear($this);
  2879. }
  2880. /**
  2881. * Returns if the set date is todays date
  2882. *
  2883. * @return boolean
  2884. */
  2885. public function isToday()
  2886. {
  2887. $today = $this->date('Ymd', $this->_getTime());
  2888. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2889. return ($today == $day);
  2890. }
  2891. /**
  2892. * Returns if the set date is yesterdays date
  2893. *
  2894. * @return boolean
  2895. */
  2896. public function isYesterday()
  2897. {
  2898. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  2899. // adjusts for leap days and DST changes that are timezone specific
  2900. $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
  2901. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2902. return $day == $yesterday;
  2903. }
  2904. /**
  2905. * Returns if the set date is tomorrows date
  2906. *
  2907. * @return boolean
  2908. */
  2909. public function isTomorrow()
  2910. {
  2911. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  2912. // adjusts for leap days and DST changes that are timezone specific
  2913. $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
  2914. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2915. return $day == $tomorrow;
  2916. }
  2917. /**
  2918. * Returns the actual date as new date object
  2919. *
  2920. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2921. * @return Zend_Date
  2922. */
  2923. public static function now($locale = null)
  2924. {
  2925. return new Zend_Date(time(), self::TIMESTAMP, $locale);
  2926. }
  2927. /**
  2928. * Calculate date details
  2929. *
  2930. * @param string $calc Calculation to make
  2931. * @param string|integer|array|Zend_Date $date Date or Part to calculate
  2932. * @param string $part Datepart for Calculation
  2933. * @param string|Zend_Locale $locale Locale for parsing input
  2934. * @return integer|string new date
  2935. * @throws Zend_Date_Exception
  2936. */
  2937. private function _calcdetail($calc, $date, $type, $locale)
  2938. {
  2939. switch($calc) {
  2940. case 'set' :
  2941. return $this->set($date, $type, $locale);
  2942. break;
  2943. case 'add' :
  2944. return $this->add($date, $type, $locale);
  2945. break;
  2946. case 'sub' :
  2947. return $this->sub($date, $type, $locale);
  2948. break;
  2949. }
  2950. return $this->compare($date, $type, $locale);
  2951. }
  2952. /**
  2953. * Internal calculation, returns the requested date type
  2954. *
  2955. * @param string $calc Calculation to make
  2956. * @param string|integer|Zend_Date $value Datevalue to calculate with, if null the actual value is taken
  2957. * @param string|Zend_Locale $locale Locale for parsing input
  2958. * @return integer|Zend_Date new date
  2959. * @throws Zend_Date_Exception
  2960. */
  2961. private function _calcvalue($calc, $value, $type, $parameter, $locale)
  2962. {
  2963. if (is_null($value)) {
  2964. #require_once 'Zend/Date/Exception.php';
  2965. throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
  2966. }
  2967. if ($locale === null) {
  2968. $locale = $this->getLocale();
  2969. }
  2970. if ($value instanceof Zend_Date) {
  2971. // extract value from object
  2972. $value = $value->get($parameter, $locale);
  2973. } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
  2974. #require_once 'Zend/Date/Exception.php';
  2975. throw new Zend_Date_Exception("invalid $type ($value) operand", $value);
  2976. }
  2977. $return = $this->_calcdetail($calc, $value, $parameter, $locale);
  2978. if ($calc != 'cmp') {
  2979. return $this;
  2980. }
  2981. return $return;
  2982. }
  2983. /**
  2984. * Returns only the year from the date object as new object.
  2985. * For example: 10.May.2000 10:30:00 -> 01.Jan.2000 00:00:00
  2986. *
  2987. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2988. * @return Zend_Date
  2989. */
  2990. public function getYear($locale = null)
  2991. {
  2992. return $this->copyPart(self::YEAR, $locale);
  2993. }
  2994. /**
  2995. * Sets a new year
  2996. * If the year is between 0 and 69, 2000 will be set (2000-2069)
  2997. * If the year if between 70 and 99, 1999 will be set (1970-1999)
  2998. * 3 or 4 digit years are set as expected. If you need to set year 0-99
  2999. * use set() instead.
  3000. * Returned is the new date object
  3001. *
  3002. * @param string|integer|array|Zend_Date $date Year to set
  3003. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3004. * @return Zend_Date new date
  3005. * @throws Zend_Date_Exception
  3006. */
  3007. public function setYear($year, $locale = null)
  3008. {
  3009. return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
  3010. }
  3011. /**
  3012. * Adds the year to the existing date object
  3013. * If the year is between 0 and 69, 2000 will be added (2000-2069)
  3014. * If the year if between 70 and 99, 1999 will be added (1970-1999)
  3015. * 3 or 4 digit years are added as expected. If you need to add years from 0-99
  3016. * use add() instead.
  3017. * Returned is the new date object
  3018. *
  3019. * @param string|integer|array|Zend_Date $date Year to add
  3020. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3021. * @return Zend_Date new date
  3022. * @throws Zend_Date_Exception
  3023. */
  3024. public function addYear($year, $locale = null)
  3025. {
  3026. return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
  3027. }
  3028. /**
  3029. * Subs the year from the existing date object
  3030. * If the year is between 0 and 69, 2000 will be subtracted (2000-2069)
  3031. * If the year if between 70 and 99, 1999 will be subtracted (1970-1999)
  3032. * 3 or 4 digit years are subtracted as expected. If you need to subtract years from 0-99
  3033. * use sub() instead.
  3034. * Returned is the new date object
  3035. *
  3036. * @param string|integer|array|Zend_Date $date Year to sub
  3037. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3038. * @return Zend_Date new date
  3039. * @throws Zend_Date_Exception
  3040. */
  3041. public function subYear($year, $locale = null)
  3042. {
  3043. return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
  3044. }
  3045. /**
  3046. * Compares the year with the existing date object, ignoring other date parts.
  3047. * For example: 10.03.2000 -> 15.02.2000 -> true
  3048. * Returns if equal, earlier or later
  3049. *
  3050. * @param string|integer|array|Zend_Date $year Year to compare
  3051. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3052. * @return integer 0 = equal, 1 = later, -1 = earlier
  3053. * @throws Zend_Date_Exception
  3054. */
  3055. public function compareYear($year, $locale = null)
  3056. {
  3057. return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
  3058. }
  3059. /**
  3060. * Returns only the month from the date object as new object.
  3061. * For example: 10.May.2000 10:30:00 -> 01.May.1970 00:00:00
  3062. *
  3063. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3064. * @return Zend_Date
  3065. */
  3066. public function getMonth($locale = null)
  3067. {
  3068. return $this->copyPart(self::MONTH, $locale);
  3069. }
  3070. /**
  3071. * Returns the calculated month
  3072. *
  3073. * @param string $calc Calculation to make
  3074. * @param string|integer|array|Zend_Date $month Month to calculate with, if null the actual month is taken
  3075. * @param string|Zend_Locale $locale Locale for parsing input
  3076. * @return integer|Zend_Date new time
  3077. * @throws Zend_Date_Exception
  3078. */
  3079. private function _month($calc, $month, $locale)
  3080. {
  3081. if (is_null($month)) {
  3082. #require_once 'Zend/Date/Exception.php';
  3083. throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
  3084. }
  3085. if ($locale === null) {
  3086. $locale = $this->getLocale();
  3087. }
  3088. if ($month instanceof Zend_Date) {
  3089. // extract month from object
  3090. $found = $month->get(self::MONTH_SHORT, $locale);
  3091. } else {
  3092. if (is_numeric($month)) {
  3093. $found = $month;
  3094. } else if (is_array($month)) {
  3095. if (isset($month['month']) === true) {
  3096. $month = $month['month'];
  3097. } else {
  3098. #require_once 'Zend/Date/Exception.php';
  3099. throw new Zend_Date_Exception("no month given in array");
  3100. }
  3101. } else {
  3102. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  3103. $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  3104. $monthlist = array_merge($monthlist, $monthlist2);
  3105. $found = 0;
  3106. $cnt = 0;
  3107. foreach ($monthlist as $key => $value) {
  3108. if (strtoupper($value) == strtoupper($month)) {
  3109. $found = $key + 1;
  3110. break;
  3111. }
  3112. ++$cnt;
  3113. }
  3114. if ($found == 0) {
  3115. foreach ($monthlist2 as $key => $value) {
  3116. if (strtoupper(iconv_substr($value, 0, 1)) == strtoupper($month)) {
  3117. $found = $key + 1;
  3118. break;
  3119. }
  3120. ++$cnt;
  3121. }
  3122. }
  3123. if ($found == 0) {
  3124. #require_once 'Zend/Date/Exception.php';
  3125. throw new Zend_Date_Exception("unknown month name ($month)", $month);
  3126. }
  3127. }
  3128. }
  3129. $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
  3130. if ($calc != 'cmp') {
  3131. return $this;
  3132. }
  3133. return $return;
  3134. }
  3135. /**
  3136. * Sets a new month
  3137. * The month can be a number or a string. Setting months lower then 0 and greater then 12
  3138. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3139. * If a localized monthname is given it will be parsed with the default locale or the optional
  3140. * set locale.
  3141. * Returned is the new date object
  3142. *
  3143. * @param string|integer|array|Zend_Date $month Month to set
  3144. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3145. * @return Zend_Date new date
  3146. * @throws Zend_Date_Exception
  3147. */
  3148. public function setMonth($month, $locale = null)
  3149. {
  3150. return $this->_month('set', $month, $locale);
  3151. }
  3152. /**
  3153. * Adds months to the existing date object.
  3154. * The month can be a number or a string. Adding months lower then 0 and greater then 12
  3155. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3156. * If a localized monthname is given it will be parsed with the default locale or the optional
  3157. * set locale.
  3158. * Returned is the new date object
  3159. *
  3160. * @param string|integer|array|Zend_Date $month Month to add
  3161. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3162. * @return Zend_Date new date
  3163. * @throws Zend_Date_Exception
  3164. */
  3165. public function addMonth($month, $locale = null)
  3166. {
  3167. return $this->_month('add', $month, $locale);
  3168. }
  3169. /**
  3170. * Subtracts months from the existing date object.
  3171. * The month can be a number or a string. Subtracting months lower then 0 and greater then 12
  3172. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3173. * If a localized monthname is given it will be parsed with the default locale or the optional
  3174. * set locale.
  3175. * Returned is the new date object
  3176. *
  3177. * @param string|integer|array|Zend_Date $month Month to sub
  3178. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3179. * @return Zend_Date new date
  3180. * @throws Zend_Date_Exception
  3181. */
  3182. public function subMonth($month, $locale = null)
  3183. {
  3184. return $this->_month('sub', $month, $locale);
  3185. }
  3186. /**
  3187. * Compares the month with the existing date object, ignoring other date parts.
  3188. * For example: 10.03.2000 -> 15.03.1950 -> true
  3189. * Returns if equal, earlier or later
  3190. *
  3191. * @param string|integer|array|Zend_Date $month Month to compare
  3192. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3193. * @return integer 0 = equal, 1 = later, -1 = earlier
  3194. * @throws Zend_Date_Exception
  3195. */
  3196. public function compareMonth($month, $locale = null)
  3197. {
  3198. return $this->_month('cmp', $month, $locale);
  3199. }
  3200. /**
  3201. * Returns the day as new date object
  3202. * Example: 20.May.1986 -> 20.Jan.1970 00:00:00
  3203. *
  3204. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3205. * @return Zend_Date
  3206. */
  3207. public function getDay($locale = null)
  3208. {
  3209. return $this->copyPart(self::DAY_SHORT, $locale);
  3210. }
  3211. /**
  3212. * Returns the calculated day
  3213. *
  3214. * @param $calc string Type of calculation to make
  3215. * @param $day string|integer|Zend_Date Day to calculate, when null the actual day is calculated
  3216. * @param $locale string|Zend_Locale Locale for parsing input
  3217. * @return Zend_Date|integer
  3218. */
  3219. private function _day($calc, $day, $locale)
  3220. {
  3221. if (is_null($day)) {
  3222. #require_once 'Zend/Date/Exception.php';
  3223. throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
  3224. }
  3225. if ($locale === null) {
  3226. $locale = $this->getLocale();
  3227. }
  3228. if ($day instanceof Zend_Date) {
  3229. $day = $day->get(self::DAY_SHORT, $locale);
  3230. }
  3231. if (is_numeric($day)) {
  3232. $type = self::DAY_SHORT;
  3233. } else if (is_array($day)) {
  3234. if (isset($day['day']) === true) {
  3235. $day = $day['day'];
  3236. $type = self::WEEKDAY;
  3237. } else {
  3238. #require_once 'Zend/Date/Exception.php';
  3239. throw new Zend_Date_Exception("no day given in array");
  3240. }
  3241. } else {
  3242. switch (iconv_strlen($day)) {
  3243. case 1 :
  3244. $type = self::WEEKDAY_NARROW;
  3245. break;
  3246. case 2:
  3247. $type = self::WEEKDAY_NAME;
  3248. break;
  3249. case 3:
  3250. $type = self::WEEKDAY_SHORT;
  3251. break;
  3252. default:
  3253. $type = self::WEEKDAY;
  3254. break;
  3255. }
  3256. }
  3257. $return = $this->_calcdetail($calc, $day, $type, $locale);
  3258. if ($calc != 'cmp') {
  3259. return $this;
  3260. }
  3261. return $return;
  3262. }
  3263. /**
  3264. * Sets a new day
  3265. * The day can be a number or a string. Setting days lower then 0 or greater than the number of this months days
  3266. * will result in adding or subtracting the relevant month.
  3267. * If a localized dayname is given it will be parsed with the default locale or the optional
  3268. * set locale.
  3269. * Returned is the new date object
  3270. * Example: setDay('Montag', 'de_AT'); will set the monday of this week as day.
  3271. *
  3272. * @param string|integer|array|Zend_Date $month Day to set
  3273. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3274. * @return Zend_Date new date
  3275. * @throws Zend_Date_Exception
  3276. */
  3277. public function setDay($day, $locale = null)
  3278. {
  3279. return $this->_day('set', $day, $locale);
  3280. }
  3281. /**
  3282. * Adds days to the existing date object.
  3283. * The day can be a number or a string. Adding days lower then 0 or greater than the number of this months days
  3284. * will result in adding or subtracting the relevant month.
  3285. * If a localized dayname is given it will be parsed with the default locale or the optional
  3286. * set locale.
  3287. * Returned is the new date object
  3288. * Example: addDay('Montag', 'de_AT'); will add the number of days until the next monday
  3289. *
  3290. * @param string|integer|array|Zend_Date $month Day to add
  3291. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3292. * @return Zend_Date new date
  3293. * @throws Zend_Date_Exception
  3294. */
  3295. public function addDay($day, $locale = null)
  3296. {
  3297. return $this->_day('add', $day, $locale);
  3298. }
  3299. /**
  3300. * Subtracts days from the existing date object.
  3301. * The day can be a number or a string. Subtracting days lower then 0 or greater than the number of this months days
  3302. * will result in adding or subtracting the relevant month.
  3303. * If a localized dayname is given it will be parsed with the default locale or the optional
  3304. * set locale.
  3305. * Returned is the new date object
  3306. * Example: subDay('Montag', 'de_AT'); will sub the number of days until the previous monday
  3307. *
  3308. * @param string|integer|array|Zend_Date $month Day to sub
  3309. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3310. * @return Zend_Date new date
  3311. * @throws Zend_Date_Exception
  3312. */
  3313. public function subDay($day, $locale = null)
  3314. {
  3315. return $this->_day('sub', $day, $locale);
  3316. }
  3317. /**
  3318. * Compares the day with the existing date object, ignoring other date parts.
  3319. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3320. * Returns if equal, earlier or later
  3321. *
  3322. * @param string|integer|array|Zend_Date $day Day to compare
  3323. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3324. * @return integer 0 = equal, 1 = later, -1 = earlier
  3325. * @throws Zend_Date_Exception
  3326. */
  3327. public function compareDay($day, $locale = null)
  3328. {
  3329. return $this->_day('cmp', $day, $locale);
  3330. }
  3331. /**
  3332. * Returns the weekday as new date object
  3333. * Weekday is always from 1-7
  3334. * Example: 09-Jan-2007 -> 2 = Tuesday -> 02-Jan-1970 (when 02.01.1970 is also Tuesday)
  3335. *
  3336. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3337. * @return Zend_Date
  3338. */
  3339. public function getWeekday($locale = null)
  3340. {
  3341. return $this->copyPart(self::WEEKDAY, $locale);
  3342. }
  3343. /**
  3344. * Returns the calculated weekday
  3345. *
  3346. * @param $calc string Type of calculation to make
  3347. * @param $weekday string|integer|array|Zend_Date Weekday to calculate, when null the actual weekday is calculated
  3348. * @param $locale string|Zend_Locale Locale for parsing input
  3349. * @return Zend_Date|integer
  3350. * @throws Zend_Date_Exception
  3351. */
  3352. private function _weekday($calc, $weekday, $locale)
  3353. {
  3354. if (is_null($weekday)) {
  3355. #require_once 'Zend/Date/Exception.php';
  3356. throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
  3357. }
  3358. if ($locale === null) {
  3359. $locale = $this->getLocale();
  3360. }
  3361. if ($weekday instanceof Zend_Date) {
  3362. $weekday = $weekday->get(self::WEEKDAY_8601, $locale);
  3363. }
  3364. if (is_numeric($weekday)) {
  3365. $type = self::WEEKDAY_8601;
  3366. } else if (is_array($weekday)) {
  3367. if (isset($weekday['weekday']) === true) {
  3368. $weekday = $weekday['weekday'];
  3369. $type = self::WEEKDAY;
  3370. } else {
  3371. #require_once 'Zend/Date/Exception.php';
  3372. throw new Zend_Date_Exception("no weekday given in array");
  3373. }
  3374. } else {
  3375. switch(iconv_strlen($weekday)) {
  3376. case 1:
  3377. $type = self::WEEKDAY_NARROW;
  3378. break;
  3379. case 2:
  3380. $type = self::WEEKDAY_NAME;
  3381. break;
  3382. case 3:
  3383. $type = self::WEEKDAY_SHORT;
  3384. break;
  3385. default:
  3386. $type = self::WEEKDAY;
  3387. break;
  3388. }
  3389. }
  3390. $return = $this->_calcdetail($calc, $weekday, $type, $locale);
  3391. if ($calc != 'cmp') {
  3392. return $this;
  3393. }
  3394. return $return;
  3395. }
  3396. /**
  3397. * Sets a new weekday
  3398. * The weekday can be a number or a string. If a localized weekday name is given,
  3399. * then it will be parsed as a date in $locale (defaults to the same locale as $this).
  3400. * Returned is the new date object.
  3401. * Example: setWeekday(3); will set the wednesday of this week as day.
  3402. *
  3403. * @param string|integer|array|Zend_Date $month Weekday to set
  3404. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3405. * @return Zend_Date new date
  3406. * @throws Zend_Date_Exception
  3407. */
  3408. public function setWeekday($weekday, $locale = null)
  3409. {
  3410. return $this->_weekday('set', $weekday, $locale);
  3411. }
  3412. /**
  3413. * Adds weekdays to the existing date object.
  3414. * The weekday can be a number or a string.
  3415. * If a localized dayname is given it will be parsed with the default locale or the optional
  3416. * set locale.
  3417. * Returned is the new date object
  3418. * Example: addWeekday(3); will add the difference of days from the begining of the month until
  3419. * wednesday.
  3420. *
  3421. * @param string|integer|array|Zend_Date $month Weekday to add
  3422. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3423. * @return Zend_Date new date
  3424. * @throws Zend_Date_Exception
  3425. */
  3426. public function addWeekday($weekday, $locale = null)
  3427. {
  3428. return $this->_weekday('add', $weekday, $locale);
  3429. }
  3430. /**
  3431. * Subtracts weekdays from the existing date object.
  3432. * The weekday can be a number or a string.
  3433. * If a localized dayname is given it will be parsed with the default locale or the optional
  3434. * set locale.
  3435. * Returned is the new date object
  3436. * Example: subWeekday(3); will subtract the difference of days from the begining of the month until
  3437. * wednesday.
  3438. *
  3439. * @param string|integer|array|Zend_Date $month Weekday to sub
  3440. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3441. * @return Zend_Date new date
  3442. * @throws Zend_Date_Exception
  3443. */
  3444. public function subWeekday($weekday, $locale = null)
  3445. {
  3446. return $this->_weekday('sub', $weekday, $locale);
  3447. }
  3448. /**
  3449. * Compares the weekday with the existing date object, ignoring other date parts.
  3450. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3451. * Returns if equal, earlier or later
  3452. *
  3453. * @param string|integer|array|Zend_Date $weekday Weekday to compare
  3454. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3455. * @return integer 0 = equal, 1 = later, -1 = earlier
  3456. * @throws Zend_Date_Exception
  3457. */
  3458. public function compareWeekday($weekday, $locale = null)
  3459. {
  3460. return $this->_weekday('cmp', $weekday, $locale);
  3461. }
  3462. /**
  3463. * Returns the day of year as new date object
  3464. * Example: 02.Feb.1986 10:00:00 -> 02.Feb.1970 00:00:00
  3465. *
  3466. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3467. * @return Zend_Date
  3468. */
  3469. public function getDayOfYear($locale = null)
  3470. {
  3471. return $this->copyPart(self::DAY_OF_YEAR, $locale);
  3472. }
  3473. /**
  3474. * Sets a new day of year
  3475. * The day of year is always a number.
  3476. * Returned is the new date object
  3477. * Example: 04.May.2004 -> setDayOfYear(10) -> 10.Jan.2004
  3478. *
  3479. * @param string|integer|array|Zend_Date $day Day of Year to set
  3480. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3481. * @return Zend_Date new date
  3482. * @throws Zend_Date_Exception
  3483. */
  3484. public function setDayOfYear($day, $locale = null)
  3485. {
  3486. return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3487. }
  3488. /**
  3489. * Adds a day of year to the existing date object.
  3490. * The day of year is always a number.
  3491. * Returned is the new date object
  3492. * Example: addDayOfYear(10); will add 10 days to the existing date object.
  3493. *
  3494. * @param string|integer|array|Zend_Date $day Day of Year to add
  3495. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3496. * @return Zend_Date new date
  3497. * @throws Zend_Date_Exception
  3498. */
  3499. public function addDayOfYear($day, $locale = null)
  3500. {
  3501. return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3502. }
  3503. /**
  3504. * Subtracts a day of year from the existing date object.
  3505. * The day of year is always a number.
  3506. * Returned is the new date object
  3507. * Example: subDayOfYear(10); will subtract 10 days from the existing date object.
  3508. *
  3509. * @param string|integer|array|Zend_Date $day Day of Year to sub
  3510. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3511. * @return Zend_Date new date
  3512. * @throws Zend_Date_Exception
  3513. */
  3514. public function subDayOfYear($day, $locale = null)
  3515. {
  3516. return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3517. }
  3518. /**
  3519. * Compares the day of year with the existing date object.
  3520. * For example: compareDayOfYear(33) -> 02.Feb.2007 -> 0
  3521. * Returns if equal, earlier or later
  3522. *
  3523. * @param string|integer|array|Zend_Date $day Day of Year to compare
  3524. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3525. * @return integer 0 = equal, 1 = later, -1 = earlier
  3526. * @throws Zend_Date_Exception
  3527. */
  3528. public function compareDayOfYear($day, $locale = null)
  3529. {
  3530. return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3531. }
  3532. /**
  3533. * Returns the hour as new date object
  3534. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 10:00:00
  3535. *
  3536. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3537. * @return Zend_Date
  3538. */
  3539. public function getHour($locale = null)
  3540. {
  3541. return $this->copyPart(self::HOUR, $locale);
  3542. }
  3543. /**
  3544. * Sets a new hour
  3545. * The hour is always a number.
  3546. * Returned is the new date object
  3547. * Example: 04.May.1993 13:07:25 -> setHour(7); -> 04.May.1993 07:07:25
  3548. *
  3549. * @param string|integer|array|Zend_Date $hour Hour to set
  3550. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3551. * @return Zend_Date new date
  3552. * @throws Zend_Date_Exception
  3553. */
  3554. public function setHour($hour, $locale = null)
  3555. {
  3556. return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
  3557. }
  3558. /**
  3559. * Adds hours to the existing date object.
  3560. * The hour is always a number.
  3561. * Returned is the new date object
  3562. * Example: 04.May.1993 13:07:25 -> addHour(12); -> 05.May.1993 01:07:25
  3563. *
  3564. * @param string|integer|array|Zend_Date $hour Hour to add
  3565. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3566. * @return Zend_Date new date
  3567. * @throws Zend_Date_Exception
  3568. */
  3569. public function addHour($hour, $locale = null)
  3570. {
  3571. return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
  3572. }
  3573. /**
  3574. * Subtracts hours from the existing date object.
  3575. * The hour is always a number.
  3576. * Returned is the new date object
  3577. * Example: 04.May.1993 13:07:25 -> subHour(6); -> 05.May.1993 07:07:25
  3578. *
  3579. * @param string|integer|array|Zend_Date $hour Hour to sub
  3580. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3581. * @return Zend_Date new date
  3582. * @throws Zend_Date_Exception
  3583. */
  3584. public function subHour($hour, $locale = null)
  3585. {
  3586. return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
  3587. }
  3588. /**
  3589. * Compares the hour with the existing date object.
  3590. * For example: 10:30:25 -> compareHour(10) -> 0
  3591. * Returns if equal, earlier or later
  3592. *
  3593. * @param string|integer|array|Zend_Date $hour Hour to compare
  3594. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3595. * @return integer 0 = equal, 1 = later, -1 = earlier
  3596. * @throws Zend_Date_Exception
  3597. */
  3598. public function compareHour($hour, $locale = null)
  3599. {
  3600. return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
  3601. }
  3602. /**
  3603. * Returns the minute as new date object
  3604. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:30:00
  3605. *
  3606. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3607. * @return Zend_Date
  3608. */
  3609. public function getMinute($locale = null)
  3610. {
  3611. return $this->copyPart(self::MINUTE, $locale);
  3612. }
  3613. /**
  3614. * Sets a new minute
  3615. * The minute is always a number.
  3616. * Returned is the new date object
  3617. * Example: 04.May.1993 13:07:25 -> setMinute(29); -> 04.May.1993 13:29:25
  3618. *
  3619. * @param string|integer|array|Zend_Date $minute Minute to set
  3620. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3621. * @return Zend_Date new date
  3622. * @throws Zend_Date_Exception
  3623. */
  3624. public function setMinute($minute, $locale = null)
  3625. {
  3626. return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3627. }
  3628. /**
  3629. * Adds minutes to the existing date object.
  3630. * The minute is always a number.
  3631. * Returned is the new date object
  3632. * Example: 04.May.1993 13:07:25 -> addMinute(65); -> 04.May.1993 13:12:25
  3633. *
  3634. * @param string|integer|array|Zend_Date $minute Minute to add
  3635. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3636. * @return Zend_Date new date
  3637. * @throws Zend_Date_Exception
  3638. */
  3639. public function addMinute($minute, $locale = null)
  3640. {
  3641. return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3642. }
  3643. /**
  3644. * Subtracts minutes from the existing date object.
  3645. * The minute is always a number.
  3646. * Returned is the new date object
  3647. * Example: 04.May.1993 13:07:25 -> subMinute(9); -> 04.May.1993 12:58:25
  3648. *
  3649. * @param string|integer|array|Zend_Date $minute Minute to sub
  3650. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3651. * @return Zend_Date new date
  3652. * @throws Zend_Date_Exception
  3653. */
  3654. public function subMinute($minute, $locale = null)
  3655. {
  3656. return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3657. }
  3658. /**
  3659. * Compares the minute with the existing date object.
  3660. * For example: 10:30:25 -> compareMinute(30) -> 0
  3661. * Returns if equal, earlier or later
  3662. *
  3663. * @param string|integer|array|Zend_Date $minute Hour to compare
  3664. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3665. * @return integer 0 = equal, 1 = later, -1 = earlier
  3666. * @throws Zend_Date_Exception
  3667. */
  3668. public function compareMinute($minute, $locale = null)
  3669. {
  3670. return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3671. }
  3672. /**
  3673. * Returns the second as new date object
  3674. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:00:25
  3675. *
  3676. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3677. * @return Zend_Date
  3678. */
  3679. public function getSecond($locale = null)
  3680. {
  3681. return $this->copyPart(self::SECOND, $locale);
  3682. }
  3683. /**
  3684. * Sets new seconds to the existing date object.
  3685. * The second is always a number.
  3686. * Returned is the new date object
  3687. * Example: 04.May.1993 13:07:25 -> setSecond(100); -> 04.May.1993 13:08:40
  3688. *
  3689. * @param string|integer|array|Zend_Date $second Second to set
  3690. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3691. * @return Zend_Date new date
  3692. * @throws Zend_Date_Exception
  3693. */
  3694. public function setSecond($second, $locale = null)
  3695. {
  3696. return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
  3697. }
  3698. /**
  3699. * Adds seconds to the existing date object.
  3700. * The second is always a number.
  3701. * Returned is the new date object
  3702. * Example: 04.May.1993 13:07:25 -> addSecond(65); -> 04.May.1993 13:08:30
  3703. *
  3704. * @param string|integer|array|Zend_Date $second Second to add
  3705. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3706. * @return Zend_Date new date
  3707. * @throws Zend_Date_Exception
  3708. */
  3709. public function addSecond($second, $locale = null)
  3710. {
  3711. return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
  3712. }
  3713. /**
  3714. * Subtracts seconds from the existing date object.
  3715. * The second is always a number.
  3716. * Returned is the new date object
  3717. * Example: 04.May.1993 13:07:25 -> subSecond(10); -> 04.May.1993 13:07:15
  3718. *
  3719. * @param string|integer|array|Zend_Date $second Second to sub
  3720. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3721. * @return Zend_Date new date
  3722. * @throws Zend_Date_Exception
  3723. */
  3724. public function subSecond($second, $locale = null)
  3725. {
  3726. return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
  3727. }
  3728. /**
  3729. * Compares the second with the existing date object.
  3730. * For example: 10:30:25 -> compareSecond(25) -> 0
  3731. * Returns if equal, earlier or later
  3732. *
  3733. * @param string|integer|array|Zend_Date $second Second to compare
  3734. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3735. * @return integer 0 = equal, 1 = later, -1 = earlier
  3736. * @throws Zend_Date_Exception
  3737. */
  3738. public function compareSecond($second, $locale = null)
  3739. {
  3740. return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
  3741. }
  3742. /**
  3743. * Returns the precision for fractional seconds
  3744. *
  3745. * @return integer
  3746. */
  3747. public function getFractionalPrecision()
  3748. {
  3749. return $this->_precision;
  3750. }
  3751. /**
  3752. * Sets a new precision for fractional seconds
  3753. *
  3754. * @param integer $precision Precision for the fractional datepart 3 = milliseconds
  3755. * @throws Zend_Date_Exception
  3756. * @return void
  3757. */
  3758. public function setFractionalPrecision($precision)
  3759. {
  3760. if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
  3761. #require_once 'Zend/Date/Exception.php';
  3762. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3763. }
  3764. $this->_precision = (int) $precision;
  3765. }
  3766. /**
  3767. * Returns the milliseconds of the date object
  3768. *
  3769. * @return integer
  3770. */
  3771. public function getMilliSecond()
  3772. {
  3773. return $this->_fractional;
  3774. }
  3775. /**
  3776. * Sets new milliseconds for the date object
  3777. * Example: setMilliSecond(550, 2) -> equals +5 Sec +50 MilliSec
  3778. *
  3779. * @param integer|Zend_Date $milli (Optional) Millisecond to set, when null the actual millisecond is set
  3780. * @param integer $precision (Optional) Fraction precision of the given milliseconds
  3781. * @return integer|string
  3782. */
  3783. public function setMilliSecond($milli = null, $precision = null)
  3784. {
  3785. if ($milli === null) {
  3786. list($milli, $time) = explode(" ", microtime());
  3787. $milli = intval($milli);
  3788. $precision = 6;
  3789. } else if (!is_numeric($milli)) {
  3790. #require_once 'Zend/Date/Exception.php';
  3791. throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
  3792. }
  3793. if ($precision === null) {
  3794. $precision = $this->_precision;
  3795. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3796. #require_once 'Zend/Date/Exception.php';
  3797. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3798. }
  3799. $this->_fractional = 0;
  3800. $this->addMilliSecond($milli, $precision);
  3801. return $this->_fractional;
  3802. }
  3803. /**
  3804. * Adds milliseconds to the date object
  3805. *
  3806. * @param integer|Zend_Date $milli (Optional) Millisecond to add, when null the actual millisecond is added
  3807. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3808. * @return integer|string
  3809. */
  3810. public function addMilliSecond($milli = null, $precision = null)
  3811. {
  3812. if ($milli === null) {
  3813. list($milli, $time) = explode(" ", microtime());
  3814. $milli = intval($milli);
  3815. } else if (!is_numeric($milli)) {
  3816. #require_once 'Zend/Date/Exception.php';
  3817. throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
  3818. }
  3819. if ($precision === null) {
  3820. $precision = $this->_precision;
  3821. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3822. #require_once 'Zend/Date/Exception.php';
  3823. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3824. }
  3825. if ($precision != $this->_precision) {
  3826. if ($precision > $this->_precision) {
  3827. $diff = $precision - $this->_precision;
  3828. $milli = (int) ($milli / (10 * $diff));
  3829. } else {
  3830. $diff = $this->_precision - $precision;
  3831. $milli = (int) ($milli * (10 * $diff));
  3832. }
  3833. }
  3834. $this->_fractional += $milli;
  3835. // Add/sub milliseconds + add/sub seconds
  3836. $max = pow(10, $this->_precision);
  3837. // Milli includes seconds
  3838. if ($this->_fractional >= $max) {
  3839. while ($this->_fractional >= $max) {
  3840. $this->addSecond(1);
  3841. $this->_fractional -= $max;
  3842. }
  3843. }
  3844. if ($this->_fractional < 0) {
  3845. while ($this->_fractional < 0) {
  3846. $this->subSecond(1);
  3847. $this->_fractional += $max;
  3848. }
  3849. }
  3850. return $this->_fractional;
  3851. }
  3852. /**
  3853. * Subtracts a millisecond
  3854. *
  3855. * @param integer|Zend_Date $milli (Optional) Millisecond to sub, when null the actual millisecond is subtracted
  3856. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3857. * @return integer
  3858. */
  3859. public function subMilliSecond($milli = null, $precision = null)
  3860. {
  3861. return $this->addMilliSecond(0 - $milli);
  3862. }
  3863. /**
  3864. * Compares only the millisecond part, returning the difference
  3865. *
  3866. * @param integer|Zend_Date $milli OPTIONAL Millisecond to compare, when null the actual millisecond is compared
  3867. * @param integer $precision OPTIONAL Fractional precision for the given milliseconds
  3868. * @return integer
  3869. */
  3870. public function compareMilliSecond($milli = null, $precision = null)
  3871. {
  3872. if ($milli === null) {
  3873. list($milli, $time) = explode(" ", microtime());
  3874. $milli = intval($milli);
  3875. } else if (is_numeric($milli) === false) {
  3876. #require_once 'Zend/Date/Exception.php';
  3877. throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
  3878. }
  3879. if ($precision === null) {
  3880. $precision = $this->_precision;
  3881. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3882. #require_once 'Zend/Date/Exception.php';
  3883. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3884. }
  3885. if ($precision === 0) {
  3886. #require_once 'Zend/Date/Exception.php';
  3887. throw new Zend_Date_Exception('precision is 0');
  3888. }
  3889. if ($precision != $this->_precision) {
  3890. if ($precision > $this->_precision) {
  3891. $diff = $precision - $this->_precision;
  3892. $milli = (int) ($milli / (10 * $diff));
  3893. } else {
  3894. $diff = $this->_precision - $precision;
  3895. $milli = (int) ($milli * (10 * $diff));
  3896. }
  3897. }
  3898. $comp = $this->_fractional - $milli;
  3899. if ($comp < 0) {
  3900. return -1;
  3901. } else if ($comp > 0) {
  3902. return 1;
  3903. }
  3904. return 0;
  3905. }
  3906. /**
  3907. * Returns the week as new date object using monday as begining of the week
  3908. * Example: 12.Jan.2007 -> 08.Jan.1970 00:00:00
  3909. *
  3910. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3911. * @return Zend_Date
  3912. */
  3913. public function getWeek($locale = null)
  3914. {
  3915. return $this->copyPart(self::WEEK, $locale);
  3916. }
  3917. /**
  3918. * Sets a new week. The week is always a number. The day of week is not changed.
  3919. * Returned is the new date object
  3920. * Example: 09.Jan.2007 13:07:25 -> setWeek(1); -> 02.Jan.2007 13:07:25
  3921. *
  3922. * @param string|integer|array|Zend_Date $week Week to set
  3923. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3924. * @return Zend_Date
  3925. * @throws Zend_Date_Exception
  3926. */
  3927. public function setWeek($week, $locale = null)
  3928. {
  3929. return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
  3930. }
  3931. /**
  3932. * Adds a week. The week is always a number. The day of week is not changed.
  3933. * Returned is the new date object
  3934. * Example: 09.Jan.2007 13:07:25 -> addWeek(1); -> 16.Jan.2007 13:07:25
  3935. *
  3936. * @param string|integer|array|Zend_Date $week Week to add
  3937. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3938. * @return Zend_Date
  3939. * @throws Zend_Date_Exception
  3940. */
  3941. public function addWeek($week, $locale = null)
  3942. {
  3943. return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
  3944. }
  3945. /**
  3946. * Subtracts a week. The week is always a number. The day of week is not changed.
  3947. * Returned is the new date object
  3948. * Example: 09.Jan.2007 13:07:25 -> subWeek(1); -> 02.Jan.2007 13:07:25
  3949. *
  3950. * @param string|integer|array|Zend_Date $week Week to sub
  3951. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3952. * @return Zend_Date
  3953. * @throws Zend_Date_Exception
  3954. */
  3955. public function subWeek($week, $locale = null)
  3956. {
  3957. return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
  3958. }
  3959. /**
  3960. * Compares only the week part, returning the difference
  3961. * Returned is the new date object
  3962. * Returns if equal, earlier or later
  3963. * Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
  3964. *
  3965. * @param string|integer|array|Zend_Date $week Week to compare
  3966. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3967. * @return integer 0 = equal, 1 = later, -1 = earlier
  3968. */
  3969. public function compareWeek($week, $locale = null)
  3970. {
  3971. return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
  3972. }
  3973. /**
  3974. * Sets a new standard locale for the date object.
  3975. * This locale will be used for all functions
  3976. * Returned is the really set locale.
  3977. * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
  3978. * 'xx_YY' will be set to 'root' because 'xx' does not exist
  3979. *
  3980. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3981. * @throws Zend_Date_Exception When the given locale does not exist
  3982. * @return Zend_Date Provides fluent interface
  3983. */
  3984. public function setLocale($locale = null)
  3985. {
  3986. try {
  3987. $this->_locale = Zend_Locale::findLocale($locale);
  3988. } catch (Zend_Locale_Exception $e) {
  3989. #require_once 'Zend/Date/Exception.php';
  3990. throw new Zend_Date_Exception($e->getMessage());
  3991. }
  3992. return $this;
  3993. }
  3994. /**
  3995. * Returns the actual set locale
  3996. *
  3997. * @return string
  3998. */
  3999. public function getLocale()
  4000. {
  4001. return $this->_locale;
  4002. }
  4003. /**
  4004. * Checks if the given date is a real date or datepart.
  4005. * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  4006. * But the check will only be done for the expected dateparts which are given by format.
  4007. * If no format is given the standard dateformat for the actual locale is used.
  4008. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  4009. *
  4010. * @param string $date Date to parse for correctness
  4011. * @param string $format (Optional) Format for parsing the date string
  4012. * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts
  4013. * @return boolean True when all date parts are correct
  4014. */
  4015. public static function isDate($date, $format = null, $locale = null)
  4016. {
  4017. if (!is_string($date) and !is_numeric($date) and !($date instanceof Zend_Date)) {
  4018. return false;
  4019. }
  4020. if (($format !== null) and (Zend_Locale::isLocale($format, null, false))) {
  4021. $locale = $format;
  4022. $format = null;
  4023. }
  4024. $locale = Zend_Locale::findLocale($locale);
  4025. if ($format === null) {
  4026. $format = Zend_Locale_Format::getDateFormat($locale);
  4027. } else if (self::$_options['format_type'] == 'php') {
  4028. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  4029. }
  4030. try {
  4031. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
  4032. 'date_format' => $format, 'format_type' => 'iso',
  4033. 'fix_date' => false));
  4034. if (isset($parsed['year']) and ((strpos(strtoupper($format), 'YY') !== false) and
  4035. (strpos(strtoupper($format), 'YYYY') === false))) {
  4036. $parsed['year'] = self::getFullYear($parsed['year']);
  4037. }
  4038. } catch (Zend_Locale_Exception $e) {
  4039. // Date can not be parsed
  4040. return false;
  4041. }
  4042. if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
  4043. (!isset($parsed['year']))) {
  4044. // Year expected but not found
  4045. return false;
  4046. }
  4047. if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
  4048. // Month expected but not found
  4049. return false;
  4050. }
  4051. if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
  4052. // Day expected but not found
  4053. return false;
  4054. }
  4055. if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
  4056. (!isset($parsed['hour']))) {
  4057. // Hour expected but not found
  4058. return false;
  4059. }
  4060. if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
  4061. // Minute expected but not found
  4062. return false;
  4063. }
  4064. if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
  4065. // Second expected but not found
  4066. return false;
  4067. }
  4068. // Set not given dateparts
  4069. if (isset($parsed['hour']) === false) {
  4070. $parsed['hour'] = 0;
  4071. }
  4072. if (isset($parsed['minute']) === false) {
  4073. $parsed['minute'] = 0;
  4074. }
  4075. if (isset($parsed['second']) === false) {
  4076. $parsed['second'] = 0;
  4077. }
  4078. if (isset($parsed['month']) === false) {
  4079. $parsed['month'] = 1;
  4080. }
  4081. if (isset($parsed['day']) === false) {
  4082. $parsed['day'] = 1;
  4083. }
  4084. if (isset($parsed['year']) === false) {
  4085. $parsed['year'] = 1970;
  4086. }
  4087. $date = new self($parsed, null, $locale);
  4088. $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
  4089. $parsed['month'], $parsed['day'], $parsed['year']);
  4090. if ($parsed['year'] != $date->date('Y', $timestamp)) {
  4091. // Given year differs from parsed year
  4092. return false;
  4093. }
  4094. if ($parsed['month'] != $date->date('n', $timestamp)) {
  4095. // Given month differs from parsed month
  4096. return false;
  4097. }
  4098. if ($parsed['day'] != $date->date('j', $timestamp)) {
  4099. // Given day differs from parsed day
  4100. return false;
  4101. }
  4102. if ($parsed['hour'] != $date->date('G', $timestamp)) {
  4103. // Given hour differs from parsed hour
  4104. return false;
  4105. }
  4106. if ($parsed['minute'] != $date->date('i', $timestamp)) {
  4107. // Given minute differs from parsed minute
  4108. return false;
  4109. }
  4110. if ($parsed['second'] != $date->date('s', $timestamp)) {
  4111. // Given second differs from parsed second
  4112. return false;
  4113. }
  4114. return true;
  4115. }
  4116. }