Index: trunk/kernel/include/adodb/adodb-time.inc.php =================================================================== diff -u -N -r535 -r3282 --- trunk/kernel/include/adodb/adodb-time.inc.php (.../adodb-time.inc.php) (revision 535) +++ trunk/kernel/include/adodb/adodb-time.inc.php (.../adodb-time.inc.php) (revision 3282) @@ -23,7 +23,9 @@ date() with adodb_date() gmdate() with adodb_gmdate() mktime() with adodb_mktime() - gmmktime() with adodb_gmmktime()45 + gmmktime() with adodb_gmmktime() + strftime() with adodb_strftime() + strftime() with adodb_gmstrftime() The parameters are identical, except that adodb_date() accepts a subset @@ -55,8 +57,8 @@ COPYRIGHT -(c) 2003 John Lim and released under BSD-style license except for code by jackbbs, -which includes adodb_mktime, adodb_get_gmt_different, adodb_is_leap_year +(c) 2003-2005 John Lim and released under BSD-style license except for code by +jackbbs, which includes adodb_mktime, adodb_get_gmt_diff, adodb_is_leap_year and originally found at http://www.php.net/manual/en/function.mktime.php ============================================================================= @@ -72,82 +74,155 @@ FUNCTION DESCRIPTIONS -FUNCTION adodb_getdate($date=false) +** FUNCTION adodb_getdate($date=false) Returns an array containing date information, as getdate(), but supports -dates greater than 1901 to 2038. +dates greater than 1901 to 2038. The local date/time format is derived from a +heuristic the first time adodb_getdate is called. + + +** FUNCTION adodb_date($fmt, $timestamp = false) - -FUNCTION adodb_date($fmt, $timestamp = false) - Convert a timestamp to a formatted local date. If $timestamp is not defined, the current timestamp is used. Unlike the function date(), it supports dates outside the 1901 to 2038 range. The format fields that adodb_date supports:
-a - "am" or "pm" 
-A - "AM" or "PM" 
-d - day of the month, 2 digits with leading zeros; i.e. "01" to "31" 
-D - day of the week, textual, 3 letters; e.g. "Fri" 
-F - month, textual, long; e.g. "January" 
-g - hour, 12-hour format without leading zeros; i.e. "1" to "12" 
-G - hour, 24-hour format without leading zeros; i.e. "0" to "23" 
-h - hour, 12-hour format; i.e. "01" to "12" 
-H - hour, 24-hour format; i.e. "00" to "23" 
-i - minutes; i.e. "00" to "59" 
-j - day of the month without leading zeros; i.e. "1" to "31" 
-l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"  
-L - boolean for whether it is a leap year; i.e. "0" or "1" 
-m - month; i.e. "01" to "12" 
-M - month, textual, 3 letters; e.g. "Jan" 
-n - month without leading zeros; i.e. "1" to "12" 
-O - Difference to Greenwich time in hours; e.g. "+0200" 
-r - RFC 822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200" 
-s - seconds; i.e. "00" to "59" 
-S - English ordinal suffix for the day of the month, 2 characters; 
-   			i.e. "st", "nd", "rd" or "th" 
-t - number of days in the given month; i.e. "28" to "31"
-T - Timezone setting of this machine; e.g. "EST" or "MDT" 
-U - seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)  
-w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday) 
-Y - year, 4 digits; e.g. "1999" 
-y - year, 2 digits; e.g. "99" 
-z - day of the year; i.e. "0" to "365" 
-Z - timezone offset in seconds (i.e. "-43200" to "43200"). 
-   			The offset for timezones west of UTC is always negative, 
-			and for those east of UTC is always positive. 
+	a - "am" or "pm" 
+	A - "AM" or "PM" 
+	d - day of the month, 2 digits with leading zeros; i.e. "01" to "31" 
+	D - day of the week, textual, 3 letters; e.g. "Fri" 
+	F - month, textual, long; e.g. "January" 
+	g - hour, 12-hour format without leading zeros; i.e. "1" to "12" 
+	G - hour, 24-hour format without leading zeros; i.e. "0" to "23" 
+	h - hour, 12-hour format; i.e. "01" to "12" 
+	H - hour, 24-hour format; i.e. "00" to "23" 
+	i - minutes; i.e. "00" to "59" 
+	j - day of the month without leading zeros; i.e. "1" to "31" 
+	l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"  
+	L - boolean for whether it is a leap year; i.e. "0" or "1" 
+	m - month; i.e. "01" to "12" 
+	M - month, textual, 3 letters; e.g. "Jan" 
+	n - month without leading zeros; i.e. "1" to "12" 
+	O - Difference to Greenwich time in hours; e.g. "+0200" 
+	Q - Quarter, as in 1, 2, 3, 4 
+	r - RFC 822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200" 
+	s - seconds; i.e. "00" to "59" 
+	S - English ordinal suffix for the day of the month, 2 characters; 
+	   			i.e. "st", "nd", "rd" or "th" 
+	t - number of days in the given month; i.e. "28" to "31"
+	T - Timezone setting of this machine; e.g. "EST" or "MDT" 
+	U - seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)  
+	w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday) 
+	Y - year, 4 digits; e.g. "1999" 
+	y - year, 2 digits; e.g. "99" 
+	z - day of the year; i.e. "0" to "365" 
+	Z - timezone offset in seconds (i.e. "-43200" to "43200"). 
+	   			The offset for timezones west of UTC is always negative, 
+				and for those east of UTC is always positive. 
 
