PageRenderTime 79ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/libs/devblocks/libs/ZendFramework/Zend/Date.php

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