PageRenderTime 67ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Date/DateObject.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 1089 lines | 708 code | 162 blank | 219 comment | 159 complexity | 899fd38da15853b138ea351d1a5bbbfa MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Date
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: DateObject.php 23775 2011-03-01 17:25:24Z ralph $
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Date
  24. * @subpackage Zend_Date_DateObject
  25. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. abstract class Zend_Date_DateObject {
  29. /**
  30. * UNIX Timestamp
  31. */
  32. private $_unixTimestamp;
  33. protected static $_cache = null;
  34. protected static $_cacheTags = false;
  35. protected static $_defaultOffset = 0;
  36. /**
  37. * active timezone
  38. */
  39. private $_timezone = 'UTC';
  40. private $_offset = 0;
  41. private $_syncronised = 0;
  42. // turn off DST correction if UTC or GMT
  43. protected $_dst = true;
  44. /**
  45. * Table of Monthdays
  46. */
  47. private static $_monthTable = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  48. /**
  49. * Table of Years
  50. */
  51. private static $_yearTable = array(
  52. 1970 => 0, 1960 => -315619200, 1950 => -631152000,
  53. 1940 => -946771200, 1930 => -1262304000, 1920 => -1577923200,
  54. 1910 => -1893456000, 1900 => -2208988800, 1890 => -2524521600,
  55. 1880 => -2840140800, 1870 => -3155673600, 1860 => -3471292800,
  56. 1850 => -3786825600, 1840 => -4102444800, 1830 => -4417977600,
  57. 1820 => -4733596800, 1810 => -5049129600, 1800 => -5364662400,
  58. 1790 => -5680195200, 1780 => -5995814400, 1770 => -6311347200,
  59. 1760 => -6626966400, 1750 => -6942499200, 1740 => -7258118400,
  60. 1730 => -7573651200, 1720 => -7889270400, 1710 => -8204803200,
  61. 1700 => -8520336000, 1690 => -8835868800, 1680 => -9151488000,
  62. 1670 => -9467020800, 1660 => -9782640000, 1650 => -10098172800,
  63. 1640 => -10413792000, 1630 => -10729324800, 1620 => -11044944000,
  64. 1610 => -11360476800, 1600 => -11676096000);
  65. /**
  66. * Set this object to have a new UNIX timestamp.
  67. *
  68. * @param string|integer $timestamp OPTIONAL timestamp; defaults to local time using time()
  69. * @return string|integer old timestamp
  70. * @throws Zend_Date_Exception
  71. */
  72. protected function setUnixTimestamp($timestamp = null)
  73. {
  74. $old = $this->_unixTimestamp;
  75. if (is_numeric($timestamp)) {
  76. $this->_unixTimestamp = $timestamp;
  77. } else if ($timestamp === null) {
  78. $this->_unixTimestamp = time();
  79. } else {
  80. require_once 'Zend/Date/Exception.php';
  81. throw new Zend_Date_Exception('\'' . $timestamp . '\' is not a valid UNIX timestamp', 0, null, $timestamp);
  82. }
  83. return $old;
  84. }
  85. /**
  86. * Returns this object's UNIX timestamp
  87. * A timestamp greater then the integer range will be returned as string
  88. * This function does not return the timestamp as object. Use copy() instead.
  89. *
  90. * @return integer|string timestamp
  91. */
  92. protected function getUnixTimestamp()
  93. {
  94. if ($this->_unixTimestamp === intval($this->_unixTimestamp)) {
  95. return (int) $this->_unixTimestamp;
  96. } else {
  97. return (string) $this->_unixTimestamp;
  98. }
  99. }
  100. /**
  101. * Internal function.
  102. * Returns time(). This method exists to allow unit tests to work-around methods that might otherwise
  103. * be hard-coded to use time(). For example, this makes it possible to test isYesterday() in Date.php.
  104. *
  105. * @param integer $sync OPTIONAL time syncronisation value
  106. * @return integer timestamp
  107. */
  108. protected function _getTime($sync = null)
  109. {
  110. if ($sync !== null) {
  111. $this->_syncronised = round($sync);
  112. }
  113. return (time() + $this->_syncronised);
  114. }
  115. /**
  116. * Internal mktime function used by Zend_Date.
  117. * The timestamp returned by mktime() can exceed the precision of traditional UNIX timestamps,
  118. * by allowing PHP to auto-convert to using a float value.
  119. *
  120. * Returns a timestamp relative to 1970/01/01 00:00:00 GMT/UTC.
  121. * DST (Summer/Winter) is depriciated since php 5.1.0.
  122. * Year has to be 4 digits otherwise it would be recognised as
  123. * year 70 AD instead of 1970 AD as expected !!
  124. *
  125. * @param integer $hour
  126. * @param integer $minute
  127. * @param integer $second
  128. * @param integer $month
  129. * @param integer $day
  130. * @param integer $year
  131. * @param boolean $gmt OPTIONAL true = other arguments are for UTC time, false = arguments are for local time/date
  132. * @return integer|float timestamp (number of seconds elapsed relative to 1970/01/01 00:00:00 GMT/UTC)
  133. */
  134. protected function mktime($hour, $minute, $second, $month, $day, $year, $gmt = false)
  135. {
  136. // complete date but in 32bit timestamp - use PHP internal
  137. if ((1901 < $year) and ($year < 2038)) {
  138. $oldzone = @date_default_timezone_get();
  139. // Timezone also includes DST settings, therefor substracting the GMT offset is not enough
  140. // We have to set the correct timezone to get the right value
  141. if (($this->_timezone != $oldzone) and ($gmt === false)) {
  142. date_default_timezone_set($this->_timezone);
  143. }
  144. $result = ($gmt) ? @gmmktime($hour, $minute, $second, $month, $day, $year)
  145. : @mktime($hour, $minute, $second, $month, $day, $year);
  146. date_default_timezone_set($oldzone);
  147. return $result;
  148. }
  149. if ($gmt !== true) {
  150. $second += $this->_offset;
  151. }
  152. if (isset(self::$_cache)) {
  153. $id = strtr('Zend_DateObject_mkTime_' . $this->_offset . '_' . $year.$month.$day.'_'.$hour.$minute.$second . '_'.(int)$gmt, '-','_');
  154. if ($result = self::$_cache->load($id)) {
  155. return unserialize($result);
  156. }
  157. }
  158. // date to integer
  159. $day = intval($day);
  160. $month = intval($month);
  161. $year = intval($year);
  162. // correct months > 12 and months < 1
  163. if ($month > 12) {
  164. $overlap = floor($month / 12);
  165. $year += $overlap;
  166. $month -= $overlap * 12;
  167. } else {
  168. $overlap = ceil((1 - $month) / 12);
  169. $year -= $overlap;
  170. $month += $overlap * 12;
  171. }
  172. $date = 0;
  173. if ($year >= 1970) {
  174. // Date is after UNIX epoch
  175. // go through leapyears
  176. // add months from latest given year
  177. for ($count = 1970; $count <= $year; $count++) {
  178. $leapyear = self::isYearLeapYear($count);
  179. if ($count < $year) {
  180. $date += 365;
  181. if ($leapyear === true) {
  182. $date++;
  183. }
  184. } else {
  185. for ($mcount = 0; $mcount < ($month - 1); $mcount++) {
  186. $date += self::$_monthTable[$mcount];
  187. if (($leapyear === true) and ($mcount == 1)) {
  188. $date++;
  189. }
  190. }
  191. }
  192. }
  193. $date += $day - 1;
  194. $date = (($date * 86400) + ($hour * 3600) + ($minute * 60) + $second);
  195. } else {
  196. // Date is before UNIX epoch
  197. // go through leapyears
  198. // add months from latest given year
  199. for ($count = 1969; $count >= $year; $count--) {
  200. $leapyear = self::isYearLeapYear($count);
  201. if ($count > $year)
  202. {
  203. $date += 365;
  204. if ($leapyear === true)
  205. $date++;
  206. } else {
  207. for ($mcount = 11; $mcount > ($month - 1); $mcount--) {
  208. $date += self::$_monthTable[$mcount];
  209. if (($leapyear === true) and ($mcount == 2)) {
  210. $date++;
  211. }
  212. }
  213. }
  214. }
  215. $date += (self::$_monthTable[$month - 1] - $day);
  216. $date = -(($date * 86400) + (86400 - (($hour * 3600) + ($minute * 60) + $second)));
  217. // gregorian correction for 5.Oct.1582
  218. if ($date < -12220185600) {
  219. $date += 864000;
  220. } else if ($date < -12219321600) {
  221. $date = -12219321600;
  222. }
  223. }
  224. if (isset(self::$_cache)) {
  225. if (self::$_cacheTags) {
  226. self::$_cache->save( serialize($date), $id, array('Zend_Date'));
  227. } else {
  228. self::$_cache->save( serialize($date), $id);
  229. }
  230. }
  231. return $date;
  232. }
  233. /**
  234. * Returns true, if given $year is a leap year.
  235. *
  236. * @param integer $year
  237. * @return boolean true, if year is leap year
  238. */
  239. protected static function isYearLeapYear($year)
  240. {
  241. // all leapyears can be divided through 4
  242. if (($year % 4) != 0) {
  243. return false;
  244. }
  245. // all leapyears can be divided through 400
  246. if ($year % 400 == 0) {
  247. return true;
  248. } else if (($year > 1582) and ($year % 100 == 0)) {
  249. return false;
  250. }
  251. return true;
  252. }
  253. /**
  254. * Internal mktime function used by Zend_Date for handling 64bit timestamps.
  255. *
  256. * Returns a formatted date for a given timestamp.
  257. *
  258. * @param string $format format for output
  259. * @param mixed $timestamp
  260. * @param boolean $gmt OPTIONAL true = other arguments are for UTC time, false = arguments are for local time/date
  261. * @return string
  262. */
  263. protected function date($format, $timestamp = null, $gmt = false)
  264. {
  265. $oldzone = @date_default_timezone_get();
  266. if ($this->_timezone != $oldzone) {
  267. date_default_timezone_set($this->_timezone);
  268. }
  269. if ($timestamp === null) {
  270. $result = ($gmt) ? @gmdate($format) : @date($format);
  271. date_default_timezone_set($oldzone);
  272. return $result;
  273. }
  274. if (abs($timestamp) <= 0x7FFFFFFF) {
  275. $result = ($gmt) ? @gmdate($format, $timestamp) : @date($format, $timestamp);
  276. date_default_timezone_set($oldzone);
  277. return $result;
  278. }
  279. $jump = false;
  280. $origstamp = $timestamp;
  281. if (isset(self::$_cache)) {
  282. $idstamp = strtr('Zend_DateObject_date_' . $this->_offset . '_'. $timestamp . '_'.(int)$gmt, '-','_');
  283. if ($result2 = self::$_cache->load($idstamp)) {
  284. $timestamp = unserialize($result2);
  285. $jump = true;
  286. }
  287. }
  288. // check on false or null alone fails
  289. if (empty($gmt) and empty($jump)) {
  290. $tempstamp = $timestamp;
  291. if ($tempstamp > 0) {
  292. while (abs($tempstamp) > 0x7FFFFFFF) {
  293. $tempstamp -= (86400 * 23376);
  294. }
  295. $dst = date("I", $tempstamp);
  296. if ($dst === 1) {
  297. $timestamp += 3600;
  298. }
  299. $temp = date('Z', $tempstamp);
  300. $timestamp += $temp;
  301. }
  302. if (isset(self::$_cache)) {
  303. if (self::$_cacheTags) {
  304. self::$_cache->save( serialize($timestamp), $idstamp, array('Zend_Date'));
  305. } else {
  306. self::$_cache->save( serialize($timestamp), $idstamp);
  307. }
  308. }
  309. }
  310. if (($timestamp < 0) and ($gmt !== true)) {
  311. $timestamp -= $this->_offset;
  312. }
  313. date_default_timezone_set($oldzone);
  314. $date = $this->getDateParts($timestamp, true);
  315. $length = strlen($format);
  316. $output = '';
  317. for ($i = 0; $i < $length; $i++) {
  318. switch($format[$i]) {
  319. // day formats
  320. case 'd': // day of month, 2 digits, with leading zero, 01 - 31
  321. $output .= (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']);
  322. break;
  323. case 'D': // day of week, 3 letters, Mon - Sun
  324. $output .= date('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
  325. break;
  326. case 'j': // day of month, without leading zero, 1 - 31
  327. $output .= $date['mday'];
  328. break;
  329. case 'l': // day of week, full string name, Sunday - Saturday
  330. $output .= date('l', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
  331. break;
  332. case 'N': // ISO 8601 numeric day of week, 1 - 7
  333. $day = self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
  334. if ($day == 0) {
  335. $day = 7;
  336. }
  337. $output .= $day;
  338. break;
  339. case 'S': // english suffix for day of month, st nd rd th
  340. if (($date['mday'] % 10) == 1) {
  341. $output .= 'st';
  342. } else if ((($date['mday'] % 10) == 2) and ($date['mday'] != 12)) {
  343. $output .= 'nd';
  344. } else if (($date['mday'] % 10) == 3) {
  345. $output .= 'rd';
  346. } else {
  347. $output .= 'th';
  348. }
  349. break;
  350. case 'w': // numeric day of week, 0 - 6
  351. $output .= self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
  352. break;
  353. case 'z': // day of year, 0 - 365
  354. $output .= $date['yday'];
  355. break;
  356. // week formats
  357. case 'W': // ISO 8601, week number of year
  358. $output .= $this->weekNumber($date['year'], $date['mon'], $date['mday']);
  359. break;
  360. // month formats
  361. case 'F': // string month name, january - december
  362. $output .= date('F', mktime(0, 0, 0, $date['mon'], 2, 1971));
  363. break;
  364. case 'm': // number of month, with leading zeros, 01 - 12
  365. $output .= (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']);
  366. break;
  367. case 'M': // 3 letter month name, Jan - Dec
  368. $output .= date('M',mktime(0, 0, 0, $date['mon'], 2, 1971));
  369. break;
  370. case 'n': // number of month, without leading zeros, 1 - 12
  371. $output .= $date['mon'];
  372. break;
  373. case 't': // number of day in month
  374. $output .= self::$_monthTable[$date['mon'] - 1];
  375. break;
  376. // year formats
  377. case 'L': // is leap year ?
  378. $output .= (self::isYearLeapYear($date['year'])) ? '1' : '0';
  379. break;
  380. case 'o': // ISO 8601 year number
  381. $week = $this->weekNumber($date['year'], $date['mon'], $date['mday']);
  382. if (($week > 50) and ($date['mon'] == 1)) {
  383. $output .= ($date['year'] - 1);
  384. } else {
  385. $output .= $date['year'];
  386. }
  387. break;
  388. case 'Y': // year number, 4 digits
  389. $output .= $date['year'];
  390. break;
  391. case 'y': // year number, 2 digits
  392. $output .= substr($date['year'], strlen($date['year']) - 2, 2);
  393. break;
  394. // time formats
  395. case 'a': // lower case am/pm
  396. $output .= (($date['hours'] >= 12) ? 'pm' : 'am');
  397. break;
  398. case 'A': // upper case am/pm
  399. $output .= (($date['hours'] >= 12) ? 'PM' : 'AM');
  400. break;
  401. case 'B': // swatch internet time
  402. $dayseconds = ($date['hours'] * 3600) + ($date['minutes'] * 60) + $date['seconds'];
  403. if ($gmt === true) {
  404. $dayseconds += 3600;
  405. }
  406. $output .= (int) (($dayseconds % 86400) / 86.4);
  407. break;
  408. case 'g': // hours without leading zeros, 12h format
  409. if ($date['hours'] > 12) {
  410. $hour = $date['hours'] - 12;
  411. } else {
  412. if ($date['hours'] == 0) {
  413. $hour = '12';
  414. } else {
  415. $hour = $date['hours'];
  416. }
  417. }
  418. $output .= $hour;
  419. break;
  420. case 'G': // hours without leading zeros, 24h format
  421. $output .= $date['hours'];
  422. break;
  423. case 'h': // hours with leading zeros, 12h format
  424. if ($date['hours'] > 12) {
  425. $hour = $date['hours'] - 12;
  426. } else {
  427. if ($date['hours'] == 0) {
  428. $hour = '12';
  429. } else {
  430. $hour = $date['hours'];
  431. }
  432. }
  433. $output .= (($hour < 10) ? '0'.$hour : $hour);
  434. break;
  435. case 'H': // hours with leading zeros, 24h format
  436. $output .= (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']);
  437. break;
  438. case 'i': // minutes with leading zeros
  439. $output .= (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']);
  440. break;
  441. case 's': // seconds with leading zeros
  442. $output .= (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']);
  443. break;
  444. // timezone formats
  445. case 'e': // timezone identifier
  446. if ($gmt === true) {
  447. $output .= gmdate('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
  448. $date['mon'], $date['mday'], 2000));
  449. } else {
  450. $output .= date('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
  451. $date['mon'], $date['mday'], 2000));
  452. }
  453. break;
  454. case 'I': // daylight saving time or not
  455. if ($gmt === true) {
  456. $output .= gmdate('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
  457. $date['mon'], $date['mday'], 2000));
  458. } else {
  459. $output .= date('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
  460. $date['mon'], $date['mday'], 2000));
  461. }
  462. break;
  463. case 'O': // difference to GMT in hours
  464. $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
  465. $output .= sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
  466. break;
  467. case 'P': // difference to GMT with colon
  468. $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
  469. $gmtstr = sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
  470. $output = $output . substr($gmtstr, 0, 3) . ':' . substr($gmtstr, 3);
  471. break;
  472. case 'T': // timezone settings
  473. if ($gmt === true) {
  474. $output .= gmdate('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
  475. $date['mon'], $date['mday'], 2000));
  476. } else {
  477. $output .= date('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
  478. $date['mon'], $date['mday'], 2000));
  479. }
  480. break;
  481. case 'Z': // timezone offset in seconds
  482. $output .= ($gmt === true) ? 0 : -$this->getGmtOffset();
  483. break;
  484. // complete time formats
  485. case 'c': // ISO 8601 date format
  486. $difference = $this->getGmtOffset();
  487. $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
  488. $difference = substr($difference, 0, 3) . ':' . substr($difference, 3);
  489. $output .= $date['year'] . '-'
  490. . (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']) . '-'
  491. . (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']) . 'T'
  492. . (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']) . ':'
  493. . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
  494. . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds'])
  495. . $difference;
  496. break;
  497. case 'r': // RFC 2822 date format
  498. $difference = $this->getGmtOffset();
  499. $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
  500. $output .= gmdate('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday']))) . ', '
  501. . (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']) . ' '
  502. . date('M', mktime(0, 0, 0, $date['mon'], 2, 1971)) . ' '
  503. . $date['year'] . ' '
  504. . (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']) . ':'
  505. . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
  506. . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']) . ' '
  507. . $difference;
  508. break;
  509. case 'U': // Unix timestamp
  510. $output .= $origstamp;
  511. break;
  512. // special formats
  513. case "\\": // next letter to print with no format
  514. $i++;
  515. if ($i < $length) {
  516. $output .= $format[$i];
  517. }
  518. break;
  519. default: // letter is no format so add it direct
  520. $output .= $format[$i];
  521. break;
  522. }
  523. }
  524. return (string) $output;
  525. }
  526. /**
  527. * Returns the day of week for a Gregorian calendar date.
  528. * 0 = sunday, 6 = saturday
  529. *
  530. * @param integer $year
  531. * @param integer $month
  532. * @param integer $day
  533. * @return integer dayOfWeek
  534. */
  535. protected static function dayOfWeek($year, $month, $day)
  536. {
  537. if ((1901 < $year) and ($year < 2038)) {
  538. return (int) date('w', mktime(0, 0, 0, $month, $day, $year));
  539. }
  540. // gregorian correction
  541. $correction = 0;
  542. if (($year < 1582) or (($year == 1582) and (($month < 10) or (($month == 10) && ($day < 15))))) {
  543. $correction = 3;
  544. }
  545. if ($month > 2) {
  546. $month -= 2;
  547. } else {
  548. $month += 10;
  549. $year--;
  550. }
  551. $day = floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4);
  552. $day += floor(($year / 100) / 4) - 2 * floor($year / 100) + 77 + $correction;
  553. return (int) ($day - 7 * floor($day / 7));
  554. }
  555. /**
  556. * Internal getDateParts function for handling 64bit timestamps, similar to:
  557. * http://www.php.net/getdate
  558. *
  559. * Returns an array of date parts for $timestamp, relative to 1970/01/01 00:00:00 GMT/UTC.
  560. *
  561. * $fast specifies ALL date parts should be returned (slower)
  562. * Default is false, and excludes $dayofweek, weekday, month and timestamp from parts returned.
  563. *
  564. * @param mixed $timestamp
  565. * @param boolean $fast OPTIONAL defaults to fast (false), resulting in fewer date parts
  566. * @return array
  567. */
  568. protected function getDateParts($timestamp = null, $fast = null)
  569. {
  570. // actual timestamp
  571. if (!is_numeric($timestamp)) {
  572. return getdate();
  573. }
  574. // 32bit timestamp
  575. if (abs($timestamp) <= 0x7FFFFFFF) {
  576. return @getdate((int) $timestamp);
  577. }
  578. if (isset(self::$_cache)) {
  579. $id = strtr('Zend_DateObject_getDateParts_' . $timestamp.'_'.(int)$fast, '-','_');
  580. if ($result = self::$_cache->load($id)) {
  581. return unserialize($result);
  582. }
  583. }
  584. $otimestamp = $timestamp;
  585. $numday = 0;
  586. $month = 0;
  587. // gregorian correction
  588. if ($timestamp < -12219321600) {
  589. $timestamp -= 864000;
  590. }
  591. // timestamp lower 0
  592. if ($timestamp < 0) {
  593. $sec = 0;
  594. $act = 1970;
  595. // iterate through 10 years table, increasing speed
  596. foreach(self::$_yearTable as $year => $seconds) {
  597. if ($timestamp >= $seconds) {
  598. $i = $act;
  599. break;
  600. }
  601. $sec = $seconds;
  602. $act = $year;
  603. }
  604. $timestamp -= $sec;
  605. if (!isset($i)) {
  606. $i = $act;
  607. }
  608. // iterate the max last 10 years
  609. do {
  610. --$i;
  611. $day = $timestamp;
  612. $timestamp += 31536000;
  613. $leapyear = self::isYearLeapYear($i);
  614. if ($leapyear === true) {
  615. $timestamp += 86400;
  616. }
  617. if ($timestamp >= 0) {
  618. $year = $i;
  619. break;
  620. }
  621. } while ($timestamp < 0);
  622. $secondsPerYear = 86400 * ($leapyear ? 366 : 365) + $day;
  623. $timestamp = $day;
  624. // iterate through months
  625. for ($i = 12; --$i >= 0;) {
  626. $day = $timestamp;
  627. $timestamp += self::$_monthTable[$i] * 86400;
  628. if (($leapyear === true) and ($i == 1)) {
  629. $timestamp += 86400;
  630. }
  631. if ($timestamp >= 0) {
  632. $month = $i;
  633. $numday = self::$_monthTable[$i];
  634. if (($leapyear === true) and ($i == 1)) {
  635. ++$numday;
  636. }
  637. break;
  638. }
  639. }
  640. $timestamp = $day;
  641. $numberdays = $numday + ceil(($timestamp + 1) / 86400);
  642. $timestamp += ($numday - $numberdays + 1) * 86400;
  643. $hours = floor($timestamp / 3600);
  644. } else {
  645. // iterate through years
  646. for ($i = 1970;;$i++) {
  647. $day = $timestamp;
  648. $timestamp -= 31536000;
  649. $leapyear = self::isYearLeapYear($i);
  650. if ($leapyear === true) {
  651. $timestamp -= 86400;
  652. }
  653. if ($timestamp < 0) {
  654. $year = $i;
  655. break;
  656. }
  657. }
  658. $secondsPerYear = $day;
  659. $timestamp = $day;
  660. // iterate through months
  661. for ($i = 0; $i <= 11; $i++) {
  662. $day = $timestamp;
  663. $timestamp -= self::$_monthTable[$i] * 86400;
  664. if (($leapyear === true) and ($i == 1)) {
  665. $timestamp -= 86400;
  666. }
  667. if ($timestamp < 0) {
  668. $month = $i;
  669. $numday = self::$_monthTable[$i];
  670. if (($leapyear === true) and ($i == 1)) {
  671. ++$numday;
  672. }
  673. break;
  674. }
  675. }
  676. $timestamp = $day;
  677. $numberdays = ceil(($timestamp + 1) / 86400);
  678. $timestamp = $timestamp - ($numberdays - 1) * 86400;
  679. $hours = floor($timestamp / 3600);
  680. }
  681. $timestamp -= $hours * 3600;
  682. $month += 1;
  683. $minutes = floor($timestamp / 60);
  684. $seconds = $timestamp - $minutes * 60;
  685. if ($fast === true) {
  686. $array = array(
  687. 'seconds' => $seconds,
  688. 'minutes' => $minutes,
  689. 'hours' => $hours,
  690. 'mday' => $numberdays,
  691. 'mon' => $month,
  692. 'year' => $year,
  693. 'yday' => floor($secondsPerYear / 86400),
  694. );
  695. } else {
  696. $dayofweek = self::dayOfWeek($year, $month, $numberdays);
  697. $array = array(
  698. 'seconds' => $seconds,
  699. 'minutes' => $minutes,
  700. 'hours' => $hours,
  701. 'mday' => $numberdays,
  702. 'wday' => $dayofweek,
  703. 'mon' => $month,
  704. 'year' => $year,
  705. 'yday' => floor($secondsPerYear / 86400),
  706. 'weekday' => gmdate('l', 86400 * (3 + $dayofweek)),
  707. 'month' => gmdate('F', mktime(0, 0, 0, $month, 1, 1971)),
  708. 0 => $otimestamp
  709. );
  710. }
  711. if (isset(self::$_cache)) {
  712. if (self::$_cacheTags) {
  713. self::$_cache->save( serialize($array), $id, array('Zend_Date'));
  714. } else {
  715. self::$_cache->save( serialize($array), $id);
  716. }
  717. }
  718. return $array;
  719. }
  720. /**
  721. * Internal getWeekNumber function for handling 64bit timestamps
  722. *
  723. * Returns the ISO 8601 week number of a given date
  724. *
  725. * @param integer $year
  726. * @param integer $month
  727. * @param integer $day
  728. * @return integer
  729. */
  730. protected function weekNumber($year, $month, $day)
  731. {
  732. if ((1901 < $year) and ($year < 2038)) {
  733. return (int) date('W', mktime(0, 0, 0, $month, $day, $year));
  734. }
  735. $dayofweek = self::dayOfWeek($year, $month, $day);
  736. $firstday = self::dayOfWeek($year, 1, 1);
  737. if (($month == 1) and (($firstday < 1) or ($firstday > 4)) and ($day < 4)) {
  738. $firstday = self::dayOfWeek($year - 1, 1, 1);
  739. $month = 12;
  740. $day = 31;
  741. } else if (($month == 12) and ((self::dayOfWeek($year + 1, 1, 1) < 5) and
  742. (self::dayOfWeek($year + 1, 1, 1) > 0))) {
  743. return 1;
  744. }
  745. return intval (((self::dayOfWeek($year, 1, 1) < 5) and (self::dayOfWeek($year, 1, 1) > 0)) +
  746. 4 * ($month - 1) + (2 * ($month - 1) + ($day - 1) + $firstday - $dayofweek + 6) * 36 / 256);
  747. }
  748. /**
  749. * Internal _range function
  750. * Sets the value $a to be in the range of [0, $b]
  751. *
  752. * @param float $a - value to correct
  753. * @param float $b - maximum range to set
  754. */
  755. private function _range($a, $b) {
  756. while ($a < 0) {
  757. $a += $b;
  758. }
  759. while ($a >= $b) {
  760. $a -= $b;
  761. }
  762. return $a;
  763. }
  764. /**
  765. * Calculates the sunrise or sunset based on a location
  766. *
  767. * @param array $location Location for calculation MUST include 'latitude', 'longitude', 'horizon'
  768. * @param bool $horizon true: sunrise; false: sunset
  769. * @return mixed - false: midnight sun, integer:
  770. */
  771. protected function calcSun($location, $horizon, $rise = false)
  772. {
  773. // timestamp within 32bit
  774. if (abs($this->_unixTimestamp) <= 0x7FFFFFFF) {
  775. if ($rise === false) {
  776. return date_sunset($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
  777. $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
  778. }
  779. return date_sunrise($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
  780. $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
  781. }
  782. // self calculation - timestamp bigger than 32bit
  783. // fix circle values
  784. $quarterCircle = 0.5 * M_PI;
  785. $halfCircle = M_PI;
  786. $threeQuarterCircle = 1.5 * M_PI;
  787. $fullCircle = 2 * M_PI;
  788. // radiant conversion for coordinates
  789. $radLatitude = $location['latitude'] * $halfCircle / 180;
  790. $radLongitude = $location['longitude'] * $halfCircle / 180;
  791. // get solar coordinates
  792. $tmpRise = $rise ? $quarterCircle : $threeQuarterCircle;
  793. $radDay = $this->date('z',$this->_unixTimestamp) + ($tmpRise - $radLongitude) / $fullCircle;
  794. // solar anomoly and longitude
  795. $solAnomoly = $radDay * 0.017202 - 0.0574039;
  796. $solLongitude = $solAnomoly + 0.0334405 * sin($solAnomoly);
  797. $solLongitude += 4.93289 + 3.49066E-4 * sin(2 * $solAnomoly);
  798. // get quadrant
  799. $solLongitude = $this->_range($solLongitude, $fullCircle);
  800. if (($solLongitude / $quarterCircle) - intval($solLongitude / $quarterCircle) == 0) {
  801. $solLongitude += 4.84814E-6;
  802. }
  803. // solar ascension
  804. $solAscension = sin($solLongitude) / cos($solLongitude);
  805. $solAscension = atan2(0.91746 * $solAscension, 1);
  806. // adjust quadrant
  807. if ($solLongitude > $threeQuarterCircle) {
  808. $solAscension += $fullCircle;
  809. } else if ($solLongitude > $quarterCircle) {
  810. $solAscension += $halfCircle;
  811. }
  812. // solar declination
  813. $solDeclination = 0.39782 * sin($solLongitude);
  814. $solDeclination /= sqrt(-$solDeclination * $solDeclination + 1);
  815. $solDeclination = atan2($solDeclination, 1);
  816. $solHorizon = $horizon - sin($solDeclination) * sin($radLatitude);
  817. $solHorizon /= cos($solDeclination) * cos($radLatitude);
  818. // midnight sun, always night
  819. if (abs($solHorizon) > 1) {
  820. return false;
  821. }
  822. $solHorizon /= sqrt(-$solHorizon * $solHorizon + 1);
  823. $solHorizon = $quarterCircle - atan2($solHorizon, 1);
  824. if ($rise) {
  825. $solHorizon = $fullCircle - $solHorizon;
  826. }
  827. // time calculation
  828. $localTime = $solHorizon + $solAscension - 0.0172028 * $radDay - 1.73364;
  829. $universalTime = $localTime - $radLongitude;
  830. // determinate quadrant
  831. $universalTime = $this->_range($universalTime, $fullCircle);
  832. // radiant to hours
  833. $universalTime *= 24 / $fullCircle;
  834. // convert to time
  835. $hour = intval($universalTime);
  836. $universalTime = ($universalTime - $hour) * 60;
  837. $min = intval($universalTime);
  838. $universalTime = ($universalTime - $min) * 60;
  839. $sec = intval($universalTime);
  840. return $this->mktime($hour, $min, $sec, $this->date('m', $this->_unixTimestamp),
  841. $this->date('j', $this->_unixTimestamp), $this->date('Y', $this->_unixTimestamp),
  842. -1, true);
  843. }
  844. /**
  845. * Sets a new timezone for calculation of $this object's gmt offset.
  846. * For a list of supported timezones look here: http://php.net/timezones
  847. * If no timezone can be detected or the given timezone is wrong UTC will be set.
  848. *
  849. * @param string $zone OPTIONAL timezone for date calculation; defaults to date_default_timezone_get()
  850. * @return Zend_Date_DateObject Provides fluent interface
  851. * @throws Zend_Date_Exception
  852. */
  853. public function setTimezone($zone = null)
  854. {
  855. $oldzone = @date_default_timezone_get();
  856. if ($zone === null) {
  857. $zone = $oldzone;
  858. }
  859. // throw an error on false input, but only if the new date extension is available
  860. if (function_exists('timezone_open')) {
  861. if (!@timezone_open($zone)) {
  862. require_once 'Zend/Date/Exception.php';
  863. throw new Zend_Date_Exception("timezone ($zone) is not a known timezone", 0, null, $zone);
  864. }
  865. }
  866. // this can generate an error if the date extension is not available and a false timezone is given
  867. $result = @date_default_timezone_set($zone);
  868. if ($result === true) {
  869. $this->_offset = mktime(0, 0, 0, 1, 2, 1970) - gmmktime(0, 0, 0, 1, 2, 1970);
  870. $this->_timezone = $zone;
  871. }
  872. date_default_timezone_set($oldzone);
  873. if (($zone == 'UTC') or ($zone == 'GMT')) {
  874. $this->_dst = false;
  875. } else {
  876. $this->_dst = true;
  877. }
  878. return $this;
  879. }
  880. /**
  881. * Return the timezone of $this object.
  882. * The timezone is initially set when the object is instantiated.
  883. *
  884. * @return string actual set timezone string
  885. */
  886. public function getTimezone()
  887. {
  888. return $this->_timezone;
  889. }
  890. /**
  891. * Return the offset to GMT of $this object's timezone.
  892. * The offset to GMT is initially set when the object is instantiated using the currently,
  893. * in effect, default timezone for PHP functions.
  894. *
  895. * @return integer seconds difference between GMT timezone and timezone when object was instantiated
  896. */
  897. public function getGmtOffset()
  898. {
  899. $date = $this->getDateParts($this->getUnixTimestamp(), true);
  900. $zone = @date_default_timezone_get();
  901. $result = @date_default_timezone_set($this->_timezone);
  902. if ($result === true) {
  903. $offset = $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
  904. $date['mon'], $date['mday'], $date['year'], false)
  905. - $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
  906. $date['mon'], $date['mday'], $date['year'], true);
  907. }
  908. date_default_timezone_set($zone);
  909. return $offset;
  910. }
  911. /**
  912. * Internal method to check if the given cache supports tags
  913. *
  914. * @param Zend_Cache $cache
  915. */
  916. protected static function _getTagSupportForCache()
  917. {
  918. $backend = self::$_cache->getBackend();
  919. if ($backend instanceof Zend_Cache_Backend_ExtendedInterface) {
  920. $cacheOptions = $backend->getCapabilities();
  921. self::$_cacheTags = $cacheOptions['tags'];
  922. } else {
  923. self::$_cacheTags = false;
  924. }
  925. return self::$_cacheTags;
  926. }
  927. }