PageRenderTime 38ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Zend/Date.php

https://github.com/MrRio/magento
PHP | 4665 lines | 2721 code | 542 blank | 1402 comment | 520 complexity | c7bd4cfd3969f020b1023d229c98ffa1 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. #require_once 'Zend/Date/DateObject.php';
  25. #require_once 'Zend/Locale.php';
  26. #require_once 'Zend/Locale/Format.php';
  27. #require_once 'Zend/Locale/Math.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Date
  31. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Date extends Zend_Date_DateObject
  35. {
  36. private $_locale = null;
  37. // Fractional second variables
  38. private $_fractional = 0;
  39. private $_precision = 3;
  40. private static $_options = array(
  41. 'format_type' => 'iso', // format for date strings 'iso' or 'php'
  42. 'fix_dst' => true, // fix dst on summer/winter time change
  43. 'extend_month' => false, // false - addMonth like SQL, true like excel
  44. 'cache' => null, // cache to set
  45. 'timesync' => null // timesync server to set
  46. );
  47. // Class wide Date Constants
  48. // day formats
  49. const DAY = 'DAY'; // d - 2 digit day of month, 01-31
  50. const DAY_SHORT = 'DAY_SHORT'; // j - 1,2 digit day of month, 1-31
  51. const DAY_SUFFIX = 'DAY_SUFFIX'; // S - english suffix day of month, st-th
  52. const DAY_OF_YEAR = 'DAY_OF_YEAR'; // z - Number of day of year
  53. const WEEKDAY = 'WEEKDAY'; // l - full day name - locale aware, Monday - Sunday
  54. const WEEKDAY_SHORT = 'WEEKDAY_SHORT'; // --- 3 letter day of week - locale aware, Mon-Sun
  55. const WEEKDAY_NARROW = 'WEEKDAY_NARROW'; // --- 1 letter day name - locale aware, M-S
  56. const WEEKDAY_NAME = 'WEEKDAY_NAME'; // D - abbreviated day name, 1-3 letters - locale aware, Mon-Sun
  57. const WEEKDAY_8601 = 'WEEKDAY_8601'; // N - digit weekday ISO 8601, 1-7 1 = monday, 7=sunday
  58. const WEEKDAY_DIGIT = 'WEEKDAY_DIGIT'; // w - weekday, 0-6 0=sunday, 6=saturday
  59. // week formats
  60. const WEEK = 'WEEK'; // W - number of week ISO8601, 1-53
  61. // month formats
  62. const MONTH = 'MONTH'; // m - 2 digit month, 01-12
  63. const MONTH_SHORT = 'MONTH_SHORT'; // n - 1 digit month, no leading zeros, 1-12
  64. const MONTH_DAYS = 'MONTH_DAYS'; // t - Number of days this month
  65. const MONTH_NAME = 'MONTH_NAME'; // F - full month name - locale aware, January-December
  66. const MONTH_NAME_SHORT = 'MONTH_NAME_SHORT'; // M - 3 letter monthname - locale aware, Jan-Dec
  67. const MONTH_NAME_NARROW = 'MONTH_NAME_NARROW'; // --- 1 letter month name - locale aware, J-D
  68. // year formats
  69. const YEAR = 'YEAR'; // Y - 4 digit year
  70. const YEAR_SHORT = 'YEAR_SHORT'; // y - 2 digit year, leading zeros 00-99
  71. const YEAR_8601 = 'YEAR_8601'; // o - number of year ISO8601
  72. const YEAR_SHORT_8601= 'YEAR_SHORT_8601';// --- 2 digit number of year ISO8601
  73. const LEAPYEAR = 'LEAPYEAR'; // L - is leapyear ?, 0-1
  74. // time formats
  75. const MERIDIEM = 'MERIDIEM'; // A,a - AM/PM - locale aware, AM/PM
  76. const SWATCH = 'SWATCH'; // B - Swatch Internet Time
  77. const HOUR = 'HOUR'; // H - 2 digit hour, leading zeros, 00-23
  78. const HOUR_SHORT = 'HOUR_SHORT'; // G - 1 digit hour, no leading zero, 0-23
  79. const HOUR_AM = 'HOUR_AM'; // h - 2 digit hour, leading zeros, 01-12 am/pm
  80. const HOUR_SHORT_AM = 'HOUR_SHORT_AM'; // g - 1 digit hour, no leading zero, 1-12 am/pm
  81. const MINUTE = 'MINUTE'; // i - 2 digit minute, leading zeros, 00-59
  82. const MINUTE_SHORT = 'MINUTE_SHORT'; // --- 1 digit minute, no leading zero, 0-59
  83. const SECOND = 'SECOND'; // s - 2 digit second, leading zeros, 00-59
  84. const SECOND_SHORT = 'SECOND_SHORT'; // --- 1 digit second, no leading zero, 0-59
  85. const MILLISECOND = 'MILLISECOND'; // --- milliseconds
  86. // timezone formats
  87. const TIMEZONE_NAME = 'TIMEZONE_NAME'; // e - timezone string
  88. const DAYLIGHT = 'DAYLIGHT'; // I - is Daylight saving time ?, 0-1
  89. const GMT_DIFF = 'GMT_DIFF'; // O - GMT difference, -1200 +1200
  90. const GMT_DIFF_SEP = 'GMT_DIFF_SEP'; // P - seperated GMT diff, -12:00 +12:00
  91. const TIMEZONE = 'TIMEZONE'; // T - timezone, EST, GMT, MDT
  92. const TIMEZONE_SECS = 'TIMEZONE_SECS'; // Z - timezone offset in seconds, -43200 +43200
  93. // date strings
  94. const ISO_8601 = 'ISO_8601'; // c - ISO 8601 date string
  95. const RFC_2822 = 'RFC_2822'; // r - RFC 2822 date string
  96. const TIMESTAMP = 'TIMESTAMP'; // U - unix timestamp
  97. // additional formats
  98. const ERA = 'ERA'; // --- short name of era, locale aware,
  99. const ERA_NAME = 'ERA_NAME'; // --- full name of era, locale aware,
  100. const DATES = 'DATES'; // --- standard date, locale aware
  101. const DATE_FULL = 'DATE_FULL'; // --- full date, locale aware
  102. const DATE_LONG = 'DATE_LONG'; // --- long date, locale aware
  103. const DATE_MEDIUM = 'DATE_MEDIUM'; // --- medium date, locale aware
  104. const DATE_SHORT = 'DATE_SHORT'; // --- short date, locale aware
  105. const TIMES = 'TIMES'; // --- standard time, locale aware
  106. const TIME_FULL = 'TIME_FULL'; // --- full time, locale aware
  107. const TIME_LONG = 'TIME_LONG'; // --- long time, locale aware
  108. const TIME_MEDIUM = 'TIME_MEDIUM'; // --- medium time, locale aware
  109. const TIME_SHORT = 'TIME_SHORT'; // --- short time, locale aware
  110. const ATOM = 'ATOM'; // --- DATE_ATOM
  111. const COOKIE = 'COOKIE'; // --- DATE_COOKIE
  112. const RFC_822 = 'RFC_822'; // --- DATE_RFC822
  113. const RFC_850 = 'RFC_850'; // --- DATE_RFC850
  114. const RFC_1036 = 'RFC_1036'; // --- DATE_RFC1036
  115. const RFC_1123 = 'RFC_1123'; // --- DATE_RFC1123
  116. const RFC_3339 = 'RFC_3339'; // --- DATE_RFC3339
  117. const RSS = 'RSS'; // --- DATE_RSS
  118. const W3C = 'W3C'; // --- DATE_W3C
  119. /**
  120. * Generates the standard date object, could be a unix timestamp, localized date,
  121. * string, integer, array and so on. Also parts of dates or time are supported
  122. * Always set the default timezone: http://php.net/date_default_timezone_set
  123. * For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
  124. * For detailed instructions please look in the docu.
  125. *
  126. * @param string|integer|Zend_Date|array $date OPTIONAL Date value or value of date part to set
  127. * ,depending on $part. If null the actual time is set
  128. * @param string $part OPTIONAL Defines the input format of $date
  129. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  130. * @return Zend_Date
  131. * @throws Zend_Date_Exception
  132. */
  133. public function __construct($date = null, $part = null, $locale = null)
  134. {
  135. if (($date !== null) and !($date instanceof Zend_TimeSync_Protocol) and (Zend_Locale::isLocale($date, true, false))) {
  136. $locale = $date;
  137. $date = null;
  138. $part = null;
  139. } else if (($part !== null) and (Zend_Locale::isLocale($part, null, false))) {
  140. $locale = $part;
  141. $part = null;
  142. }
  143. $this->setLocale($locale);
  144. if (is_string($date) && defined('self::' . $date)) {
  145. $part = $date;
  146. $date = null;
  147. }
  148. if (is_null($date)) {
  149. $date = self::now($locale);
  150. if (($part !== null) && ($part !== self::TIMESTAMP)) {
  151. $date = $date->get($part);
  152. }
  153. }
  154. if ($date instanceof Zend_TimeSync_Protocol) {
  155. $date = $date->getInfo();
  156. $date = $this->_getTime($date['offset']);
  157. $part = null;
  158. } else if (parent::$_defaultOffset != 0) {
  159. $date = $this->_getTime(parent::$_defaultOffset);
  160. }
  161. // set the timezone and offset for $this
  162. $zone = @date_default_timezone_get();
  163. $this->setTimezone($zone);
  164. // try to get timezone from date-string
  165. if (!is_int($date)) {
  166. $zone = $this->getTimezoneFromString($date);
  167. $this->setTimezone($zone);
  168. }
  169. // set datepart
  170. if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
  171. // switch off dst handling for value setting
  172. $this->setUnixTimestamp($this->getGmtOffset());
  173. $this->set($date, $part, $this->_locale);
  174. // DST fix
  175. if ((is_array($date) === true) and (isset($date['hour']) === true)) {
  176. $hour = $this->toString('H');
  177. $hour = $date['hour'] - $hour;
  178. if ($hour !== 0) {
  179. $this->addTimestamp($hour * 3600);
  180. }
  181. }
  182. } else {
  183. $this->setUnixTimestamp($date);
  184. }
  185. }
  186. /**
  187. * Sets class wide options, if no option was given, the actual set options will be returned
  188. *
  189. * @param array $options Options to set
  190. * @throws Zend_Date_Exception
  191. * @return Options array if no option was given
  192. */
  193. public static function setOptions(array $options = array())
  194. {
  195. if (empty($options)) {
  196. return self::$_options;
  197. }
  198. foreach ($options as $name => $value) {
  199. $name = strtolower($name);
  200. if (array_key_exists($name, self::$_options)) {
  201. switch($name) {
  202. case 'format_type' :
  203. if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
  204. #require_once 'Zend/Date/Exception.php';
  205. throw new Zend_Date_Exception("Unknown format type ($value) for dates, only 'iso' and 'php' supported", $value);
  206. }
  207. break;
  208. case 'fix_dst' :
  209. if (!is_bool($value)) {
  210. #require_once 'Zend/Date/Exception.php';
  211. throw new Zend_Date_Exception("'fix_dst' has to be boolean", $value);
  212. }
  213. break;
  214. case 'extend_month' :
  215. if (!is_bool($value)) {
  216. #require_once 'Zend/Date/Exception.php';
  217. throw new Zend_Date_Exception("'extend_month' has to be boolean", $value);
  218. }
  219. break;
  220. case 'cache' :
  221. if (!$value instanceof Zend_Cache_Core) {
  222. #require_once 'Zend/Date/Exception.php';
  223. throw new Zend_Date_Exception("Instance of Zend_Cache expected");
  224. }
  225. parent::$_cache = $value;
  226. Zend_Locale_Data::setCache($value);
  227. break;
  228. case 'timesync' :
  229. if (!$value instanceof Zend_TimeSync_Protocol) {
  230. #require_once 'Zend/Date/Exception.php';
  231. throw new Zend_Date_Exception("Instance of Zend_TimeSync expected");
  232. }
  233. $date = $value->getInfo();
  234. parent::$_defaultOffset = $date['offset'];
  235. break;
  236. }
  237. self::$_options[$name] = $value;
  238. }
  239. else {
  240. #require_once 'Zend/Date/Exception.php';
  241. throw new Zend_Date_Exception("Unknown option: $name = $value");
  242. }
  243. }
  244. }
  245. /**
  246. * Returns this object's internal UNIX timestamp (equivalent to Zend_Date::TIMESTAMP).
  247. * If the timestamp is too large for integers, then the return value will be a string.
  248. * This function does not return the timestamp as an object.
  249. * Use clone() or copyPart() instead.
  250. *
  251. * @return integer|string UNIX timestamp
  252. */
  253. public function getTimestamp()
  254. {
  255. return $this->getUnixTimestamp();
  256. }
  257. /**
  258. * Returns the calculated timestamp
  259. * HINT: timestamps are always GMT
  260. *
  261. * @param string $calc Type of calculation to make
  262. * @param string|integer|array|Zend_Date $stamp Timestamp to calculate, when null the actual timestamp is calculated
  263. * @return Zend_Date|integer
  264. * @throws Zend_Date_Exception
  265. */
  266. private function _timestamp($calc, $stamp)
  267. {
  268. if ($stamp instanceof Zend_Date) {
  269. // extract timestamp from object
  270. $stamp = $stamp->get(self::TIMESTAMP, true);
  271. }
  272. if (is_array($stamp)) {
  273. if (isset($stamp['timestamp']) === true) {
  274. $stamp = $stamp['timestamp'];
  275. } else {
  276. #require_once 'Zend/Date/Exception.php';
  277. throw new Zend_Date_Exception('no timestamp given in array');
  278. }
  279. }
  280. if ($calc === 'set') {
  281. $return = $this->setUnixTimestamp($stamp);
  282. } else {
  283. $return = $this->_calcdetail($calc, $stamp, self::TIMESTAMP, null);
  284. }
  285. if ($calc != 'cmp') {
  286. return $this;
  287. }
  288. return $return;
  289. }
  290. /**
  291. * Sets a new timestamp
  292. *
  293. * @param integer|string|array|Zend_Date $timestamp Timestamp to set
  294. * @return Zend_Date
  295. * @throws Zend_Date_Exception
  296. */
  297. public function setTimestamp($timestamp)
  298. {
  299. return $this->_timestamp('set', $timestamp);
  300. }
  301. /**
  302. * Adds a timestamp
  303. *
  304. * @param integer|string|array|Zend_Date $timestamp Timestamp to add
  305. * @return Zend_Date
  306. * @throws Zend_Date_Exception
  307. */
  308. public function addTimestamp($timestamp)
  309. {
  310. return $this->_timestamp('add', $timestamp);
  311. }
  312. /**
  313. * Subtracts a timestamp
  314. *
  315. * @param integer|string|array|Zend_Date $timestamp Timestamp to sub
  316. * @return Zend_Date
  317. * @throws Zend_Date_Exception
  318. */
  319. public function subTimestamp($timestamp)
  320. {
  321. return $this->_timestamp('sub', $timestamp);
  322. }
  323. /**
  324. * Compares two timestamps, returning the difference as integer
  325. *
  326. * @param integer|string|array|Zend_Date $timestamp Timestamp to compare
  327. * @return integer 0 = equal, 1 = later, -1 = earlier
  328. * @throws Zend_Date_Exception
  329. */
  330. public function compareTimestamp($timestamp)
  331. {
  332. return $this->_timestamp('cmp', $timestamp);
  333. }
  334. /**
  335. * Returns a string representation of the object
  336. * Supported format tokens are:
  337. * G - era, y - year, Y - ISO year, M - month, w - week of year, D - day of year, d - day of month
  338. * E - day of week, e - number of weekday (1-7), h - hour 1-12, H - hour 0-23, m - minute, s - second
  339. * A - milliseconds of day, z - timezone, Z - timezone offset, S - fractional second, a - period of day
  340. *
  341. * Additionally format tokens but non ISO conform are:
  342. * SS - day suffix, eee - php number of weekday(0-6), ddd - number of days per month
  343. * l - Leap year, B - swatch internet time, I - daylight saving time, X - timezone offset in seconds
  344. * r - RFC2822 format, U - unix timestamp
  345. *
  346. * Not supported ISO tokens are
  347. * u - extended year, Q - quarter, q - quarter, L - stand alone month, W - week of month
  348. * F - day of week of month, g - modified julian, c - stand alone weekday, k - hour 0-11, K - hour 1-24
  349. * v - wall zone
  350. *
  351. * @param string $format OPTIONAL Rule for formatting output. If null the default date format is used
  352. * @param string $type OPTIONAL Type for the format string which overrides the standard setting
  353. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  354. * @return string
  355. */
  356. public function toString($format = null, $type = null, $locale = null)
  357. {
  358. if ((strlen($format) != 2) and ($format !== null) and (Zend_Locale::isLocale($format, null, false))) {
  359. $locale = $format;
  360. $format = null;
  361. }
  362. if (($type !== null) and (Zend_Locale::isLocale($type, null, false))) {
  363. $locale = $type;
  364. $type = null;
  365. }
  366. if ($locale === null) {
  367. $locale = $this->getLocale();
  368. }
  369. if ($format === null) {
  370. $format = Zend_Locale_Format::getDateFormat($locale) . ' ' . Zend_Locale_Format::getTimeFormat($locale);
  371. } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
  372. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  373. }
  374. // get format tokens
  375. $j = 0;
  376. $comment = false;
  377. $output = array();
  378. for($i = 0; $i < strlen($format); ++$i) {
  379. if ($format[$i] == "'") {
  380. if ($comment == false) {
  381. $comment = true;
  382. ++$j;
  383. $output[$j] = "'";
  384. } else if (isset($format[$i+1]) and ($format[$i+1] == "'")) {
  385. $output[$j] .= "'";
  386. ++$i;
  387. } else {
  388. $comment = false;
  389. }
  390. continue;
  391. }
  392. if (isset($output[$j]) and ($output[$j][0] == $format[$i]) or
  393. ($comment == true)) {
  394. $output[$j] .= $format[$i];
  395. } else {
  396. ++$j;
  397. $output[$j] = $format[$i];
  398. }
  399. }
  400. $notset = false;
  401. // fill format tokens with date information
  402. for($i = 1; $i <= count($output); ++$i) {
  403. // fill fixed tokens
  404. switch ($output[$i]) {
  405. // special formats
  406. case 'SS' :
  407. $output[$i] = $this->date('S', $this->getUnixTimestamp(), false);
  408. break;
  409. case 'eee' :
  410. $output[$i] = $this->date('N', $this->getUnixTimestamp(), false);
  411. break;
  412. case 'ddd' :
  413. $output[$i] = $this->date('t', $this->getUnixTimestamp(), false);
  414. break;
  415. case 'l' :
  416. $output[$i] = $this->date('L', $this->getUnixTimestamp(), false);
  417. break;
  418. case 'B' :
  419. $output[$i] = $this->date('B', $this->getUnixTimestamp(), false);
  420. break;
  421. case 'I' :
  422. $output[$i] = $this->date('I', $this->getUnixTimestamp(), false);
  423. break;
  424. case 'X' :
  425. $output[$i] = $this->date('Z', $this->getUnixTimestamp(), false);
  426. break;
  427. case 'r' :
  428. $output[$i] = $this->date('r', $this->getUnixTimestamp(), false);
  429. break;
  430. case 'U' :
  431. $output[$i] = $this->getUnixTimestamp();
  432. break;
  433. // eras
  434. case 'GGGGG' :
  435. $output[$i] = iconv_substr($this->get(self::ERA, $locale), 0, 1) . ".";
  436. break;
  437. case 'GGGG' :
  438. $output[$i] = $this->get(self::ERA_NAME, $locale);
  439. break;
  440. case 'GGG' :
  441. case 'GG' :
  442. case 'G' :
  443. $output[$i] = $this->get(self::ERA, $locale);
  444. break;
  445. // years
  446. case 'yy' :
  447. $output[$i] = str_pad($this->get(self::YEAR_SHORT, $locale), 2, '0', STR_PAD_LEFT);
  448. break;
  449. // ISO years
  450. case 'YY' :
  451. $output[$i] = str_pad($this->get(self::YEAR_SHORT_8601, $locale), 2, '0', STR_PAD_LEFT);
  452. break;
  453. // months
  454. case 'MMMMM' :
  455. $output[$i] = iconv_substr($this->get(self::MONTH_NAME_NARROW, $locale), 0, 1);
  456. break;
  457. case 'MMMM' :
  458. $output[$i] = $this->get(self::MONTH_NAME, $locale);
  459. break;
  460. case 'MMM' :
  461. $output[$i] = $this->get(self::MONTH_NAME_SHORT, $locale);
  462. break;
  463. case 'MM' :
  464. $output[$i] = $this->get(self::MONTH, $locale);
  465. break;
  466. case 'M' :
  467. $output[$i] = $this->get(self::MONTH_SHORT, $locale);
  468. break;
  469. // week
  470. case 'ww' :
  471. $output[$i] = str_pad($this->get(self::WEEK, $locale), 2, '0', STR_PAD_LEFT);
  472. break;
  473. case 'w' :
  474. $output[$i] = $this->get(self::WEEK, $locale);
  475. break;
  476. // monthday
  477. case 'dd' :
  478. $output[$i] = $this->get(self::DAY, $locale);
  479. break;
  480. case 'd' :
  481. $output[$i] = $this->get(self::DAY_SHORT, $locale);
  482. break;
  483. // yearday
  484. case 'DDD' :
  485. $output[$i] = str_pad($this->get(self::DAY_OF_YEAR, $locale), 3, '0', STR_PAD_LEFT);
  486. break;
  487. case 'DD' :
  488. $output[$i] = str_pad($this->get(self::DAY_OF_YEAR, $locale), 2, '0', STR_PAD_LEFT);
  489. break;
  490. case 'D' :
  491. $output[$i] = $this->get(self::DAY_OF_YEAR, $locale);
  492. break;
  493. // weekday
  494. case 'EEEEE' :
  495. $output[$i] = $this->get(self::WEEKDAY_NARROW, $locale);
  496. break;
  497. case 'EEEE' :
  498. $output[$i] = $this->get(self::WEEKDAY, $locale);
  499. break;
  500. case 'EEE' :
  501. $output[$i] = $this->get(self::WEEKDAY_SHORT, $locale);
  502. break;
  503. case 'EE' :
  504. $output[$i] = $this->get(self::WEEKDAY_NAME, $locale);
  505. break;
  506. case 'E' :
  507. $output[$i] = $this->get(self::WEEKDAY_NARROW, $locale);
  508. break;
  509. // weekday number
  510. case 'ee' :
  511. $output[$i] = str_pad($this->get(self::WEEKDAY_8601, $locale), 2, '0', STR_PAD_LEFT);
  512. break;
  513. case 'e' :
  514. $output[$i] = $this->get(self::WEEKDAY_8601, $locale);
  515. break;
  516. // period
  517. case 'a' :
  518. $output[$i] = $this->get(self::MERIDIEM, $locale);
  519. break;
  520. // hour
  521. case 'hh' :
  522. $output[$i] = $this->get(self::HOUR_AM, $locale);
  523. break;
  524. case 'h' :
  525. $output[$i] = $this->get(self::HOUR_SHORT_AM, $locale);
  526. break;
  527. case 'HH' :
  528. $output[$i] = $this->get(self::HOUR, $locale);
  529. break;
  530. case 'H' :
  531. $output[$i] = $this->get(self::HOUR_SHORT, $locale);
  532. break;
  533. // minute
  534. case 'mm' :
  535. $output[$i] = $this->get(self::MINUTE, $locale);
  536. break;
  537. case 'm' :
  538. $output[$i] = $this->get(self::MINUTE_SHORT, $locale);
  539. break;
  540. // second
  541. case 'ss' :
  542. $output[$i] = $this->get(self::SECOND, $locale);
  543. break;
  544. case 's' :
  545. $output[$i] = $this->get(self::SECOND_SHORT, $locale);
  546. break;
  547. case 'S' :
  548. $output[$i] = $this->get(self::MILLISECOND, $locale);
  549. break;
  550. // zone
  551. // @todo v needs to be reworked as it's the long wall time and not the timezone
  552. case 'vvvv' :
  553. case 'zzzz' :
  554. $output[$i] = $this->get(self::TIMEZONE_NAME, $locale);
  555. break;
  556. // @todo v needs to be reworked as it's the short wall time and not the timezone
  557. case 'v' :
  558. case 'zzz' :
  559. case 'zz' :
  560. case 'z' :
  561. $output[$i] = $this->get(self::TIMEZONE, $locale);
  562. break;
  563. // zone offset
  564. case 'ZZZZ' :
  565. $output[$i] = $this->get(self::GMT_DIFF_SEP, $locale);
  566. break;
  567. case 'ZZZ' :
  568. case 'ZZ' :
  569. case 'Z' :
  570. $output[$i] = $this->get(self::GMT_DIFF, $locale);
  571. break;
  572. default :
  573. $notset = true;
  574. break;
  575. }
  576. // fill variable tokens
  577. if ($notset == true) {
  578. if (($output[$i][0] !== "'") and (preg_match('/y+/', $output[$i]))) {
  579. $length = iconv_strlen($output[$i]);
  580. $output[$i] = $this->get(self::YEAR, $locale);
  581. $output[$i] = str_pad($output[$i], $length, '0', STR_PAD_LEFT);
  582. }
  583. if (($output[$i][0] !== "'") and (preg_match('/Y+/', $output[$i]))) {
  584. $length = iconv_strlen($output[$i]);
  585. $output[$i] = $this->get(self::YEAR_8601, $locale);
  586. $output[$i] = str_pad($output[$i], $length, '0', STR_PAD_LEFT);
  587. }
  588. if (($output[$i][0] !== "'") and (preg_match('/A+/', $output[$i]))) {
  589. $length = iconv_strlen($output[$i]);
  590. $hour = $this->get(self::HOUR, $locale);
  591. $minute = $this->get(self::MINUTE, $locale);
  592. $second = $this->get(self::SECOND, $locale);
  593. $milli = $this->get(self::MILLISECOND, $locale);
  594. $seconds = $milli + ($second * 1000) + ($minute * 60000) + ($hour * 3600000);
  595. $output[$i] = str_pad($seconds, $length, '0', STR_PAD_LEFT);
  596. }
  597. if ($output[$i][0] === "'") {
  598. $output[$i] = iconv_substr($output[$i], 1);
  599. }
  600. }
  601. $notset = false;
  602. }
  603. return implode('', $output);
  604. }
  605. /**
  606. * Returns a string representation of the date which is equal with the timestamp
  607. *
  608. * @return string
  609. */
  610. public function __toString()
  611. {
  612. return $this->toString(null, $this->_locale);
  613. }
  614. /**
  615. * Returns a integer representation of the object
  616. * But returns false when the given part is no value f.e. Month-Name
  617. *
  618. * @param string|integer|Zend_Date $part OPTIONAL Defines the date or datepart to return as integer
  619. * @return integer|false
  620. */
  621. public function toValue($part = null)
  622. {
  623. $result = $this->get($part);
  624. if (is_numeric($result)) {
  625. return intval("$result");
  626. } else {
  627. return false;
  628. }
  629. }
  630. /**
  631. * Returns an array representation of the object
  632. *
  633. * @return array
  634. */
  635. public function toArray()
  636. {
  637. return array('day' => $this->get(self::DAY_SHORT),
  638. 'month' => $this->get(self::MONTH_SHORT),
  639. 'year' => $this->get(self::YEAR),
  640. 'hour' => $this->get(self::HOUR_SHORT),
  641. 'minute' => $this->get(self::MINUTE_SHORT),
  642. 'second' => $this->get(self::SECOND_SHORT),
  643. 'timezone' => $this->get(self::TIMEZONE),
  644. 'timestamp' => $this->get(self::TIMESTAMP),
  645. 'weekday' => $this->get(self::WEEKDAY_8601),
  646. 'dayofyear' => $this->get(self::DAY_OF_YEAR),
  647. 'week' => $this->get(self::WEEK),
  648. 'gmtsecs' => $this->get(self::TIMEZONE_SECS));
  649. }
  650. /**
  651. * Returns a representation of a date or datepart
  652. * This could be for example a localized monthname, the time without date,
  653. * the era or only the fractional seconds. There are about 50 different supported date parts.
  654. * For a complete list of supported datepart values look into the docu
  655. *
  656. * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned
  657. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  658. * @return integer|string date or datepart
  659. */
  660. public function get($part = null, $locale = null)
  661. {
  662. if ($locale === null) {
  663. $locale = $this->getLocale();
  664. }
  665. if (($part !== null) and (Zend_Locale::isLocale($part, null, false))) {
  666. $locale = $part;
  667. $part = null;
  668. }
  669. if ($part === null) {
  670. $part = self::TIMESTAMP;
  671. }
  672. if (!defined("self::".$part)) {
  673. return $this->toString($part, $locale);
  674. }
  675. switch($part) {
  676. // day formats
  677. case self::DAY :
  678. return $this->date('d', $this->getUnixTimestamp(), false);
  679. break;
  680. case self::WEEKDAY_SHORT :
  681. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  682. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
  683. return iconv_substr($day, 0, 3);
  684. break;
  685. case self::DAY_SHORT :
  686. return $this->date('j', $this->getUnixTimestamp(), false);
  687. break;
  688. case self::WEEKDAY :
  689. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  690. return Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
  691. break;
  692. case self::WEEKDAY_8601 :
  693. return $this->date('N', $this->getUnixTimestamp(), false);
  694. break;
  695. case self::DAY_SUFFIX :
  696. return $this->date('S', $this->getUnixTimestamp(), false);
  697. break;
  698. case self::WEEKDAY_DIGIT :
  699. return $this->date('w', $this->getUnixTimestamp(), false);
  700. break;
  701. case self::DAY_OF_YEAR :
  702. return $this->date('z', $this->getUnixTimestamp(), false);
  703. break;
  704. case self::WEEKDAY_NARROW :
  705. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  706. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
  707. return iconv_substr($day, 0, 1);
  708. break;
  709. case self::WEEKDAY_NAME :
  710. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  711. return Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
  712. break;
  713. // week formats
  714. case self::WEEK :
  715. return $this->date('W', $this->getUnixTimestamp(), false);
  716. break;
  717. // month formats
  718. case self::MONTH_NAME :
  719. $month = $this->date('n', $this->getUnixTimestamp(), false);
  720. return Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month));
  721. break;
  722. case self::MONTH :
  723. return $this->date('m', $this->getUnixTimestamp(), false);
  724. break;
  725. case self::MONTH_NAME_SHORT :
  726. $month = $this->date('n', $this->getUnixTimestamp(), false);
  727. return Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
  728. break;
  729. case self::MONTH_SHORT :
  730. return $this->date('n', $this->getUnixTimestamp(), false);
  731. break;
  732. case self::MONTH_DAYS :
  733. return $this->date('t', $this->getUnixTimestamp(), false);
  734. break;
  735. case self::MONTH_NAME_NARROW :
  736. $month = $this->date('n', $this->getUnixTimestamp(), false);
  737. $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
  738. return iconv_substr($mon, 0, 1);
  739. break;
  740. // year formats
  741. case self::LEAPYEAR :
  742. return $this->date('L', $this->getUnixTimestamp(), false);
  743. break;
  744. case self::YEAR_8601 :
  745. return $this->date('o', $this->getUnixTimestamp(), false);
  746. break;
  747. case self::YEAR :
  748. return $this->date('Y', $this->getUnixTimestamp(), false);
  749. break;
  750. case self::YEAR_SHORT :
  751. return $this->date('y', $this->getUnixTimestamp(), false);
  752. break;
  753. case self::YEAR_SHORT_8601 :
  754. $year = $this->date('o', $this->getUnixTimestamp(), false);
  755. return iconv_substr($year, -2);
  756. break;
  757. // time formats
  758. case self::MERIDIEM :
  759. $am = $this->date('a', $this->getUnixTimestamp(), false);
  760. if ($am == 'am') {
  761. return Zend_Locale_Data::getContent($locale, 'am');
  762. }
  763. return Zend_Locale_Data::getContent($locale, 'pm');
  764. break;
  765. case self::SWATCH :
  766. return $this->date('B', $this->getUnixTimestamp(), false);
  767. break;
  768. case self::HOUR_SHORT_AM :
  769. return $this->date('g', $this->getUnixTimestamp(), false);
  770. break;
  771. case self::HOUR_SHORT :
  772. return $this->date('G', $this->getUnixTimestamp(), false);
  773. break;
  774. case self::HOUR_AM :
  775. return $this->date('h', $this->getUnixTimestamp(), false);
  776. break;
  777. case self::HOUR :
  778. return $this->date('H', $this->getUnixTimestamp(), false);
  779. break;
  780. case self::MINUTE :
  781. return $this->date('i', $this->getUnixTimestamp(), false);
  782. break;
  783. case self::SECOND :
  784. return $this->date('s', $this->getUnixTimestamp(), false);
  785. break;
  786. case self::MINUTE_SHORT :
  787. return $this->date('i', $this->getUnixTimestamp(), false);
  788. break;
  789. case self::SECOND_SHORT :
  790. return $this->date('s', $this->getUnixTimestamp(), false);
  791. break;
  792. case self::MILLISECOND :
  793. return $this->_fractional;
  794. break;
  795. // timezone formats
  796. case self::TIMEZONE_NAME :
  797. return $this->date('e', $this->getUnixTimestamp(), false);
  798. break;
  799. case self::DAYLIGHT :
  800. return $this->date('I', $this->getUnixTimestamp(), false);
  801. break;
  802. case self::GMT_DIFF :
  803. return $this->date('O', $this->getUnixTimestamp(), false);
  804. break;
  805. case self::GMT_DIFF_SEP :
  806. return $this->date('P', $this->getUnixTimestamp(), false);
  807. break;
  808. case self::TIMEZONE :
  809. return $this->date('T', $this->getUnixTimestamp(), false);
  810. break;
  811. case self::TIMEZONE_SECS :
  812. return $this->date('Z', $this->getUnixTimestamp(), false);
  813. break;
  814. // date strings
  815. case self::ISO_8601 :
  816. return $this->date('c', $this->getUnixTimestamp(), false);
  817. break;
  818. case self::RFC_2822 :
  819. return $this->date('r', $this->getUnixTimestamp(), false);
  820. break;
  821. case self::TIMESTAMP :
  822. return $this->getUnixTimestamp();
  823. break;
  824. // additional formats
  825. case self::ERA :
  826. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  827. if ($year < 0) {
  828. return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0'));
  829. }
  830. return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1'));
  831. break;
  832. case self::ERA_NAME :
  833. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  834. if ($year < 0) {
  835. return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0'));
  836. }
  837. return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1'));
  838. break;
  839. case self::DATES :
  840. return $this->toString(Zend_Locale_Format::getDateFormat($locale), 'iso', $locale);
  841. break;
  842. case self::DATE_FULL :
  843. $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  844. return $this->toString($date, 'iso', $locale);
  845. break;
  846. case self::DATE_LONG :
  847. $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  848. return $this->toString($date, 'iso', $locale);
  849. break;
  850. case self::DATE_MEDIUM :
  851. $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  852. return $this->toString($date, 'iso', $locale);
  853. break;
  854. case self::DATE_SHORT :
  855. $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  856. return $this->toString($date, 'iso', $locale);
  857. break;
  858. case self::TIMES :
  859. return $this->toString(Zend_Locale_Format::getTimeFormat($locale), 'iso', $locale);
  860. break;
  861. case self::TIME_FULL :
  862. $time = Zend_Locale_Data::getContent($locale, 'time', 'full');
  863. return $this->toString($time, 'iso', $locale);
  864. break;
  865. case self::TIME_LONG :
  866. $time = Zend_Locale_Data::getContent($locale, 'time', 'long');
  867. return $this->toString($time, 'iso', $locale);
  868. break;
  869. case self::TIME_MEDIUM :
  870. $time = Zend_Locale_Data::getContent($locale, 'time', 'medium');
  871. return $this->toString($time, 'iso', $locale);
  872. break;
  873. case self::TIME_SHORT :
  874. $time = Zend_Locale_Data::getContent($locale, 'time', 'short');
  875. return $this->toString($time, 'iso', $locale);
  876. break;
  877. case self::ATOM :
  878. return $this->date('Y\-m\-d\TH\:i\:sP', $this->getUnixTimestamp(), false);
  879. break;
  880. case self::COOKIE :
  881. return $this->date('l\, d\-M\-y H\:i\:s e', $this->getUnixTimestamp(), false);
  882. break;
  883. case self::RFC_822 :
  884. return $this->date('D\, d M y H\:i\:s O', $this->getUnixTimestamp(), false);
  885. break;
  886. case self::RFC_850 :
  887. return $this->date('l\, d\-M\-y H\:i\:s e', $this->getUnixTimestamp(), false);
  888. break;
  889. case self::RFC_1036 :
  890. return $this->date('D\, d M y H\:i\:s O', $this->getUnixTimestamp(), false);
  891. break;
  892. case self::RFC_1123 :
  893. return $this->date('D\, d M Y H\:i\:s O', $this->getUnixTimestamp(), false);
  894. break;
  895. case self::RFC_3339 :
  896. return $this->date('Y\-m\-d\TH\:i\:sP', $this->getUnixTimestamp(), false);
  897. break;
  898. case self::RSS :
  899. return $this->date('D\, d M Y H\:i\:s O', $this->getUnixTimestamp(), false);
  900. break;
  901. case self::W3C :
  902. return $this->date('Y\-m\-d\TH\:i\:sP', $this->getUnixTimestamp(), false);
  903. break;
  904. }
  905. }
  906. /**
  907. * Return digit from standard names (english)
  908. * Faster implementation than locale aware searching
  909. *
  910. * @param string $name
  911. * @return integer Number of this month
  912. * @throws Zend_Date_Exception
  913. */
  914. private function _getDigitFromName($name)
  915. {
  916. switch($name) {
  917. case "Jan":
  918. return 1;
  919. case "Feb":
  920. return 2;
  921. case "Mar":
  922. return 3;
  923. case "Apr":
  924. return 4;
  925. case "May":
  926. return 5;
  927. case "Jun":
  928. return 6;
  929. case "Jul":
  930. return 7;
  931. case "Aug":
  932. return 8;
  933. case "Sep":
  934. return 9;
  935. case "Oct":
  936. return 10;
  937. case "Nov":
  938. return 11;
  939. case "Dec":
  940. return 12;
  941. default:
  942. #require_once 'Zend/Date/Exception.php';
  943. throw new Zend_Date_Exception('Month ($name) is not a known month');
  944. }
  945. }
  946. /**
  947. * Counts the exact year number
  948. * < 70 - 2000 added, >70 < 100 - 1900, others just returned
  949. *
  950. * @param integer $value year number
  951. * @return integer Number of year
  952. */
  953. public static function getFullYear($value)
  954. {
  955. if ($value >= 0) {
  956. if ($value < 70) {
  957. $value += 2000;
  958. } else if ($value < 100) {
  959. $value += 1900;
  960. }
  961. }
  962. return $value;
  963. }
  964. /**
  965. * Sets the given date as new date or a given datepart as new datepart returning the new datepart
  966. * This could be for example a localized dayname, the date without time,
  967. * the month or only the seconds. There are about 50 different supported date parts.
  968. * For a complete list of supported datepart values look into the docu
  969. *
  970. * @param string|integer|array|Zend_Date $date Date or datepart to set
  971. * @param string $part OPTIONAL Part of the date to set, if null the timestamp is set
  972. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  973. * @return integer|string new datepart
  974. * @throws Zend_Date_Exception
  975. */
  976. public function set($date, $part = null, $locale = null)
  977. {
  978. $zone = $this->getTimezoneFromString($date);
  979. $this->setTimezone($zone);
  980. $result = $this->_calculate('set', $date, $part, $locale);
  981. return $result;
  982. }
  983. /**
  984. * Adds a date or datepart to the existing date, by extracting $part from $date,
  985. * and modifying this object by adding that part. The $part is then extracted from
  986. * this object and returned as an integer or numeric string (for large values, or $part's
  987. * corresponding to pre-defined formatted date strings).
  988. * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
  989. * There are about 50 different supported date parts.
  990. * For a complete list of supported datepart values look into the docu.
  991. *
  992. * @param string|integer|array|Zend_Date $date Date or datepart to add
  993. * @param string $part OPTIONAL Part of the date to add, if null the timestamp is added
  994. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  995. * @return integer|string new datepart
  996. * @throws Zend_Date_Exception
  997. */
  998. public function add($date, $part = null, $locale = null)
  999. {
  1000. $this->_calculate('add', $date, $part, $locale);
  1001. $result = $this->get($part, $locale);
  1002. return $result;
  1003. }
  1004. /**
  1005. * Subtracts a date from another date.
  1006. * This could be for example a RFC2822 date, the time,
  1007. * the year or only the timestamp. There are about 50 different supported date parts.
  1008. * For a complete list of supported datepart values look into the docu
  1009. * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
  1010. *
  1011. * @param string|integer|array|Zend_Date $date Date or datepart to subtract
  1012. * @param string $part OPTIONAL Part of the date to sub, if null the timestamp is subtracted
  1013. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  1014. * @return integer|string new datepart
  1015. * @throws Zend_Date_Exception
  1016. */
  1017. public function sub($date, $part = null, $locale = null)
  1018. {
  1019. $this->_calculate('sub', $date, $part, $locale);
  1020. $result = $this->get($part, $locale);
  1021. return $result;
  1022. }
  1023. /**
  1024. * Compares a date or datepart with the existing one.
  1025. * Returns -1 if earlier, 0 if equal and 1 if later.
  1026. *
  1027. * @param string|integer|array|Zend_Date $date Date or datepart to compare with the date object
  1028. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is subtracted
  1029. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  1030. * @return integer 0 = equal, 1 = later, -1 = earlier
  1031. * @throws Zend_Date_Exception
  1032. */
  1033. public function compare($date, $part = null, $locale = null)
  1034. {
  1035. $compare = $this->_calculate('cmp', $date, $part, $locale);
  1036. if ($compare > 0) {
  1037. return 1;
  1038. } else if ($compare < 0) {
  1039. return -1;
  1040. }
  1041. return 0;
  1042. }
  1043. /**
  1044. * Returns a new instance of Zend_Date with the selected part copied.
  1045. * To make an exact copy, use PHP's clone keyword.
  1046. * For a complete list of supported date part values look into the docu.
  1047. * If a date part is copied, all other date parts are set to standard values.
  1048. * For example: If only YEAR is copied, the returned date object is equal to
  1049. * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
  1050. * If only HOUR is copied, the returned date object is equal to
  1051. * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
  1052. *
  1053. * @param string $part Part of the date to compare, if null the timestamp is subtracted
  1054. * @param string|Zend_Locale $locale OPTIONAL New object's locale. No adjustments to timezone are made.
  1055. * @return Zend_Date
  1056. */
  1057. public function copyPart($part, $locale = null)
  1058. {
  1059. $clone = clone $this; // copy all instance variables
  1060. $clone->setUnixTimestamp(0); // except the timestamp
  1061. if ($locale != null) {
  1062. $clone->setLocale($locale); // set an other locale if selected
  1063. }
  1064. $clone->set($this, $part);
  1065. return $clone;
  1066. }
  1067. /**
  1068. * Internal function, returns the offset of a given timezone
  1069. *
  1070. * @param string $zone
  1071. * @return integer
  1072. */
  1073. public function getTimezoneFromString($zone)
  1074. {
  1075. if (is_array($zone)) {
  1076. return $this->getTimezone();
  1077. }
  1078. if ($zone instanceof Zend_Date) {
  1079. return $zone->getTimezone();
  1080. }
  1081. preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
  1082. if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
  1083. $zone = "Etc/GMT";
  1084. $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
  1085. $zone .= (int) abs($match[count($match) - 1]);
  1086. return $zone;
  1087. }
  1088. preg_match('/([[:alpha:]\/]{3,30})/', $zone, $match);
  1089. try {
  1090. if (!empty($match) and (!is_int($match[count($match) - 1]))) {
  1091. $oldzone = $this->getTimezone();
  1092. $this->setTimezone($match[count($match) - 1]);
  1093. $result = $this->getTimezone();
  1094. $this->setTimezone($oldzone);
  1095. if ($result !== $oldzone) {
  1096. return $match[count($match) - 1];
  1097. }
  1098. }
  1099. } catch (Exception $e) {
  1100. // fall through
  1101. }
  1102. return $this->getTimezone();
  1103. }
  1104. /**
  1105. * Calculates the date or object
  1106. *
  1107. * @param string $calc Calculation to make
  1108. * @param string|integer $date Date for calculation
  1109. * @param string|integer $comp Second date for calculation
  1110. * @param boolean|integer $dst Use dst correction if option is set
  1111. * @return integer|string|Zend_Date new timestamp or Zend_Date depending on calculation
  1112. */
  1113. private function _assign($calc, $date, $comp = 0, $dst = false)
  1114. {
  1115. switch ($calc) {
  1116. case 'set' :
  1117. if (!empty($comp)) {
  1118. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $comp));
  1119. }
  1120. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1121. $value = $this->getUnixTimestamp();
  1122. break;
  1123. case 'add' :
  1124. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1125. $value = $this->getUnixTimestamp();
  1126. break;
  1127. case 'sub' :
  1128. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $date));
  1129. $value = $this->getUnixTimestamp();
  1130. break;
  1131. default :
  1132. // cmp - compare
  1133. return call_user_func(Zend_Locale_Math::$comp, $comp, $date);
  1134. break;
  1135. }
  1136. // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
  1137. if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
  1138. $hour = $this->get(self::HOUR);
  1139. if ($hour != $dst) {
  1140. if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
  1141. $value += 3600;
  1142. } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
  1143. $value -= 3600;
  1144. }
  1145. $this->setUnixTimestamp($value);
  1146. }
  1147. }
  1148. return $this->getUnixTimestamp();
  1149. }
  1150. /**
  1151. * Calculates the date or object
  1152. *
  1153. * @param string $calc Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set'
  1154. * @param string|integer|array|Zend_Date $date Date or datepart to calculate with
  1155. * @param string $part Part of the date to calculate, if null the timestamp is used
  1156. * @param string|Zend_Locale $locale Locale for parsing input
  1157. * @return integer|string|Zend_Date new timestamp
  1158. * @throws Zend_Date_Exception
  1159. */
  1160. private function _calculate($calc, $date, $part, $locale)
  1161. {
  1162. if (is_null($date) === true) {
  1163. #require_once 'Zend/Date/Exception.php';
  1164. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  1165. }
  1166. if (($part !== null) and (Zend_Locale::isLocale($part, null, false))) {
  1167. $locale = $part;
  1168. $part = null;
  1169. }
  1170. if ($locale === null) {
  1171. $locale = $this->getLocale();
  1172. }
  1173. $locale = (string) $locale;
  1174. // Create date parts
  1175. $year = $this->get(self::YEAR);
  1176. $month = $this->get(self::MONTH_SHORT);
  1177. $day = $this->get(self::DAY_SHORT);
  1178. $hour = $this->get(self::HOUR_SHORT);
  1179. $minute = $this->get(self::MINUTE_SHORT);
  1180. $second = $this->get(self::SECOND_SHORT);
  1181. // If object extract value
  1182. if ($date instanceof Zend_Date) {
  1183. $date = $date->get($part, $locale);
  1184. }
  1185. if (is_array($date) === true) {
  1186. if (empty($part) === false) {
  1187. switch($part) {
  1188. // Fall through
  1189. case self::DAY:
  1190. case self::DAY_SHORT:
  1191. if (isset($date['day']) === true) {
  1192. $date = $date['day'];
  1193. }
  1194. break;
  1195. // Fall through
  1196. case self::WEEKDAY_SHORT:
  1197. case self::WEEKDAY:
  1198. case self::WEEKDAY_8601:
  1199. case self::WEEKDAY_DIGIT:
  1200. case self::WEEKDAY_NARROW:
  1201. case self::WEEKDAY_NAME:
  1202. if (isset($date['weekday']) === true) {
  1203. $date = $date['weekday'];
  1204. $part = self::WEEKDAY_DIGIT;
  1205. }
  1206. break;
  1207. case self::DAY_OF_YEAR:
  1208. if (isset($date['day_of_year']) === true) {
  1209. $date = $date['day_of_year'];
  1210. }
  1211. break;
  1212. // Fall through
  1213. case self::MONTH:
  1214. case self::MONTH_SHORT:
  1215. case self::MONTH_NAME:
  1216. case self::MONTH_NAME_SHORT:
  1217. case self::MONTH_NAME_NARROW:
  1218. if (isset($date['month']) === true) {
  1219. $date = $date['month'];
  1220. }
  1221. break;
  1222. // Fall through
  1223. case self::YEAR:
  1224. case self::YEAR_SHORT:
  1225. case self::YEAR_8601:
  1226. case self::YEAR_SHORT_8601:
  1227. if (isset($date['year']) === true) {
  1228. $date = $date['year'];
  1229. }
  1230. break;
  1231. // Fall through
  1232. case self::HOUR:
  1233. case self::HOUR_AM:
  1234. case self::HOUR_SHORT:
  1235. case self::HOUR_SHORT_AM:
  1236. if (isset($date['hour']) === true) {
  1237. $date = $date['hour'];
  1238. }
  1239. break;
  1240. // Fall through
  1241. case self::MINUTE:
  1242. case self::MINUTE_SHORT:
  1243. if (isset($date['minute']) === true) {
  1244. $date = $date['minute'];
  1245. }
  1246. break;
  1247. // Fall through
  1248. case self::SECOND:
  1249. case self::SECOND_SHORT:
  1250. if (isset($date['second']) === true) {
  1251. $date = $date['second'];
  1252. }
  1253. break;
  1254. // Fall through
  1255. case self::TIMEZONE:
  1256. case self::TIMEZONE_NAME:
  1257. if (isset($date['timezone']) === true) {
  1258. $date = $date['timezone'];
  1259. }
  1260. break;
  1261. case self::TIMESTAMP:
  1262. if (isset($date['timestamp']) === true) {
  1263. $date = $date['timestamp'];
  1264. }
  1265. break;
  1266. case self::WEEK:
  1267. if (isset($date['week']) === true) {
  1268. $date = $date['week'];
  1269. }
  1270. break;
  1271. case self::TIMEZONE_SECS:
  1272. if (isset($date['gmtsecs']) === true) {
  1273. $date = $date['gmtsecs'];
  1274. }
  1275. break;
  1276. default:
  1277. #require_once 'Zend/Date/Exception.php';
  1278. throw new Zend_Date_Exception("datepart for part ($part) not found in array");
  1279. break;
  1280. }
  1281. } else {
  1282. $hours = 0;
  1283. if (isset($date['hour']) === true) {
  1284. $hours = $date['hour'];
  1285. }
  1286. $minutes = 0;
  1287. if (isset($date['minute']) === true) {
  1288. $minutes = $date['minute'];
  1289. }
  1290. $seconds = 0;
  1291. if (isset($date['second']) === true) {
  1292. $seconds = $date['second'];
  1293. }
  1294. $months = 0;
  1295. if (isset($date['month']) === true) {
  1296. $months = $date['month'];
  1297. }
  1298. $days = 0;
  1299. if (isset($date['day']) === true) {
  1300. $days = $date['day'];
  1301. }
  1302. $years = 0;
  1303. if (isset($date['year']) === true) {
  1304. $years = $date['year'];
  1305. }
  1306. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true),
  1307. $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour);
  1308. }
  1309. }
  1310. // $date as object, part of foreign date as own date
  1311. switch($part) {
  1312. // day formats
  1313. case self::DAY:
  1314. if (is_numeric($date)) {
  1315. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1316. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1317. }
  1318. #require_once 'Zend/Date/Exception.php';
  1319. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
  1320. break;
  1321. case self::WEEKDAY_SHORT:
  1322. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1323. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1324. $cnt = 0;
  1325. foreach ($daylist as $key => $value) {
  1326. if (strtoupper(iconv_substr($value, 0, 3)) == strtoupper($date)) {
  1327. $found = $cnt;
  1328. break;
  1329. }
  1330. ++$cnt;
  1331. }
  1332. // Weekday found
  1333. if ($cnt < 7) {
  1334. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1335. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1336. }
  1337. // Weekday not found
  1338. #require_once 'Zend/Date/Exception.php';
  1339. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1340. break;
  1341. case self::DAY_SHORT:
  1342. if (is_numeric($date)) {
  1343. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1344. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1345. }
  1346. #require_once 'Zend/Date/Exception.php';
  1347. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
  1348. break;
  1349. case self::WEEKDAY:
  1350. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1351. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1352. $cnt = 0;
  1353. foreach ($daylist as $key => $value) {
  1354. if (strtoupper($value) == strtoupper($date)) {
  1355. $found = $cnt;
  1356. break;
  1357. }
  1358. ++$cnt;
  1359. }
  1360. // Weekday found
  1361. if ($cnt < 7) {
  1362. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1363. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1364. }
  1365. // Weekday not found
  1366. #require_once 'Zend/Date/Exception.php';
  1367. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1368. break;
  1369. case self::WEEKDAY_8601:
  1370. $weekday = (int) $this->get(self::WEEKDAY_8601, $locale);
  1371. if ((intval($date) > 0) and (intval($date) < 8)) {
  1372. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1373. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1374. }
  1375. // Weekday not found
  1376. #require_once 'Zend/Date/Exception.php';
  1377. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1378. break;
  1379. case self::DAY_SUFFIX:
  1380. #require_once 'Zend/Date/Exception.php';
  1381. throw new Zend_Date_Exception('day suffix not supported', $date);
  1382. break;
  1383. case self::WEEKDAY_DIGIT:
  1384. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1385. if (is_numeric($date) and (intval($date) >= 0) and (intval($date) < 7)) {
  1386. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
  1387. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1388. }
  1389. // Weekday not found
  1390. #require_once 'Zend/Date/Exception.php';
  1391. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1392. break;
  1393. case self::DAY_OF_YEAR:
  1394. if (is_numeric($date)) {
  1395. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
  1396. $this->mktime(0, 0, 0, $month, 1 + $day, 1970, true), $hour);
  1397. }
  1398. #require_once 'Zend/Date/Exception.php';
  1399. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
  1400. break;
  1401. case self::WEEKDAY_NARROW:
  1402. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1403. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1404. $cnt = 0;
  1405. foreach ($daylist as $key => $value) {
  1406. if (strtoupper(iconv_substr($value, 0, 1)) == strtoupper($date)) {
  1407. $found = $cnt;
  1408. break;
  1409. }
  1410. ++$cnt;
  1411. }
  1412. // Weekday found
  1413. if ($cnt < 7) {
  1414. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1415. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1416. }
  1417. // Weekday not found
  1418. #require_once 'Zend/Date/Exception.php';
  1419. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1420. break;
  1421. case self::WEEKDAY_NAME:
  1422. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1423. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1424. $cnt = 0;
  1425. foreach ($daylist as $key => $value) {
  1426. if (strtoupper($value) == strtoupper($date)) {
  1427. $found = $cnt;
  1428. break;
  1429. }
  1430. ++$cnt;
  1431. }
  1432. // Weekday found
  1433. if ($cnt < 7) {
  1434. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1435. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1436. }
  1437. // Weekday not found
  1438. #require_once 'Zend/Date/Exception.php';
  1439. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1440. break;
  1441. // week formats
  1442. case self::WEEK:
  1443. if (is_numeric($date)) {
  1444. $week = (int) $this->get(self::WEEK, $locale);
  1445. return $this->_assign($calc, parent::mktime(0, 0, 0, 1, 1 + ($date * 7), 1970, true),
  1446. parent::mktime(0, 0, 0, 1, 1 + ($week * 7), 1970, true), $hour);
  1447. }
  1448. #require_once 'Zend/Date/Exception.php';
  1449. throw new Zend_Date_Exception("invalid date ($date) operand, week expected", $date);
  1450. break;
  1451. // month formats
  1452. case self::MONTH_NAME:
  1453. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  1454. $cnt = 0;
  1455. foreach ($monthlist as $key => $value) {
  1456. if (strtoupper($value) == strtoupper($date)) {
  1457. $found = $key;
  1458. break;
  1459. }
  1460. ++$cnt;
  1461. }
  1462. $date = array_search($date, $monthlist);
  1463. // Monthname found
  1464. if ($cnt < 12) {
  1465. $fixday = 0;
  1466. if ($calc == 'add') {
  1467. $date += $found;
  1468. $calc = 'set';
  1469. if (self::$_options['extend_month'] == false) {
  1470. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1471. if ($parts['mday'] != $day) {
  1472. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1473. }
  1474. }
  1475. } else if ($calc == 'sub') {
  1476. $date = $month - $found;
  1477. $calc = 'set';
  1478. if (self::$_options['extend_month'] == false) {
  1479. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1480. if ($parts['mday'] != $day) {
  1481. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1482. }
  1483. }
  1484. }
  1485. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1486. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1487. }
  1488. // Monthname not found
  1489. #require_once 'Zend/Date/Exception.php';
  1490. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1491. break;
  1492. case self::MONTH:
  1493. if (is_numeric($date)) {
  1494. $fixday = 0;
  1495. if ($calc == 'add') {
  1496. $date += $month;
  1497. $calc = 'set';
  1498. if (self::$_options['extend_month'] == false) {
  1499. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1500. if ($parts['mday'] != $day) {
  1501. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1502. }
  1503. }
  1504. } else if ($calc == 'sub') {
  1505. $date = $month - $date;
  1506. $calc = 'set';
  1507. if (self::$_options['extend_month'] == false) {
  1508. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1509. if ($parts['mday'] != $day) {
  1510. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1511. }
  1512. }
  1513. }
  1514. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1515. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1516. }
  1517. #require_once 'Zend/Date/Exception.php';
  1518. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1519. break;
  1520. case self::MONTH_NAME_SHORT:
  1521. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  1522. $cnt = 0;
  1523. foreach ($monthlist as $key => $value) {
  1524. if (strtoupper($value) == strtoupper($date)) {
  1525. $found = $key;
  1526. break;
  1527. }
  1528. ++$cnt;
  1529. }
  1530. $date = array_search($date, $monthlist);
  1531. // Monthname found
  1532. if ($cnt < 12) {
  1533. $fixday = 0;
  1534. if ($calc == 'add') {
  1535. $date += $found;
  1536. $calc = 'set';
  1537. if (self::$_options['extend_month'] === false) {
  1538. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1539. if ($parts['mday'] != $day) {
  1540. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1541. }
  1542. }
  1543. } else if ($calc == 'sub') {
  1544. $date = $month - $found;
  1545. $calc = 'set';
  1546. if (self::$_options['extend_month'] === false) {
  1547. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1548. if ($parts['mday'] != $day) {
  1549. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1550. }
  1551. }
  1552. }
  1553. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1554. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1555. }
  1556. // Monthname not found
  1557. #require_once 'Zend/Date/Exception.php';
  1558. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1559. break;
  1560. case self::MONTH_SHORT:
  1561. if (is_numeric($date) === true) {
  1562. $fixday = 0;
  1563. if ($calc === 'add') {
  1564. $date += $month;
  1565. $calc = 'set';
  1566. if (self::$_options['extend_month'] === false) {
  1567. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1568. if ($parts['mday'] != $day) {
  1569. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1570. }
  1571. }
  1572. } else if ($calc === 'sub') {
  1573. $date = $month - $date;
  1574. $calc = 'set';
  1575. if (self::$_options['extend_month'] === false) {
  1576. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1577. if ($parts['mday'] != $day) {
  1578. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1579. }
  1580. }
  1581. }
  1582. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1583. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1584. }
  1585. #require_once 'Zend/Date/Exception.php';
  1586. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1587. break;
  1588. case self::MONTH_DAYS:
  1589. #require_once 'Zend/Date/Exception.php';
  1590. throw new Zend_Date_Exception('month days not supported', $date);
  1591. break;
  1592. case self::MONTH_NAME_NARROW:
  1593. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow'));
  1594. $cnt = 0;
  1595. foreach ($monthlist as $key => $value) {
  1596. if (strtoupper($value) === strtoupper($date)) {
  1597. $found = $key;
  1598. break;
  1599. }
  1600. ++$cnt;
  1601. }
  1602. $date = array_search($date, $monthlist);
  1603. // Monthname found
  1604. if ($cnt < 12) {
  1605. $fixday = 0;
  1606. if ($calc === 'add') {
  1607. $date += $found;
  1608. $calc = 'set';
  1609. if (self::$_options['extend_month'] === false) {
  1610. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1611. if ($parts['mday'] != $day) {
  1612. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1613. }
  1614. }
  1615. } else if ($calc === 'sub') {
  1616. $date = $month - $found;
  1617. $calc = 'set';
  1618. if (self::$_options['extend_month'] === false) {
  1619. $parts = $this->getDateParts($this->mktime(0, 0, 0, $date, $day, $year, false));
  1620. if ($parts['mday'] != $day) {
  1621. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1622. }
  1623. }
  1624. }
  1625. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1626. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1627. }
  1628. // Monthname not found
  1629. #require_once 'Zend/Date/Exception.php';
  1630. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1631. break;
  1632. // year formats
  1633. case self::LEAPYEAR:
  1634. #require_once 'Zend/Date/Exception.php';
  1635. throw new Zend_Date_Exception('leap year not supported', $date);
  1636. break;
  1637. case self::YEAR_8601:
  1638. if (is_numeric($date)) {
  1639. if ($calc === 'add') {
  1640. $date += $year;
  1641. $calc = 'set';
  1642. } else if ($calc === 'sub') {
  1643. $date = $year - $date;
  1644. $calc = 'set';
  1645. }
  1646. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1647. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1648. }
  1649. #require_once 'Zend/Date/Exception.php';
  1650. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1651. break;
  1652. case self::YEAR:
  1653. if (is_numeric($date)) {
  1654. if ($calc === 'add') {
  1655. $date += $year;
  1656. $calc = 'set';
  1657. } else if ($calc === 'sub') {
  1658. $date = $year - $date;
  1659. $calc = 'set';
  1660. }
  1661. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1662. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1663. }
  1664. #require_once 'Zend/Date/Exception.php';
  1665. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1666. break;
  1667. case self::YEAR_SHORT:
  1668. if (is_numeric($date)) {
  1669. $date = intval($date);
  1670. if (($calc == 'set') || ($calc == 'cmp')) {
  1671. $date = self::getFullYear($date);
  1672. }
  1673. if ($calc === 'add') {
  1674. $date += $year;
  1675. $calc = 'set';
  1676. } else if ($calc === 'sub') {
  1677. $date = $year - $date;
  1678. $calc = 'set';
  1679. }
  1680. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1681. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1682. }
  1683. #require_once 'Zend/Date/Exception.php';
  1684. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1685. break;
  1686. case self::YEAR_SHORT_8601:
  1687. if (is_numeric($date)) {
  1688. $date = intval($date);
  1689. if (($calc === 'set') || ($calc === 'cmp')) {
  1690. $date = self::getFullYear($date);
  1691. }
  1692. if ($calc === 'add') {
  1693. $date += $year;
  1694. $calc = 'set';
  1695. } else if ($calc === 'sub') {
  1696. $date = $year - $date;
  1697. $calc = 'set';
  1698. }
  1699. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1700. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1701. }
  1702. #require_once 'Zend/Date/Exception.php';
  1703. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1704. break;
  1705. // time formats
  1706. case self::MERIDIEM:
  1707. #require_once 'Zend/Date/Exception.php';
  1708. throw new Zend_Date_Exception('meridiem not supported', $date);
  1709. break;
  1710. case self::SWATCH:
  1711. if (is_numeric($date)) {
  1712. $rest = intval($date);
  1713. $hours = floor($rest * 24 / 1000);
  1714. $rest = $rest - ($hours * 1000 / 24);
  1715. $minutes = floor($rest * 1440 / 1000);
  1716. $rest = $rest - ($minutes * 1000 / 1440);
  1717. $seconds = floor($rest * 86400 / 1000);
  1718. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true),
  1719. $this->mktime($hour, $minute, $second, 1, 1, 1970, true), false);
  1720. }
  1721. #require_once 'Zend/Date/Exception.php';
  1722. throw new Zend_Date_Exception("invalid date ($date) operand, swatchstamp expected", $date);
  1723. break;
  1724. case self::HOUR_SHORT_AM:
  1725. if (is_numeric($date)) {
  1726. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1727. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1728. }
  1729. #require_once 'Zend/Date/Exception.php';
  1730. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1731. break;
  1732. case self::HOUR_SHORT:
  1733. if (is_numeric($date)) {
  1734. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1735. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1736. }
  1737. #require_once 'Zend/Date/Exception.php';
  1738. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1739. break;
  1740. case self::HOUR_AM:
  1741. if (is_numeric($date)) {
  1742. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1743. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1744. }
  1745. #require_once 'Zend/Date/Exception.php';
  1746. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1747. break;
  1748. case self::HOUR:
  1749. if (is_numeric($date)) {
  1750. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1751. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1752. }
  1753. #require_once 'Zend/Date/Exception.php';
  1754. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1755. break;
  1756. case self::MINUTE:
  1757. if (is_numeric($date)) {
  1758. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1759. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1760. }
  1761. #require_once 'Zend/Date/Exception.php';
  1762. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", $date);
  1763. break;
  1764. case self::SECOND:
  1765. if (is_numeric($date)) {
  1766. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1767. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1768. }
  1769. #require_once 'Zend/Date/Exception.php';
  1770. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", $date);
  1771. break;
  1772. case self::MILLISECOND:
  1773. if (is_numeric($date)) {
  1774. switch($calc) {
  1775. case 'set' :
  1776. return $this->setMillisecond($date);
  1777. break;
  1778. case 'add' :
  1779. return $this->addMillisecond($date);
  1780. break;
  1781. case 'sub' :
  1782. return $this->subMillisecond($date);
  1783. break;
  1784. }
  1785. return $this->compareMillisecond($date);
  1786. }
  1787. #require_once 'Zend/Date/Exception.php';
  1788. throw new Zend_Date_Exception("invalid date ($date) operand, milliseconds expected", $date);
  1789. break;
  1790. case self::MINUTE_SHORT:
  1791. if (is_numeric($date)) {
  1792. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1793. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1794. }
  1795. #require_once 'Zend/Date/Exception.php';
  1796. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", $date);
  1797. break;
  1798. case self::SECOND_SHORT:
  1799. if (is_numeric($date)) {
  1800. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1801. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1802. }
  1803. #require_once 'Zend/Date/Exception.php';
  1804. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", $date);
  1805. break;
  1806. // timezone formats
  1807. // break intentionally omitted
  1808. case self::TIMEZONE_NAME:
  1809. case self::TIMEZONE:
  1810. case self::TIMEZONE_SECS:
  1811. #require_once 'Zend/Date/Exception.php';
  1812. throw new Zend_Date_Exception('timezone not supported', $date);
  1813. break;
  1814. case self::DAYLIGHT:
  1815. #require_once 'Zend/Date/Exception.php';
  1816. throw new Zend_Date_Exception('daylight not supported', $date);
  1817. break;
  1818. case self::GMT_DIFF:
  1819. case self::GMT_DIFF_SEP:
  1820. #require_once 'Zend/Date/Exception.php';
  1821. throw new Zend_Date_Exception('gmtdiff not supported', $date);
  1822. break;
  1823. // date strings
  1824. case self::ISO_8601:
  1825. // (-)YYYY-MM-dd
  1826. preg_match('/^(-{0,1}\d{4})-(\d{2})-(\d{2})/', $date, $datematch);
  1827. // (-)YY-MM-dd
  1828. if (empty($datematch)) {
  1829. preg_match('/^(-{0,1}\d{2})-(\d{2})-(\d{2})/', $date, $datematch);
  1830. }
  1831. // (-)YYYYMMdd
  1832. if (empty($datematch)) {
  1833. preg_match('/^(-{0,1}\d{4})(\d{2})(\d{2})/', $date, $datematch);
  1834. }
  1835. // (-)YYMMdd
  1836. if (empty($datematch)) {
  1837. preg_match('/^(-{0,1}\d{2})(\d{2})(\d{2})/', $date, $datematch);
  1838. }
  1839. $tmpdate = $date;
  1840. if (!empty($datematch)) {
  1841. $tmpdate = iconv_substr($date, iconv_strlen($datematch[0]));
  1842. }
  1843. // (T)hh:mm:ss
  1844. preg_match('/[T,\s]{0,1}(\d{2}):(\d{2}):(\d{2})/', $tmpdate, $timematch);
  1845. if (empty($timematch)) {
  1846. preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
  1847. }
  1848. if (empty($datematch) and empty($timematch)) {
  1849. #require_once 'Zend/Date/Exception.php';
  1850. throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", $date);
  1851. }
  1852. if (!empty($timematch)) {
  1853. $tmpdate = iconv_substr($tmpdate, iconv_strlen($timematch[0]));
  1854. }
  1855. if (empty($datematch)) {
  1856. $datematch[1] = 1970;
  1857. $datematch[2] = 1;
  1858. $datematch[3] = 1;
  1859. } else if (iconv_strlen($datematch[1]) == 2) {
  1860. $datematch[1] = self::getFullYear($datematch[1]);
  1861. }
  1862. if (empty($timematch)) {
  1863. $timematch[1] = 0;
  1864. $timematch[2] = 0;
  1865. $timematch[3] = 0;
  1866. }
  1867. if (($calc == 'set') || ($calc == 'cmp')) {
  1868. --$datematch[2];
  1869. --$month;
  1870. --$datematch[3];
  1871. --$day;
  1872. $datematch[1] -= 1970;
  1873. $year -= 1970;
  1874. }
  1875. return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false),
  1876. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1877. break;
  1878. case self::RFC_2822:
  1879. $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);
  1880. if (!$result) {
  1881. #require_once 'Zend/Date/Exception.php';
  1882. throw new Zend_Date_Exception("no RFC 2822 format ($date)", $date);
  1883. }
  1884. $months = $this->_getDigitFromName($match[2]);
  1885. if (($calc == 'set') || ($calc == 'cmp')) {
  1886. --$months;
  1887. --$month;
  1888. --$match[1];
  1889. --$day;
  1890. $match[3] -= 1970;
  1891. $year -= 1970;
  1892. }
  1893. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  1894. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1895. break;
  1896. case self::TIMESTAMP:
  1897. if (is_numeric($date)) {
  1898. return $this->_assign($calc, $date, $this->getUnixTimestamp());
  1899. }
  1900. #require_once 'Zend/Date/Exception.php';
  1901. throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", $date);
  1902. break;
  1903. // additional formats
  1904. // break intentionally omitted
  1905. case self::ERA:
  1906. case self::ERA_NAME:
  1907. #require_once 'Zend/Date/Exception.php';
  1908. throw new Zend_Date_Exception('era not supported', $date);
  1909. break;
  1910. case self::DATES:
  1911. try {
  1912. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  1913. if (($calc == 'set') || ($calc == 'cmp')) {
  1914. --$parsed['month'];
  1915. --$month;
  1916. --$parsed['day'];
  1917. --$day;
  1918. $parsed['year'] -= 1970;
  1919. $year -= 1970;
  1920. }
  1921. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1922. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1923. } catch (Zend_Locale_Exception $e) {
  1924. #require_once 'Zend/Date/Exception.php';
  1925. throw new Zend_Date_Exception($e->getMessage(), $date);
  1926. }
  1927. break;
  1928. case self::DATE_FULL:
  1929. try {
  1930. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  1931. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1932. if (($calc == 'set') || ($calc == 'cmp')) {
  1933. --$parsed['month'];
  1934. --$month;
  1935. --$parsed['day'];
  1936. --$day;
  1937. $parsed['year'] -= 1970;
  1938. $year -= 1970;
  1939. }
  1940. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1941. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1942. } catch (Zend_Locale_Exception $e) {
  1943. #require_once 'Zend/Date/Exception.php';
  1944. throw new Zend_Date_Exception($e->getMessage(), $date);
  1945. }
  1946. break;
  1947. case self::DATE_LONG:
  1948. try {
  1949. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  1950. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1951. if (($calc == 'set') || ($calc == 'cmp')){
  1952. --$parsed['month'];
  1953. --$month;
  1954. --$parsed['day'];
  1955. --$day;
  1956. $parsed['year'] -= 1970;
  1957. $year -= 1970;
  1958. }
  1959. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1960. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1961. } catch (Zend_Locale_Exception $e) {
  1962. #require_once 'Zend/Date/Exception.php';
  1963. throw new Zend_Date_Exception($e->getMessage(), $date);
  1964. }
  1965. break;
  1966. case self::DATE_MEDIUM:
  1967. try {
  1968. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  1969. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1970. if (($calc == 'set') || ($calc == 'cmp')) {
  1971. --$parsed['month'];
  1972. --$month;
  1973. --$parsed['day'];
  1974. --$day;
  1975. $parsed['year'] -= 1970;
  1976. $year -= 1970;
  1977. }
  1978. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1979. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1980. } catch (Zend_Locale_Exception $e) {
  1981. #require_once 'Zend/Date/Exception.php';
  1982. throw new Zend_Date_Exception($e->getMessage(), $date);
  1983. }
  1984. break;
  1985. case self::DATE_SHORT:
  1986. try {
  1987. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  1988. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1989. $parsed['year'] = self::getFullYear($parsed['year']);
  1990. if (($calc == 'set') || ($calc == 'cmp')) {
  1991. --$parsed['month'];
  1992. --$month;
  1993. --$parsed['day'];
  1994. --$day;
  1995. $parsed['year'] -= 1970;
  1996. $year -= 1970;
  1997. }
  1998. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1999. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2000. } catch (Zend_Locale_Exception $e) {
  2001. #require_once 'Zend/Date/Exception.php';
  2002. throw new Zend_Date_Exception($e->getMessage(), $date);
  2003. }
  2004. break;
  2005. case self::TIMES:
  2006. try {
  2007. if ($calc != 'set') {
  2008. $month = 1;
  2009. $day = 1;
  2010. $year = 1970;
  2011. }
  2012. $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  2013. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2014. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2015. } catch (Zend_Locale_Exception $e) {
  2016. #require_once 'Zend/Date/Exception.php';
  2017. throw new Zend_Date_Exception($e->getMessage(), $date);
  2018. }
  2019. break;
  2020. case self::TIME_FULL:
  2021. try {
  2022. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  2023. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2024. if ($calc != 'set') {
  2025. $month = 1;
  2026. $day = 1;
  2027. $year = 1970;
  2028. }
  2029. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], 0, $month, $day, $year, true),
  2030. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2031. } catch (Zend_Locale_Exception $e) {
  2032. #require_once 'Zend/Date/Exception.php';
  2033. throw new Zend_Date_Exception($e->getMessage(), $date);
  2034. }
  2035. break;
  2036. case self::TIME_LONG:
  2037. try {
  2038. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
  2039. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2040. if ($calc != 'set') {
  2041. $month = 1;
  2042. $day = 1;
  2043. $year = 1970;
  2044. }
  2045. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2046. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2047. } catch (Zend_Locale_Exception $e) {
  2048. #require_once 'Zend/Date/Exception.php';
  2049. throw new Zend_Date_Exception($e->getMessage(), $date);
  2050. }
  2051. break;
  2052. case self::TIME_MEDIUM:
  2053. try {
  2054. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
  2055. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2056. if ($calc != 'set') {
  2057. $month = 1;
  2058. $day = 1;
  2059. $year = 1970;
  2060. }
  2061. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2062. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2063. } catch (Zend_Locale_Exception $e) {
  2064. #require_once 'Zend/Date/Exception.php';
  2065. throw new Zend_Date_Exception($e->getMessage(), $date);
  2066. }
  2067. break;
  2068. case self::TIME_SHORT:
  2069. try {
  2070. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
  2071. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2072. if ($calc != 'set') {
  2073. $month = 1;
  2074. $day = 1;
  2075. $year = 1970;
  2076. }
  2077. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], 0, $month, $day, $year, true),
  2078. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2079. } catch (Zend_Locale_Exception $e) {
  2080. #require_once 'Zend/Date/Exception.php';
  2081. throw new Zend_Date_Exception($e->getMessage(), $date);
  2082. }
  2083. break;
  2084. // ATOM and RFC_3339 are identical
  2085. case self::ATOM:
  2086. case self::RFC_3339:
  2087. $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);
  2088. if (!$result) {
  2089. #require_once 'Zend/Date/Exception.php';
  2090. throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", $date);
  2091. }
  2092. if (($calc == 'set') || ($calc == 'cmp')) {
  2093. --$match[2];
  2094. --$month;
  2095. --$match[3];
  2096. --$day;
  2097. $match[1] -= 1970;
  2098. $year -= 1970;
  2099. }
  2100. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2101. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2102. break;
  2103. case self::COOKIE:
  2104. $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);
  2105. if (!$result) {
  2106. #require_once 'Zend/Date/Exception.php';
  2107. throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", $date);
  2108. }
  2109. $match[0] = iconv_substr($match[0], iconv_strpos($match[0], ' ')+1);
  2110. $months = $this->_getDigitFromName($match[2]);
  2111. $match[3] = self::getFullYear($match[3]);
  2112. if (($calc == 'set') || ($calc == 'cmp')) {
  2113. --$months;
  2114. --$month;
  2115. --$match[1];
  2116. --$day;
  2117. $match[3] -= 1970;
  2118. $year -= 1970;
  2119. }
  2120. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2121. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2122. break;
  2123. case self::RFC_822:
  2124. case self::RFC_1036:
  2125. // new RFC 822 format, identical to RFC 1036 standard
  2126. $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);
  2127. if (!$result) {
  2128. #require_once 'Zend/Date/Exception.php';
  2129. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", $date);
  2130. }
  2131. $months = $this->_getDigitFromName($match[2]);
  2132. $match[3] = self::getFullYear($match[3]);
  2133. if (($calc == 'set') || ($calc == 'cmp')) {
  2134. --$months;
  2135. --$month;
  2136. --$match[1];
  2137. --$day;
  2138. $match[3] -= 1970;
  2139. $year -= 1970;
  2140. }
  2141. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  2142. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  2143. break;
  2144. case self::RFC_850:
  2145. $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);
  2146. if (!$result) {
  2147. #require_once 'Zend/Date/Exception.php';
  2148. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", $date);
  2149. }
  2150. $months = $this->_getDigitFromName($match[2]);
  2151. $match[3] = self::getFullYear($match[3]);
  2152. if (($calc == 'set') || ($calc == 'cmp')) {
  2153. --$months;
  2154. --$month;
  2155. --$match[1];
  2156. --$day;
  2157. $match[3] -= 1970;
  2158. $year -= 1970;
  2159. }
  2160. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2161. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2162. break;
  2163. case self::RFC_1123:
  2164. $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);
  2165. if (!$result) {
  2166. #require_once 'Zend/Date/Exception.php';
  2167. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", $date);
  2168. }
  2169. $months = $this->_getDigitFromName($match[2]);
  2170. if (($calc == 'set') || ($calc == 'cmp')) {
  2171. --$months;
  2172. --$month;
  2173. --$match[1];
  2174. --$day;
  2175. $match[3] -= 1970;
  2176. $year -= 1970;
  2177. }
  2178. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2179. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2180. break;
  2181. case self::RSS:
  2182. $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);
  2183. if (!$result) {
  2184. #require_once 'Zend/Date/Exception.php';
  2185. throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", $date);
  2186. }
  2187. $months = $this->_getDigitFromName($match[2]);
  2188. $match[3] = self::getFullYear($match[3]);
  2189. if (($calc == 'set') || ($calc == 'cmp')) {
  2190. --$months;
  2191. --$month;
  2192. --$match[1];
  2193. --$day;
  2194. $match[3] -= 1970;
  2195. $year -= 1970;
  2196. }
  2197. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2198. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2199. break;
  2200. case self::W3C:
  2201. $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
  2202. if (!$result) {
  2203. #require_once 'Zend/Date/Exception.php';
  2204. throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", $date);
  2205. }
  2206. if (($calc == 'set') || ($calc == 'cmp')) {
  2207. --$match[2];
  2208. --$month;
  2209. --$match[3];
  2210. --$day;
  2211. $match[1] -= 1970;
  2212. $year -= 1970;
  2213. }
  2214. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2215. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2216. break;
  2217. default:
  2218. if (!is_numeric($date) || !empty($part)) {
  2219. try {
  2220. if (self::$_options['format_type'] == 'php') {
  2221. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  2222. }
  2223. if (empty($part)) {
  2224. $part = Zend_Locale_Format::getDateFormat($locale) . " ";
  2225. $part .= Zend_Locale_Format::getTimeFormat($locale);
  2226. }
  2227. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
  2228. if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
  2229. $parsed['year'] = self::getFullYear($parsed['year']);
  2230. }
  2231. if (($calc == 'set') || ($calc == 'cmp')) {
  2232. if (isset($parsed['month'])) {
  2233. --$parsed['month'];
  2234. } else {
  2235. $parsed['month'] = 0;
  2236. }
  2237. if (isset($parsed['day'])) {
  2238. --$parsed['day'];
  2239. } else {
  2240. $parsed['day'] = 0;
  2241. }
  2242. if (isset($parsed['year'])) {
  2243. $parsed['year'] -= 1970;
  2244. } else {
  2245. $parsed['year'] = 0;
  2246. }
  2247. }
  2248. return $this->_assign($calc, $this->mktime(
  2249. isset($parsed['hour']) ? $parsed['hour'] : 0,
  2250. isset($parsed['minute']) ? $parsed['minute'] : 0,
  2251. isset($parsed['second']) ? $parsed['second'] : 0,
  2252. 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'],
  2253. false), $this->getUnixTimestamp(), false);
  2254. } catch (Zend_Locale_Exception $e) {
  2255. if (!is_numeric($date)) {
  2256. #require_once 'Zend/Date/Exception.php';
  2257. throw new Zend_Date_Exception($e->getMessage(), $date);
  2258. }
  2259. }
  2260. }
  2261. return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
  2262. break;
  2263. }
  2264. }
  2265. /**
  2266. * Returns true when both date objects or date parts are equal.
  2267. * For example:
  2268. * 15.May.2000 <-> 15.June.2000 Equals only for Day or Year... all other will return false
  2269. *
  2270. * @param string|integer|array|Zend_Date $date Date or datepart to equal with
  2271. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2272. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2273. * @return boolean
  2274. * @throws Zend_Date_Exception
  2275. */
  2276. public function equals($date, $part = null, $locale = null)
  2277. {
  2278. $result = $this->compare($date, $part, $locale);
  2279. if ($result == 0) {
  2280. return true;
  2281. }
  2282. return false;
  2283. }
  2284. /**
  2285. * Returns if the given date or datepart is earlier
  2286. * For example:
  2287. * 15.May.2000 <-> 13.June.1999 will return true for day, year and date, but not for month
  2288. *
  2289. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2290. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2291. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2292. * @return boolean
  2293. * @throws Zend_Date_Exception
  2294. */
  2295. public function isEarlier($date, $part = null, $locale = null)
  2296. {
  2297. $result = $this->compare($date, $part, $locale);
  2298. if ($result == -1) {
  2299. return true;
  2300. }
  2301. return false;
  2302. }
  2303. /**
  2304. * Returns if the given date or datepart is later
  2305. * For example:
  2306. * 15.May.2000 <-> 13.June.1999 will return true for month but false for day, year and date
  2307. * Returns if the given date is later
  2308. *
  2309. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2310. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2311. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2312. * @return boolean
  2313. * @throws Zend_Date_Exception
  2314. */
  2315. public function isLater($date, $part = null, $locale = null)
  2316. {
  2317. $result = $this->compare($date, $part, $locale);
  2318. if ($result == 1) {
  2319. return true;
  2320. }
  2321. return false;
  2322. }
  2323. /**
  2324. * Returns only the time of the date as new Zend_Date object
  2325. * For example:
  2326. * 15.May.2000 10:11:23 will return a dateobject equal to 01.Jan.1970 10:11:23
  2327. *
  2328. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2329. * @return Zend_Date
  2330. */
  2331. public function getTime($locale = null)
  2332. {
  2333. return $this->copyPart(self::TIME_MEDIUM, $locale);
  2334. }
  2335. /**
  2336. * Returns the calculated time
  2337. *
  2338. * @param string $calc Calculation to make
  2339. * @param string|integer|array|Zend_Date $time Time to calculate with, if null the actual time is taken
  2340. * @param string $format Timeformat for parsing input
  2341. * @param string|Zend_Locale $locale Locale for parsing input
  2342. * @return integer|Zend_Date new time
  2343. * @throws Zend_Date_Exception
  2344. */
  2345. private function _time($calc, $time, $format, $locale)
  2346. {
  2347. if (is_null($time)) {
  2348. #require_once 'Zend/Date/Exception.php';
  2349. throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
  2350. }
  2351. if ($locale === null) {
  2352. $locale = $this->getLocale();
  2353. }
  2354. if ($time instanceof Zend_Date) {
  2355. // extract time from object
  2356. $time = $time->get(self::TIME_MEDIUM, $locale);
  2357. } else {
  2358. if (is_array($time)) {
  2359. if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
  2360. (isset($time['second']) === true)) {
  2361. $parsed = $time;
  2362. } else {
  2363. #require_once 'Zend/Date/Exception.php';
  2364. throw new Zend_Date_Exception("no hour, minute or second given in array");
  2365. }
  2366. } else {
  2367. if (self::$_options['format_type'] == 'php') {
  2368. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2369. }
  2370. try {
  2371. $parsed = Zend_Locale_Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2372. } catch (Zend_Locale_Exception $e) {
  2373. #require_once 'Zend/Date/Exception.php';
  2374. throw new Zend_Date_Exception($e->getMessage());
  2375. }
  2376. }
  2377. $time = new self(0, self::TIMESTAMP, $locale);
  2378. $time->setTimezone('UTC');
  2379. $time->set($parsed['hour'], self::HOUR);
  2380. $time->set($parsed['minute'], self::MINUTE);
  2381. $time->set($parsed['second'], self::SECOND);
  2382. $time = $time->get(self::TIME_MEDIUM, $locale);
  2383. }
  2384. $return = $this->_calcdetail($calc, $time, self::TIME_MEDIUM, $locale);
  2385. if ($calc != 'cmp') {
  2386. return $this;
  2387. }
  2388. return $return;
  2389. }
  2390. /**
  2391. * Sets a new time for the date object. Format defines how to parse the time string.
  2392. * Also a complete date can be given, but only the time is used for setting.
  2393. * For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
  2394. * Returned is the new date object and the existing date is left as it was before
  2395. *
  2396. * @param string|integer|array|Zend_Date $time Time to set
  2397. * @param string $format OPTIONAL Timeformat for parsing input
  2398. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2399. * @return Zend_Date new time
  2400. * @throws Zend_Date_Exception
  2401. */
  2402. public function setTime($time, $format = null, $locale = null)
  2403. {
  2404. return $this->_time('set', $time, $format, $locale);
  2405. }
  2406. /**
  2407. * Adds a time to the existing date. Format defines how to parse the time string.
  2408. * If only parts are given the other parts are set to 0.
  2409. * If no format is given, the standardformat of this locale is used.
  2410. * For example: HH:mm:ss -> 10 -> +10 hours
  2411. *
  2412. * @param string|integer|array|Zend_Date $time Time to add
  2413. * @param string $format OPTIONAL Timeformat for parsing input
  2414. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2415. * @return Zend_Date new time
  2416. * @throws Zend_Date_Exception
  2417. */
  2418. public function addTime($time, $format = null, $locale = null)
  2419. {
  2420. return $this->_time('add', $time, $format, $locale);
  2421. }
  2422. /**
  2423. * Subtracts a time from the existing date. Format defines how to parse the time string.
  2424. * If only parts are given the other parts are set to 0.
  2425. * If no format is given, the standardformat of this locale is used.
  2426. * For example: HH:mm:ss -> 10 -> -10 hours
  2427. *
  2428. * @param string|integer|array|Zend_Date $time Time to sub
  2429. * @param string $format OPTIONAL Timeformat for parsing input
  2430. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2431. * @return Zend_Date new time
  2432. * @throws Zend_Date_Exception
  2433. */
  2434. public function subTime($time, $format = null, $locale = null)
  2435. {
  2436. return $this->_time('sub', $time, $format, $locale);
  2437. }
  2438. /**
  2439. * Compares the time from the existing date. Format defines how to parse the time string.
  2440. * If only parts are given the other parts are set to default.
  2441. * If no format us given, the standardformat of this locale is used.
  2442. * For example: HH:mm:ss -> 10 -> 10 hours
  2443. *
  2444. * @param string|integer|array|Zend_Date $time Time to compare
  2445. * @param string $format OPTIONAL Timeformat for parsing input
  2446. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2447. * @return integer 0 = equal, 1 = later, -1 = earlier
  2448. * @throws Zend_Date_Exception
  2449. */
  2450. public function compareTime($time, $format = null, $locale = null)
  2451. {
  2452. return $this->_time('cmp', $time, $format, $locale);
  2453. }
  2454. /**
  2455. * Returns a clone of $this, with the time part set to 00:00:00.
  2456. *
  2457. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2458. * @return Zend_Date
  2459. */
  2460. public function getDate($locale = null)
  2461. {
  2462. $date = $this->copyPart(self::DATE_FULL, $locale);
  2463. $date->addTimestamp($this->getGmtOffset());
  2464. return $date;
  2465. }
  2466. /**
  2467. * Returns the calculated date
  2468. *
  2469. * @param string $calc Calculation to make
  2470. * @param string|integer|array|Zend_Date $date Date to calculate with, if null the actual date is taken
  2471. * @param string $format Date format for parsing
  2472. * @param string|Zend_Locale $locale Locale for parsing input
  2473. * @return integer|Zend_Date new date
  2474. * @throws Zend_Date_Exception
  2475. */
  2476. private function _date($calc, $date, $format, $locale)
  2477. {
  2478. if (is_null($date)) {
  2479. #require_once 'Zend/Date/Exception.php';
  2480. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  2481. }
  2482. if ($locale === null) {
  2483. $locale = $this->getLocale();
  2484. }
  2485. if ($date instanceof Zend_Date) {
  2486. // extract date from object
  2487. $date = $date->get(self::DATE_FULL, $locale);
  2488. } else {
  2489. if (is_array($date)) {
  2490. if ((isset($time['year']) === true) or (isset($time['month']) === true) or
  2491. (isset($time['day']) === true)) {
  2492. $parsed = $time;
  2493. } else {
  2494. #require_once 'Zend/Date/Exception.php';
  2495. throw new Zend_Date_Exception("no day,month or year given in array");
  2496. }
  2497. } else {
  2498. if (self::$_options['format_type'] == 'php') {
  2499. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2500. }
  2501. try {
  2502. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2503. if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
  2504. $parsed['year'] = self::getFullYear($parsed['year']);
  2505. }
  2506. } catch (Zend_Locale_Exception $e) {
  2507. #require_once 'Zend/Date/Exception.php';
  2508. throw new Zend_Date_Exception($e->getMessage());
  2509. }
  2510. }
  2511. $date = new self(0, self::TIMESTAMP, $locale);
  2512. $date->setTimezone('UTC');
  2513. $date->set($parsed['year'], self::YEAR);
  2514. $date->set($parsed['month'], self::MONTH);
  2515. $date->set($parsed['day'], self::DAY);
  2516. $date = $date->get(self::DATE_FULL, $locale);
  2517. }
  2518. $return = $this->_calcdetail($calc, $date, self::DATE_FULL, $locale);
  2519. if ($calc != 'cmp') {
  2520. return $this;
  2521. }
  2522. return $return;
  2523. }
  2524. /**
  2525. * Sets a new date for the date object. Format defines how to parse the date string.
  2526. * Also a complete date with time can be given, but only the date is used for setting.
  2527. * For example: MMMM.yy HH:mm-> May.07 22:11 => 01.May.07 00:00
  2528. * Returned is the new date object and the existing time is left as it was before
  2529. *
  2530. * @param string|integer|array|Zend_Date $date Date to set
  2531. * @param string $format OPTIONAL Date format for parsing
  2532. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2533. * @return integer|Zend_Date new date
  2534. * @throws Zend_Date_Exception
  2535. */
  2536. public function setDate($date, $format = null, $locale = null)
  2537. {
  2538. return $this->_date('set', $date, $format, $locale);
  2539. }
  2540. /**
  2541. * Adds a date to the existing date object. Format defines how to parse the date string.
  2542. * If only parts are given the other parts are set to 0.
  2543. * If no format is given, the standardformat of this locale is used.
  2544. * For example: MM.dd.YYYY -> 10 -> +10 months
  2545. *
  2546. * @param string|integer|array|Zend_Date $date Date to add
  2547. * @param string $format OPTIONAL Date format for parsing input
  2548. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2549. * @return Zend_Date new date
  2550. * @throws Zend_Date_Exception
  2551. */
  2552. public function addDate($date, $format = null, $locale = null)
  2553. {
  2554. return $this->_date('add', $date, $format, $locale);
  2555. }
  2556. /**
  2557. * Subtracts a date from the existing date object. Format defines how to parse the date string.
  2558. * If only parts are given the other parts are set to 0.
  2559. * If no format is given, the standardformat of this locale is used.
  2560. * For example: MM.dd.YYYY -> 10 -> -10 months
  2561. * Be aware: Subtracting 2 months is not equal to Adding -2 months !!!
  2562. *
  2563. * @param string|integer|array|Zend_Date $date Date to sub
  2564. * @param string $format OPTIONAL Date format for parsing input
  2565. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2566. * @return Zend_Date new date
  2567. * @throws Zend_Date_Exception
  2568. */
  2569. public function subDate($date, $format = null, $locale = null)
  2570. {
  2571. return $this->_date('sub', $date, $format, $locale);
  2572. }
  2573. /**
  2574. * Compares the date from the existing date object, ignoring the time.
  2575. * Format defines how to parse the date string.
  2576. * If only parts are given the other parts are set to 0.
  2577. * If no format is given, the standardformat of this locale is used.
  2578. * For example: 10.01.2000 => 10.02.1999 -> false
  2579. *
  2580. * @param string|integer|array|Zend_Date $date Date to compare
  2581. * @param string $format OPTIONAL Date format for parsing input
  2582. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2583. * @return Zend_Date new date
  2584. * @throws Zend_Date_Exception
  2585. */
  2586. public function compareDate($date, $format = null, $locale = null)
  2587. {
  2588. return $this->_date('cmp', $date, $format, $locale);
  2589. }
  2590. /**
  2591. * Returns the full ISO 8601 date from the date object.
  2592. * Always the complete ISO 8601 specifiction is used. If an other ISO date is needed
  2593. * (ISO 8601 defines several formats) use toString() instead.
  2594. * This function does not return the ISO date as object. Use copy() instead.
  2595. *
  2596. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2597. * @return string
  2598. */
  2599. public function getIso($locale = null)
  2600. {
  2601. return $this->get(self::ISO_8601, $locale);
  2602. }
  2603. /**
  2604. * Sets a new date for the date object. Not given parts are set to default.
  2605. * Only supported ISO 8601 formats are accepted.
  2606. * For example: 050901 -> 01.Sept.2005 00:00:00, 20050201T10:00:30 -> 01.Feb.2005 10h00m30s
  2607. * Returned is the new date object
  2608. *
  2609. * @param string|integer|Zend_Date $date ISO Date to set
  2610. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2611. * @return integer|Zend_Date new date
  2612. * @throws Zend_Date_Exception
  2613. */
  2614. public function setIso($date, $locale = null)
  2615. {
  2616. return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
  2617. }
  2618. /**
  2619. * Adds a ISO date to the date object. Not given parts are set to default.
  2620. * Only supported ISO 8601 formats are accepted.
  2621. * For example: 050901 -> + 01.Sept.2005 00:00:00, 10:00:00 -> +10h
  2622. * Returned is the new date object
  2623. *
  2624. * @param string|integer|Zend_Date $date ISO Date to add
  2625. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2626. * @return integer|Zend_Date new date
  2627. * @throws Zend_Date_Exception
  2628. */
  2629. public function addIso($date, $locale = null)
  2630. {
  2631. return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
  2632. }
  2633. /**
  2634. * Subtracts a ISO date from the date object. Not given parts are set to default.
  2635. * Only supported ISO 8601 formats are accepted.
  2636. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2637. * Returned is the new date object
  2638. *
  2639. * @param string|integer|Zend_Date $date ISO Date to sub
  2640. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2641. * @return integer|Zend_Date new date
  2642. * @throws Zend_Date_Exception
  2643. */
  2644. public function subIso($date, $locale = null)
  2645. {
  2646. return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
  2647. }
  2648. /**
  2649. * Compares a ISO date with the date object. Not given parts are set to default.
  2650. * Only supported ISO 8601 formats are accepted.
  2651. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2652. * Returns if equal, earlier or later
  2653. *
  2654. * @param string|integer|Zend_Date $date ISO Date to sub
  2655. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2656. * @return integer 0 = equal, 1 = later, -1 = earlier
  2657. * @throws Zend_Date_Exception
  2658. */
  2659. public function compareIso($date, $locale = null)
  2660. {
  2661. return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
  2662. }
  2663. /**
  2664. * Returns a RFC 822 compilant datestring from the date object.
  2665. * This function does not return the RFC date as object. Use copy() instead.
  2666. *
  2667. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2668. * @return string
  2669. */
  2670. public function getArpa($locale = null)
  2671. {
  2672. return $this->get(self::RFC_822, $locale);
  2673. }
  2674. /**
  2675. * Sets a RFC 822 date as new date for the date object.
  2676. * Only RFC 822 compilant date strings are accepted.
  2677. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2678. * Returned is the new date object
  2679. *
  2680. * @param string|integer|Zend_Date $date RFC 822 to set
  2681. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2682. * @return integer|Zend_Date new date
  2683. * @throws Zend_Date_Exception
  2684. */
  2685. public function setArpa($date, $locale = null)
  2686. {
  2687. return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
  2688. }
  2689. /**
  2690. * Adds a RFC 822 date to the date object.
  2691. * ARPA messages are used in emails or HTTP Headers.
  2692. * Only RFC 822 compilant date strings are accepted.
  2693. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2694. * Returned is the new date object
  2695. *
  2696. * @param string|integer|Zend_Date $date RFC 822 Date to add
  2697. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2698. * @return integer|Zend_Date new date
  2699. * @throws Zend_Date_Exception
  2700. */
  2701. public function addArpa($date, $locale = null)
  2702. {
  2703. return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
  2704. }
  2705. /**
  2706. * Subtracts a RFC 822 date from the date object.
  2707. * ARPA messages are used in emails or HTTP Headers.
  2708. * Only RFC 822 compilant date strings are accepted.
  2709. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2710. * Returned is the new date object
  2711. *
  2712. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2713. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2714. * @return integer|Zend_Date new date
  2715. * @throws Zend_Date_Exception
  2716. */
  2717. public function subArpa($date, $locale = null)
  2718. {
  2719. return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
  2720. }
  2721. /**
  2722. * Compares a RFC 822 compilant date with the date object.
  2723. * ARPA messages are used in emails or HTTP Headers.
  2724. * Only RFC 822 compilant date strings are accepted.
  2725. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2726. * Returns if equal, earlier or later
  2727. *
  2728. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2729. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2730. * @return integer 0 = equal, 1 = later, -1 = earlier
  2731. * @throws Zend_Date_Exception
  2732. */
  2733. public function compareArpa($date, $locale = null)
  2734. {
  2735. return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
  2736. }
  2737. /**
  2738. * Check if location is supported
  2739. *
  2740. * @param $location array - locations array
  2741. * @return $horizon float
  2742. */
  2743. private function _checkLocation($location)
  2744. {
  2745. if (!isset($location['longitude']) or !isset($location['latitude'])) {
  2746. #require_once 'Zend/Date/Exception.php';
  2747. throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', $location);
  2748. }
  2749. if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
  2750. #require_once 'Zend/Date/Exception.php';
  2751. throw new Zend_Date_Exception('Longitude must be between -180 and 180', $location);
  2752. }
  2753. if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
  2754. #require_once 'Zend/Date/Exception.php';
  2755. throw new Zend_Date_Exception('Latitude must be between -90 and 90', $location);
  2756. }
  2757. if (!isset($location['horizon'])){
  2758. $location['horizon'] = 'effective';
  2759. }
  2760. switch ($location['horizon']) {
  2761. case 'civil' :
  2762. return -0.104528;
  2763. break;
  2764. case 'nautic' :
  2765. return -0.207912;
  2766. break;
  2767. case 'astronomic' :
  2768. return -0.309017;
  2769. break;
  2770. default :
  2771. return -0.0145439;
  2772. break;
  2773. }
  2774. }
  2775. /**
  2776. * Returns the time of sunrise for this date and a given location as new date object
  2777. * For a list of cities and correct locations use the class Zend_Date_Cities
  2778. *
  2779. * @param $location array - location of sunrise
  2780. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2781. * ['longitude'] -> longitude of location
  2782. * ['latitude'] -> latitude of location
  2783. * @return Zend_Date
  2784. * @throws Zend_Date_Exception
  2785. */
  2786. public function getSunrise($location)
  2787. {
  2788. $horizon = $this->_checkLocation($location);
  2789. $result = clone $this;
  2790. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2791. return $result;
  2792. }
  2793. /**
  2794. * Returns the time of sunset for this date and a given location as new date object
  2795. * For a list of cities and correct locations use the class Zend_Date_Cities
  2796. *
  2797. * @param $location array - location of sunset
  2798. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2799. * ['longitude'] -> longitude of location
  2800. * ['latitude'] -> latitude of location
  2801. * @return Zend_Date
  2802. * @throws Zend_Date_Exception
  2803. */
  2804. public function getSunset($location)
  2805. {
  2806. $horizon = $this->_checkLocation($location);
  2807. $result = clone $this;
  2808. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2809. return $result;
  2810. }
  2811. /**
  2812. * Returns an array with the sunset and sunrise dates for all horizon types
  2813. * For a list of cities and correct locations use the class Zend_Date_Cities
  2814. *
  2815. * @param $location array - location of suninfo
  2816. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2817. * ['longitude'] -> longitude of location
  2818. * ['latitude'] -> latitude of location
  2819. * @return array - [sunset|sunrise][effective|civil|nautic|astronomic]
  2820. * @throws Zend_Date_Exception
  2821. */
  2822. public function getSunInfo($location)
  2823. {
  2824. $suninfo = array();
  2825. for ($i = 0; $i < 4; ++$i) {
  2826. switch ($i) {
  2827. case 0 :
  2828. $location['horizon'] = 'effective';
  2829. break;
  2830. case 1 :
  2831. $location['horizon'] = 'civil';
  2832. break;
  2833. case 2 :
  2834. $location['horizon'] = 'nautic';
  2835. break;
  2836. case 3 :
  2837. $location['horizon'] = 'astronomic';
  2838. break;
  2839. }
  2840. $horizon = $this->_checkLocation($location);
  2841. $result = clone $this;
  2842. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2843. $suninfo['sunrise'][$location['horizon']] = $result;
  2844. $result = clone $this;
  2845. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2846. $suninfo['sunset'][$location['horizon']] = $result;
  2847. }
  2848. return $suninfo;
  2849. }
  2850. /**
  2851. * Check a given year for leap year.
  2852. *
  2853. * @param integer|array|Zend_Date $year Year to check
  2854. * @return boolean
  2855. */
  2856. public static function checkLeapYear($year)
  2857. {
  2858. if ($year instanceof Zend_Date) {
  2859. $year = (int) $year->get(self::YEAR);
  2860. }
  2861. if (is_array($year)) {
  2862. if (isset($year['year']) === true) {
  2863. $year = $year['year'];
  2864. } else {
  2865. #require_once 'Zend/Date/Exception.php';
  2866. throw new Zend_Date_Exception("no year given in array");
  2867. }
  2868. }
  2869. if (!is_numeric($year)) {
  2870. #require_once 'Zend/Date/Exception.php';
  2871. throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", $year);
  2872. }
  2873. return (bool) parent::isYearLeapYear($year);
  2874. }
  2875. /**
  2876. * Returns true, if the year is a leap year.
  2877. *
  2878. * @return boolean
  2879. */
  2880. public function isLeapYear()
  2881. {
  2882. return self::checkLeapYear($this);
  2883. }
  2884. /**
  2885. * Returns if the set date is todays date
  2886. *
  2887. * @return boolean
  2888. */
  2889. public function isToday()
  2890. {
  2891. $today = $this->date('Ymd', $this->_getTime());
  2892. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2893. return ($today == $day);
  2894. }
  2895. /**
  2896. * Returns if the set date is yesterdays date
  2897. *
  2898. * @return boolean
  2899. */
  2900. public function isYesterday()
  2901. {
  2902. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  2903. // adjusts for leap days and DST changes that are timezone specific
  2904. $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
  2905. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2906. return $day == $yesterday;
  2907. }
  2908. /**
  2909. * Returns if the set date is tomorrows date
  2910. *
  2911. * @return boolean
  2912. */
  2913. public function isTomorrow()
  2914. {
  2915. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  2916. // adjusts for leap days and DST changes that are timezone specific
  2917. $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
  2918. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2919. return $day == $tomorrow;
  2920. }
  2921. /**
  2922. * Returns the actual date as new date object
  2923. *
  2924. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2925. * @return Zend_Date
  2926. */
  2927. public static function now($locale = null)
  2928. {
  2929. return new Zend_Date(time(), self::TIMESTAMP, $locale);
  2930. }
  2931. /**
  2932. * Calculate date details
  2933. *
  2934. * @param string $calc Calculation to make
  2935. * @param string|integer|array|Zend_Date $date Date or Part to calculate
  2936. * @param string $part Datepart for Calculation
  2937. * @param string|Zend_Locale $locale Locale for parsing input
  2938. * @return integer|string new date
  2939. * @throws Zend_Date_Exception
  2940. */
  2941. private function _calcdetail($calc, $date, $type, $locale)
  2942. {
  2943. switch($calc) {
  2944. case 'set' :
  2945. return $this->set($date, $type, $locale);
  2946. break;
  2947. case 'add' :
  2948. return $this->add($date, $type, $locale);
  2949. break;
  2950. case 'sub' :
  2951. return $this->sub($date, $type, $locale);
  2952. break;
  2953. }
  2954. return $this->compare($date, $type, $locale);
  2955. }
  2956. /**
  2957. * Internal calculation, returns the requested date type
  2958. *
  2959. * @param string $calc Calculation to make
  2960. * @param string|integer|Zend_Date $value Datevalue to calculate with, if null the actual value is taken
  2961. * @param string|Zend_Locale $locale Locale for parsing input
  2962. * @return integer|Zend_Date new date
  2963. * @throws Zend_Date_Exception
  2964. */
  2965. private function _calcvalue($calc, $value, $type, $parameter, $locale)
  2966. {
  2967. if (is_null($value)) {
  2968. #require_once 'Zend/Date/Exception.php';
  2969. throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
  2970. }
  2971. if ($locale === null) {
  2972. $locale = $this->getLocale();
  2973. }
  2974. if ($value instanceof Zend_Date) {
  2975. // extract value from object
  2976. $value = $value->get($parameter, $locale);
  2977. } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
  2978. #require_once 'Zend/Date/Exception.php';
  2979. throw new Zend_Date_Exception("invalid $type ($value) operand", $value);
  2980. }
  2981. $return = $this->_calcdetail($calc, $value, $parameter, $locale);
  2982. if ($calc != 'cmp') {
  2983. return $this;
  2984. }
  2985. return $return;
  2986. }
  2987. /**
  2988. * Returns only the year from the date object as new object.
  2989. * For example: 10.May.2000 10:30:00 -> 01.Jan.2000 00:00:00
  2990. *
  2991. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2992. * @return Zend_Date
  2993. */
  2994. public function getYear($locale = null)
  2995. {
  2996. return $this->copyPart(self::YEAR, $locale);
  2997. }
  2998. /**
  2999. * Sets a new year
  3000. * If the year is between 0 and 69, 2000 will be set (2000-2069)
  3001. * If the year if between 70 and 99, 1999 will be set (1970-1999)
  3002. * 3 or 4 digit years are set as expected. If you need to set year 0-99
  3003. * use set() instead.
  3004. * Returned is the new date object
  3005. *
  3006. * @param string|integer|array|Zend_Date $date Year to set
  3007. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3008. * @return Zend_Date new date
  3009. * @throws Zend_Date_Exception
  3010. */
  3011. public function setYear($year, $locale = null)
  3012. {
  3013. return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
  3014. }
  3015. /**
  3016. * Adds the year to the existing date object
  3017. * If the year is between 0 and 69, 2000 will be added (2000-2069)
  3018. * If the year if between 70 and 99, 1999 will be added (1970-1999)
  3019. * 3 or 4 digit years are added as expected. If you need to add years from 0-99
  3020. * use add() instead.
  3021. * Returned is the new date object
  3022. *
  3023. * @param string|integer|array|Zend_Date $date Year to add
  3024. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3025. * @return Zend_Date new date
  3026. * @throws Zend_Date_Exception
  3027. */
  3028. public function addYear($year, $locale = null)
  3029. {
  3030. return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
  3031. }
  3032. /**
  3033. * Subs the year from the existing date object
  3034. * If the year is between 0 and 69, 2000 will be subtracted (2000-2069)
  3035. * If the year if between 70 and 99, 1999 will be subtracted (1970-1999)
  3036. * 3 or 4 digit years are subtracted as expected. If you need to subtract years from 0-99
  3037. * use sub() instead.
  3038. * Returned is the new date object
  3039. *
  3040. * @param string|integer|array|Zend_Date $date Year to sub
  3041. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3042. * @return Zend_Date new date
  3043. * @throws Zend_Date_Exception
  3044. */
  3045. public function subYear($year, $locale = null)
  3046. {
  3047. return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
  3048. }
  3049. /**
  3050. * Compares the year with the existing date object, ignoring other date parts.
  3051. * For example: 10.03.2000 -> 15.02.2000 -> true
  3052. * Returns if equal, earlier or later
  3053. *
  3054. * @param string|integer|array|Zend_Date $year Year to compare
  3055. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3056. * @return integer 0 = equal, 1 = later, -1 = earlier
  3057. * @throws Zend_Date_Exception
  3058. */
  3059. public function compareYear($year, $locale = null)
  3060. {
  3061. return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
  3062. }
  3063. /**
  3064. * Returns only the month from the date object as new object.
  3065. * For example: 10.May.2000 10:30:00 -> 01.May.1970 00:00:00
  3066. *
  3067. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3068. * @return Zend_Date
  3069. */
  3070. public function getMonth($locale = null)
  3071. {
  3072. return $this->copyPart(self::MONTH, $locale);
  3073. }
  3074. /**
  3075. * Returns the calculated month
  3076. *
  3077. * @param string $calc Calculation to make
  3078. * @param string|integer|array|Zend_Date $month Month to calculate with, if null the actual month is taken
  3079. * @param string|Zend_Locale $locale Locale for parsing input
  3080. * @return integer|Zend_Date new time
  3081. * @throws Zend_Date_Exception
  3082. */
  3083. private function _month($calc, $month, $locale)
  3084. {
  3085. if (is_null($month)) {
  3086. #require_once 'Zend/Date/Exception.php';
  3087. throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
  3088. }
  3089. if ($locale === null) {
  3090. $locale = $this->getLocale();
  3091. }
  3092. if ($month instanceof Zend_Date) {
  3093. // extract month from object
  3094. $found = $month->get(self::MONTH_SHORT, $locale);
  3095. } else {
  3096. if (is_numeric($month)) {
  3097. $found = $month;
  3098. } else if (is_array($month)) {
  3099. if (isset($month['month']) === true) {
  3100. $month = $month['month'];
  3101. } else {
  3102. #require_once 'Zend/Date/Exception.php';
  3103. throw new Zend_Date_Exception("no month given in array");
  3104. }
  3105. } else {
  3106. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  3107. $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  3108. $monthlist = array_merge($monthlist, $monthlist2);
  3109. $found = 0;
  3110. $cnt = 0;
  3111. foreach ($monthlist as $key => $value) {
  3112. if (strtoupper($value) == strtoupper($month)) {
  3113. $found = $key + 1;
  3114. break;
  3115. }
  3116. ++$cnt;
  3117. }
  3118. if ($found == 0) {
  3119. foreach ($monthlist2 as $key => $value) {
  3120. if (strtoupper(iconv_substr($value, 0, 1)) == strtoupper($month)) {
  3121. $found = $key + 1;
  3122. break;
  3123. }
  3124. ++$cnt;
  3125. }
  3126. }
  3127. if ($found == 0) {
  3128. #require_once 'Zend/Date/Exception.php';
  3129. throw new Zend_Date_Exception("unknown month name ($month)", $month);
  3130. }
  3131. }
  3132. }
  3133. $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
  3134. if ($calc != 'cmp') {
  3135. return $this;
  3136. }
  3137. return $return;
  3138. }
  3139. /**
  3140. * Sets a new month
  3141. * The month can be a number or a string. Setting months lower then 0 and greater then 12
  3142. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3143. * If a localized monthname is given it will be parsed with the default locale or the optional
  3144. * set locale.
  3145. * Returned is the new date object
  3146. *
  3147. * @param string|integer|array|Zend_Date $month Month to set
  3148. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3149. * @return Zend_Date new date
  3150. * @throws Zend_Date_Exception
  3151. */
  3152. public function setMonth($month, $locale = null)
  3153. {
  3154. return $this->_month('set', $month, $locale);
  3155. }
  3156. /**
  3157. * Adds months to the existing date object.
  3158. * The month can be a number or a string. Adding months lower then 0 and greater then 12
  3159. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3160. * If a localized monthname is given it will be parsed with the default locale or the optional
  3161. * set locale.
  3162. * Returned is the new date object
  3163. *
  3164. * @param string|integer|array|Zend_Date $month Month to add
  3165. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3166. * @return Zend_Date new date
  3167. * @throws Zend_Date_Exception
  3168. */
  3169. public function addMonth($month, $locale = null)
  3170. {
  3171. return $this->_month('add', $month, $locale);
  3172. }
  3173. /**
  3174. * Subtracts months from the existing date object.
  3175. * The month can be a number or a string. Subtracting months lower then 0 and greater then 12
  3176. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3177. * If a localized monthname is given it will be parsed with the default locale or the optional
  3178. * set locale.
  3179. * Returned is the new date object
  3180. *
  3181. * @param string|integer|array|Zend_Date $month Month to sub
  3182. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3183. * @return Zend_Date new date
  3184. * @throws Zend_Date_Exception
  3185. */
  3186. public function subMonth($month, $locale = null)
  3187. {
  3188. return $this->_month('sub', $month, $locale);
  3189. }
  3190. /**
  3191. * Compares the month with the existing date object, ignoring other date parts.
  3192. * For example: 10.03.2000 -> 15.03.1950 -> true
  3193. * Returns if equal, earlier or later
  3194. *
  3195. * @param string|integer|array|Zend_Date $month Month to compare
  3196. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3197. * @return integer 0 = equal, 1 = later, -1 = earlier
  3198. * @throws Zend_Date_Exception
  3199. */
  3200. public function compareMonth($month, $locale = null)
  3201. {
  3202. return $this->_month('cmp', $month, $locale);
  3203. }
  3204. /**
  3205. * Returns the day as new date object
  3206. * Example: 20.May.1986 -> 20.Jan.1970 00:00:00
  3207. *
  3208. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3209. * @return Zend_Date
  3210. */
  3211. public function getDay($locale = null)
  3212. {
  3213. return $this->copyPart(self::DAY_SHORT, $locale);
  3214. }
  3215. /**
  3216. * Returns the calculated day
  3217. *
  3218. * @param $calc string Type of calculation to make
  3219. * @param $day string|integer|Zend_Date Day to calculate, when null the actual day is calculated
  3220. * @param $locale string|Zend_Locale Locale for parsing input
  3221. * @return Zend_Date|integer
  3222. */
  3223. private function _day($calc, $day, $locale)
  3224. {
  3225. if (is_null($day)) {
  3226. #require_once 'Zend/Date/Exception.php';
  3227. throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
  3228. }
  3229. if ($locale === null) {
  3230. $locale = $this->getLocale();
  3231. }
  3232. if ($day instanceof Zend_Date) {
  3233. $day = $day->get(self::DAY_SHORT, $locale);
  3234. }
  3235. if (is_numeric($day)) {
  3236. $type = self::DAY_SHORT;
  3237. } else if (is_array($day)) {
  3238. if (isset($day['day']) === true) {
  3239. $day = $day['day'];
  3240. $type = self::WEEKDAY;
  3241. } else {
  3242. #require_once 'Zend/Date/Exception.php';
  3243. throw new Zend_Date_Exception("no day given in array");
  3244. }
  3245. } else {
  3246. switch (iconv_strlen($day)) {
  3247. case 1 :
  3248. $type = self::WEEKDAY_NARROW;
  3249. break;
  3250. case 2:
  3251. $type = self::WEEKDAY_NAME;
  3252. break;
  3253. case 3:
  3254. $type = self::WEEKDAY_SHORT;
  3255. break;
  3256. default:
  3257. $type = self::WEEKDAY;
  3258. break;
  3259. }
  3260. }
  3261. $return = $this->_calcdetail($calc, $day, $type, $locale);
  3262. if ($calc != 'cmp') {
  3263. return $this;
  3264. }
  3265. return $return;
  3266. }
  3267. /**
  3268. * Sets a new day
  3269. * The day can be a number or a string. Setting days lower then 0 or greater than the number of this months days
  3270. * will result in adding or subtracting the relevant month.
  3271. * If a localized dayname is given it will be parsed with the default locale or the optional
  3272. * set locale.
  3273. * Returned is the new date object
  3274. * Example: setDay('Montag', 'de_AT'); will set the monday of this week as day.
  3275. *
  3276. * @param string|integer|array|Zend_Date $month Day to set
  3277. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3278. * @return Zend_Date new date
  3279. * @throws Zend_Date_Exception
  3280. */
  3281. public function setDay($day, $locale = null)
  3282. {
  3283. return $this->_day('set', $day, $locale);
  3284. }
  3285. /**
  3286. * Adds days to the existing date object.
  3287. * The day can be a number or a string. Adding days lower then 0 or greater than the number of this months days
  3288. * will result in adding or subtracting the relevant month.
  3289. * If a localized dayname is given it will be parsed with the default locale or the optional
  3290. * set locale.
  3291. * Returned is the new date object
  3292. * Example: addDay('Montag', 'de_AT'); will add the number of days until the next monday
  3293. *
  3294. * @param string|integer|array|Zend_Date $month Day to add
  3295. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3296. * @return Zend_Date new date
  3297. * @throws Zend_Date_Exception
  3298. */
  3299. public function addDay($day, $locale = null)
  3300. {
  3301. return $this->_day('add', $day, $locale);
  3302. }
  3303. /**
  3304. * Subtracts days from the existing date object.
  3305. * The day can be a number or a string. Subtracting days lower then 0 or greater than the number of this months days
  3306. * will result in adding or subtracting the relevant month.
  3307. * If a localized dayname is given it will be parsed with the default locale or the optional
  3308. * set locale.
  3309. * Returned is the new date object
  3310. * Example: subDay('Montag', 'de_AT'); will sub the number of days until the previous monday
  3311. *
  3312. * @param string|integer|array|Zend_Date $month Day to sub
  3313. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3314. * @return Zend_Date new date
  3315. * @throws Zend_Date_Exception
  3316. */
  3317. public function subDay($day, $locale = null)
  3318. {
  3319. return $this->_day('sub', $day, $locale);
  3320. }
  3321. /**
  3322. * Compares the day with the existing date object, ignoring other date parts.
  3323. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3324. * Returns if equal, earlier or later
  3325. *
  3326. * @param string|integer|array|Zend_Date $day Day to compare
  3327. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3328. * @return integer 0 = equal, 1 = later, -1 = earlier
  3329. * @throws Zend_Date_Exception
  3330. */
  3331. public function compareDay($day, $locale = null)
  3332. {
  3333. return $this->_day('cmp', $day, $locale);
  3334. }
  3335. /**
  3336. * Returns the weekday as new date object
  3337. * Weekday is always from 1-7
  3338. * Example: 09-Jan-2007 -> 2 = Tuesday -> 02-Jan-1970 (when 02.01.1970 is also Tuesday)
  3339. *
  3340. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3341. * @return Zend_Date
  3342. */
  3343. public function getWeekday($locale = null)
  3344. {
  3345. return $this->copyPart(self::WEEKDAY, $locale);
  3346. }
  3347. /**
  3348. * Returns the calculated weekday
  3349. *
  3350. * @param $calc string Type of calculation to make
  3351. * @param $weekday string|integer|array|Zend_Date Weekday to calculate, when null the actual weekday is calculated
  3352. * @param $locale string|Zend_Locale Locale for parsing input
  3353. * @return Zend_Date|integer
  3354. * @throws Zend_Date_Exception
  3355. */
  3356. private function _weekday($calc, $weekday, $locale)
  3357. {
  3358. if (is_null($weekday)) {
  3359. #require_once 'Zend/Date/Exception.php';
  3360. throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
  3361. }
  3362. if ($locale === null) {
  3363. $locale = $this->getLocale();
  3364. }
  3365. if ($weekday instanceof Zend_Date) {
  3366. $weekday = $weekday->get(self::WEEKDAY_8601, $locale);
  3367. }
  3368. if (is_numeric($weekday)) {
  3369. $type = self::WEEKDAY_8601;
  3370. } else if (is_array($weekday)) {
  3371. if (isset($weekday['weekday']) === true) {
  3372. $weekday = $weekday['weekday'];
  3373. $type = self::WEEKDAY;
  3374. } else {
  3375. #require_once 'Zend/Date/Exception.php';
  3376. throw new Zend_Date_Exception("no weekday given in array");
  3377. }
  3378. } else {
  3379. switch(iconv_strlen($weekday)) {
  3380. case 1:
  3381. $type = self::WEEKDAY_NARROW;
  3382. break;
  3383. case 2:
  3384. $type = self::WEEKDAY_NAME;
  3385. break;
  3386. case 3:
  3387. $type = self::WEEKDAY_SHORT;
  3388. break;
  3389. default:
  3390. $type = self::WEEKDAY;
  3391. break;
  3392. }
  3393. }
  3394. $return = $this->_calcdetail($calc, $weekday, $type, $locale);
  3395. if ($calc != 'cmp') {
  3396. return $this;
  3397. }
  3398. return $return;
  3399. }
  3400. /**
  3401. * Sets a new weekday
  3402. * The weekday can be a number or a string. If a localized weekday name is given,
  3403. * then it will be parsed as a date in $locale (defaults to the same locale as $this).
  3404. * Returned is the new date object.
  3405. * Example: setWeekday(3); will set the wednesday of this week as day.
  3406. *
  3407. * @param string|integer|array|Zend_Date $month Weekday to set
  3408. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3409. * @return Zend_Date new date
  3410. * @throws Zend_Date_Exception
  3411. */
  3412. public function setWeekday($weekday, $locale = null)
  3413. {
  3414. return $this->_weekday('set', $weekday, $locale);
  3415. }
  3416. /**
  3417. * Adds weekdays to the existing date object.
  3418. * The weekday can be a number or a string.
  3419. * If a localized dayname is given it will be parsed with the default locale or the optional
  3420. * set locale.
  3421. * Returned is the new date object
  3422. * Example: addWeekday(3); will add the difference of days from the begining of the month until
  3423. * wednesday.
  3424. *
  3425. * @param string|integer|array|Zend_Date $month Weekday to add
  3426. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3427. * @return Zend_Date new date
  3428. * @throws Zend_Date_Exception
  3429. */
  3430. public function addWeekday($weekday, $locale = null)
  3431. {
  3432. return $this->_weekday('add', $weekday, $locale);
  3433. }
  3434. /**
  3435. * Subtracts weekdays from the existing date object.
  3436. * The weekday can be a number or a string.
  3437. * If a localized dayname is given it will be parsed with the default locale or the optional
  3438. * set locale.
  3439. * Returned is the new date object
  3440. * Example: subWeekday(3); will subtract the difference of days from the begining of the month until
  3441. * wednesday.
  3442. *
  3443. * @param string|integer|array|Zend_Date $month Weekday to sub
  3444. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3445. * @return Zend_Date new date
  3446. * @throws Zend_Date_Exception
  3447. */
  3448. public function subWeekday($weekday, $locale = null)
  3449. {
  3450. return $this->_weekday('sub', $weekday, $locale);
  3451. }
  3452. /**
  3453. * Compares the weekday with the existing date object, ignoring other date parts.
  3454. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3455. * Returns if equal, earlier or later
  3456. *
  3457. * @param string|integer|array|Zend_Date $weekday Weekday to compare
  3458. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3459. * @return integer 0 = equal, 1 = later, -1 = earlier
  3460. * @throws Zend_Date_Exception
  3461. */
  3462. public function compareWeekday($weekday, $locale = null)
  3463. {
  3464. return $this->_weekday('cmp', $weekday, $locale);
  3465. }
  3466. /**
  3467. * Returns the day of year as new date object
  3468. * Example: 02.Feb.1986 10:00:00 -> 02.Feb.1970 00:00:00
  3469. *
  3470. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3471. * @return Zend_Date
  3472. */
  3473. public function getDayOfYear($locale = null)
  3474. {
  3475. return $this->copyPart(self::DAY_OF_YEAR, $locale);
  3476. }
  3477. /**
  3478. * Sets a new day of year
  3479. * The day of year is always a number.
  3480. * Returned is the new date object
  3481. * Example: 04.May.2004 -> setDayOfYear(10) -> 10.Jan.2004
  3482. *
  3483. * @param string|integer|array|Zend_Date $day Day of Year to set
  3484. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3485. * @return Zend_Date new date
  3486. * @throws Zend_Date_Exception
  3487. */
  3488. public function setDayOfYear($day, $locale = null)
  3489. {
  3490. return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3491. }
  3492. /**
  3493. * Adds a day of year to the existing date object.
  3494. * The day of year is always a number.
  3495. * Returned is the new date object
  3496. * Example: addDayOfYear(10); will add 10 days to the existing date object.
  3497. *
  3498. * @param string|integer|array|Zend_Date $day Day of Year to add
  3499. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3500. * @return Zend_Date new date
  3501. * @throws Zend_Date_Exception
  3502. */
  3503. public function addDayOfYear($day, $locale = null)
  3504. {
  3505. return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3506. }
  3507. /**
  3508. * Subtracts a day of year from the existing date object.
  3509. * The day of year is always a number.
  3510. * Returned is the new date object
  3511. * Example: subDayOfYear(10); will subtract 10 days from the existing date object.
  3512. *
  3513. * @param string|integer|array|Zend_Date $day Day of Year to sub
  3514. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3515. * @return Zend_Date new date
  3516. * @throws Zend_Date_Exception
  3517. */
  3518. public function subDayOfYear($day, $locale = null)
  3519. {
  3520. return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3521. }
  3522. /**
  3523. * Compares the day of year with the existing date object.
  3524. * For example: compareDayOfYear(33) -> 02.Feb.2007 -> 0
  3525. * Returns if equal, earlier or later
  3526. *
  3527. * @param string|integer|array|Zend_Date $day Day of Year to compare
  3528. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3529. * @return integer 0 = equal, 1 = later, -1 = earlier
  3530. * @throws Zend_Date_Exception
  3531. */
  3532. public function compareDayOfYear($day, $locale = null)
  3533. {
  3534. return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3535. }
  3536. /**
  3537. * Returns the hour as new date object
  3538. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 10:00:00
  3539. *
  3540. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3541. * @return Zend_Date
  3542. */
  3543. public function getHour($locale = null)
  3544. {
  3545. return $this->copyPart(self::HOUR, $locale);
  3546. }
  3547. /**
  3548. * Sets a new hour
  3549. * The hour is always a number.
  3550. * Returned is the new date object
  3551. * Example: 04.May.1993 13:07:25 -> setHour(7); -> 04.May.1993 07:07:25
  3552. *
  3553. * @param string|integer|array|Zend_Date $hour Hour to set
  3554. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3555. * @return Zend_Date new date
  3556. * @throws Zend_Date_Exception
  3557. */
  3558. public function setHour($hour, $locale = null)
  3559. {
  3560. return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
  3561. }
  3562. /**
  3563. * Adds hours to the existing date object.
  3564. * The hour is always a number.
  3565. * Returned is the new date object
  3566. * Example: 04.May.1993 13:07:25 -> addHour(12); -> 05.May.1993 01:07:25
  3567. *
  3568. * @param string|integer|array|Zend_Date $hour Hour to add
  3569. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3570. * @return Zend_Date new date
  3571. * @throws Zend_Date_Exception
  3572. */
  3573. public function addHour($hour, $locale = null)
  3574. {
  3575. return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
  3576. }
  3577. /**
  3578. * Subtracts hours from the existing date object.
  3579. * The hour is always a number.
  3580. * Returned is the new date object
  3581. * Example: 04.May.1993 13:07:25 -> subHour(6); -> 05.May.1993 07:07:25
  3582. *
  3583. * @param string|integer|array|Zend_Date $hour Hour to sub
  3584. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3585. * @return Zend_Date new date
  3586. * @throws Zend_Date_Exception
  3587. */
  3588. public function subHour($hour, $locale = null)
  3589. {
  3590. return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
  3591. }
  3592. /**
  3593. * Compares the hour with the existing date object.
  3594. * For example: 10:30:25 -> compareHour(10) -> 0
  3595. * Returns if equal, earlier or later
  3596. *
  3597. * @param string|integer|array|Zend_Date $hour Hour to compare
  3598. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3599. * @return integer 0 = equal, 1 = later, -1 = earlier
  3600. * @throws Zend_Date_Exception
  3601. */
  3602. public function compareHour($hour, $locale = null)
  3603. {
  3604. return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
  3605. }
  3606. /**
  3607. * Returns the minute as new date object
  3608. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:30:00
  3609. *
  3610. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3611. * @return Zend_Date
  3612. */
  3613. public function getMinute($locale = null)
  3614. {
  3615. return $this->copyPart(self::MINUTE, $locale);
  3616. }
  3617. /**
  3618. * Sets a new minute
  3619. * The minute is always a number.
  3620. * Returned is the new date object
  3621. * Example: 04.May.1993 13:07:25 -> setMinute(29); -> 04.May.1993 13:29:25
  3622. *
  3623. * @param string|integer|array|Zend_Date $minute Minute to set
  3624. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3625. * @return Zend_Date new date
  3626. * @throws Zend_Date_Exception
  3627. */
  3628. public function setMinute($minute, $locale = null)
  3629. {
  3630. return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3631. }
  3632. /**
  3633. * Adds minutes to the existing date object.
  3634. * The minute is always a number.
  3635. * Returned is the new date object
  3636. * Example: 04.May.1993 13:07:25 -> addMinute(65); -> 04.May.1993 13:12:25
  3637. *
  3638. * @param string|integer|array|Zend_Date $minute Minute to add
  3639. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3640. * @return Zend_Date new date
  3641. * @throws Zend_Date_Exception
  3642. */
  3643. public function addMinute($minute, $locale = null)
  3644. {
  3645. return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3646. }
  3647. /**
  3648. * Subtracts minutes from the existing date object.
  3649. * The minute is always a number.
  3650. * Returned is the new date object
  3651. * Example: 04.May.1993 13:07:25 -> subMinute(9); -> 04.May.1993 12:58:25
  3652. *
  3653. * @param string|integer|array|Zend_Date $minute Minute to sub
  3654. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3655. * @return Zend_Date new date
  3656. * @throws Zend_Date_Exception
  3657. */
  3658. public function subMinute($minute, $locale = null)
  3659. {
  3660. return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3661. }
  3662. /**
  3663. * Compares the minute with the existing date object.
  3664. * For example: 10:30:25 -> compareMinute(30) -> 0
  3665. * Returns if equal, earlier or later
  3666. *
  3667. * @param string|integer|array|Zend_Date $minute Hour to compare
  3668. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3669. * @return integer 0 = equal, 1 = later, -1 = earlier
  3670. * @throws Zend_Date_Exception
  3671. */
  3672. public function compareMinute($minute, $locale = null)
  3673. {
  3674. return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3675. }
  3676. /**
  3677. * Returns the second as new date object
  3678. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:00:25
  3679. *
  3680. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3681. * @return Zend_Date
  3682. */
  3683. public function getSecond($locale = null)
  3684. {
  3685. return $this->copyPart(self::SECOND, $locale);
  3686. }
  3687. /**
  3688. * Sets new seconds to the existing date object.
  3689. * The second is always a number.
  3690. * Returned is the new date object
  3691. * Example: 04.May.1993 13:07:25 -> setSecond(100); -> 04.May.1993 13:08:40
  3692. *
  3693. * @param string|integer|array|Zend_Date $second Second to set
  3694. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3695. * @return Zend_Date new date
  3696. * @throws Zend_Date_Exception
  3697. */
  3698. public function setSecond($second, $locale = null)
  3699. {
  3700. return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
  3701. }
  3702. /**
  3703. * Adds seconds to the existing date object.
  3704. * The second is always a number.
  3705. * Returned is the new date object
  3706. * Example: 04.May.1993 13:07:25 -> addSecond(65); -> 04.May.1993 13:08:30
  3707. *
  3708. * @param string|integer|array|Zend_Date $second Second to add
  3709. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3710. * @return Zend_Date new date
  3711. * @throws Zend_Date_Exception
  3712. */
  3713. public function addSecond($second, $locale = null)
  3714. {
  3715. return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
  3716. }
  3717. /**
  3718. * Subtracts seconds from the existing date object.
  3719. * The second is always a number.
  3720. * Returned is the new date object
  3721. * Example: 04.May.1993 13:07:25 -> subSecond(10); -> 04.May.1993 13:07:15
  3722. *
  3723. * @param string|integer|array|Zend_Date $second Second to sub
  3724. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3725. * @return Zend_Date new date
  3726. * @throws Zend_Date_Exception
  3727. */
  3728. public function subSecond($second, $locale = null)
  3729. {
  3730. return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
  3731. }
  3732. /**
  3733. * Compares the second with the existing date object.
  3734. * For example: 10:30:25 -> compareSecond(25) -> 0
  3735. * Returns if equal, earlier or later
  3736. *
  3737. * @param string|integer|array|Zend_Date $second Second to compare
  3738. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3739. * @return integer 0 = equal, 1 = later, -1 = earlier
  3740. * @throws Zend_Date_Exception
  3741. */
  3742. public function compareSecond($second, $locale = null)
  3743. {
  3744. return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
  3745. }
  3746. /**
  3747. * Returns the precision for fractional seconds
  3748. *
  3749. * @return integer
  3750. */
  3751. public function getFractionalPrecision()
  3752. {
  3753. return $this->_precision;
  3754. }
  3755. /**
  3756. * Sets a new precision for fractional seconds
  3757. *
  3758. * @param integer $precision Precision for the fractional datepart 3 = milliseconds
  3759. * @throws Zend_Date_Exception
  3760. * @return void
  3761. */
  3762. public function setFractionalPrecision($precision)
  3763. {
  3764. if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
  3765. #require_once 'Zend/Date/Exception.php';
  3766. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3767. }
  3768. $this->_precision = (int) $precision;
  3769. }
  3770. /**
  3771. * Returns the milliseconds of the date object
  3772. *
  3773. * @return integer
  3774. */
  3775. public function getMilliSecond()
  3776. {
  3777. return $this->_fractional;
  3778. }
  3779. /**
  3780. * Sets new milliseconds for the date object
  3781. * Example: setMilliSecond(550, 2) -> equals +5 Sec +50 MilliSec
  3782. *
  3783. * @param integer|Zend_Date $milli (Optional) Millisecond to set, when null the actual millisecond is set
  3784. * @param integer $precision (Optional) Fraction precision of the given milliseconds
  3785. * @return integer|string
  3786. */
  3787. public function setMilliSecond($milli = null, $precision = null)
  3788. {
  3789. if ($milli === null) {
  3790. list($milli, $time) = explode(" ", microtime());
  3791. $milli = intval($milli);
  3792. $precision = 6;
  3793. } else if (!is_numeric($milli)) {
  3794. #require_once 'Zend/Date/Exception.php';
  3795. throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
  3796. }
  3797. if ($precision === null) {
  3798. $precision = $this->_precision;
  3799. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3800. #require_once 'Zend/Date/Exception.php';
  3801. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3802. }
  3803. $this->_fractional = 0;
  3804. $this->addMilliSecond($milli, $precision);
  3805. return $this->_fractional;
  3806. }
  3807. /**
  3808. * Adds milliseconds to the date object
  3809. *
  3810. * @param integer|Zend_Date $milli (Optional) Millisecond to add, when null the actual millisecond is added
  3811. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3812. * @return integer|string
  3813. */
  3814. public function addMilliSecond($milli = null, $precision = null)
  3815. {
  3816. if ($milli === null) {
  3817. list($milli, $time) = explode(" ", microtime());
  3818. $milli = intval($milli);
  3819. } else if (!is_numeric($milli)) {
  3820. #require_once 'Zend/Date/Exception.php';
  3821. throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
  3822. }
  3823. if ($precision === null) {
  3824. $precision = $this->_precision;
  3825. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3826. #require_once 'Zend/Date/Exception.php';
  3827. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3828. }
  3829. if ($precision != $this->_precision) {
  3830. if ($precision > $this->_precision) {
  3831. $diff = $precision - $this->_precision;
  3832. $milli = (int) ($milli / (10 * $diff));
  3833. } else {
  3834. $diff = $this->_precision - $precision;
  3835. $milli = (int) ($milli * (10 * $diff));
  3836. }
  3837. }
  3838. $this->_fractional += $milli;
  3839. // Add/sub milliseconds + add/sub seconds
  3840. $max = pow(10, $this->_precision);
  3841. // Milli includes seconds
  3842. if ($this->_fractional >= $max) {
  3843. while ($this->_fractional >= $max) {
  3844. $this->addSecond(1);
  3845. $this->_fractional -= $max;
  3846. }
  3847. }
  3848. if ($this->_fractional < 0) {
  3849. while ($this->_fractional < 0) {
  3850. $this->subSecond(1);
  3851. $this->_fractional += $max;
  3852. }
  3853. }
  3854. return $this->_fractional;
  3855. }
  3856. /**
  3857. * Subtracts a millisecond
  3858. *
  3859. * @param integer|Zend_Date $milli (Optional) Millisecond to sub, when null the actual millisecond is subtracted
  3860. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3861. * @return integer
  3862. */
  3863. public function subMilliSecond($milli = null, $precision = null)
  3864. {
  3865. return $this->addMilliSecond(0 - $milli);
  3866. }
  3867. /**
  3868. * Compares only the millisecond part, returning the difference
  3869. *
  3870. * @param integer|Zend_Date $milli OPTIONAL Millisecond to compare, when null the actual millisecond is compared
  3871. * @param integer $precision OPTIONAL Fractional precision for the given milliseconds
  3872. * @return integer
  3873. */
  3874. public function compareMilliSecond($milli = null, $precision = null)
  3875. {
  3876. if ($milli === null) {
  3877. list($milli, $time) = explode(" ", microtime());
  3878. $milli = intval($milli);
  3879. } else if (is_numeric($milli) === false) {
  3880. #require_once 'Zend/Date/Exception.php';
  3881. throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
  3882. }
  3883. if ($precision === null) {
  3884. $precision = $this->_precision;
  3885. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3886. #require_once 'Zend/Date/Exception.php';
  3887. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3888. }
  3889. if ($precision === 0) {
  3890. #require_once 'Zend/Date/Exception.php';
  3891. throw new Zend_Date_Exception('precision is 0');
  3892. }
  3893. if ($precision != $this->_precision) {
  3894. if ($precision > $this->_precision) {
  3895. $diff = $precision - $this->_precision;
  3896. $milli = (int) ($milli / (10 * $diff));
  3897. } else {
  3898. $diff = $this->_precision - $precision;
  3899. $milli = (int) ($milli * (10 * $diff));
  3900. }
  3901. }
  3902. $comp = $this->_fractional - $milli;
  3903. if ($comp < 0) {
  3904. return -1;
  3905. } else if ($comp > 0) {
  3906. return 1;
  3907. }
  3908. return 0;
  3909. }
  3910. /**
  3911. * Returns the week as new date object using monday as begining of the week
  3912. * Example: 12.Jan.2007 -> 08.Jan.1970 00:00:00
  3913. *
  3914. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3915. * @return Zend_Date
  3916. */
  3917. public function getWeek($locale = null)
  3918. {
  3919. return $this->copyPart(self::WEEK, $locale);
  3920. }
  3921. /**
  3922. * Sets a new week. The week is always a number. The day of week is not changed.
  3923. * Returned is the new date object
  3924. * Example: 09.Jan.2007 13:07:25 -> setWeek(1); -> 02.Jan.2007 13:07:25
  3925. *
  3926. * @param string|integer|array|Zend_Date $week Week to set
  3927. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3928. * @return Zend_Date
  3929. * @throws Zend_Date_Exception
  3930. */
  3931. public function setWeek($week, $locale = null)
  3932. {
  3933. return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
  3934. }
  3935. /**
  3936. * Adds a week. The week is always a number. The day of week is not changed.
  3937. * Returned is the new date object
  3938. * Example: 09.Jan.2007 13:07:25 -> addWeek(1); -> 16.Jan.2007 13:07:25
  3939. *
  3940. * @param string|integer|array|Zend_Date $week Week to add
  3941. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3942. * @return Zend_Date
  3943. * @throws Zend_Date_Exception
  3944. */
  3945. public function addWeek($week, $locale = null)
  3946. {
  3947. return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
  3948. }
  3949. /**
  3950. * Subtracts a week. The week is always a number. The day of week is not changed.
  3951. * Returned is the new date object
  3952. * Example: 09.Jan.2007 13:07:25 -> subWeek(1); -> 02.Jan.2007 13:07:25
  3953. *
  3954. * @param string|integer|array|Zend_Date $week Week to sub
  3955. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3956. * @return Zend_Date
  3957. * @throws Zend_Date_Exception
  3958. */
  3959. public function subWeek($week, $locale = null)
  3960. {
  3961. return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
  3962. }
  3963. /**
  3964. * Compares only the week part, returning the difference
  3965. * Returned is the new date object
  3966. * Returns if equal, earlier or later
  3967. * Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
  3968. *
  3969. * @param string|integer|array|Zend_Date $week Week to compare
  3970. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3971. * @return integer 0 = equal, 1 = later, -1 = earlier
  3972. */
  3973. public function compareWeek($week, $locale = null)
  3974. {
  3975. return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
  3976. }
  3977. /**
  3978. * Sets a new standard locale for the date object.
  3979. * This locale will be used for all functions
  3980. * Returned is the really set locale.
  3981. * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
  3982. * 'xx_YY' will be set to 'root' because 'xx' does not exist
  3983. *
  3984. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3985. * @throws Zend_Date_Exception When the given locale does not exist
  3986. * @return Zend_Date Provides fluent interface
  3987. */
  3988. public function setLocale($locale = null)
  3989. {
  3990. try {
  3991. $this->_locale = Zend_Locale::findLocale($locale);
  3992. } catch (Zend_Locale_Exception $e) {
  3993. #require_once 'Zend/Date/Exception.php';
  3994. throw new Zend_Date_Exception($e->getMessage());
  3995. }
  3996. return $this;
  3997. }
  3998. /**
  3999. * Returns the actual set locale
  4000. *
  4001. * @return string
  4002. */
  4003. public function getLocale()
  4004. {
  4005. return $this->_locale;
  4006. }
  4007. /**
  4008. * Checks if the given date is a real date or datepart.
  4009. * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  4010. * But the check will only be done for the expected dateparts which are given by format.
  4011. * If no format is given the standard dateformat for the actual locale is used.
  4012. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  4013. *
  4014. * @param string $date Date to parse for correctness
  4015. * @param string $format (Optional) Format for parsing the date string
  4016. * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts
  4017. * @return boolean True when all date parts are correct
  4018. */
  4019. public static function isDate($date, $format = null, $locale = null)
  4020. {
  4021. if (!is_string($date) and !is_numeric($date) and !($date instanceof Zend_Date)) {
  4022. return false;
  4023. }
  4024. if (($format !== null) and (Zend_Locale::isLocale($format, null, false))) {
  4025. $locale = $format;
  4026. $format = null;
  4027. }
  4028. $locale = Zend_Locale::findLocale($locale);
  4029. if ($format === null) {
  4030. $format = Zend_Locale_Format::getDateFormat($locale);
  4031. } else if (self::$_options['format_type'] == 'php') {
  4032. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  4033. }
  4034. try {
  4035. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
  4036. 'date_format' => $format, 'format_type' => 'iso',
  4037. 'fix_date' => false));
  4038. if (isset($parsed['year']) and ((strpos(strtoupper($format), 'YY') !== false) and
  4039. (strpos(strtoupper($format), 'YYYY') === false))) {
  4040. $parsed['year'] = self::getFullYear($parsed['year']);
  4041. }
  4042. } catch (Zend_Locale_Exception $e) {
  4043. // Date can not be parsed
  4044. return false;
  4045. }
  4046. if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
  4047. (!isset($parsed['year']))) {
  4048. // Year expected but not found
  4049. return false;
  4050. }
  4051. if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
  4052. // Month expected but not found
  4053. return false;
  4054. }
  4055. if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
  4056. // Day expected but not found
  4057. return false;
  4058. }
  4059. if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
  4060. (!isset($parsed['hour']))) {
  4061. // Hour expected but not found
  4062. return false;
  4063. }
  4064. if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
  4065. // Minute expected but not found
  4066. return false;
  4067. }
  4068. if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
  4069. // Second expected but not found
  4070. return false;
  4071. }
  4072. // Set not given dateparts
  4073. if (isset($parsed['hour']) === false) {
  4074. $parsed['hour'] = 0;
  4075. }
  4076. if (isset($parsed['minute']) === false) {
  4077. $parsed['minute'] = 0;
  4078. }
  4079. if (isset($parsed['second']) === false) {
  4080. $parsed['second'] = 0;
  4081. }
  4082. if (isset($parsed['month']) === false) {
  4083. $parsed['month'] = 1;
  4084. }
  4085. if (isset($parsed['day']) === false) {
  4086. $parsed['day'] = 1;
  4087. }
  4088. if (isset($parsed['year']) === false) {
  4089. $parsed['year'] = 1970;
  4090. }
  4091. $date = new self($parsed, null, $locale);
  4092. $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
  4093. $parsed['month'], $parsed['day'], $parsed['year']);
  4094. if ($parsed['year'] != $date->date('Y', $timestamp)) {
  4095. // Given year differs from parsed year
  4096. return false;
  4097. }
  4098. if ($parsed['month'] != $date->date('n', $timestamp)) {
  4099. // Given month differs from parsed month
  4100. return false;
  4101. }
  4102. if ($parsed['day'] != $date->date('j', $timestamp)) {
  4103. // Given day differs from parsed day
  4104. return false;
  4105. }
  4106. if ($parsed['hour'] != $date->date('G', $timestamp)) {
  4107. // Given hour differs from parsed hour
  4108. return false;
  4109. }
  4110. if ($parsed['minute'] != $date->date('i', $timestamp)) {
  4111. // Given minute differs from parsed minute
  4112. return false;
  4113. }
  4114. if ($parsed['second'] != $date->date('s', $timestamp)) {
  4115. // Given second differs from parsed second
  4116. return false;
  4117. }
  4118. return true;
  4119. }
  4120. }