PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/rdfapi-php/api/util/adodb/adodb-time.inc.php

https://github.com/komagata/plnet
PHP | 967 lines | 504 code | 97 blank | 366 comment | 193 complexity | b75dbb98a3c8930e74a2e26558666abe MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. ADOdb Date Library, part of the ADOdb abstraction library
  4. Download: http://php.weblogs.com/adodb_date_time_library
  5. PHP native date functions use integer timestamps for computations.
  6. Because of this, dates are restricted to the years 1901-2038 on Unix
  7. and 1970-2038 on Windows due to integer overflow for dates beyond
  8. those years. This library overcomes these limitations by replacing the
  9. native function's signed integers (normally 32-bits) with PHP floating
  10. point numbers (normally 64-bits).
  11. Dates from 100 A.D. to 3000 A.D. and later
  12. have been tested. The minimum is 100 A.D. as <100 will invoke the
  13. 2 => 4 digit year conversion. The maximum is billions of years in the
  14. future, but this is a theoretical limit as the computation of that year
  15. would take too long with the current implementation of adodb_mktime().
  16. This library replaces native functions as follows:
  17. <pre>
  18. getdate() with adodb_getdate()
  19. date() with adodb_date()
  20. gmdate() with adodb_gmdate()
  21. mktime() with adodb_mktime()
  22. gmmktime() with adodb_gmmktime()
  23. </pre>
  24. The parameters are identical, except that adodb_date() accepts a subset
  25. of date()'s field formats. Mktime() will convert from local time to GMT,
  26. and date() will convert from GMT to local time, but daylight savings is
  27. not handled currently.
  28. This library is independant of the rest of ADOdb, and can be used
  29. as standalone code.
  30. PERFORMANCE
  31. For high speed, this library uses the native date functions where
  32. possible, and only switches to PHP code when the dates fall outside
  33. the 32-bit signed integer range.
  34. GREGORIAN CORRECTION
  35. Pope Gregory shortened October of A.D. 1582 by ten days. Thursday,
  36. October 4, 1582 (Julian) was followed immediately by Friday, October 15,
  37. 1582 (Gregorian).
  38. Since 0.06, we handle this correctly, so:
  39. adodb_mktime(0,0,0,10,15,1582) - adodb_mktime(0,0,0,10,4,1582)
  40. == 24 * 3600 (1 day)
  41. =============================================================================
  42. COPYRIGHT
  43. (c) 2003 John Lim and released under BSD-style license except for code by jackbbs,
  44. which includes adodb_mktime, adodb_get_gmt_diff, adodb_is_leap_year
  45. and originally found at http://www.php.net/manual/en/function.mktime.php
  46. =============================================================================
  47. BUG REPORTS
  48. These should be posted to the ADOdb forums at
  49. http://phplens.com/lens/lensforum/topics.php?id=4
  50. =============================================================================
  51. FUNCTION DESCRIPTIONS
  52. FUNCTION adodb_getdate($date=false)
  53. Returns an array containing date information, as getdate(), but supports
  54. dates greater than 1901 to 2038.
  55. FUNCTION adodb_date($fmt, $timestamp = false)
  56. Convert a timestamp to a formatted local date. If $timestamp is not defined, the
  57. current timestamp is used. Unlike the function date(), it supports dates
  58. outside the 1901 to 2038 range.
  59. The format fields that adodb_date supports:
  60. <pre>
  61. a - "am" or "pm"
  62. A - "AM" or "PM"
  63. d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
  64. D - day of the week, textual, 3 letters; e.g. "Fri"
  65. F - month, textual, long; e.g. "January"
  66. g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
  67. G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
  68. h - hour, 12-hour format; i.e. "01" to "12"
  69. H - hour, 24-hour format; i.e. "00" to "23"
  70. i - minutes; i.e. "00" to "59"
  71. j - day of the month without leading zeros; i.e. "1" to "31"
  72. l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"
  73. L - boolean for whether it is a leap year; i.e. "0" or "1"
  74. m - month; i.e. "01" to "12"
  75. M - month, textual, 3 letters; e.g. "Jan"
  76. n - month without leading zeros; i.e. "1" to "12"
  77. O - Difference to Greenwich time in hours; e.g. "+0200"
  78. Q - Quarter, as in 1, 2, 3, 4
  79. r - RFC 822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200"
  80. s - seconds; i.e. "00" to "59"
  81. S - English ordinal suffix for the day of the month, 2 characters;
  82. i.e. "st", "nd", "rd" or "th"
  83. t - number of days in the given month; i.e. "28" to "31"
  84. T - Timezone setting of this machine; e.g. "EST" or "MDT"
  85. U - seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  86. w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
  87. Y - year, 4 digits; e.g. "1999"
  88. y - year, 2 digits; e.g. "99"
  89. z - day of the year; i.e. "0" to "365"
  90. Z - timezone offset in seconds (i.e. "-43200" to "43200").
  91. The offset for timezones west of UTC is always negative,
  92. and for those east of UTC is always positive.
  93. </pre>
  94. Unsupported:
  95. <pre>
  96. B - Swatch Internet time
  97. I (capital i) - "1" if Daylight Savings Time, "0" otherwise.
  98. W - ISO-8601 week number of year, weeks starting on Monday
  99. </pre>
  100. FUNCTION adodb_date2($fmt, $isoDateString = false)
  101. Same as adodb_date, but 2nd parameter accepts iso date, eg.
  102. adodb_date2('d-M-Y H:i','2003-12-25 13:01:34');
  103. FUNCTION adodb_gmdate($fmt, $timestamp = false)
  104. Convert a timestamp to a formatted GMT date. If $timestamp is not defined, the
  105. current timestamp is used. Unlike the function date(), it supports dates
  106. outside the 1901 to 2038 range.
  107. FUNCTION adodb_mktime($hr, $min, $sec [, $month, $day, $year])
  108. Converts a local date to a unix timestamp. Unlike the function mktime(), it supports
  109. dates outside the 1901 to 2038 range. Differs from mktime() in that all parameters
  110. are currently compulsory.
  111. FUNCTION adodb_gmmktime($hr, $min, $sec [, $month, $day, $year])
  112. Converts a gmt date to a unix timestamp. Unlike the function gmmktime(), it supports
  113. dates outside the 1901 to 2038 range. Differs from gmmktime() in that all parameters
  114. are currently compulsory.
  115. =============================================================================
  116. NOTES
  117. Useful url for generating test timestamps:
  118. http://www.4webhelp.net/us/timestamp.php
  119. Possible future optimizations include
  120. a. Using an algorithm similar to Plauger's in "The Standard C Library"
  121. (page 428, xttotm.c _Ttotm() function). Plauger's algorithm will not
  122. work outside 32-bit signed range, so i decided not to implement it.
  123. b. Iterate over a block of years (say 12) when searching for the
  124. correct year.
  125. c. Implement daylight savings, which looks awfully complicated, see
  126. http://webexhibits.org/daylightsaving/
  127. CHANGELOG
  128. - 18 July 2004 0.15
  129. All params in adodb_mktime were formerly compulsory. Now only the hour, min, secs is compulsory. This
  130. brings it more in line with mktime (still not identical).
  131. - 23 June 2004 0.14
  132. Allow you to define your own daylights savings function, adodb_daylight_sv.
  133. If the function is defined (somewhere in an include), then you can correct for daylights savings.
  134. In this example, we apply daylights savings in June or July, adding one hour. This is extremely
  135. unrealistic as it does not take into account time-zone, geographic location, current year.
  136. function adodb_daylight_sv(&$arr, $is_gmt)
  137. {
  138. if ($is_gmt) return;
  139. $m = $arr['mon'];
  140. if ($m == 6 || $m == 7) $arr['hours'] += 1;
  141. }
  142. This is only called by adodb_date() and not by adodb_mktime().
  143. The format of $arr is
  144. Array (
  145. [seconds] => 0
  146. [minutes] => 0
  147. [hours] => 0
  148. [mday] => 1 # day of month, eg 1st day of the month
  149. [mon] => 2 # month (eg. Feb)
  150. [year] => 2102
  151. [yday] => 31 # days in current year
  152. [leap] => # true if leap year
  153. [ndays] => 28 # no of days in current month
  154. )
  155. - 28 Apr 2004 0.13
  156. Fixed adodb_date to properly support $is_gmt. Thx to Dimitar Angelov.
  157. - 20 Mar 2004 0.12
  158. Fixed month calculation error in adodb_date. 2102-June-01 appeared as 2102-May-32.
  159. - 26 Oct 2003 0.11
  160. Because of daylight savings problems (some systems apply daylight savings to
  161. January!!!), changed adodb_get_gmt_diff() to ignore daylight savings.
  162. - 9 Aug 2003 0.10
  163. Fixed bug with dates after 2038.
  164. See http://phplens.com/lens/lensforum/msgs.php?id=6980
  165. - 1 July 2003 0.09
  166. Added support for Q (Quarter).
  167. Added adodb_date2(), which accepts ISO date in 2nd param
  168. - 3 March 2003 0.08
  169. Added support for 'S' adodb_date() format char. Added constant ADODB_ALLOW_NEGATIVE_TS
  170. if you want PHP to handle negative timestamps between 1901 to 1969.
  171. - 27 Feb 2003 0.07
  172. All negative numbers handled by adodb now because of RH 7.3+ problems.
  173. See http://bugs.php.net/bug.php?id=20048&edit=2
  174. - 4 Feb 2003 0.06
  175. Fixed a typo, 1852 changed to 1582! This means that pre-1852 dates
  176. are now correctly handled.
  177. - 29 Jan 2003 0.05
  178. Leap year checking differs under Julian calendar (pre 1582). Also
  179. leap year code optimized by checking for most common case first.
  180. We also handle month overflow correctly in mktime (eg month set to 13).
  181. Day overflow for less than one month's days is supported.
  182. - 28 Jan 2003 0.04
  183. Gregorian correction handled. In PHP5, we might throw an error if
  184. mktime uses invalid dates around 5-14 Oct 1582. Released with ADOdb 3.10.
  185. Added limbo 5-14 Oct 1582 check, when we set to 15 Oct 1582.
  186. - 27 Jan 2003 0.03
  187. Fixed some more month problems due to gmt issues. Added constant ADODB_DATE_VERSION.
  188. Fixed calculation of days since start of year for <1970.
  189. - 27 Jan 2003 0.02
  190. Changed _adodb_getdate() to inline leap year checking for better performance.
  191. Fixed problem with time-zones west of GMT +0000.
  192. - 24 Jan 2003 0.01
  193. First implementation.
  194. */
  195. /* Initialization */
  196. /*
  197. Version Number
  198. */
  199. define('ADODB_DATE_VERSION',0.15);
  200. /*
  201. We check for Windows as only +ve ints are accepted as dates on Windows.
  202. Apparently this problem happens also with Linux, RH 7.3 and later!
  203. glibc-2.2.5-34 and greater has been changed to return -1 for dates <
  204. 1970. This used to work. The problem exists with RedHat 7.3 and 8.0
  205. echo (mktime(0, 0, 0, 1, 1, 1960)); // prints -1
  206. References:
  207. http://bugs.php.net/bug.php?id=20048&edit=2
  208. http://lists.debian.org/debian-glibc/2002/debian-glibc-200205/msg00010.html
  209. */
  210. if (!defined('ADODB_ALLOW_NEGATIVE_TS')) define('ADODB_NO_NEGATIVE_TS',1);
  211. function adodb_date_test_date($y1,$m)
  212. {
  213. //print " $y1/$m ";
  214. $t = adodb_mktime(0,0,0,$m,13,$y1);
  215. if ("$y1-$m-13 00:00:00" != adodb_date('Y-n-d H:i:s',$t)) {
  216. print "<b>$y1 error</b><br>";
  217. return false;
  218. }
  219. return true;
  220. }
  221. /**
  222. Test Suite
  223. */
  224. function adodb_date_test()
  225. {
  226. error_reporting(E_ALL);
  227. print "<h4>Testing adodb_date and adodb_mktime. version=".ADODB_DATE_VERSION. "</h4>";
  228. @set_time_limit(0);
  229. $fail = false;
  230. // This flag disables calling of PHP native functions, so we can properly test the code
  231. if (!defined('ADODB_TEST_DATES')) define('ADODB_TEST_DATES',1);
  232. $t = adodb_mktime(0,0,0);
  233. if (!(adodb_date('Y-m-d') == date('Y-m-d'))) print 'Error in '.adodb_mktime(0,0,0).'<br>';
  234. $t = adodb_mktime(0,0,0,6,1,2102);
  235. if (!(adodb_date('Y-m-d',$t) == '2102-06-01')) print 'Error in '.adodb_date('Y-m-d',$t).'<br>';
  236. $t = adodb_mktime(0,0,0,2,1,2102);
  237. if (!(adodb_date('Y-m-d',$t) == '2102-02-01')) print 'Error in '.adodb_date('Y-m-d',$t).'<br>';
  238. print "<p>Testing gregorian <=> julian conversion<p>";
  239. $t = adodb_mktime(0,0,0,10,11,1492);
  240. //http://www.holidayorigins.com/html/columbus_day.html - Friday check
  241. if (!(adodb_date('D Y-m-d',$t) == 'Fri 1492-10-11')) print 'Error in Columbus landing<br>';
  242. $t = adodb_mktime(0,0,0,2,29,1500);
  243. if (!(adodb_date('Y-m-d',$t) == '1500-02-29')) print 'Error in julian leap years<br>';
  244. $t = adodb_mktime(0,0,0,2,29,1700);
  245. if (!(adodb_date('Y-m-d',$t) == '1700-03-01')) print 'Error in gregorian leap years<br>';
  246. print adodb_mktime(0,0,0,10,4,1582).' ';
  247. print adodb_mktime(0,0,0,10,15,1582);
  248. $diff = (adodb_mktime(0,0,0,10,15,1582) - adodb_mktime(0,0,0,10,4,1582));
  249. if ($diff != 3600*24) print " <b>Error in gregorian correction = ".($diff/3600/24)." days </b><br>";
  250. print " 15 Oct 1582, Fri=".(adodb_dow(1582,10,15) == 5 ? 'Fri' : '<b>Error</b>')."<br>";
  251. print " 4 Oct 1582, Thu=".(adodb_dow(1582,10,4) == 4 ? 'Thu' : '<b>Error</b>')."<br>";
  252. print "<p>Testing overflow<p>";
  253. $t = adodb_mktime(0,0,0,3,33,1965);
  254. if (!(adodb_date('Y-m-d',$t) == '1965-04-02')) print 'Error in day overflow 1 <br>';
  255. $t = adodb_mktime(0,0,0,4,33,1971);
  256. if (!(adodb_date('Y-m-d',$t) == '1971-05-03')) print 'Error in day overflow 2 <br>';
  257. $t = adodb_mktime(0,0,0,1,60,1965);
  258. if (!(adodb_date('Y-m-d',$t) == '1965-03-01')) print 'Error in day overflow 3 '.adodb_date('Y-m-d',$t).' <br>';
  259. $t = adodb_mktime(0,0,0,12,32,1965);
  260. if (!(adodb_date('Y-m-d',$t) == '1966-01-01')) print 'Error in day overflow 4 '.adodb_date('Y-m-d',$t).' <br>';
  261. $t = adodb_mktime(0,0,0,12,63,1965);
  262. if (!(adodb_date('Y-m-d',$t) == '1966-02-01')) print 'Error in day overflow 5 '.adodb_date('Y-m-d',$t).' <br>';
  263. $t = adodb_mktime(0,0,0,13,3,1965);
  264. if (!(adodb_date('Y-m-d',$t) == '1966-01-03')) print 'Error in mth overflow 1 <br>';
  265. print "Testing 2-digit => 4-digit year conversion<p>";
  266. if (adodb_year_digit_check(00) != 2000) print "Err 2-digit 2000<br>";
  267. if (adodb_year_digit_check(10) != 2010) print "Err 2-digit 2010<br>";
  268. if (adodb_year_digit_check(20) != 2020) print "Err 2-digit 2020<br>";
  269. if (adodb_year_digit_check(30) != 2030) print "Err 2-digit 2030<br>";
  270. if (adodb_year_digit_check(40) != 1940) print "Err 2-digit 1940<br>";
  271. if (adodb_year_digit_check(50) != 1950) print "Err 2-digit 1950<br>";
  272. if (adodb_year_digit_check(90) != 1990) print "Err 2-digit 1990<br>";
  273. // Test string formating
  274. print "<p>Testing date formating</p>";
  275. $fmt = '\d\a\t\e T Y-m-d H:i:s a A d D F g G h H i j l L m M n O \R\F\C822 r s t U w y Y z Z 2003';
  276. $s1 = date($fmt,0);
  277. $s2 = adodb_date($fmt,0);
  278. if ($s1 != $s2) {
  279. print " date() 0 failed<br>$s1<br>$s2<br>";
  280. }
  281. flush();
  282. for ($i=100; --$i > 0; ) {
  283. $ts = 3600.0*((rand()%60000)+(rand()%60000))+(rand()%60000);
  284. $s1 = date($fmt,$ts);
  285. $s2 = adodb_date($fmt,$ts);
  286. //print "$s1 <br>$s2 <p>";
  287. $pos = strcmp($s1,$s2);
  288. if (($s1) != ($s2)) {
  289. for ($j=0,$k=strlen($s1); $j < $k; $j++) {
  290. if ($s1[$j] != $s2[$j]) {
  291. print substr($s1,$j).' ';
  292. break;
  293. }
  294. }
  295. print "<b>Error date(): $ts<br><pre>
  296. &nbsp; \"$s1\" (date len=".strlen($s1).")
  297. &nbsp; \"$s2\" (adodb_date len=".strlen($s2).")</b></pre><br>";
  298. $fail = true;
  299. }
  300. $a1 = getdate($ts);
  301. $a2 = adodb_getdate($ts);
  302. $rez = array_diff($a1,$a2);
  303. if (sizeof($rez)>0) {
  304. print "<b>Error getdate() $ts</b><br>";
  305. print_r($a1);
  306. print "<br>";
  307. print_r($a2);
  308. print "<p>";
  309. $fail = true;
  310. }
  311. }
  312. // Test generation of dates outside 1901-2038
  313. print "<p>Testing random dates between 100 and 4000</p>";
  314. adodb_date_test_date(100,1);
  315. for ($i=100; --$i >= 0;) {
  316. $y1 = 100+rand(0,1970-100);
  317. $m = rand(1,12);
  318. adodb_date_test_date($y1,$m);
  319. $y1 = 3000-rand(0,3000-1970);
  320. adodb_date_test_date($y1,$m);
  321. }
  322. print '<p>';
  323. $start = 1960+rand(0,10);
  324. $yrs = 12;
  325. $i = 365.25*86400*($start-1970);
  326. $offset = 36000+rand(10000,60000);
  327. $max = 365*$yrs*86400;
  328. $lastyear = 0;
  329. // we generate a timestamp, convert it to a date, and convert it back to a timestamp
  330. // and check if the roundtrip broke the original timestamp value.
  331. print "Testing $start to ".($start+$yrs).", or $max seconds, offset=$offset: ";
  332. $cnt = 0;
  333. for ($max += $i; $i < $max; $i += $offset) {
  334. $ret = adodb_date('m,d,Y,H,i,s',$i);
  335. $arr = explode(',',$ret);
  336. if ($lastyear != $arr[2]) {
  337. $lastyear = $arr[2];
  338. print " $lastyear ";
  339. flush();
  340. }
  341. $newi = adodb_mktime($arr[3],$arr[4],$arr[5],$arr[0],$arr[1],$arr[2]);
  342. if ($i != $newi) {
  343. print "Error at $i, adodb_mktime returned $newi ($ret)";
  344. $fail = true;
  345. break;
  346. }
  347. $cnt += 1;
  348. }
  349. echo "Tested $cnt dates<br>";
  350. if (!$fail) print "<p>Passed !</p>";
  351. else print "<p><b>Failed</b> :-(</p>";
  352. }
  353. /**
  354. Returns day of week, 0 = Sunday,... 6=Saturday.
  355. Algorithm from PEAR::Date_Calc
  356. */
  357. function adodb_dow($year, $month, $day)
  358. {
  359. /*
  360. Pope Gregory removed 10 days - October 5 to October 14 - from the year 1582 and
  361. proclaimed that from that time onwards 3 days would be dropped from the calendar
  362. every 400 years.
  363. Thursday, October 4, 1582 (Julian) was followed immediately by Friday, October 15, 1582 (Gregorian).
  364. */
  365. if ($year <= 1582) {
  366. if ($year < 1582 ||
  367. ($year == 1582 && ($month < 10 || ($month == 10 && $day < 15)))) $greg_correction = 3;
  368. else
  369. $greg_correction = 0;
  370. } else
  371. $greg_correction = 0;
  372. if($month > 2)
  373. $month -= 2;
  374. else {
  375. $month += 10;
  376. $year--;
  377. }
  378. $day = ( floor((13 * $month - 1) / 5) +
  379. $day + ($year % 100) +
  380. floor(($year % 100) / 4) +
  381. floor(($year / 100) / 4) - 2 *
  382. floor($year / 100) + 77);
  383. return (($day - 7 * floor($day / 7))) + $greg_correction;
  384. }
  385. /**
  386. Checks for leap year, returns true if it is. No 2-digit year check. Also
  387. handles julian calendar correctly.
  388. */
  389. function _adodb_is_leap_year($year)
  390. {
  391. if ($year % 4 != 0) return false;
  392. if ($year % 400 == 0) {
  393. return true;
  394. // if gregorian calendar (>1582), century not-divisible by 400 is not leap
  395. } else if ($year > 1582 && $year % 100 == 0 ) {
  396. return false;
  397. }
  398. return true;
  399. }
  400. /**
  401. checks for leap year, returns true if it is. Has 2-digit year check
  402. */
  403. function adodb_is_leap_year($year)
  404. {
  405. return _adodb_is_leap_year(adodb_year_digit_check($year));
  406. }
  407. /**
  408. Fix 2-digit years. Works for any century.
  409. Assumes that if 2-digit is more than 30 years in future, then previous century.
  410. */
  411. function adodb_year_digit_check($y)
  412. {
  413. if ($y < 100) {
  414. $yr = (integer) date("Y");
  415. $century = (integer) ($yr /100);
  416. if ($yr%100 > 50) {
  417. $c1 = $century + 1;
  418. $c0 = $century;
  419. } else {
  420. $c1 = $century;
  421. $c0 = $century - 1;
  422. }
  423. $c1 *= 100;
  424. // if 2-digit year is less than 30 years in future, set it to this century
  425. // otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
  426. if (($y + $c1) < $yr+30) $y = $y + $c1;
  427. else $y = $y + $c0*100;
  428. }
  429. return $y;
  430. }
  431. /**
  432. get local time zone offset from GMT
  433. */
  434. function adodb_get_gmt_diff()
  435. {
  436. static $TZ;
  437. if (isset($TZ)) return $TZ;
  438. $TZ = mktime(0,0,0,1,2,1970,0) - gmmktime(0,0,0,1,2,1970,0);
  439. return $TZ;
  440. }
  441. /**
  442. Returns an array with date info.
  443. */
  444. function adodb_getdate($d=false,$fast=false)
  445. {
  446. if ($d === false) return getdate();
  447. if (!defined('ADODB_TEST_DATES')) {
  448. if ((abs($d) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
  449. if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer
  450. return @getdate($d);
  451. }
  452. }
  453. return _adodb_getdate($d);
  454. }
  455. /**
  456. Low-level function that returns the getdate() array. We have a special
  457. $fast flag, which if set to true, will return fewer array values,
  458. and is much faster as it does not calculate dow, etc.
  459. */
  460. function _adodb_getdate($origd=false,$fast=false,$is_gmt=false)
  461. {
  462. $d = $origd - ($is_gmt ? 0 : adodb_get_gmt_diff());
  463. $_day_power = 86400;
  464. $_hour_power = 3600;
  465. $_min_power = 60;
  466. if ($d < -12219321600) $d -= 86400*10; // if 15 Oct 1582 or earlier, gregorian correction
  467. $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
  468. $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
  469. $d366 = $_day_power * 366;
  470. $d365 = $_day_power * 365;
  471. if ($d < 0) {
  472. $origd = $d;
  473. // The valid range of a 32bit signed timestamp is typically from
  474. // Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT
  475. for ($a = 1970 ; --$a >= 0;) {
  476. $lastd = $d;
  477. if ($leaf = _adodb_is_leap_year($a)) $d += $d366;
  478. else $d += $d365;
  479. if ($d >= 0) {
  480. $year = $a;
  481. break;
  482. }
  483. }
  484. $secsInYear = 86400 * ($leaf ? 366 : 365) + $lastd;
  485. $d = $lastd;
  486. $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal;
  487. for ($a = 13 ; --$a > 0;) {
  488. $lastd = $d;
  489. $d += $mtab[$a] * $_day_power;
  490. if ($d >= 0) {
  491. $month = $a;
  492. $ndays = $mtab[$a];
  493. break;
  494. }
  495. }
  496. $d = $lastd;
  497. $day = $ndays + ceil(($d+1) / ($_day_power));
  498. $d += ($ndays - $day+1)* $_day_power;
  499. $hour = floor($d/$_hour_power);
  500. } else {
  501. for ($a = 1970 ;; $a++) {
  502. $lastd = $d;
  503. if ($leaf = _adodb_is_leap_year($a)) $d -= $d366;
  504. else $d -= $d365;
  505. if ($d < 0) {
  506. $year = $a;
  507. break;
  508. }
  509. }
  510. $secsInYear = $lastd;
  511. $d = $lastd;
  512. $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal;
  513. for ($a = 1 ; $a <= 12; $a++) {
  514. $lastd = $d;
  515. $d -= $mtab[$a] * $_day_power;
  516. if ($d < 0) {
  517. $month = $a;
  518. $ndays = $mtab[$a];
  519. break;
  520. }
  521. }
  522. $d = $lastd;
  523. $day = ceil(($d+1) / $_day_power);
  524. $d = $d - ($day-1) * $_day_power;
  525. $hour = floor($d /$_hour_power);
  526. }
  527. $d -= $hour * $_hour_power;
  528. $min = floor($d/$_min_power);
  529. $secs = $d - $min * $_min_power;
  530. if ($fast) {
  531. return array(
  532. 'seconds' => $secs,
  533. 'minutes' => $min,
  534. 'hours' => $hour,
  535. 'mday' => $day,
  536. 'mon' => $month,
  537. 'year' => $year,
  538. 'yday' => floor($secsInYear/$_day_power),
  539. 'leap' => $leaf,
  540. 'ndays' => $ndays
  541. );
  542. }
  543. $dow = adodb_dow($year,$month,$day);
  544. return array(
  545. 'seconds' => $secs,
  546. 'minutes' => $min,
  547. 'hours' => $hour,
  548. 'mday' => $day,
  549. 'wday' => $dow,
  550. 'mon' => $month,
  551. 'year' => $year,
  552. 'yday' => floor($secsInYear/$_day_power),
  553. 'weekday' => gmdate('l',$_day_power*(3+$dow)),
  554. 'month' => gmdate('F',mktime(0,0,0,$month,2,1971)),
  555. 0 => $origd
  556. );
  557. }
  558. function adodb_gmdate($fmt,$d=false)
  559. {
  560. return adodb_date($fmt,$d,true);
  561. }
  562. // accepts unix timestamp and iso date format in $d
  563. function adodb_date2($fmt, $d=false, $is_gmt=false)
  564. {
  565. if ($d !== false) {
  566. if (!preg_match(
  567. "|^([0-9]{4})[-/\.]?([0-9]{1,2})[-/\.]?([0-9]{1,2})[ -]?(([0-9]{1,2}):?([0-9]{1,2}):?([0-9\.]{1,4}))?|",
  568. ($d), $rr)) return adodb_date($fmt,false,$is_gmt);
  569. if ($rr[1] <= 100 && $rr[2]<= 1) return adodb_date($fmt,false,$is_gmt);
  570. // h-m-s-MM-DD-YY
  571. if (!isset($rr[5])) $d = adodb_mktime(0,0,0,$rr[2],$rr[3],$rr[1]);
  572. else $d = @adodb_mktime($rr[5],$rr[6],$rr[7],$rr[2],$rr[3],$rr[1]);
  573. }
  574. return adodb_date($fmt,$d,$is_gmt);
  575. }
  576. /**
  577. Return formatted date based on timestamp $d
  578. */
  579. function adodb_date($fmt,$d=false,$is_gmt=false)
  580. {
  581. static $daylight;
  582. if ($d === false) return ($is_gmt)? @gmdate($fmt): @date($fmt);
  583. if (!defined('ADODB_TEST_DATES')) {
  584. if ((abs($d) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
  585. if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer
  586. return ($is_gmt)? @gmdate($fmt,$d): @date($fmt,$d);
  587. }
  588. }
  589. $_day_power = 86400;
  590. $arr = _adodb_getdate($d,true,$is_gmt);
  591. if (!isset($daylight)) $daylight = function_exists('adodb_daylight_sv');
  592. if ($daylight) adodb_daylight_sv($arr, $is_gmt);
  593. $year = $arr['year'];
  594. $month = $arr['mon'];
  595. $day = $arr['mday'];
  596. $hour = $arr['hours'];
  597. $min = $arr['minutes'];
  598. $secs = $arr['seconds'];
  599. $max = strlen($fmt);
  600. $dates = '';
  601. /*
  602. at this point, we have the following integer vars to manipulate:
  603. $year, $month, $day, $hour, $min, $secs
  604. */
  605. for ($i=0; $i < $max; $i++) {
  606. switch($fmt[$i]) {
  607. case 'T': $dates .= date('T');break;
  608. // YEAR
  609. case 'L': $dates .= $arr['leap'] ? '1' : '0'; break;
  610. case 'r': // Thu, 21 Dec 2000 16:01:07 +0200
  611. $dates .= gmdate('D',$_day_power*(3+adodb_dow($year,$month,$day))).', '
  612. . ($day<10?' '.$day:$day) . ' '.date('M',mktime(0,0,0,$month,2,1971)).' '.$year.' ';
  613. if ($hour < 10) $dates .= '0'.$hour; else $dates .= $hour;
  614. if ($min < 10) $dates .= ':0'.$min; else $dates .= ':'.$min;
  615. if ($secs < 10) $dates .= ':0'.$secs; else $dates .= ':'.$secs;
  616. $gmt = adodb_get_gmt_diff();
  617. $dates .= sprintf(' %s%04d',($gmt<0)?'+':'-',abs($gmt)/36); break;
  618. case 'Y': $dates .= $year; break;
  619. case 'y': $dates .= substr($year,strlen($year)-2,2); break;
  620. // MONTH
  621. case 'm': if ($month<10) $dates .= '0'.$month; else $dates .= $month; break;
  622. case 'Q': $dates .= ($month+3)>>2; break;
  623. case 'n': $dates .= $month; break;
  624. case 'M': $dates .= date('M',mktime(0,0,0,$month,2,1971)); break;
  625. case 'F': $dates .= date('F',mktime(0,0,0,$month,2,1971)); break;
  626. // DAY
  627. case 't': $dates .= $arr['ndays']; break;
  628. case 'z': $dates .= $arr['yday']; break;
  629. case 'w': $dates .= adodb_dow($year,$month,$day); break;
  630. case 'l': $dates .= gmdate('l',$_day_power*(3+adodb_dow($year,$month,$day))); break;
  631. case 'D': $dates .= gmdate('D',$_day_power*(3+adodb_dow($year,$month,$day))); break;
  632. case 'j': $dates .= $day; break;
  633. case 'd': if ($day<10) $dates .= '0'.$day; else $dates .= $day; break;
  634. case 'S':
  635. $d10 = $day % 10;
  636. if ($d10 == 1) $dates .= 'st';
  637. else if ($d10 == 2 && $day != 12) $dates .= 'nd';
  638. else if ($d10 == 3) $dates .= 'rd';
  639. else $dates .= 'th';
  640. break;
  641. // HOUR
  642. case 'Z':
  643. $dates .= ($is_gmt) ? 0 : -adodb_get_gmt_diff(); break;
  644. case 'O':
  645. $gmt = ($is_gmt) ? 0 : adodb_get_gmt_diff();
  646. $dates .= sprintf('%s%04d',($gmt<0)?'+':'-',abs($gmt)/36); break;
  647. case 'H':
  648. if ($hour < 10) $dates .= '0'.$hour;
  649. else $dates .= $hour;
  650. break;
  651. case 'h':
  652. if ($hour > 12) $hh = $hour - 12;
  653. else {
  654. if ($hour == 0) $hh = '12';
  655. else $hh = $hour;
  656. }
  657. if ($hh < 10) $dates .= '0'.$hh;
  658. else $dates .= $hh;
  659. break;
  660. case 'G':
  661. $dates .= $hour;
  662. break;
  663. case 'g':
  664. if ($hour > 12) $hh = $hour - 12;
  665. else {
  666. if ($hour == 0) $hh = '12';
  667. else $hh = $hour;
  668. }
  669. $dates .= $hh;
  670. break;
  671. // MINUTES
  672. case 'i': if ($min < 10) $dates .= '0'.$min; else $dates .= $min; break;
  673. // SECONDS
  674. case 'U': $dates .= $d; break;
  675. case 's': if ($secs < 10) $dates .= '0'.$secs; else $dates .= $secs; break;
  676. // AM/PM
  677. // Note 00:00 to 11:59 is AM, while 12:00 to 23:59 is PM
  678. case 'a':
  679. if ($hour>=12) $dates .= 'pm';
  680. else $dates .= 'am';
  681. break;
  682. case 'A':
  683. if ($hour>=12) $dates .= 'PM';
  684. else $dates .= 'AM';
  685. break;
  686. default:
  687. $dates .= $fmt[$i]; break;
  688. // ESCAPE
  689. case "\\":
  690. $i++;
  691. if ($i < $max) $dates .= $fmt[$i];
  692. break;
  693. }
  694. }
  695. return $dates;
  696. }
  697. /**
  698. Returns a timestamp given a GMT/UTC time.
  699. Note that $is_dst is not implemented and is ignored.
  700. */
  701. function adodb_gmmktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false)
  702. {
  703. return adodb_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,true);
  704. }
  705. /**
  706. Return a timestamp given a local time. Originally by jackbbs.
  707. Note that $is_dst is not implemented and is ignored.
  708. Not a very fast algorithm - O(n) operation. Could be optimized to O(1).
  709. */
  710. function adodb_mktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false,$is_gmt=false)
  711. {
  712. if (!defined('ADODB_TEST_DATES')) {
  713. // for windows, we don't check 1970 because with timezone differences,
  714. // 1 Jan 1970 could generate negative timestamp, which is illegal
  715. if (1971 < $year && $year < 2038
  716. || $mon === false
  717. || !defined('ADODB_NO_NEGATIVE_TS') && (1901 < $year && $year < 2038)
  718. )
  719. return $is_gmt?
  720. @gmmktime($hr,$min,$sec,$mon,$day,$year):
  721. @mktime($hr,$min,$sec,$mon,$day,$year);
  722. }
  723. $gmt_different = ($is_gmt) ? 0 : adodb_get_gmt_diff();
  724. $hr = intval($hr);
  725. $min = intval($min);
  726. $sec = intval($sec);
  727. $mon = intval($mon);
  728. $day = intval($day);
  729. $year = intval($year);
  730. $year = adodb_year_digit_check($year);
  731. if ($mon > 12) {
  732. $y = floor($mon / 12);
  733. $year += $y;
  734. $mon -= $y*12;
  735. }
  736. $_day_power = 86400;
  737. $_hour_power = 3600;
  738. $_min_power = 60;
  739. $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
  740. $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
  741. $_total_date = 0;
  742. if ($year >= 1970) {
  743. for ($a = 1970 ; $a <= $year; $a++) {
  744. $leaf = _adodb_is_leap_year($a);
  745. if ($leaf == true) {
  746. $loop_table = $_month_table_leaf;
  747. $_add_date = 366;
  748. } else {
  749. $loop_table = $_month_table_normal;
  750. $_add_date = 365;
  751. }
  752. if ($a < $year) {
  753. $_total_date += $_add_date;
  754. } else {
  755. for($b=1;$b<$mon;$b++) {
  756. $_total_date += $loop_table[$b];
  757. }
  758. }
  759. }
  760. $_total_date +=$day-1;
  761. $ret = $_total_date * $_day_power + $hr * $_hour_power + $min * $_min_power + $sec + $gmt_different;
  762. } else {
  763. for ($a = 1969 ; $a >= $year; $a--) {
  764. $leaf = _adodb_is_leap_year($a);
  765. if ($leaf == true) {
  766. $loop_table = $_month_table_leaf;
  767. $_add_date = 366;
  768. } else {
  769. $loop_table = $_month_table_normal;
  770. $_add_date = 365;
  771. }
  772. if ($a > $year) { $_total_date += $_add_date;
  773. } else {
  774. for($b=12;$b>$mon;$b--) {
  775. $_total_date += $loop_table[$b];
  776. }
  777. }
  778. }
  779. $_total_date += $loop_table[$mon] - $day;
  780. $_day_time = $hr * $_hour_power + $min * $_min_power + $sec;
  781. $_day_time = $_day_power - $_day_time;
  782. $ret = -( $_total_date * $_day_power + $_day_time - $gmt_different);
  783. if ($ret < -12220185600) $ret += 10*86400; // if earlier than 5 Oct 1582 - gregorian correction
  784. else if ($ret < -12219321600) $ret = -12219321600; // if in limbo, reset to 15 Oct 1582.
  785. }
  786. //print " dmy=$day/$mon/$year $hr:$min:$sec => " .$ret;
  787. return $ret;
  788. }
  789. ?>