PageRenderTime 50ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/adodb.493a/adodb-time.inc.php

https://github.com/guzzisto/retrospect-gds
PHP | 1337 lines | 687 code | 137 blank | 513 comment | 231 complexity | 896ad0bea1be2ac4c6dbcb5e6ecd1406 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. ADOdb Date Library, part of the ADOdb abstraction library
  4. Download: http://phplens.com/phpeverywhere/
  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. strftime() with adodb_strftime()
  24. strftime() with adodb_gmstrftime()
  25. </pre>
  26. The parameters are identical, except that adodb_date() accepts a subset
  27. of date()'s field formats. Mktime() will convert from local time to GMT,
  28. and date() will convert from GMT to local time, but daylight savings is
  29. not handled currently.
  30. This library is independant of the rest of ADOdb, and can be used
  31. as standalone code.
  32. PERFORMANCE
  33. For high speed, this library uses the native date functions where
  34. possible, and only switches to PHP code when the dates fall outside
  35. the 32-bit signed integer range.
  36. GREGORIAN CORRECTION
  37. Pope Gregory shortened October of A.D. 1582 by ten days. Thursday,
  38. October 4, 1582 (Julian) was followed immediately by Friday, October 15,
  39. 1582 (Gregorian).
  40. Since 0.06, we handle this correctly, so:
  41. adodb_mktime(0,0,0,10,15,1582) - adodb_mktime(0,0,0,10,4,1582)
  42. == 24 * 3600 (1 day)
  43. =============================================================================
  44. COPYRIGHT
  45. (c) 2003-2005 John Lim and released under BSD-style license except for code by
  46. jackbbs, which includes adodb_mktime, adodb_get_gmt_diff, adodb_is_leap_year
  47. and originally found at http://www.php.net/manual/en/function.mktime.php
  48. =============================================================================
  49. BUG REPORTS
  50. These should be posted to the ADOdb forums at
  51. http://phplens.com/lens/lensforum/topics.php?id=4
  52. =============================================================================
  53. FUNCTION DESCRIPTIONS
  54. ** FUNCTION adodb_getdate($date=false)
  55. Returns an array containing date information, as getdate(), but supports
  56. dates greater than 1901 to 2038. The local date/time format is derived from a
  57. heuristic the first time adodb_getdate is called.
  58. ** FUNCTION adodb_date($fmt, $timestamp = false)
  59. Convert a timestamp to a formatted local date. If $timestamp is not defined, the
  60. current timestamp is used. Unlike the function date(), it supports dates
  61. outside the 1901 to 2038 range.
  62. The format fields that adodb_date supports:
  63. <pre>
  64. a - "am" or "pm"
  65. A - "AM" or "PM"
  66. d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
  67. D - day of the week, textual, 3 letters; e.g. "Fri"
  68. F - month, textual, long; e.g. "January"
  69. g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
  70. G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
  71. h - hour, 12-hour format; i.e. "01" to "12"
  72. H - hour, 24-hour format; i.e. "00" to "23"
  73. i - minutes; i.e. "00" to "59"
  74. j - day of the month without leading zeros; i.e. "1" to "31"
  75. l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"
  76. L - boolean for whether it is a leap year; i.e. "0" or "1"
  77. m - month; i.e. "01" to "12"
  78. M - month, textual, 3 letters; e.g. "Jan"
  79. n - month without leading zeros; i.e. "1" to "12"
  80. O - Difference to Greenwich time in hours; e.g. "+0200"
  81. Q - Quarter, as in 1, 2, 3, 4
  82. r - RFC 2822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200"
  83. s - seconds; i.e. "00" to "59"
  84. S - English ordinal suffix for the day of the month, 2 characters;
  85. i.e. "st", "nd", "rd" or "th"
  86. t - number of days in the given month; i.e. "28" to "31"
  87. T - Timezone setting of this machine; e.g. "EST" or "MDT"
  88. U - seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  89. w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
  90. Y - year, 4 digits; e.g. "1999"
  91. y - year, 2 digits; e.g. "99"
  92. z - day of the year; i.e. "0" to "365"
  93. Z - timezone offset in seconds (i.e. "-43200" to "43200").
  94. The offset for timezones west of UTC is always negative,
  95. and for those east of UTC is always positive.
  96. </pre>
  97. Unsupported:
  98. <pre>
  99. B - Swatch Internet time
  100. I (capital i) - "1" if Daylight Savings Time, "0" otherwise.
  101. W - ISO-8601 week number of year, weeks starting on Monday
  102. </pre>
  103. ** FUNCTION adodb_date2($fmt, $isoDateString = false)
  104. Same as adodb_date, but 2nd parameter accepts iso date, eg.
  105. adodb_date2('d-M-Y H:i','2003-12-25 13:01:34');
  106. ** FUNCTION adodb_gmdate($fmt, $timestamp = false)
  107. Convert a timestamp to a formatted GMT date. If $timestamp is not defined, the
  108. current timestamp is used. Unlike the function date(), it supports dates
  109. outside the 1901 to 2038 range.
  110. ** FUNCTION adodb_mktime($hr, $min, $sec[, $month, $day, $year])
  111. Converts a local date to a unix timestamp. Unlike the function mktime(), it supports
  112. dates outside the 1901 to 2038 range. All parameters are optional.
  113. ** FUNCTION adodb_gmmktime($hr, $min, $sec [, $month, $day, $year])
  114. Converts a gmt date to a unix timestamp. Unlike the function gmmktime(), it supports
  115. dates outside the 1901 to 2038 range. Differs from gmmktime() in that all parameters
  116. are currently compulsory.
  117. ** FUNCTION adodb_gmstrftime($fmt, $timestamp = false)
  118. Convert a timestamp to a formatted GMT date.
  119. ** FUNCTION adodb_strftime($fmt, $timestamp = false)
  120. Convert a timestamp to a formatted local date. Internally converts $fmt into
  121. adodb_date format, then echo result.
  122. For best results, you can define the local date format yourself. Define a global
  123. variable $ADODB_DATE_LOCALE which is an array, 1st element is date format using
  124. adodb_date syntax, and 2nd element is the time format, also in adodb_date syntax.
  125. eg. $ADODB_DATE_LOCALE = array('d/m/Y','H:i:s');
  126. Supported format codes:
  127. <pre>
  128. %a - abbreviated weekday name according to the current locale
  129. %A - full weekday name according to the current locale
  130. %b - abbreviated month name according to the current locale
  131. %B - full month name according to the current locale
  132. %c - preferred date and time representation for the current locale
  133. %d - day of the month as a decimal number (range 01 to 31)
  134. %D - same as %m/%d/%y
  135. %e - day of the month as a decimal number, a single digit is preceded by a space (range ' 1' to '31')
  136. %h - same as %b
  137. %H - hour as a decimal number using a 24-hour clock (range 00 to 23)
  138. %I - hour as a decimal number using a 12-hour clock (range 01 to 12)
  139. %m - month as a decimal number (range 01 to 12)
  140. %M - minute as a decimal number
  141. %n - newline character
  142. %p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale
  143. %r - time in a.m. and p.m. notation
  144. %R - time in 24 hour notation
  145. %S - second as a decimal number
  146. %t - tab character
  147. %T - current time, equal to %H:%M:%S
  148. %x - preferred date representation for the current locale without the time
  149. %X - preferred time representation for the current locale without the date
  150. %y - year as a decimal number without a century (range 00 to 99)
  151. %Y - year as a decimal number including the century
  152. %Z - time zone or name or abbreviation
  153. %% - a literal `%' character
  154. </pre>
  155. Unsupported codes:
  156. <pre>
  157. %C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)
  158. %g - like %G, but without the century.
  159. %G - The 4-digit year corresponding to the ISO week number (see %V).
  160. This has the same format and value as %Y, except that if the ISO week number belongs
  161. to the previous or next year, that year is used instead.
  162. %j - day of the year as a decimal number (range 001 to 366)
  163. %u - weekday as a decimal number [1,7], with 1 representing Monday
  164. %U - week number of the current year as a decimal number, starting
  165. with the first Sunday as the first day of the first week
  166. %V - The ISO 8601:1988 week number of the current year as a decimal number,
  167. range 01 to 53, where week 1 is the first week that has at least 4 days in the
  168. current year, and with Monday as the first day of the week. (Use %G or %g for
  169. the year component that corresponds to the week number for the specified timestamp.)
  170. %w - day of the week as a decimal, Sunday being 0
  171. %W - week number of the current year as a decimal number, starting with the
  172. first Monday as the first day of the first week
  173. </pre>
  174. =============================================================================
  175. NOTES
  176. Useful url for generating test timestamps:
  177. http://www.4webhelp.net/us/timestamp.php
  178. Possible future optimizations include
  179. a. Using an algorithm similar to Plauger's in "The Standard C Library"
  180. (page 428, xttotm.c _Ttotm() function). Plauger's algorithm will not
  181. work outside 32-bit signed range, so i decided not to implement it.
  182. b. Implement daylight savings, which looks awfully complicated, see
  183. http://webexhibits.org/daylightsaving/
  184. CHANGELOG
  185. - 19 March 2006 0.24
  186. Changed strftime() locale detection, because some locales prepend the day of week to the date when %c is used.
  187. - 10 Feb 2006 0.23
  188. PHP5 compat: when we detect PHP5, the RFC2822 format for gmt 0000hrs is changed from -0000 to +0000.
  189. In PHP4, we will still use -0000 for 100% compat with PHP4.
  190. - 08 Sept 2005 0.22
  191. In adodb_date2(), $is_gmt not supported properly. Fixed.
  192. - 18 July 2005 0.21
  193. In PHP 4.3.11, the 'r' format has changed. Leading 0 in day is added. Changed for compat.
  194. Added support for negative months in adodb_mktime().
  195. - 24 Feb 2005 0.20
  196. Added limited strftime/gmstrftime support. x10 improvement in performance of adodb_date().
  197. - 21 Dec 2004 0.17
  198. In adodb_getdate(), the timestamp was accidentally converted to gmt when $is_gmt is false.
  199. Also adodb_mktime(0,0,0) did not work properly. Both fixed thx Mauro.
  200. - 17 Nov 2004 0.16
  201. Removed intval typecast in adodb_mktime() for secs, allowing:
  202. adodb_mktime(0,0,0 + 2236672153,1,1,1934);
  203. Suggested by Ryan.
  204. - 18 July 2004 0.15
  205. All params in adodb_mktime were formerly compulsory. Now only the hour, min, secs is compulsory.
  206. This brings it more in line with mktime (still not identical).
  207. - 23 June 2004 0.14
  208. Allow you to define your own daylights savings function, adodb_daylight_sv.
  209. If the function is defined (somewhere in an include), then you can correct for daylights savings.
  210. In this example, we apply daylights savings in June or July, adding one hour. This is extremely
  211. unrealistic as it does not take into account time-zone, geographic location, current year.
  212. function adodb_daylight_sv(&$arr, $is_gmt)
  213. {
  214. if ($is_gmt) return;
  215. $m = $arr['mon'];
  216. if ($m == 6 || $m == 7) $arr['hours'] += 1;
  217. }
  218. This is only called by adodb_date() and not by adodb_mktime().
  219. The format of $arr is
  220. Array (
  221. [seconds] => 0
  222. [minutes] => 0
  223. [hours] => 0
  224. [mday] => 1 # day of month, eg 1st day of the month
  225. [mon] => 2 # month (eg. Feb)
  226. [year] => 2102
  227. [yday] => 31 # days in current year
  228. [leap] => # true if leap year
  229. [ndays] => 28 # no of days in current month
  230. )
  231. - 28 Apr 2004 0.13
  232. Fixed adodb_date to properly support $is_gmt. Thx to Dimitar Angelov.
  233. - 20 Mar 2004 0.12
  234. Fixed month calculation error in adodb_date. 2102-June-01 appeared as 2102-May-32.
  235. - 26 Oct 2003 0.11
  236. Because of daylight savings problems (some systems apply daylight savings to
  237. January!!!), changed adodb_get_gmt_diff() to ignore daylight savings.
  238. - 9 Aug 2003 0.10
  239. Fixed bug with dates after 2038.
  240. See http://phplens.com/lens/lensforum/msgs.php?id=6980
  241. - 1 July 2003 0.09
  242. Added support for Q (Quarter).
  243. Added adodb_date2(), which accepts ISO date in 2nd param
  244. - 3 March 2003 0.08
  245. Added support for 'S' adodb_date() format char. Added constant ADODB_ALLOW_NEGATIVE_TS
  246. if you want PHP to handle negative timestamps between 1901 to 1969.
  247. - 27 Feb 2003 0.07
  248. All negative numbers handled by adodb now because of RH 7.3+ problems.
  249. See http://bugs.php.net/bug.php?id=20048&edit=2
  250. - 4 Feb 2003 0.06
  251. Fixed a typo, 1852 changed to 1582! This means that pre-1852 dates
  252. are now correctly handled.
  253. - 29 Jan 2003 0.05
  254. Leap year checking differs under Julian calendar (pre 1582). Also
  255. leap year code optimized by checking for most common case first.
  256. We also handle month overflow correctly in mktime (eg month set to 13).
  257. Day overflow for less than one month's days is supported.
  258. - 28 Jan 2003 0.04
  259. Gregorian correction handled. In PHP5, we might throw an error if
  260. mktime uses invalid dates around 5-14 Oct 1582. Released with ADOdb 3.10.
  261. Added limbo 5-14 Oct 1582 check, when we set to 15 Oct 1582.
  262. - 27 Jan 2003 0.03
  263. Fixed some more month problems due to gmt issues. Added constant ADODB_DATE_VERSION.
  264. Fixed calculation of days since start of year for <1970.
  265. - 27 Jan 2003 0.02
  266. Changed _adodb_getdate() to inline leap year checking for better performance.
  267. Fixed problem with time-zones west of GMT +0000.
  268. - 24 Jan 2003 0.01
  269. First implementation.
  270. */
  271. /* Initialization */
  272. /*
  273. Version Number
  274. */
  275. define('ADODB_DATE_VERSION',0.24);
  276. /*
  277. This code was originally for windows. But apparently this problem happens
  278. also with Linux, RH 7.3 and later!
  279. glibc-2.2.5-34 and greater has been changed to return -1 for dates <
  280. 1970. This used to work. The problem exists with RedHat 7.3 and 8.0
  281. echo (mktime(0, 0, 0, 1, 1, 1960)); // prints -1
  282. References:
  283. http://bugs.php.net/bug.php?id=20048&edit=2
  284. http://lists.debian.org/debian-glibc/2002/debian-glibc-200205/msg00010.html
  285. */
  286. if (!defined('ADODB_ALLOW_NEGATIVE_TS')) define('ADODB_NO_NEGATIVE_TS',1);
  287. function adodb_date_test_date($y1,$m,$d=13)
  288. {
  289. $t = adodb_mktime(0,0,0,$m,$d,$y1);
  290. $rez = adodb_date('Y-n-j H:i:s',$t);
  291. if ("$y1-$m-$d 00:00:00" != $rez) {
  292. print "<b>$y1 error, expected=$y1-$m-$d 00:00:00, adodb=$rez</b><br>";
  293. return false;
  294. }
  295. return true;
  296. }
  297. function adodb_date_test_strftime($fmt)
  298. {
  299. $s1 = strftime($fmt);
  300. $s2 = adodb_strftime($fmt);
  301. if ($s1 == $s2) return true;
  302. echo "error for $fmt, strftime=$s1, $adodb=$s2<br>";
  303. return false;
  304. }
  305. /**
  306. Test Suite
  307. */
  308. function adodb_date_test()
  309. {
  310. error_reporting(E_ALL);
  311. print "<h4>Testing adodb_date and adodb_mktime. version=".ADODB_DATE_VERSION.' PHP='.PHP_VERSION."</h4>";
  312. @set_time_limit(0);
  313. $fail = false;
  314. // This flag disables calling of PHP native functions, so we can properly test the code
  315. if (!defined('ADODB_TEST_DATES')) define('ADODB_TEST_DATES',1);
  316. adodb_date_test_strftime('%Y %m %x %X');
  317. adodb_date_test_strftime("%A %d %B %Y");
  318. adodb_date_test_strftime("%H %M S");
  319. $t = adodb_mktime(0,0,0);
  320. if (!(adodb_date('Y-m-d') == date('Y-m-d'))) print 'Error in '.adodb_mktime(0,0,0).'<br>';
  321. $t = adodb_mktime(0,0,0,6,1,2102);
  322. if (!(adodb_date('Y-m-d',$t) == '2102-06-01')) print 'Error in '.adodb_date('Y-m-d',$t).'<br>';
  323. $t = adodb_mktime(0,0,0,2,1,2102);
  324. if (!(adodb_date('Y-m-d',$t) == '2102-02-01')) print 'Error in '.adodb_date('Y-m-d',$t).'<br>';
  325. print "<p>Testing gregorian <=> julian conversion<p>";
  326. $t = adodb_mktime(0,0,0,10,11,1492);
  327. //http://www.holidayorigins.com/html/columbus_day.html - Friday check
  328. if (!(adodb_date('D Y-m-d',$t) == 'Fri 1492-10-11')) print 'Error in Columbus landing<br>';
  329. $t = adodb_mktime(0,0,0,2,29,1500);
  330. if (!(adodb_date('Y-m-d',$t) == '1500-02-29')) print 'Error in julian leap years<br>';
  331. $t = adodb_mktime(0,0,0,2,29,1700);
  332. if (!(adodb_date('Y-m-d',$t) == '1700-03-01')) print 'Error in gregorian leap years<br>';
  333. print adodb_mktime(0,0,0,10,4,1582).' ';
  334. print adodb_mktime(0,0,0,10,15,1582);
  335. $diff = (adodb_mktime(0,0,0,10,15,1582) - adodb_mktime(0,0,0,10,4,1582));
  336. if ($diff != 3600*24) print " <b>Error in gregorian correction = ".($diff/3600/24)." days </b><br>";
  337. print " 15 Oct 1582, Fri=".(adodb_dow(1582,10,15) == 5 ? 'Fri' : '<b>Error</b>')."<br>";
  338. print " 4 Oct 1582, Thu=".(adodb_dow(1582,10,4) == 4 ? 'Thu' : '<b>Error</b>')."<br>";
  339. print "<p>Testing overflow<p>";
  340. $t = adodb_mktime(0,0,0,3,33,1965);
  341. if (!(adodb_date('Y-m-d',$t) == '1965-04-02')) print 'Error in day overflow 1 <br>';
  342. $t = adodb_mktime(0,0,0,4,33,1971);
  343. if (!(adodb_date('Y-m-d',$t) == '1971-05-03')) print 'Error in day overflow 2 <br>';
  344. $t = adodb_mktime(0,0,0,1,60,1965);
  345. if (!(adodb_date('Y-m-d',$t) == '1965-03-01')) print 'Error in day overflow 3 '.adodb_date('Y-m-d',$t).' <br>';
  346. $t = adodb_mktime(0,0,0,12,32,1965);
  347. if (!(adodb_date('Y-m-d',$t) == '1966-01-01')) print 'Error in day overflow 4 '.adodb_date('Y-m-d',$t).' <br>';
  348. $t = adodb_mktime(0,0,0,12,63,1965);
  349. if (!(adodb_date('Y-m-d',$t) == '1966-02-01')) print 'Error in day overflow 5 '.adodb_date('Y-m-d',$t).' <br>';
  350. $t = adodb_mktime(0,0,0,13,3,1965);
  351. if (!(adodb_date('Y-m-d',$t) == '1966-01-03')) print 'Error in mth overflow 1 <br>';
  352. print "Testing 2-digit => 4-digit year conversion<p>";
  353. if (adodb_year_digit_check(00) != 2000) print "Err 2-digit 2000<br>";
  354. if (adodb_year_digit_check(10) != 2010) print "Err 2-digit 2010<br>";
  355. if (adodb_year_digit_check(20) != 2020) print "Err 2-digit 2020<br>";
  356. if (adodb_year_digit_check(30) != 2030) print "Err 2-digit 2030<br>";
  357. if (adodb_year_digit_check(40) != 1940) print "Err 2-digit 1940<br>";
  358. if (adodb_year_digit_check(50) != 1950) print "Err 2-digit 1950<br>";
  359. if (adodb_year_digit_check(90) != 1990) print "Err 2-digit 1990<br>";
  360. // Test string formating
  361. print "<p>Testing date formating</p>";
  362. $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\C2822 r s t U w y Y z Z 2003';
  363. $s1 = date($fmt,0);
  364. $s2 = adodb_date($fmt,0);
  365. if ($s1 != $s2) {
  366. print " date() 0 failed<br>$s1<br>$s2<br>";
  367. }
  368. flush();
  369. for ($i=100; --$i > 0; ) {
  370. $ts = 3600.0*((rand()%60000)+(rand()%60000))+(rand()%60000);
  371. $s1 = date($fmt,$ts);
  372. $s2 = adodb_date($fmt,$ts);
  373. //print "$s1 <br>$s2 <p>";
  374. $pos = strcmp($s1,$s2);
  375. if (($s1) != ($s2)) {
  376. for ($j=0,$k=strlen($s1); $j < $k; $j++) {
  377. if ($s1[$j] != $s2[$j]) {
  378. print substr($s1,$j).' ';
  379. break;
  380. }
  381. }
  382. print "<b>Error date(): $ts<br><pre>
  383. &nbsp; \"$s1\" (date len=".strlen($s1).")
  384. &nbsp; \"$s2\" (adodb_date len=".strlen($s2).")</b></pre><br>";
  385. $fail = true;
  386. }
  387. $a1 = getdate($ts);
  388. $a2 = adodb_getdate($ts);
  389. $rez = array_diff($a1,$a2);
  390. if (sizeof($rez)>0) {
  391. print "<b>Error getdate() $ts</b><br>";
  392. print_r($a1);
  393. print "<br>";
  394. print_r($a2);
  395. print "<p>";
  396. $fail = true;
  397. }
  398. }
  399. // Test generation of dates outside 1901-2038
  400. print "<p>Testing random dates between 100 and 4000</p>";
  401. adodb_date_test_date(100,1);
  402. for ($i=100; --$i >= 0;) {
  403. $y1 = 100+rand(0,1970-100);
  404. $m = rand(1,12);
  405. adodb_date_test_date($y1,$m);
  406. $y1 = 3000-rand(0,3000-1970);
  407. adodb_date_test_date($y1,$m);
  408. }
  409. print '<p>';
  410. $start = 1960+rand(0,10);
  411. $yrs = 12;
  412. $i = 365.25*86400*($start-1970);
  413. $offset = 36000+rand(10000,60000);
  414. $max = 365*$yrs*86400;
  415. $lastyear = 0;
  416. // we generate a timestamp, convert it to a date, and convert it back to a timestamp
  417. // and check if the roundtrip broke the original timestamp value.
  418. print "Testing $start to ".($start+$yrs).", or $max seconds, offset=$offset: ";
  419. $cnt = 0;
  420. for ($max += $i; $i < $max; $i += $offset) {
  421. $ret = adodb_date('m,d,Y,H,i,s',$i);
  422. $arr = explode(',',$ret);
  423. if ($lastyear != $arr[2]) {
  424. $lastyear = $arr[2];
  425. print " $lastyear ";
  426. flush();
  427. }
  428. $newi = adodb_mktime($arr[3],$arr[4],$arr[5],$arr[0],$arr[1],$arr[2]);
  429. if ($i != $newi) {
  430. print "Error at $i, adodb_mktime returned $newi ($ret)";
  431. $fail = true;
  432. break;
  433. }
  434. $cnt += 1;
  435. }
  436. echo "Tested $cnt dates<br>";
  437. if (!$fail) print "<p>Passed !</p>";
  438. else print "<p><b>Failed</b> :-(</p>";
  439. }
  440. /**
  441. Returns day of week, 0 = Sunday,... 6=Saturday.
  442. Algorithm from PEAR::Date_Calc
  443. */
  444. function adodb_dow($year, $month, $day)
  445. {
  446. /*
  447. Pope Gregory removed 10 days - October 5 to October 14 - from the year 1582 and
  448. proclaimed that from that time onwards 3 days would be dropped from the calendar
  449. every 400 years.
  450. Thursday, October 4, 1582 (Julian) was followed immediately by Friday, October 15, 1582 (Gregorian).
  451. */
  452. if ($year <= 1582) {
  453. if ($year < 1582 ||
  454. ($year == 1582 && ($month < 10 || ($month == 10 && $day < 15)))) $greg_correction = 3;
  455. else
  456. $greg_correction = 0;
  457. } else
  458. $greg_correction = 0;
  459. if($month > 2)
  460. $month -= 2;
  461. else {
  462. $month += 10;
  463. $year--;
  464. }
  465. $day = floor((13 * $month - 1) / 5) +
  466. $day + ($year % 100) +
  467. floor(($year % 100) / 4) +
  468. floor(($year / 100) / 4) - 2 *
  469. floor($year / 100) + 77 + $greg_correction;
  470. return $day - 7 * floor($day / 7);
  471. }
  472. /**
  473. Checks for leap year, returns true if it is. No 2-digit year check. Also
  474. handles julian calendar correctly.
  475. */
  476. function _adodb_is_leap_year($year)
  477. {
  478. if ($year % 4 != 0) return false;
  479. if ($year % 400 == 0) {
  480. return true;
  481. // if gregorian calendar (>1582), century not-divisible by 400 is not leap
  482. } else if ($year > 1582 && $year % 100 == 0 ) {
  483. return false;
  484. }
  485. return true;
  486. }
  487. /**
  488. checks for leap year, returns true if it is. Has 2-digit year check
  489. */
  490. function adodb_is_leap_year($year)
  491. {
  492. return _adodb_is_leap_year(adodb_year_digit_check($year));
  493. }
  494. /**
  495. Fix 2-digit years. Works for any century.
  496. Assumes that if 2-digit is more than 30 years in future, then previous century.
  497. */
  498. function adodb_year_digit_check($y)
  499. {
  500. if ($y < 100) {
  501. $yr = (integer) date("Y");
  502. $century = (integer) ($yr /100);
  503. if ($yr%100 > 50) {
  504. $c1 = $century + 1;
  505. $c0 = $century;
  506. } else {
  507. $c1 = $century;
  508. $c0 = $century - 1;
  509. }
  510. $c1 *= 100;
  511. // if 2-digit year is less than 30 years in future, set it to this century
  512. // otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
  513. if (($y + $c1) < $yr+30) $y = $y + $c1;
  514. else $y = $y + $c0*100;
  515. }
  516. return $y;
  517. }
  518. /**
  519. get local time zone offset from GMT
  520. */
  521. function adodb_get_gmt_diff()
  522. {
  523. static $TZ;
  524. if (isset($TZ)) return $TZ;
  525. $TZ = mktime(0,0,0,1,2,1970,0) - gmmktime(0,0,0,1,2,1970,0);
  526. return $TZ;
  527. }
  528. /**
  529. Returns an array with date info.
  530. */
  531. function adodb_getdate($d=false,$fast=false)
  532. {
  533. if ($d === false) return getdate();
  534. if (!defined('ADODB_TEST_DATES')) {
  535. if ((abs($d) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
  536. if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer
  537. return @getdate($d);
  538. }
  539. }
  540. return _adodb_getdate($d);
  541. }
  542. /*
  543. // generate $YRS table for _adodb_getdate()
  544. function adodb_date_gentable($out=true)
  545. {
  546. for ($i=1970; $i >= 1600; $i-=10) {
  547. $s = adodb_gmmktime(0,0,0,1,1,$i);
  548. echo "$i => $s,<br>";
  549. }
  550. }
  551. adodb_date_gentable();
  552. for ($i=1970; $i > 1500; $i--) {
  553. echo "<hr />$i ";
  554. adodb_date_test_date($i,1,1);
  555. }
  556. */
  557. $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
  558. $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
  559. function adodb_validdate($y,$m,$d)
  560. {
  561. global $_month_table_normal,$_month_table_leaf;
  562. if (_adodb_is_leap_year($y)) $marr =& $_month_table_leaf;
  563. else $marr =& $_month_table_normal;
  564. if ($m > 12 || $m < 1) return false;
  565. if ($d > 31 || $d < 1) return false;
  566. if ($marr[$m] < $d) return false;
  567. if ($y < 1000 && $y > 3000) return false;
  568. return true;
  569. }
  570. /**
  571. Low-level function that returns the getdate() array. We have a special
  572. $fast flag, which if set to true, will return fewer array values,
  573. and is much faster as it does not calculate dow, etc.
  574. */
  575. function _adodb_getdate($origd=false,$fast=false,$is_gmt=false)
  576. {
  577. static $YRS;
  578. global $_month_table_normal,$_month_table_leaf;
  579. $d = $origd - ($is_gmt ? 0 : adodb_get_gmt_diff());
  580. $_day_power = 86400;
  581. $_hour_power = 3600;
  582. $_min_power = 60;
  583. if ($d < -12219321600) $d -= 86400*10; // if 15 Oct 1582 or earlier, gregorian correction
  584. $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
  585. $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
  586. $d366 = $_day_power * 366;
  587. $d365 = $_day_power * 365;
  588. if ($d < 0) {
  589. if (empty($YRS)) $YRS = array(
  590. 1970 => 0,
  591. 1960 => -315619200,
  592. 1950 => -631152000,
  593. 1940 => -946771200,
  594. 1930 => -1262304000,
  595. 1920 => -1577923200,
  596. 1910 => -1893456000,
  597. 1900 => -2208988800,
  598. 1890 => -2524521600,
  599. 1880 => -2840140800,
  600. 1870 => -3155673600,
  601. 1860 => -3471292800,
  602. 1850 => -3786825600,
  603. 1840 => -4102444800,
  604. 1830 => -4417977600,
  605. 1820 => -4733596800,
  606. 1810 => -5049129600,
  607. 1800 => -5364662400,
  608. 1790 => -5680195200,
  609. 1780 => -5995814400,
  610. 1770 => -6311347200,
  611. 1760 => -6626966400,
  612. 1750 => -6942499200,
  613. 1740 => -7258118400,
  614. 1730 => -7573651200,
  615. 1720 => -7889270400,
  616. 1710 => -8204803200,
  617. 1700 => -8520336000,
  618. 1690 => -8835868800,
  619. 1680 => -9151488000,
  620. 1670 => -9467020800,
  621. 1660 => -9782640000,
  622. 1650 => -10098172800,
  623. 1640 => -10413792000,
  624. 1630 => -10729324800,
  625. 1620 => -11044944000,
  626. 1610 => -11360476800,
  627. 1600 => -11676096000);
  628. if ($is_gmt) $origd = $d;
  629. // The valid range of a 32bit signed timestamp is typically from
  630. // Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT
  631. //
  632. # old algorithm iterates through all years. new algorithm does it in
  633. # 10 year blocks
  634. /*
  635. # old algo
  636. for ($a = 1970 ; --$a >= 0;) {
  637. $lastd = $d;
  638. if ($leaf = _adodb_is_leap_year($a)) $d += $d366;
  639. else $d += $d365;
  640. if ($d >= 0) {
  641. $year = $a;
  642. break;
  643. }
  644. }
  645. */
  646. $lastsecs = 0;
  647. $lastyear = 1970;
  648. foreach($YRS as $year => $secs) {
  649. if ($d >= $secs) {
  650. $a = $lastyear;
  651. break;
  652. }
  653. $lastsecs = $secs;
  654. $lastyear = $year;
  655. }
  656. $d -= $lastsecs;
  657. if (!isset($a)) $a = $lastyear;
  658. //echo ' yr=',$a,' ', $d,'.';
  659. for (; --$a >= 0;) {
  660. $lastd = $d;
  661. if ($leaf = _adodb_is_leap_year($a)) $d += $d366;
  662. else $d += $d365;
  663. if ($d >= 0) {
  664. $year = $a;
  665. break;
  666. }
  667. }
  668. /**/
  669. $secsInYear = 86400 * ($leaf ? 366 : 365) + $lastd;
  670. $d = $lastd;
  671. $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal;
  672. for ($a = 13 ; --$a > 0;) {
  673. $lastd = $d;
  674. $d += $mtab[$a] * $_day_power;
  675. if ($d >= 0) {
  676. $month = $a;
  677. $ndays = $mtab[$a];
  678. break;
  679. }
  680. }
  681. $d = $lastd;
  682. $day = $ndays + ceil(($d+1) / ($_day_power));
  683. $d += ($ndays - $day+1)* $_day_power;
  684. $hour = floor($d/$_hour_power);
  685. } else {
  686. for ($a = 1970 ;; $a++) {
  687. $lastd = $d;
  688. if ($leaf = _adodb_is_leap_year($a)) $d -= $d366;
  689. else $d -= $d365;
  690. if ($d < 0) {
  691. $year = $a;
  692. break;
  693. }
  694. }
  695. $secsInYear = $lastd;
  696. $d = $lastd;
  697. $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal;
  698. for ($a = 1 ; $a <= 12; $a++) {
  699. $lastd = $d;
  700. $d -= $mtab[$a] * $_day_power;
  701. if ($d < 0) {
  702. $month = $a;
  703. $ndays = $mtab[$a];
  704. break;
  705. }
  706. }
  707. $d = $lastd;
  708. $day = ceil(($d+1) / $_day_power);
  709. $d = $d - ($day-1) * $_day_power;
  710. $hour = floor($d /$_hour_power);
  711. }
  712. $d -= $hour * $_hour_power;
  713. $min = floor($d/$_min_power);
  714. $secs = $d - $min * $_min_power;
  715. if ($fast) {
  716. return array(
  717. 'seconds' => $secs,
  718. 'minutes' => $min,
  719. 'hours' => $hour,
  720. 'mday' => $day,
  721. 'mon' => $month,
  722. 'year' => $year,
  723. 'yday' => floor($secsInYear/$_day_power),
  724. 'leap' => $leaf,
  725. 'ndays' => $ndays
  726. );
  727. }
  728. $dow = adodb_dow($year,$month,$day);
  729. return array(
  730. 'seconds' => $secs,
  731. 'minutes' => $min,
  732. 'hours' => $hour,
  733. 'mday' => $day,
  734. 'wday' => $dow,
  735. 'mon' => $month,
  736. 'year' => $year,
  737. 'yday' => floor($secsInYear/$_day_power),
  738. 'weekday' => gmdate('l',$_day_power*(3+$dow)),
  739. 'month' => gmdate('F',mktime(0,0,0,$month,2,1971)),
  740. 0 => $origd
  741. );
  742. }
  743. function adodb_gmdate($fmt,$d=false)
  744. {
  745. return adodb_date($fmt,$d,true);
  746. }
  747. // accepts unix timestamp and iso date format in $d
  748. function adodb_date2($fmt, $d=false, $is_gmt=false)
  749. {
  750. if ($d !== false) {
  751. if (!preg_match(
  752. "|^([0-9]{4})[-/\.]?([0-9]{1,2})[-/\.]?([0-9]{1,2})[ -]?(([0-9]{1,2}):?([0-9]{1,2}):?([0-9\.]{1,4}))?|",
  753. ($d), $rr)) return adodb_date($fmt,false,$is_gmt);
  754. if ($rr[1] <= 100 && $rr[2]<= 1) return adodb_date($fmt,false,$is_gmt);
  755. // h-m-s-MM-DD-YY
  756. if (!isset($rr[5])) $d = adodb_mktime(0,0,0,$rr[2],$rr[3],$rr[1],false,$is_gmt);
  757. else $d = @adodb_mktime($rr[5],$rr[6],$rr[7],$rr[2],$rr[3],$rr[1],false,$is_gmt);
  758. }
  759. return adodb_date($fmt,$d,$is_gmt);
  760. }
  761. /**
  762. Return formatted date based on timestamp $d
  763. */
  764. function adodb_date($fmt,$d=false,$is_gmt=false)
  765. {
  766. static $daylight;
  767. if ($d === false) return ($is_gmt)? @gmdate($fmt): @date($fmt);
  768. if (!defined('ADODB_TEST_DATES')) {
  769. if ((abs($d) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
  770. if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer
  771. return ($is_gmt)? @gmdate($fmt,$d): @date($fmt,$d);
  772. }
  773. }
  774. $_day_power = 86400;
  775. $arr = _adodb_getdate($d,true,$is_gmt);
  776. if (!isset($daylight)) $daylight = function_exists('adodb_daylight_sv');
  777. if ($daylight) adodb_daylight_sv($arr, $is_gmt);
  778. $year = $arr['year'];
  779. $month = $arr['mon'];
  780. $day = $arr['mday'];
  781. $hour = $arr['hours'];
  782. $min = $arr['minutes'];
  783. $secs = $arr['seconds'];
  784. $max = strlen($fmt);
  785. $dates = '';
  786. $isphp5 = PHP_VERSION >= 5;
  787. /*
  788. at this point, we have the following integer vars to manipulate:
  789. $year, $month, $day, $hour, $min, $secs
  790. */
  791. for ($i=0; $i < $max; $i++) {
  792. switch($fmt[$i]) {
  793. case 'T': $dates .= date('T');break;
  794. // YEAR
  795. case 'L': $dates .= $arr['leap'] ? '1' : '0'; break;
  796. case 'r': // Thu, 21 Dec 2000 16:01:07 +0200
  797. // 4.3.11 uses '04 Jun 2004'
  798. // 4.3.8 uses ' 4 Jun 2004'
  799. $dates .= gmdate('D',$_day_power*(3+adodb_dow($year,$month,$day))).', '
  800. . ($day<10?'0'.$day:$day) . ' '.date('M',mktime(0,0,0,$month,2,1971)).' '.$year.' ';
  801. if ($hour < 10) $dates .= '0'.$hour; else $dates .= $hour;
  802. if ($min < 10) $dates .= ':0'.$min; else $dates .= ':'.$min;
  803. if ($secs < 10) $dates .= ':0'.$secs; else $dates .= ':'.$secs;
  804. $gmt = adodb_get_gmt_diff();
  805. if ($isphp5)
  806. $dates .= sprintf(' %s%04d',($gmt<=0)?'+':'-',abs($gmt)/36);
  807. else
  808. $dates .= sprintf(' %s%04d',($gmt<0)?'+':'-',abs($gmt)/36);
  809. break;
  810. case 'Y': $dates .= $year; break;
  811. case 'y': $dates .= substr($year,strlen($year)-2,2); break;
  812. // MONTH
  813. case 'm': if ($month<10) $dates .= '0'.$month; else $dates .= $month; break;
  814. case 'Q': $dates .= ($month+3)>>2; break;
  815. case 'n': $dates .= $month; break;
  816. case 'M': $dates .= date('M',mktime(0,0,0,$month,2,1971)); break;
  817. case 'F': $dates .= date('F',mktime(0,0,0,$month,2,1971)); break;
  818. // DAY
  819. case 't': $dates .= $arr['ndays']; break;
  820. case 'z': $dates .= $arr['yday']; break;
  821. case 'w': $dates .= adodb_dow($year,$month,$day); break;
  822. case 'l': $dates .= gmdate('l',$_day_power*(3+adodb_dow($year,$month,$day))); break;
  823. case 'D': $dates .= gmdate('D',$_day_power*(3+adodb_dow($year,$month,$day))); break;
  824. case 'j': $dates .= $day; break;
  825. case 'd': if ($day<10) $dates .= '0'.$day; else $dates .= $day; break;
  826. case 'S':
  827. $d10 = $day % 10;
  828. if ($d10 == 1) $dates .= 'st';
  829. else if ($d10 == 2 && $day != 12) $dates .= 'nd';
  830. else if ($d10 == 3) $dates .= 'rd';
  831. else $dates .= 'th';
  832. break;
  833. // HOUR
  834. case 'Z':
  835. $dates .= ($is_gmt) ? 0 : -adodb_get_gmt_diff(); break;
  836. case 'O':
  837. $gmt = ($is_gmt) ? 0 : adodb_get_gmt_diff();
  838. if ($isphp5)
  839. $dates .= sprintf('%s%04d',($gmt<=0)?'+':'-',abs($gmt)/36);
  840. else
  841. $dates .= sprintf('%s%04d',($gmt<0)?'+':'-',abs($gmt)/36);
  842. break;
  843. case 'H':
  844. if ($hour < 10) $dates .= '0'.$hour;
  845. else $dates .= $hour;
  846. break;
  847. case 'h':
  848. if ($hour > 12) $hh = $hour - 12;
  849. else {
  850. if ($hour == 0) $hh = '12';
  851. else $hh = $hour;
  852. }
  853. if ($hh < 10) $dates .= '0'.$hh;
  854. else $dates .= $hh;
  855. break;
  856. case 'G':
  857. $dates .= $hour;
  858. break;
  859. case 'g':
  860. if ($hour > 12) $hh = $hour - 12;
  861. else {
  862. if ($hour == 0) $hh = '12';
  863. else $hh = $hour;
  864. }
  865. $dates .= $hh;
  866. break;
  867. // MINUTES
  868. case 'i': if ($min < 10) $dates .= '0'.$min; else $dates .= $min; break;
  869. // SECONDS
  870. case 'U': $dates .= $d; break;
  871. case 's': if ($secs < 10) $dates .= '0'.$secs; else $dates .= $secs; break;
  872. // AM/PM
  873. // Note 00:00 to 11:59 is AM, while 12:00 to 23:59 is PM
  874. case 'a':
  875. if ($hour>=12) $dates .= 'pm';
  876. else $dates .= 'am';
  877. break;
  878. case 'A':
  879. if ($hour>=12) $dates .= 'PM';
  880. else $dates .= 'AM';
  881. break;
  882. default:
  883. $dates .= $fmt[$i]; break;
  884. // ESCAPE
  885. case "\\":
  886. $i++;
  887. if ($i < $max) $dates .= $fmt[$i];
  888. break;
  889. }
  890. }
  891. return $dates;
  892. }
  893. /**
  894. Returns a timestamp given a GMT/UTC time.
  895. Note that $is_dst is not implemented and is ignored.
  896. */
  897. function adodb_gmmktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false)
  898. {
  899. return adodb_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,true);
  900. }
  901. /**
  902. Return a timestamp given a local time. Originally by jackbbs.
  903. Note that $is_dst is not implemented and is ignored.
  904. Not a very fast algorithm - O(n) operation. Could be optimized to O(1).
  905. */
  906. function adodb_mktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false,$is_gmt=false)
  907. {
  908. if (!defined('ADODB_TEST_DATES')) {
  909. if ($mon === false) {
  910. return $is_gmt? @gmmktime($hr,$min,$sec): @mktime($hr,$min,$sec);
  911. }
  912. // for windows, we don't check 1970 because with timezone differences,
  913. // 1 Jan 1970 could generate negative timestamp, which is illegal
  914. if (1971 < $year && $year < 2038
  915. || !defined('ADODB_NO_NEGATIVE_TS') && (1901 < $year && $year < 2038)
  916. ) {
  917. return $is_gmt ?
  918. @gmmktime($hr,$min,$sec,$mon,$day,$year):
  919. @mktime($hr,$min,$sec,$mon,$day,$year);
  920. }
  921. }
  922. $gmt_different = ($is_gmt) ? 0 : adodb_get_gmt_diff();
  923. /*
  924. # disabled because some people place large values in $sec.
  925. # however we need it for $mon because we use an array...
  926. $hr = intval($hr);
  927. $min = intval($min);
  928. $sec = intval($sec);
  929. */
  930. $mon = intval($mon);
  931. $day = intval($day);
  932. $year = intval($year);
  933. $year = adodb_year_digit_check($year);
  934. if ($mon > 12) {
  935. $y = floor($mon / 12);
  936. $year += $y;
  937. $mon -= $y*12;
  938. } else if ($mon < 1) {
  939. $y = ceil((1-$mon) / 12);
  940. $year -= $y;
  941. $mon += $y*12;
  942. }
  943. $_day_power = 86400;
  944. $_hour_power = 3600;
  945. $_min_power = 60;
  946. $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
  947. $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
  948. $_total_date = 0;
  949. if ($year >= 1970) {
  950. for ($a = 1970 ; $a <= $year; $a++) {
  951. $leaf = _adodb_is_leap_year($a);
  952. if ($leaf == true) {
  953. $loop_table = $_month_table_leaf;
  954. $_add_date = 366;
  955. } else {
  956. $loop_table = $_month_table_normal;
  957. $_add_date = 365;
  958. }
  959. if ($a < $year) {
  960. $_total_date += $_add_date;
  961. } else {
  962. for($b=1;$b<$mon;$b++) {
  963. $_total_date += $loop_table[$b];
  964. }
  965. }
  966. }
  967. $_total_date +=$day-1;
  968. $ret = $_total_date * $_day_power + $hr * $_hour_power + $min * $_min_power + $sec + $gmt_different;
  969. } else {
  970. for ($a = 1969 ; $a >= $year; $a--) {
  971. $leaf = _adodb_is_leap_year($a);
  972. if ($leaf == true) {
  973. $loop_table = $_month_table_leaf;
  974. $_add_date = 366;
  975. } else {
  976. $loop_table = $_month_table_normal;
  977. $_add_date = 365;
  978. }
  979. if ($a > $year) { $_total_date += $_add_date;
  980. } else {
  981. for($b=12;$b>$mon;$b--) {
  982. $_total_date += $loop_table[$b];
  983. }
  984. }
  985. }
  986. $_total_date += $loop_table[$mon] - $day;
  987. $_day_time = $hr * $_hour_power + $min * $_min_power + $sec;
  988. $_day_time = $_day_power - $_day_time;
  989. $ret = -( $_total_date * $_day_power + $_day_time - $gmt_different);
  990. if ($ret < -12220185600) $ret += 10*86400; // if earlier than 5 Oct 1582 - gregorian correction
  991. else if ($ret < -12219321600) $ret = -12219321600; // if in limbo, reset to 15 Oct 1582.
  992. }
  993. //print " dmy=$day/$mon/$year $hr:$min:$sec => " .$ret;
  994. return $ret;
  995. }
  996. function adodb_gmstrftime($fmt, $ts=false)
  997. {
  998. return adodb_strftime($fmt,$ts,true);
  999. }
  1000. // hack - convert to adodb_date
  1001. function adodb_strftime($fmt, $ts=false,$is_gmt=false)
  1002. {
  1003. global $ADODB_DATE_LOCALE;
  1004. if (!defined('ADODB_TEST_DATES')) {
  1005. if ((abs($ts) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
  1006. if (!defined('ADODB_NO_NEGATIVE_TS') || $ts >= 0) // if windows, must be +ve integer
  1007. return ($is_gmt)? @gmstrftime($fmt,$ts): @strftime($fmt,$ts);
  1008. }
  1009. }
  1010. if (empty($ADODB_DATE_LOCALE)) {
  1011. /*
  1012. $tstr = strtoupper(gmstrftime('%c',31366800)); // 30 Dec 1970, 1 am
  1013. $sep = substr($tstr,2,1);
  1014. $hasAM = strrpos($tstr,'M') !== false;
  1015. */
  1016. # see http://phplens.com/lens/lensforum/msgs.php?id=14865 for reasoning, and changelog for version 0.24
  1017. $dstr = gmstrftime('%x',31366800); // 30 Dec 1970, 1 am
  1018. $sep = substr($dstr,2,1);
  1019. $tstr = strtoupper(gmstrftime('%X',31366800)); // 30 Dec 1970, 1 am
  1020. $hasAM = strrpos($tstr,'M') !== false;
  1021. $ADODB_DATE_LOCALE = array();
  1022. $ADODB_DATE_LOCALE[] = strncmp($tstr,'30',2) == 0 ? 'd'.$sep.'m'.$sep.'y' : 'm'.$sep.'d'.$sep.'y';
  1023. $ADODB_DATE_LOCALE[] = ($hasAM) ? 'h:i:s a' : 'H:i:s';
  1024. }
  1025. $inpct = false;
  1026. $fmtdate = '';
  1027. for ($i=0,$max = strlen($fmt); $i < $max; $i++) {
  1028. $ch = $fmt[$i];
  1029. if ($ch == '%') {
  1030. if ($inpct) {
  1031. $fmtdate .= '%';
  1032. $inpct = false;
  1033. } else
  1034. $inpct = true;
  1035. } else if ($inpct) {
  1036. $inpct = false;
  1037. switch($ch) {
  1038. case '0':
  1039. case '1':
  1040. case '2':
  1041. case '3':
  1042. case '4':
  1043. case '5':
  1044. case '6':
  1045. case '7':
  1046. case '8':
  1047. case '9':
  1048. case 'E':
  1049. case 'O':
  1050. /* ignore format modifiers */
  1051. $inpct = true;
  1052. break;
  1053. case 'a': $fmtdate .= 'D'; break;
  1054. case 'A': $fmtdate .= 'l'; break;
  1055. case 'h':
  1056. case 'b': $fmtdate .= 'M'; break;
  1057. case 'B': $fmtdate .= 'F'; break;
  1058. case 'c': $fmtdate .= $ADODB_DATE_LOCALE[0].$ADODB_DATE_LOCALE[1]; break;
  1059. case 'C': $fmtdate .= '\C?'; break; // century
  1060. case 'd': $fmtdate .= 'd'; break;
  1061. case 'D': $fmtdate .= 'm/d/y'; break;
  1062. case 'e': $fmtdate .= 'j'; break;
  1063. case 'g': $fmtdate .= '\g?'; break; //?
  1064. case 'G': $fmtdate .= '\G?'; break; //?
  1065. case 'H': $fmtdate .= 'H'; break;
  1066. case 'I': $fmtdate .= 'h'; break;
  1067. case 'j': $fmtdate .= '?z'; $parsej = true; break; // wrong as j=1-based, z=0-basd
  1068. case 'm': $fmtdate .= 'm'; break;
  1069. case 'M': $fmtdate .= 'i'; break;
  1070. case 'n': $fmtdate .= "\n"; break;
  1071. case 'p': $fmtdate .= 'a'; break;
  1072. case 'r': $fmtdate .= 'h:i:s a'; break;
  1073. case 'R': $fmtdate .= 'H:i:s'; break;
  1074. case 'S': $fmtdate .= 's'; break;
  1075. case 't': $fmtdate .= "\t"; break;
  1076. case 'T': $fmtdate .= 'H:i:s'; break;
  1077. case 'u': $fmtdate .= '?u'; $parseu = true; break; // wrong strftime=1-based, date=0-based
  1078. case 'U': $fmtdate .= '?U'; $parseU = true; break;// wrong strftime=1-based, date=0-based
  1079. case 'x': $fmtdate .= $ADODB_DATE_LOCALE[0]; break;
  1080. case 'X': $fmtdate .= $ADODB_DATE_LOCALE[1]; break;
  1081. case 'w': $fmtdate .= '?w'; $parseu = true; break; // wrong strftime=1-based, date=0-based
  1082. case 'W': $fmtdate .= '?W'; $parseU = true; break;// wrong strftime=1-based, date=0-based
  1083. case 'y': $fmtdate .= 'y'; break;
  1084. case 'Y': $fmtdate .= 'Y'; break;
  1085. case 'Z': $fmtdate .= 'T'; break;
  1086. }
  1087. } else if (('A' <= ($ch) && ($ch) <= 'Z' ) || ('a' <= ($ch) && ($ch) <= 'z' ))
  1088. $fmtdate .= "\\".$ch;
  1089. else
  1090. $fmtdate .= $ch;
  1091. }
  1092. //echo "fmt=",$fmtdate,"<br>";
  1093. if ($ts === false) $ts = time();
  1094. $ret = adodb_date($fmtdate, $ts, $is_gmt);
  1095. return $ret;
  1096. }
  1097. ?>