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