Documentation TYPO3 par Ameos

adodb-time.inc.php

00001 <?php
00363 /* Initialization */
00364 
00365 /*
00366         Version Number
00367 */
00368 define('ADODB_DATE_VERSION',0.23);
00369 
00370 /*
00371         This code was originally for windows. But apparently this problem happens 
00372         also with Linux, RH 7.3 and later!
00373         
00374         glibc-2.2.5-34 and greater has been changed to return -1 for dates <
00375         1970.  This used to work.  The problem exists with RedHat 7.3 and 8.0
00376         echo (mktime(0, 0, 0, 1, 1, 1960));  // prints -1
00377         
00378         References:
00379          http://bugs.php.net/bug.php?id=20048&edit=2
00380          http://lists.debian.org/debian-glibc/2002/debian-glibc-200205/msg00010.html
00381 */
00382 
00383 if (!defined('ADODB_ALLOW_NEGATIVE_TS')) define('ADODB_NO_NEGATIVE_TS',1);
00384 
00385 function adodb_date_test_date($y1,$m,$d=13)
00386 {
00387         $t = adodb_mktime(0,0,0,$m,$d,$y1);
00388         $rez = adodb_date('Y-n-j H:i:s',$t);
00389         if ("$y1-$m-$d 00:00:00" != $rez) {
00390                 print "<b>$y1 error, expected=$y1-$m-$d 00:00:00, adodb=$rez</b><br>";
00391                 return false;
00392         }
00393         return true;
00394 }
00395 
00396 function adodb_date_test_strftime($fmt)
00397 {
00398         $s1 = strftime($fmt);
00399         $s2 = adodb_strftime($fmt);
00400         
00401         if ($s1 == $s2) return true;
00402         
00403         echo "error for $fmt,  strftime=$s1, $adodb=$s2<br>";
00404         return false;
00405 }
00406 
00410 function adodb_date_test()
00411 {
00412         
00413         error_reporting(E_ALL);
00414         print "<h4>Testing adodb_date and adodb_mktime. version=".ADODB_DATE_VERSION.' PHP='.PHP_VERSION."</h4>";
00415         @set_time_limit(0);
00416         $fail = false;
00417         
00418         // This flag disables calling of PHP native functions, so we can properly test the code
00419         if (!defined('ADODB_TEST_DATES')) define('ADODB_TEST_DATES',1);
00420         
00421         adodb_date_test_strftime('%Y %m %x %X');
00422         adodb_date_test_strftime("%A %d %B %Y");
00423         adodb_date_test_strftime("%H %M S");
00424         
00425         $t = adodb_mktime(0,0,0);
00426         if (!(adodb_date('Y-m-d') == date('Y-m-d'))) print 'Error in '.adodb_mktime(0,0,0).'<br>';
00427         
00428         $t = adodb_mktime(0,0,0,6,1,2102);
00429         if (!(adodb_date('Y-m-d',$t) == '2102-06-01')) print 'Error in '.adodb_date('Y-m-d',$t).'<br>';
00430         
00431         $t = adodb_mktime(0,0,0,2,1,2102);
00432         if (!(adodb_date('Y-m-d',$t) == '2102-02-01')) print 'Error in '.adodb_date('Y-m-d',$t).'<br>';
00433         
00434         
00435         print "<p>Testing gregorian <=> julian conversion<p>";
00436         $t = adodb_mktime(0,0,0,10,11,1492);
00437         //http://www.holidayorigins.com/html/columbus_day.html - Friday check
00438         if (!(adodb_date('D Y-m-d',$t) == 'Fri 1492-10-11')) print 'Error in Columbus landing<br>';
00439         
00440         $t = adodb_mktime(0,0,0,2,29,1500);
00441         if (!(adodb_date('Y-m-d',$t) == '1500-02-29')) print 'Error in julian leap years<br>';
00442         
00443         $t = adodb_mktime(0,0,0,2,29,1700);
00444         if (!(adodb_date('Y-m-d',$t) == '1700-03-01')) print 'Error in gregorian leap years<br>';
00445         
00446         print  adodb_mktime(0,0,0,10,4,1582).' ';
00447         print adodb_mktime(0,0,0,10,15,1582);
00448         $diff = (adodb_mktime(0,0,0,10,15,1582) - adodb_mktime(0,0,0,10,4,1582));
00449         if ($diff != 3600*24) print " <b>Error in gregorian correction = ".($diff/3600/24)." days </b><br>";
00450                 
00451         print " 15 Oct 1582, Fri=".(adodb_dow(1582,10,15) == 5 ? 'Fri' : '<b>Error</b>')."<br>";
00452         print " 4 Oct 1582, Thu=".(adodb_dow(1582,10,4) == 4 ? 'Thu' : '<b>Error</b>')."<br>";
00453         
00454         print "<p>Testing overflow<p>";
00455         
00456         $t = adodb_mktime(0,0,0,3,33,1965);
00457         if (!(adodb_date('Y-m-d',$t) == '1965-04-02')) print 'Error in day overflow 1 <br>';
00458         $t = adodb_mktime(0,0,0,4,33,1971);
00459         if (!(adodb_date('Y-m-d',$t) == '1971-05-03')) print 'Error in day overflow 2 <br>';
00460         $t = adodb_mktime(0,0,0,1,60,1965);
00461         if (!(adodb_date('Y-m-d',$t) == '1965-03-01')) print 'Error in day overflow 3 '.adodb_date('Y-m-d',$t).' <br>';
00462         $t = adodb_mktime(0,0,0,12,32,1965);
00463         if (!(adodb_date('Y-m-d',$t) == '1966-01-01')) print 'Error in day overflow 4 '.adodb_date('Y-m-d',$t).' <br>';
00464         $t = adodb_mktime(0,0,0,12,63,1965);
00465         if (!(adodb_date('Y-m-d',$t) == '1966-02-01')) print 'Error in day overflow 5 '.adodb_date('Y-m-d',$t).' <br>';
00466         $t = adodb_mktime(0,0,0,13,3,1965);
00467         if (!(adodb_date('Y-m-d',$t) == '1966-01-03')) print 'Error in mth overflow 1 <br>';
00468         
00469         print "Testing 2-digit => 4-digit year conversion<p>";
00470         if (adodb_year_digit_check(00) != 2000) print "Err 2-digit 2000<br>";
00471         if (adodb_year_digit_check(10) != 2010) print "Err 2-digit 2010<br>";
00472         if (adodb_year_digit_check(20) != 2020) print "Err 2-digit 2020<br>";
00473         if (adodb_year_digit_check(30) != 2030) print "Err 2-digit 2030<br>";
00474         if (adodb_year_digit_check(40) != 1940) print "Err 2-digit 1940<br>";
00475         if (adodb_year_digit_check(50) != 1950) print "Err 2-digit 1950<br>";
00476         if (adodb_year_digit_check(90) != 1990) print "Err 2-digit 1990<br>";
00477         
00478         // Test string formating
00479         print "<p>Testing date formating</p>";
00480         $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';
00481         $s1 = date($fmt,0);
00482         $s2 = adodb_date($fmt,0);
00483         if ($s1 != $s2) {
00484                 print " date() 0 failed<br>$s1<br>$s2<br>";
00485         }
00486         flush();
00487         for ($i=100; --$i > 0; ) {
00488 
00489                 $ts = 3600.0*((rand()%60000)+(rand()%60000))+(rand()%60000);
00490                 $s1 = date($fmt,$ts);
00491                 $s2 = adodb_date($fmt,$ts);
00492                 //print "$s1 <br>$s2 <p>";
00493                 $pos = strcmp($s1,$s2);
00494 
00495                 if (($s1) != ($s2)) {
00496                         for ($j=0,$k=strlen($s1); $j < $k; $j++) {
00497                                 if ($s1[$j] != $s2[$j]) {
00498                                         print substr($s1,$j).' ';
00499                                         break;
00500                                 }
00501                         }
00502                         print "<b>Error date(): $ts<br><pre> 
00503 &nbsp; \"$s1\" (date len=".strlen($s1).")
00504 &nbsp; \"$s2\" (adodb_date len=".strlen($s2).")</b></pre><br>";
00505                         $fail = true;
00506                 }
00507                 
00508                 $a1 = getdate($ts);
00509                 $a2 = adodb_getdate($ts);
00510                 $rez = array_diff($a1,$a2);
00511                 if (sizeof($rez)>0) {
00512                         print "<b>Error getdate() $ts</b><br>";
00513                                 print_r($a1);
00514                         print "<br>";
00515                                 print_r($a2);
00516                         print "<p>";
00517                         $fail = true;
00518                 }
00519         }
00520         
00521         // Test generation of dates outside 1901-2038
00522         print "<p>Testing random dates between 100 and 4000</p>";
00523         adodb_date_test_date(100,1);
00524         for ($i=100; --$i >= 0;) {
00525                 $y1 = 100+rand(0,1970-100);
00526                 $m = rand(1,12);
00527                 adodb_date_test_date($y1,$m);
00528                 
00529                 $y1 = 3000-rand(0,3000-1970);
00530                 adodb_date_test_date($y1,$m);
00531         }
00532         print '<p>';
00533         $start = 1960+rand(0,10);
00534         $yrs = 12;
00535         $i = 365.25*86400*($start-1970);
00536         $offset = 36000+rand(10000,60000);
00537         $max = 365*$yrs*86400;
00538         $lastyear = 0;
00539         
00540         // we generate a timestamp, convert it to a date, and convert it back to a timestamp
00541         // and check if the roundtrip broke the original timestamp value.
00542         print "Testing $start to ".($start+$yrs).", or $max seconds, offset=$offset: ";
00543         $cnt = 0;
00544         for ($max += $i; $i < $max; $i += $offset) {
00545                 $ret = adodb_date('m,d,Y,H,i,s',$i);
00546                 $arr = explode(',',$ret);
00547                 if ($lastyear != $arr[2]) {
00548                         $lastyear = $arr[2];
00549                         print " $lastyear ";
00550                         flush();
00551                 }
00552                 $newi = adodb_mktime($arr[3],$arr[4],$arr[5],$arr[0],$arr[1],$arr[2]);
00553                 if ($i != $newi) {
00554                         print "Error at $i, adodb_mktime returned $newi ($ret)";
00555                         $fail = true;
00556                         break;
00557                 }
00558                 $cnt += 1;
00559         }
00560         echo "Tested $cnt dates<br>";
00561         if (!$fail) print "<p>Passed !</p>";
00562         else print "<p><b>Failed</b> :-(</p>";
00563 }
00564 
00569 function adodb_dow($year, $month, $day)
00570 {
00571 /*
00572 Pope Gregory removed 10 days - October 5 to October 14 - from the year 1582 and 
00573 proclaimed that from that time onwards 3 days would be dropped from the calendar 
00574 every 400 years.
00575 
00576 Thursday, October 4, 1582 (Julian) was followed immediately by Friday, October 15, 1582 (Gregorian). 
00577 */
00578         if ($year <= 1582) {
00579                 if ($year < 1582 || 
00580                         ($year == 1582 && ($month < 10 || ($month == 10 && $day < 15)))) $greg_correction = 3;
00581                  else
00582                         $greg_correction = 0;
00583         } else
00584                 $greg_correction = 0;
00585         
00586         if($month > 2)
00587             $month -= 2;
00588         else {
00589             $month += 10;
00590             $year--;
00591         }
00592         
00593         $day =  floor((13 * $month - 1) / 5) +
00594                 $day + ($year % 100) +
00595                 floor(($year % 100) / 4) +
00596                 floor(($year / 100) / 4) - 2 *
00597                 floor($year / 100) + 77 + $greg_correction;
00598         
00599         return $day - 7 * floor($day / 7);
00600 }
00601 
00602 
00607 function _adodb_is_leap_year($year) 
00608 {
00609         if ($year % 4 != 0) return false;
00610         
00611         if ($year % 400 == 0) {
00612                 return true;
00613         // if gregorian calendar (>1582), century not-divisible by 400 is not leap
00614         } else if ($year > 1582 && $year % 100 == 0 ) {
00615                 return false;
00616         } 
00617         
00618         return true;
00619 }
00620 
00621 
00625 function adodb_is_leap_year($year) 
00626 {
00627         return  _adodb_is_leap_year(adodb_year_digit_check($year));
00628 }
00629 
00634 function adodb_year_digit_check($y) 
00635 {
00636         if ($y < 100) {
00637         
00638                 $yr = (integer) date("Y");
00639                 $century = (integer) ($yr /100);
00640                 
00641                 if ($yr%100 > 50) {
00642                         $c1 = $century + 1;
00643                         $c0 = $century;
00644                 } else {
00645                         $c1 = $century;
00646                         $c0 = $century - 1;
00647                 }
00648                 $c1 *= 100;
00649                 // if 2-digit year is less than 30 years in future, set it to this century
00650                 // otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
00651                 if (($y + $c1) < $yr+30) $y = $y + $c1;
00652                 else $y = $y + $c0*100;
00653         }
00654         return $y;
00655 }
00656 
00660 function adodb_get_gmt_diff() 
00661 {
00662 static $TZ;
00663         if (isset($TZ)) return $TZ;
00664         
00665         $TZ = mktime(0,0,0,1,2,1970,0) - gmmktime(0,0,0,1,2,1970,0);
00666         return $TZ;
00667 }
00668 
00672 function adodb_getdate($d=false,$fast=false)
00673 {
00674         if ($d === false) return getdate();
00675         if (!defined('ADODB_TEST_DATES')) {
00676                 if ((abs($d) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
00677                         if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer
00678                                 return @getdate($d);
00679                 }
00680         }
00681         return _adodb_getdate($d);
00682 }
00683 
00684 /*
00685 // generate $YRS table for _adodb_getdate()
00686 function adodb_date_gentable($out=true)
00687 {
00688 
00689         for ($i=1970; $i >= 1600; $i-=10) {
00690                 $s = adodb_gmmktime(0,0,0,1,1,$i);
00691                 echo "$i => $s,<br>";   
00692         }
00693 }
00694 adodb_date_gentable();
00695 
00696 for ($i=1970; $i > 1500; $i--) {
00697 
00698 echo "<hr />$i ";
00699         adodb_date_test_date($i,1,1);
00700 }
00701 
00702 */
00703 
00704 
00705 $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
00706 $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
00707         
00708 function adodb_validdate($y,$m,$d)
00709 {
00710 global $_month_table_normal,$_month_table_leaf;
00711 
00712         if (_adodb_is_leap_year($y)) $marr =& $_month_table_leaf;
00713         else $marr =& $_month_table_normal;
00714         
00715         if ($m > 12 || $m < 1) return false;
00716         
00717         if ($d > 31 || $d < 1) return false;
00718         
00719         if ($marr[$m] < $d) return false;
00720         
00721         if ($y < 1000 && $y > 3000) return false;
00722         
00723         return true;
00724 }
00725 
00731 function _adodb_getdate($origd=false,$fast=false,$is_gmt=false)
00732 {
00733 static $YRS;
00734 global $_month_table_normal,$_month_table_leaf;
00735 
00736         $d =  $origd - ($is_gmt ? 0 : adodb_get_gmt_diff());
00737         
00738         $_day_power = 86400;
00739         $_hour_power = 3600;
00740         $_min_power = 60;
00741         
00742         if ($d < -12219321600) $d -= 86400*10; // if 15 Oct 1582 or earlier, gregorian correction 
00743         
00744         $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
00745         $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
00746         
00747         $d366 = $_day_power * 366;
00748         $d365 = $_day_power * 365;
00749         
00750         if ($d < 0) {
00751                 
00752                 if (empty($YRS)) $YRS = array(
00753                         1970 => 0,
00754                         1960 => -315619200,
00755                         1950 => -631152000,
00756                         1940 => -946771200,
00757                         1930 => -1262304000,
00758                         1920 => -1577923200,
00759                         1910 => -1893456000,
00760                         1900 => -2208988800,
00761                         1890 => -2524521600,
00762                         1880 => -2840140800,
00763                         1870 => -3155673600,
00764                         1860 => -3471292800,
00765                         1850 => -3786825600,
00766                         1840 => -4102444800,
00767                         1830 => -4417977600,
00768                         1820 => -4733596800,
00769                         1810 => -5049129600,
00770                         1800 => -5364662400,
00771                         1790 => -5680195200,
00772                         1780 => -5995814400,
00773                         1770 => -6311347200,
00774                         1760 => -6626966400,
00775                         1750 => -6942499200,
00776                         1740 => -7258118400,
00777                         1730 => -7573651200,
00778                         1720 => -7889270400,
00779                         1710 => -8204803200,
00780                         1700 => -8520336000,
00781                         1690 => -8835868800,
00782                         1680 => -9151488000,
00783                         1670 => -9467020800,
00784                         1660 => -9782640000,
00785                         1650 => -10098172800,
00786                         1640 => -10413792000,
00787                         1630 => -10729324800,
00788                         1620 => -11044944000,
00789                         1610 => -11360476800,
00790                         1600 => -11676096000);
00791 
00792                 if ($is_gmt) $origd = $d;
00793                 // The valid range of a 32bit signed timestamp is typically from 
00794                 // Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT
00795                 //
00796                 
00797                 # old algorithm iterates through all years. new algorithm does it in
00798                 # 10 year blocks
00799                 
00800                 /*
00801                 # old algo
00802                 for ($a = 1970 ; --$a >= 0;) {
00803                         $lastd = $d;
00804                         
00805                         if ($leaf = _adodb_is_leap_year($a)) $d += $d366;
00806                         else $d += $d365;
00807                         
00808                         if ($d >= 0) {
00809                                 $year = $a;
00810                                 break;
00811                         }
00812                 }
00813                 */
00814                 
00815                 $lastsecs = 0;
00816                 $lastyear = 1970;
00817                 foreach($YRS as $year => $secs) {
00818                         if ($d >= $secs) {
00819                                 $a = $lastyear;
00820                                 break;
00821                         }
00822                         $lastsecs = $secs;
00823                         $lastyear = $year;
00824                 }
00825                 
00826                 $d -= $lastsecs;
00827                 if (!isset($a)) $a = $lastyear;
00828                 
00829                 //echo ' yr=',$a,' ', $d,'.';
00830                 
00831                 for (; --$a >= 0;) {
00832                         $lastd = $d;
00833                         
00834                         if ($leaf = _adodb_is_leap_year($a)) $d += $d366;
00835                         else $d += $d365;
00836                         
00837                         if ($d >= 0) {
00838                                 $year = $a;
00839                                 break;
00840                         }
00841                 }
00842                 
00843                 
00844                 $secsInYear = 86400 * ($leaf ? 366 : 365) + $lastd;
00845                 
00846                 $d = $lastd;
00847                 $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal;
00848                 for ($a = 13 ; --$a > 0;) {
00849                         $lastd = $d;
00850                         $d += $mtab[$a] * $_day_power;
00851                         if ($d >= 0) {
00852                                 $month = $a;
00853                                 $ndays = $mtab[$a];
00854                                 break;
00855                         }
00856                 }
00857                 
00858                 $d = $lastd;
00859                 $day = $ndays + ceil(($d+1) / ($_day_power));
00860 
00861                 $d += ($ndays - $day+1)* $_day_power;
00862                 $hour = floor($d/$_hour_power);
00863         
00864         } else {
00865                 for ($a = 1970 ;; $a++) {
00866                         $lastd = $d;
00867                         
00868                         if ($leaf = _adodb_is_leap_year($a)) $d -= $d366;
00869                         else $d -= $d365;
00870                         if ($d < 0) {
00871                                 $year = $a;
00872                                 break;
00873                         }
00874                 }
00875                 $secsInYear = $lastd;
00876                 $d = $lastd;
00877                 $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal;
00878                 for ($a = 1 ; $a <= 12; $a++) {
00879                         $lastd = $d;
00880                         $d -= $mtab[$a] * $_day_power;
00881                         if ($d < 0) {
00882                                 $month = $a;
00883                                 $ndays = $mtab[$a];
00884                                 break;
00885                         }
00886                 }
00887                 $d = $lastd;
00888                 $day = ceil(($d+1) / $_day_power);
00889                 $d = $d - ($day-1) * $_day_power;
00890                 $hour = floor($d /$_hour_power);
00891         }
00892         
00893         $d -= $hour * $_hour_power;
00894         $min = floor($d/$_min_power);
00895         $secs = $d - $min * $_min_power;
00896         if ($fast) {
00897                 return array(
00898                 'seconds' => $secs,
00899                 'minutes' => $min,
00900                 'hours' => $hour,
00901                 'mday' => $day,
00902                 'mon' => $month,
00903                 'year' => $year,
00904                 'yday' => floor($secsInYear/$_day_power),
00905                 'leap' => $leaf,
00906                 'ndays' => $ndays
00907                 );
00908         }
00909         
00910         
00911         $dow = adodb_dow($year,$month,$day);
00912 
00913         return array(
00914                 'seconds' => $secs,
00915                 'minutes' => $min,
00916                 'hours' => $hour,
00917                 'mday' => $day,
00918                 'wday' => $dow,
00919                 'mon' => $month,
00920                 'year' => $year,
00921                 'yday' => floor($secsInYear/$_day_power),
00922                 'weekday' => gmdate('l',$_day_power*(3+$dow)),
00923                 'month' => gmdate('F',mktime(0,0,0,$month,2,1971)),
00924                 0 => $origd
00925         );
00926 }
00927 
00928 function adodb_gmdate($fmt,$d=false)
00929 {
00930         return adodb_date($fmt,$d,true);
00931 }
00932 
00933 // accepts unix timestamp and iso date format in $d
00934 function adodb_date2($fmt, $d=false, $is_gmt=false)
00935 {
00936         if ($d !== false) {
00937                 if (!preg_match( 
00938                         "|^([0-9]{4})[-/\.]?([0-9]{1,2})[-/\.]?([0-9]{1,2})[ -]?(([0-9]{1,2}):?([0-9]{1,2}):?([0-9\.]{1,4}))?|", 
00939                         ($d), $rr)) return adodb_date($fmt,false,$is_gmt);
00940 
00941                 if ($rr[1] <= 100 && $rr[2]<= 1) return adodb_date($fmt,false,$is_gmt);
00942         
00943                 // h-m-s-MM-DD-YY
00944                 if (!isset($rr[5])) $d = adodb_mktime(0,0,0,$rr[2],$rr[3],$rr[1],false,$is_gmt);
00945                 else $d = @adodb_mktime($rr[5],$rr[6],$rr[7],$rr[2],$rr[3],$rr[1],false,$is_gmt);
00946         }
00947         
00948         return adodb_date($fmt,$d,$is_gmt);
00949 }
00950 
00951 
00955 function adodb_date($fmt,$d=false,$is_gmt=false)
00956 {
00957 static $daylight;
00958 
00959         if ($d === false) return ($is_gmt)? @gmdate($fmt): @date($fmt);
00960         if (!defined('ADODB_TEST_DATES')) {
00961                 if ((abs($d) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
00962                         if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer
00963                                 return ($is_gmt)? @gmdate($fmt,$d): @date($fmt,$d);
00964 
00965                 }
00966         }
00967         $_day_power = 86400;
00968         
00969         $arr = _adodb_getdate($d,true,$is_gmt);
00970         
00971         if (!isset($daylight)) $daylight = function_exists('adodb_daylight_sv');
00972         if ($daylight) adodb_daylight_sv($arr, $is_gmt);
00973         
00974         $year = $arr['year'];
00975         $month = $arr['mon'];
00976         $day = $arr['mday'];
00977         $hour = $arr['hours'];
00978         $min = $arr['minutes'];
00979         $secs = $arr['seconds'];
00980         
00981         $max = strlen($fmt);
00982         $dates = '';
00983         
00984         $isphp5 = PHP_VERSION >= 5;
00985         
00986         /*
00987                 at this point, we have the following integer vars to manipulate:
00988                 $year, $month, $day, $hour, $min, $secs
00989         */
00990         for ($i=0; $i < $max; $i++) {
00991                 switch($fmt[$i]) {
00992                 case 'T': $dates .= date('T');break;
00993                 // YEAR
00994                 case 'L': $dates .= $arr['leap'] ? '1' : '0'; break;
00995                 case 'r': // Thu, 21 Dec 2000 16:01:07 +0200
00996                 
00997                         // 4.3.11 uses '04 Jun 2004'
00998                         // 4.3.8 uses  ' 4 Jun 2004'
00999                         $dates .= gmdate('D',$_day_power*(3+adodb_dow($year,$month,$day))).', '         
01000                                 . ($day<10?'0'.$day:$day) . ' '.date('M',mktime(0,0,0,$month,2,1971)).' '.$year.' ';
01001                         
01002                         if ($hour < 10) $dates .= '0'.$hour; else $dates .= $hour; 
01003                         
01004                         if ($min < 10) $dates .= ':0'.$min; else $dates .= ':'.$min;
01005                         
01006                         if ($secs < 10) $dates .= ':0'.$secs; else $dates .= ':'.$secs;
01007                         
01008                         $gmt = adodb_get_gmt_diff();
01009                         if ($isphp5) 
01010                                 $dates .= sprintf(' %s%04d',($gmt<=0)?'+':'-',abs($gmt)/36); 
01011                         else
01012                                 $dates .= sprintf(' %s%04d',($gmt<0)?'+':'-',abs($gmt)/36); 
01013                         break;
01014                                 
01015                 case 'Y': $dates .= $year; break;
01016                 case 'y': $dates .= substr($year,strlen($year)-2,2); break;
01017                 // MONTH
01018                 case 'm': if ($month<10) $dates .= '0'.$month; else $dates .= $month; break;
01019                 case 'Q': $dates .= ($month+3)>>2; break;
01020                 case 'n': $dates .= $month; break;
01021                 case 'M': $dates .= date('M',mktime(0,0,0,$month,2,1971)); break;
01022                 case 'F': $dates .= date('F',mktime(0,0,0,$month,2,1971)); break;
01023                 // DAY
01024                 case 't': $dates .= $arr['ndays']; break;
01025                 case 'z': $dates .= $arr['yday']; break;
01026                 case 'w': $dates .= adodb_dow($year,$month,$day); break;
01027                 case 'l': $dates .= gmdate('l',$_day_power*(3+adodb_dow($year,$month,$day))); break;
01028                 case 'D': $dates .= gmdate('D',$_day_power*(3+adodb_dow($year,$month,$day))); break;
01029                 case 'j': $dates .= $day; break;
01030                 case 'd': if ($day<10) $dates .= '0'.$day; else $dates .= $day; break;
01031                 case 'S': 
01032                         $d10 = $day % 10;
01033                         if ($d10 == 1) $dates .= 'st';
01034                         else if ($d10 == 2 && $day != 12) $dates .= 'nd';
01035                         else if ($d10 == 3) $dates .= 'rd';
01036                         else $dates .= 'th';
01037                         break;
01038                         
01039                 // HOUR
01040                 case 'Z':
01041                         $dates .= ($is_gmt) ? 0 : -adodb_get_gmt_diff(); break;
01042                 case 'O': 
01043                         $gmt = ($is_gmt) ? 0 : adodb_get_gmt_diff();
01044                         
01045                         if ($isphp5)
01046                                 $dates .= sprintf('%s%04d',($gmt<=0)?'+':'-',abs($gmt)/36); 
01047                         else
01048                                 $dates .= sprintf('%s%04d',($gmt<0)?'+':'-',abs($gmt)/36); 
01049                         break;
01050                         
01051                 case 'H': 
01052                         if ($hour < 10) $dates .= '0'.$hour; 
01053                         else $dates .= $hour; 
01054                         break;
01055                 case 'h': 
01056                         if ($hour > 12) $hh = $hour - 12; 
01057                         else {
01058                                 if ($hour == 0) $hh = '12'; 
01059                                 else $hh = $hour;
01060                         }
01061                         
01062                         if ($hh < 10) $dates .= '0'.$hh;
01063                         else $dates .= $hh;
01064                         break;
01065                         
01066                 case 'G': 
01067                         $dates .= $hour;
01068                         break;
01069                         
01070                 case 'g':
01071                         if ($hour > 12) $hh = $hour - 12; 
01072                         else {
01073                                 if ($hour == 0) $hh = '12'; 
01074                                 else $hh = $hour; 
01075                         }
01076                         $dates .= $hh;
01077                         break;
01078                 // MINUTES
01079                 case 'i': if ($min < 10) $dates .= '0'.$min; else $dates .= $min; break;
01080                 // SECONDS
01081                 case 'U': $dates .= $d; break;
01082                 case 's': if ($secs < 10) $dates .= '0'.$secs; else $dates .= $secs; break;
01083                 // AM/PM
01084                 // Note 00:00 to 11:59 is AM, while 12:00 to 23:59 is PM
01085                 case 'a':
01086                         if ($hour>=12) $dates .= 'pm';
01087                         else $dates .= 'am';
01088                         break;
01089                 case 'A':
01090                         if ($hour>=12) $dates .= 'PM';
01091                         else $dates .= 'AM';
01092                         break;
01093                 default:
01094                         $dates .= $fmt[$i]; break;
01095                 // ESCAPE
01096                 case "\\": 
01097                         $i++;
01098                         if ($i < $max) $dates .= $fmt[$i];
01099                         break;
01100                 }
01101         }
01102         return $dates;
01103 }
01104 
01109 function adodb_gmmktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false)
01110 {
01111         return adodb_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,true);
01112 }
01113 
01120 function adodb_mktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false,$is_gmt=false) 
01121 {
01122         if (!defined('ADODB_TEST_DATES')) {
01123 
01124                 if ($mon === false) {
01125                         return $is_gmt? @gmmktime($hr,$min,$sec): @mktime($hr,$min,$sec);
01126                 }
01127                 
01128                 // for windows, we don't check 1970 because with timezone differences, 
01129                 // 1 Jan 1970 could generate negative timestamp, which is illegal
01130                 if (1971 < $year && $year < 2038
01131                         || !defined('ADODB_NO_NEGATIVE_TS') && (1901 < $year && $year < 2038)
01132                         ) {
01133                                 return $is_gmt ?
01134                                         @gmmktime($hr,$min,$sec,$mon,$day,$year):
01135                                         @mktime($hr,$min,$sec,$mon,$day,$year);
01136                         }
01137         }
01138         
01139         $gmt_different = ($is_gmt) ? 0 : adodb_get_gmt_diff();
01140 
01141         /*
01142         # disabled because some people place large values in $sec.
01143         # however we need it for $mon because we use an array...
01144         $hr = intval($hr);
01145         $min = intval($min);
01146         $sec = intval($sec);
01147         */
01148         $mon = intval($mon);
01149         $day = intval($day);
01150         $year = intval($year);
01151         
01152         
01153         $year = adodb_year_digit_check($year);
01154 
01155         if ($mon > 12) {
01156                 $y = floor($mon / 12);
01157                 $year += $y;
01158                 $mon -= $y*12;
01159         } else if ($mon < 1) {
01160                 $y = ceil((1-$mon) / 12);
01161                 $year -= $y;
01162                 $mon += $y*12;
01163         }
01164         
01165         $_day_power = 86400;
01166         $_hour_power = 3600;
01167         $_min_power = 60;
01168         
01169         $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
01170         $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
01171         
01172         $_total_date = 0;
01173         if ($year >= 1970) {
01174                 for ($a = 1970 ; $a <= $year; $a++) {
01175                         $leaf = _adodb_is_leap_year($a);
01176                         if ($leaf == true) {
01177                                 $loop_table = $_month_table_leaf;
01178                                 $_add_date = 366;
01179                         } else {
01180                                 $loop_table = $_month_table_normal;
01181                                 $_add_date = 365;
01182                         }
01183                         if ($a < $year) { 
01184                                 $_total_date += $_add_date;
01185                         } else {
01186                                 for($b=1;$b<$mon;$b++) {
01187                                         $_total_date += $loop_table[$b];
01188                                 }
01189                         }
01190                 }
01191                 $_total_date +=$day-1;
01192                 $ret = $_total_date * $_day_power + $hr * $_hour_power + $min * $_min_power + $sec + $gmt_different;
01193         
01194         } else {
01195                 for ($a = 1969 ; $a >= $year; $a--) {
01196                         $leaf = _adodb_is_leap_year($a);
01197                         if ($leaf == true) {
01198                                 $loop_table = $_month_table_leaf;
01199                                 $_add_date = 366;
01200                         } else {
01201                                 $loop_table = $_month_table_normal;
01202                                 $_add_date = 365;
01203                         }
01204                         if ($a > $year) { $_total_date += $_add_date;
01205                         } else {
01206                                 for($b=12;$b>$mon;$b--) {
01207                                         $_total_date += $loop_table[$b];
01208                                 }
01209                         }
01210                 }
01211                 $_total_date += $loop_table[$mon] - $day;
01212                 
01213                 $_day_time = $hr * $_hour_power + $min * $_min_power + $sec;
01214                 $_day_time = $_day_power - $_day_time;
01215                 $ret = -( $_total_date * $_day_power + $_day_time - $gmt_different);
01216                 if ($ret < -12220185600) $ret += 10*86400; // if earlier than 5 Oct 1582 - gregorian correction
01217                 else if ($ret < -12219321600) $ret = -12219321600; // if in limbo, reset to 15 Oct 1582.
01218         } 
01219         //print " dmy=$day/$mon/$year $hr:$min:$sec => " .$ret;
01220         return $ret;
01221 }
01222 
01223 function adodb_gmstrftime($fmt, $ts=false)
01224 {
01225         return adodb_strftime($fmt,$ts,true);
01226 }
01227 
01228 // hack - convert to adodb_date
01229 function adodb_strftime($fmt, $ts=false,$is_gmt=false)
01230 {
01231 global $ADODB_DATE_LOCALE;
01232 
01233         if (!defined('ADODB_TEST_DATES')) {
01234                 if ((abs($ts) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
01235                         if (!defined('ADODB_NO_NEGATIVE_TS') || $ts >= 0) // if windows, must be +ve integer
01236                                 return ($is_gmt)? @gmstrftime($fmt,$ts): @strftime($fmt,$ts);
01237 
01238                 }
01239         }
01240         
01241         if (empty($ADODB_DATE_LOCALE)) {
01242                 $tstr = strtoupper(gmstrftime('%c',31366800)); // 30 Dec 1970, 1 am
01243                 $sep = substr($tstr,2,1);
01244                 $hasAM = strrpos($tstr,'M') !== false;
01245                 
01246                 $ADODB_DATE_LOCALE = array();
01247                 $ADODB_DATE_LOCALE[] =  strncmp($tstr,'30',2) == 0 ? 'd'.$sep.'m'.$sep.'y' : 'm'.$sep.'d'.$sep.'y';     
01248                 $ADODB_DATE_LOCALE[]  = ($hasAM) ? 'h:i:s a' : 'H:i:s';
01249                         
01250         }
01251         $inpct = false;
01252         $fmtdate = '';
01253         for ($i=0,$max = strlen($fmt); $i < $max; $i++) {
01254                 $ch = $fmt[$i];
01255                 if ($ch == '%') {
01256                         if ($inpct) {
01257                                 $fmtdate .= '%';
01258                                 $inpct = false;
01259                         } else
01260                                 $inpct = true;
01261                 } else if ($inpct) {
01262                 
01263                         $inpct = false;
01264                         switch($ch) {
01265                         case '0':
01266                         case '1':
01267                         case '2':
01268                         case '3':
01269                         case '4':
01270                         case '5':
01271                         case '6':
01272                         case '7':
01273                         case '8':
01274                         case '9':
01275                         case 'E':
01276                         case 'O':
01277                                 /* ignore format modifiers */
01278                                 $inpct = true; 
01279                                 break;
01280                                 
01281                         case 'a': $fmtdate .= 'D'; break;
01282                         case 'A': $fmtdate .= 'l'; break;
01283                         case 'h':
01284                         case 'b': $fmtdate .= 'M'; break;
01285                         case 'B': $fmtdate .= 'F'; break;
01286                         case 'c': $fmtdate .= $ADODB_DATE_LOCALE[0].$ADODB_DATE_LOCALE[1]; break;
01287                         case 'C': $fmtdate .= '\C?'; break; // century
01288                         case 'd': $fmtdate .= 'd'; break;
01289                         case 'D': $fmtdate .= 'm/d/y'; break;
01290                         case 'e': $fmtdate .= 'j'; break;
01291                         case 'g': $fmtdate .= '\g?'; break; //?
01292                         case 'G': $fmtdate .= '\G?'; break; //?
01293                         case 'H': $fmtdate .= 'H'; break;
01294                         case 'I': $fmtdate .= 'h'; break;
01295                         case 'j': $fmtdate .= '?z'; $parsej = true; break; // wrong as j=1-based, z=0-basd
01296                         case 'm': $fmtdate .= 'm'; break;
01297                         case 'M': $fmtdate .= 'i'; break;
01298                         case 'n': $fmtdate .= "\n"; break;
01299                         case 'p': $fmtdate .= 'a'; break;
01300                         case 'r': $fmtdate .= 'h:i:s a'; break;
01301                         case 'R': $fmtdate .= 'H:i:s'; break;
01302                         case 'S': $fmtdate .= 's'; break;
01303                         case 't': $fmtdate .= "\t"; break;
01304                         case 'T': $fmtdate .= 'H:i:s'; break;
01305                         case 'u': $fmtdate .= '?u'; $parseu = true; break; // wrong strftime=1-based, date=0-based
01306                         case 'U': $fmtdate .= '?U'; $parseU = true; break;// wrong strftime=1-based, date=0-based
01307                         case 'x': $fmtdate .= $ADODB_DATE_LOCALE[0]; break;
01308                         case 'X': $fmtdate .= $ADODB_DATE_LOCALE[1]; break;
01309                         case 'w': $fmtdate .= '?w'; $parseu = true; break; // wrong strftime=1-based, date=0-based
01310                         case 'W': $fmtdate .= '?W'; $parseU = true; break;// wrong strftime=1-based, date=0-based
01311                         case 'y': $fmtdate .= 'y'; break;
01312                         case 'Y': $fmtdate .= 'Y'; break;
01313                         case 'Z': $fmtdate .= 'T'; break;
01314                         }
01315                 } else if (('A' <= ($ch) && ($ch) <= 'Z' ) || ('a' <= ($ch) && ($ch) <= 'z' ))
01316                         $fmtdate .= "\\".$ch;
01317                 else
01318                         $fmtdate .= $ch;
01319         }
01320         //echo "fmt=",$fmtdate,"<br>";
01321         if ($ts === false) $ts = time();
01322         $ret = adodb_date($fmtdate, $ts, $is_gmt);
01323         return $ret;
01324 }
01325 
01326 
01327 ?>


Généré par Le spécialiste TYPO3 avec  doxygen 1.4.6