Unsupported:
-B - Swatch Internet time 
-I (capital i) - "1" if Daylight Savings Time, "0" otherwise.
-W - ISO-8601 week number of year, weeks starting on Monday 
+	B - Swatch Internet time 
+	I (capital i) - "1" if Daylight Savings Time, "0" otherwise.
+	W - ISO-8601 week number of year, weeks starting on Monday 
 
 
-FUNCTION adodb_gmdate($fmt, $timestamp = false) +** FUNCTION adodb_date2($fmt, $isoDateString = false) +Same as adodb_date, but 2nd parameter accepts iso date, eg. + adodb_date2('d-M-Y H:i','2003-12-25 13:01:34'); + + +** FUNCTION adodb_gmdate($fmt, $timestamp = false) + Convert a timestamp to a formatted GMT date. If $timestamp is not defined, the current timestamp is used. Unlike the function date(), it supports dates outside the 1901 to 2038 range. -FUNCTION adodb_mktime($hr, $min, $sec, $month, $day, $year) +** FUNCTION adodb_mktime($hr, $min, $sec[, $month, $day, $year]) Converts a local date to a unix timestamp. Unlike the function mktime(), it supports -dates outside the 1901 to 2038 range. Differs from mktime() in that all parameters -are currently compulsory. +dates outside the 1901 to 2038 range. All parameters are optional. -FUNCTION adodb_gmmktime($hr, $min, $sec, $month, $day, $year) +** FUNCTION adodb_gmmktime($hr, $min, $sec [, $month, $day, $year]) + Converts a gmt date to a unix timestamp. Unlike the function gmmktime(), it supports dates outside the 1901 to 2038 range. Differs from gmmktime() in that all parameters are currently compulsory. +** FUNCTION adodb_gmstrftime($fmt, $timestamp = false) +Convert a timestamp to a formatted GMT date. + +** FUNCTION adodb_strftime($fmt, $timestamp = false) + +Convert a timestamp to a formatted local date. Internally converts $fmt into +adodb_date format, then echo result. + +For best results, you can define the local date format yourself. Define a global +variable $ADODB_DATE_LOCALE which is an array, 1st element is date format using +adodb_date syntax, and 2nd element is the time format, also in adodb_date syntax. + + eg. $ADODB_DATE_LOCALE = array('d/m/Y','H:i:s'); + + Supported format codes: + +
+	%a - abbreviated weekday name according to the current locale 
+	%A - full weekday name according to the current locale 
+	%b - abbreviated month name according to the current locale 
+	%B - full month name according to the current locale 
+	%c - preferred date and time representation for the current locale 
+	%d - day of the month as a decimal number (range 01 to 31) 
+	%D - same as %m/%d/%y 
+	%e - day of the month as a decimal number, a single digit is preceded by a space (range ' 1' to '31') 
+	%h - same as %b
+	%H - hour as a decimal number using a 24-hour clock (range 00 to 23) 
+	%I - hour as a decimal number using a 12-hour clock (range 01 to 12) 
+	%m - month as a decimal number (range 01 to 12) 
+	%M - minute as a decimal number 
+	%n - newline character 
+	%p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale 
+	%r - time in a.m. and p.m. notation 
+	%R - time in 24 hour notation 
+	%S - second as a decimal number 
+	%t - tab character 
+	%T - current time, equal to %H:%M:%S 
+	%x - preferred date representation for the current locale without the time 
+	%X - preferred time representation for the current locale without the date 
+	%y - year as a decimal number without a century (range 00 to 99) 
+	%Y - year as a decimal number including the century 
+	%Z - time zone or name or abbreviation 
+	%% - a literal `%' character 
+
+ + Unsupported codes: +
+	%C - century number (the year divided by 100 and truncated to an integer, range 00 to 99) 
+	%g - like %G, but without the century. 
+	%G - The 4-digit year corresponding to the ISO week number (see %V). 
+	     This has the same format and value as %Y, except that if the ISO week number belongs 
+		 to the previous or next year, that year is used instead. 
+	%j - day of the year as a decimal number (range 001 to 366) 
+	%u - weekday as a decimal number [1,7], with 1 representing Monday 
+	%U - week number of the current year as a decimal number, starting 
+	    with the first Sunday as the first day of the first week 
+	%V - The ISO 8601:1988 week number of the current year as a decimal number, 
+	     range 01 to 53, where week 1 is the first week that has at least 4 days in the 
+		 current year, and with Monday as the first day of the week. (Use %G or %g for 
+		 the year component that corresponds to the week number for the specified timestamp.) 
+	%w - day of the week as a decimal, Sunday being 0 
+	%W - week number of the current year as a decimal number, starting with the 
+	     first Monday as the first day of the first week 
+
+ ============================================================================= NOTES @@ -161,14 +236,81 @@ (page 428, xttotm.c _Ttotm() function). Plauger's algorithm will not work outside 32-bit signed range, so i decided not to implement it. -b. Iterate over a block of years (say 12) when searching for the -correct year. - -c. Implement daylight savings, which looks awfully complicated, see +b. Implement daylight savings, which looks awfully complicated, see http://webexhibits.org/daylightsaving/ CHANGELOG + +- 18 July 2005 0.21 +- In PHP 4.3.11, the 'r' format has changed. Leading 0 in day is added. Changed for compat. +- Added support for negative months in adodb_mktime(). + +- 24 Feb 2005 0.20 +Added limited strftime/gmstrftime support. x10 improvement in performance of adodb_date(). + +- 21 Dec 2004 0.17 +In adodb_getdate(), the timestamp was accidentally converted to gmt when $is_gmt is false. +Also adodb_mktime(0,0,0) did not work properly. Both fixed thx Mauro. + +- 17 Nov 2004 0.16 +Removed intval typecast in adodb_mktime() for secs, allowing: + adodb_mktime(0,0,0 + 2236672153,1,1,1934); +Suggested by Ryan. + +- 18 July 2004 0.15 +All params in adodb_mktime were formerly compulsory. Now only the hour, min, secs is compulsory. +This brings it more in line with mktime (still not identical). + +- 23 June 2004 0.14 + +Allow you to define your own daylights savings function, adodb_daylight_sv. +If the function is defined (somewhere in an include), then you can correct for daylights savings. + +In this example, we apply daylights savings in June or July, adding one hour. This is extremely +unrealistic as it does not take into account time-zone, geographic location, current year. + +function adodb_daylight_sv(&$arr, $is_gmt) +{ + if ($is_gmt) return; + $m = $arr['mon']; + if ($m == 6 || $m == 7) $arr['hours'] += 1; +} + +This is only called by adodb_date() and not by adodb_mktime(). + +The format of $arr is +Array ( + [seconds] => 0 + [minutes] => 0 + [hours] => 0 + [mday] => 1 # day of month, eg 1st day of the month + [mon] => 2 # month (eg. Feb) + [year] => 2102 + [yday] => 31 # days in current year + [leap] => # true if leap year + [ndays] => 28 # no of days in current month + ) + + +- 28 Apr 2004 0.13 +Fixed adodb_date to properly support $is_gmt. Thx to Dimitar Angelov. + +- 20 Mar 2004 0.12 +Fixed month calculation error in adodb_date. 2102-June-01 appeared as 2102-May-32. + +- 26 Oct 2003 0.11 +Because of daylight savings problems (some systems apply daylight savings to +January!!!), changed adodb_get_gmt_diff() to ignore daylight savings. + +- 9 Aug 2003 0.10 +Fixed bug with dates after 2038. +See http://phplens.com/lens/lensforum/msgs.php?id=6980 + +- 1 July 2003 0.09 +Added support for Q (Quarter). +Added adodb_date2(), which accepts ISO date in 2nd param + - 3 March 2003 0.08 Added support for 'S' adodb_date() format char. Added constant ADODB_ALLOW_NEGATIVE_TS if you want PHP to handle negative timestamps between 1901 to 1969. @@ -217,12 +359,11 @@ /* Version Number */ -define('ADODB_DATE_VERSION',0.08); +define('ADODB_DATE_VERSION',0.21); /* - We check for Windows as only +ve ints are accepted as dates on Windows. - - Apparently this problem happens also with Linux, RH 7.3 and later! + This code was originally for windows. But apparently this problem happens + also with Linux, RH 7.3 and later! glibc-2.2.5-34 and greater has been changed to return -1 for dates < 1970. This used to work. The problem exists with RedHat 7.3 and 8.0 @@ -235,30 +376,56 @@ if (!defined('ADODB_ALLOW_NEGATIVE_TS')) define('ADODB_NO_NEGATIVE_TS',1); -function adodb_date_test_date($y1,$m) +function adodb_date_test_date($y1,$m,$d=13) { - //print " $y1/$m "; - $t = adodb_mktime(0,0,0,$m,13,$y1); - if ("$y1-$m-13 00:00:00" != adodb_date('Y-n-d H:i:s',$t)) { - print "$y1 error
"; + $t = adodb_mktime(0,0,0,$m,$d,$y1); + $rez = adodb_date('Y-n-j H:i:s',$t); + if ("$y1-$m-$d 00:00:00" != $rez) { + print "$y1 error, expected=$y1-$m-$d 00:00:00, adodb=$rez
"; return false; } return true; } + +function adodb_date_test_strftime($fmt) +{ + $s1 = strftime($fmt); + $s2 = adodb_strftime($fmt); + + if ($s1 == $s2) return true; + + echo "error for $fmt, strftime=$s1, $adodb=$s2
"; + return false; +} + /** Test Suite */ function adodb_date_test() { error_reporting(E_ALL); - print "

Testing adodb_date and adodb_mktime. version=".ADODB_DATE_VERSION. "

"; - set_time_limit(0); + print "

Testing adodb_date and adodb_mktime. version=".ADODB_DATE_VERSION.' PHP='.PHP_VERSION."

"; + @set_time_limit(0); $fail = false; // This flag disables calling of PHP native functions, so we can properly test the code if (!defined('ADODB_TEST_DATES')) define('ADODB_TEST_DATES',1); + adodb_date_test_strftime('%Y %m %x %X'); + adodb_date_test_strftime("%A %d %B %Y"); + adodb_date_test_strftime("%H %M S"); + + $t = adodb_mktime(0,0,0); + if (!(adodb_date('Y-m-d') == date('Y-m-d'))) print 'Error in '.adodb_mktime(0,0,0).'
'; + + $t = adodb_mktime(0,0,0,6,1,2102); + if (!(adodb_date('Y-m-d',$t) == '2102-06-01')) print 'Error in '.adodb_date('Y-m-d',$t).'
'; + + $t = adodb_mktime(0,0,0,2,1,2102); + if (!(adodb_date('Y-m-d',$t) == '2102-02-01')) print 'Error in '.adodb_date('Y-m-d',$t).'
'; + + print "

Testing gregorian <=> julian conversion

"; $t = adodb_mktime(0,0,0,10,11,1492); //http://www.holidayorigins.com/html/columbus_day.html - Friday check @@ -367,7 +534,7 @@ // we generate a timestamp, convert it to a date, and convert it back to a timestamp // and check if the roundtrip broke the original timestamp value. print "Testing $start to ".($start+$yrs).", or $max seconds, offset=$offset: "; - + $cnt = 0; for ($max += $i; $i < $max; $i += $offset) { $ret = adodb_date('m,d,Y,H,i,s',$i); $arr = explode(',',$ret); @@ -382,8 +549,9 @@ $fail = true; break; } + $cnt += 1; } - + echo "Tested $cnt dates
"; if (!$fail) print "

Passed !

"; else print "

Failed :-(

"; } @@ -416,13 +584,13 @@ $year--; } - $day = ( floor((13 * $month - 1) / 5) + + $day = floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4) + floor(($year / 100) / 4) - 2 * - floor($year / 100) + 77); + floor($year / 100) + 77 + $greg_correction; - return (($day - 7 * floor($day / 7))) + $greg_correction; + return $day - 7 * floor($day / 7); } @@ -444,6 +612,7 @@ return true; } + /** checks for leap year, returns true if it is. Has 2-digit year check */ @@ -479,20 +648,18 @@ return $y; } - /** get local time zone offset from GMT */ -function adodb_get_gmt_different() +function adodb_get_gmt_diff() { -static $DIFF; - if (isset($DIFF)) return $DIFF; +static $TZ; + if (isset($TZ)) return $TZ; - $DIFF = mktime(0,0,0,1,2,2000) - gmmktime(0,0,0,1,2,2000); - return $DIFF; + $TZ = mktime(0,0,0,1,2,1970,0) - gmmktime(0,0,0,1,2,1970,0); + return $TZ; } - /** Returns an array with date info. */ @@ -508,14 +675,36 @@ return _adodb_getdate($d); } +/* +// generate $YRS table for _adodb_getdate() +function adodb_date_gentable($out=true) +{ + + for ($i=1970; $i >= 1600; $i-=10) { + $s = adodb_gmmktime(0,0,0,1,1,$i); + echo "$i => $s,
"; + } +} +adodb_date_gentable(); + +for ($i=1970; $i > 1500; $i--) { + +echo "
$i "; + adodb_date_test_date($i,1,1); +} + +*/ + /** Low-level function that returns the getdate() array. We have a special $fast flag, which if set to true, will return fewer array values, and is much faster as it does not calculate dow, etc. */ function _adodb_getdate($origd=false,$fast=false,$is_gmt=false) { - $d = $origd - ($is_gmt ? 0 : adodb_get_gmt_different()); +static $YRS; + + $d = $origd - ($is_gmt ? 0 : adodb_get_gmt_diff()); $_day_power = 86400; $_hour_power = 3600; @@ -526,23 +715,103 @@ $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31); $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31); + $d366 = $_day_power * 366; + $d365 = $_day_power * 365; + if ($d < 0) { - $origd = $d; + + if (empty($YRS)) $YRS = array( + 1970 => 0, + 1960 => -315619200, + 1950 => -631152000, + 1940 => -946771200, + 1930 => -1262304000, + 1920 => -1577923200, + 1910 => -1893456000, + 1900 => -2208988800, + 1890 => -2524521600, + 1880 => -2840140800, + 1870 => -3155673600, + 1860 => -3471292800, + 1850 => -3786825600, + 1840 => -4102444800, + 1830 => -4417977600, + 1820 => -4733596800, + 1810 => -5049129600, + 1800 => -5364662400, + 1790 => -5680195200, + 1780 => -5995814400, + 1770 => -6311347200, + 1760 => -6626966400, + 1750 => -6942499200, + 1740 => -7258118400, + 1730 => -7573651200, + 1720 => -7889270400, + 1710 => -8204803200, + 1700 => -8520336000, + 1690 => -8835868800, + 1680 => -9151488000, + 1670 => -9467020800, + 1660 => -9782640000, + 1650 => -10098172800, + 1640 => -10413792000, + 1630 => -10729324800, + 1620 => -11044944000, + 1610 => -11360476800, + 1600 => -11676096000); + + if ($is_gmt) $origd = $d; // The valid range of a 32bit signed timestamp is typically from // Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT + // + + # old algorithm iterates through all years. new algorithm does it in + # 10 year blocks + + /* + # old algo for ($a = 1970 ; --$a >= 0;) { $lastd = $d; - if ($leaf = _adodb_is_leap_year($a)) { - $d += $_day_power * 366; - } else - $d += $_day_power * 365; + if ($leaf = _adodb_is_leap_year($a)) $d += $d366; + else $d += $d365; + if ($d >= 0) { $year = $a; break; } } + */ + $lastsecs = 0; + $lastyear = 1970; + foreach($YRS as $year => $secs) { + if ($d >= $secs) { + $a = $lastyear; + break; + } + $lastsecs = $secs; + $lastyear = $year; + } + + $d -= $lastsecs; + if (!isset($a)) $a = $lastyear; + + //echo ' yr=',$a,' ', $d,'.'; + + for (; --$a >= 0;) { + $lastd = $d; + + if ($leaf = _adodb_is_leap_year($a)) $d += $d366; + else $d += $d365; + + if ($d >= 0) { + $year = $a; + break; + } + } + /**/ + $secsInYear = 86400 * ($leaf ? 366 : 365) + $lastd; $d = $lastd; @@ -564,15 +833,12 @@ $hour = floor($d/$_hour_power); } else { - for ($a = 1970 ;; $a++) { $lastd = $d; - if ($leaf = _adodb_is_leap_year($a)) { - $d -= $_day_power * 366; - } else - $d -= $_day_power * 365; - if ($d <= 0) { + if ($leaf = _adodb_is_leap_year($a)) $d -= $d366; + else $d -= $d365; + if ($d < 0) { $year = $a; break; } @@ -583,7 +849,7 @@ for ($a = 1 ; $a <= 12; $a++) { $lastd = $d; $d -= $mtab[$a] * $_day_power; - if ($d <= 0) { + if ($d < 0) { $month = $a; $ndays = $mtab[$a]; break; @@ -635,22 +901,47 @@ return adodb_date($fmt,$d,true); } +// accepts unix timestamp and iso date format in $d +function adodb_date2($fmt, $d=false, $is_gmt=false) +{ + if ($d !== false) { + if (!preg_match( + "|^([0-9]{4})[-/\.]?([0-9]{1,2})[-/\.]?([0-9]{1,2})[ -]?(([0-9]{1,2}):?([0-9]{1,2}):?([0-9\.]{1,4}))?|", + ($d), $rr)) return adodb_date($fmt,false,$is_gmt); + if ($rr[1] <= 100 && $rr[2]<= 1) return adodb_date($fmt,false,$is_gmt); + + // h-m-s-MM-DD-YY + if (!isset($rr[5])) $d = adodb_mktime(0,0,0,$rr[2],$rr[3],$rr[1]); + else $d = @adodb_mktime($rr[5],$rr[6],$rr[7],$rr[2],$rr[3],$rr[1]); + } + + return adodb_date($fmt,$d,$is_gmt); +} + + /** Return formatted date based on timestamp $d */ function adodb_date($fmt,$d=false,$is_gmt=false) { - if ($d === false) return date($fmt); +static $daylight; + + if ($d === false) return ($is_gmt)? @gmdate($fmt): @date($fmt); if (!defined('ADODB_TEST_DATES')) { if ((abs($d) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer - return @date($fmt,$d); + return ($is_gmt)? @gmdate($fmt,$d): @date($fmt,$d); + } } $_day_power = 86400; $arr = _adodb_getdate($d,true,$is_gmt); + + if (!isset($daylight)) $daylight = function_exists('adodb_daylight_sv'); + if ($daylight) adodb_daylight_sv($arr, $is_gmt); + $year = $arr['year']; $month = $arr['mon']; $day = $arr['mday']; @@ -672,22 +963,25 @@ case 'L': $dates .= $arr['leap'] ? '1' : '0'; break; case 'r': // Thu, 21 Dec 2000 16:01:07 +0200 + // 4.3.11 uses '04 Jun 2004' + // 4.3.8 uses ' 4 Jun 2004' $dates .= gmdate('D',$_day_power*(3+adodb_dow($year,$month,$day))).', ' - . ($day<10?' '.$day:$day) . ' '.date('M',mktime(0,0,0,$month,2,1971)).' '.$year.' '; + . ($day<10?'0'.$day:$day) . ' '.date('M',mktime(0,0,0,$month,2,1971)).' '.$year.' '; if ($hour < 10) $dates .= '0'.$hour; else $dates .= $hour; if ($min < 10) $dates .= ':0'.$min; else $dates .= ':'.$min; if ($secs < 10) $dates .= ':0'.$secs; else $dates .= ':'.$secs; - $gmt = adodb_get_gmt_different(); + $gmt = adodb_get_gmt_diff(); $dates .= sprintf(' %s%04d',($gmt<0)?'+':'-',abs($gmt)/36); break; case 'Y': $dates .= $year; break; case 'y': $dates .= substr($year,strlen($year)-2,2); break; // MONTH case 'm': if ($month<10) $dates .= '0'.$month; else $dates .= $month; break; + case 'Q': $dates .= ($month+3)>>2; break; case 'n': $dates .= $month; break; case 'M': $dates .= date('M',mktime(0,0,0,$month,2,1971)); break; case 'F': $dates .= date('F',mktime(0,0,0,$month,2,1971)); break; @@ -702,16 +996,16 @@ case 'S': $d10 = $day % 10; if ($d10 == 1) $dates .= 'st'; - else if ($d10 == 2) $dates .= 'nd'; + else if ($d10 == 2 && $day != 12) $dates .= 'nd'; else if ($d10 == 3) $dates .= 'rd'; else $dates .= 'th'; break; // HOUR case 'Z': - $dates .= ($is_gmt) ? 0 : -adodb_get_gmt_different(); break; + $dates .= ($is_gmt) ? 0 : -adodb_get_gmt_diff(); break; case 'O': - $gmt = ($is_gmt) ? 0 : adodb_get_gmt_different(); + $gmt = ($is_gmt) ? 0 : adodb_get_gmt_diff(); $dates .= sprintf('%s%04d',($gmt<0)?'+':'-',abs($gmt)/36); break; case 'H': @@ -772,41 +1066,67 @@ Returns a timestamp given a GMT/UTC time. Note that $is_dst is not implemented and is ignored. */ -function adodb_gmmktime($hr,$min,$sec,$mon,$day,$year,$is_dst=false) +function adodb_gmmktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false) { return adodb_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,true); } /** Return a timestamp given a local time. Originally by jackbbs. Note that $is_dst is not implemented and is ignored. + + Not a very fast algorithm - O(n) operation. Could be optimized to O(1). */ -function adodb_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst=false,$is_gmt=false) +function adodb_mktime($hr = null, $min = null, $sec = null, $mon = null, $day = null, $year = null,$is_dst=false,$is_gmt=false) { + if( !isset($hr) ) $hr = adodb_date('H'); + if( !isset($min) ) $min = adodb_date('i'); + if( !isset($sec) ) $sec = adodb_date('s'); + if( !isset($mon) ) $mon = adodb_date('m'); + if( !isset($day) ) $day = adodb_date('d'); + if( !isset($year) ) $year = adodb_date('Y'); + if (!defined('ADODB_TEST_DATES')) { + + if ($mon === false || !isset($mon) ) { + return $is_gmt? @gmmktime($hr,$min,$sec): @mktime($hr,$min,$sec); + } + // for windows, we don't check 1970 because with timezone differences, // 1 Jan 1970 could generate negative timestamp, which is illegal - if (!defined('ADODB_NO_NEGATIVE_TS') || ($year >= 1971)) - if (1901 < $year && $year < 2038) - return @mktime($hr,$min,$sec,$mon,$day,$year); + if (1971 < $year && $year < 2038 + || !defined('ADODB_NO_NEGATIVE_TS') && (1901 < $year && $year < 2038) + ) { + return $is_gmt ? + @gmmktime($hr,$min,$sec,$mon,$day,$year): + @mktime($hr,$min,$sec,$mon,$day,$year); + } } - $gmt_different = ($is_gmt) ? 0 : adodb_get_gmt_different(); - + $gmt_different = ($is_gmt) ? 0 : adodb_get_gmt_diff(); + + /* + # disabled because some people place large values in $sec. + # however we need it for $mon because we use an array... $hr = intval($hr); $min = intval($min); $sec = intval($sec); + */ $mon = intval($mon); $day = intval($day); $year = intval($year); $year = adodb_year_digit_check($year); - + if ($mon > 12) { $y = floor($mon / 12); $year += $y; $mon -= $y*12; + } else if ($mon < 1) { + $y = ceil((1-$mon) / 12); + $year -= $y; + $mon += $y*12; } $_day_power = 86400; @@ -867,4 +1187,108 @@ return $ret; } -?> +function adodb_gmstrftime($fmt, $ts=false) +{ + return adodb_strftime($fmt,$ts,true); +} + +// hack - convert to adodb_date +function adodb_strftime($fmt, $ts=false,$is_gmt=false) +{ +global $ADODB_DATE_LOCALE; + + if (!defined('ADODB_TEST_DATES')) { + if ((abs($ts) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range + if (!defined('ADODB_NO_NEGATIVE_TS') || $ts >= 0) // if windows, must be +ve integer + return ($is_gmt)? @gmstrftime($fmt,$ts): @strftime($fmt,$ts); + + } + } + + if (empty($ADODB_DATE_LOCALE)) { + $tstr = strtoupper(gmstrftime('%c',31366800)); // 30 Dec 1970, 1 am + $sep = substr($tstr,2,1); + $hasAM = strrpos($tstr,'M') !== false; + + $ADODB_DATE_LOCALE = array(); + $ADODB_DATE_LOCALE[] = strncmp($tstr,'30',2) == 0 ? 'd'.$sep.'m'.$sep.'y' : 'm'.$sep.'d'.$sep.'y'; + $ADODB_DATE_LOCALE[] = ($hasAM) ? 'h:i:s a' : 'H:i:s'; + + } + $inpct = false; + $fmtdate = ''; + for ($i=0,$max = strlen($fmt); $i < $max; $i++) { + $ch = $fmt[$i]; + if ($ch == '%') { + if ($inpct) { + $fmtdate .= '%'; + $inpct = false; + } else + $inpct = true; + } else if ($inpct) { + + $inpct = false; + switch($ch) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case 'E': + case 'O': + /* ignore format modifiers */ + $inpct = true; + break; + + case 'a': $fmtdate .= 'D'; break; + case 'A': $fmtdate .= 'l'; break; + case 'h': + case 'b': $fmtdate .= 'M'; break; + case 'B': $fmtdate .= 'F'; break; + case 'c': $fmtdate .= $ADODB_DATE_LOCALE[0].$ADODB_DATE_LOCALE[1]; break; + case 'C': $fmtdate .= '\C?'; break; // century + case 'd': $fmtdate .= 'd'; break; + case 'D': $fmtdate .= 'm/d/y'; break; + case 'e': $fmtdate .= 'j'; break; + case 'g': $fmtdate .= '\g?'; break; //? + case 'G': $fmtdate .= '\G?'; break; //? + case 'H': $fmtdate .= 'H'; break; + case 'I': $fmtdate .= 'h'; break; + case 'j': $fmtdate .= '?z'; $parsej = true; break; // wrong as j=1-based, z=0-basd + case 'm': $fmtdate .= 'm'; break; + case 'M': $fmtdate .= 'i'; break; + case 'n': $fmtdate .= "\n"; break; + case 'p': $fmtdate .= 'a'; break; + case 'r': $fmtdate .= 'h:i:s a'; break; + case 'R': $fmtdate .= 'H:i:s'; break; + case 'S': $fmtdate .= 's'; break; + case 't': $fmtdate .= "\t"; break; + case 'T': $fmtdate .= 'H:i:s'; break; + case 'u': $fmtdate .= '?u'; $parseu = true; break; // wrong strftime=1-based, date=0-basde + case 'U': $fmtdate .= '?U'; $parseU = true; break;// wrong strftime=1-based, date=0-based + case 'x': $fmtdate .= $ADODB_DATE_LOCALE[0]; break; + case 'X': $fmtdate .= $ADODB_DATE_LOCALE[1]; break; + case 'w': $fmtdate .= '?w'; $parseu = true; break; // wrong strftime=1-based, date=0-basde + case 'W': $fmtdate .= '?W'; $parseU = true; break;// wrong strftime=1-based, date=0-based + case 'y': $fmtdate .= 'y'; break; + case 'Y': $fmtdate .= 'Y'; break; + case 'Z': $fmtdate .= 'T'; break; + } + } else if (('A' <= ($ch) && ($ch) <= 'Z' ) || ('a' <= ($ch) && ($ch) <= 'z' )) + $fmtdate .= "\\".$ch; + else + $fmtdate .= $ch; + } + //echo "fmt=",$fmtdate,"
"; + if ($ts === false) $ts = time(); + $ret = adodb_date($fmtdate, $ts, $is_gmt); + return $ret; +} + + +?> \ No newline at end of file