r77171 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r77170‎ | r77171 | r77172 >
Date:18:25, 23 November 2010
Author:platonides
Status:resolved (Comments)
Tags:
Comment:
Bug 25451: time and date calculation in 32-bit
Follow up r75761. Now calculation is right (passes all tests)
on php 32 bits when using PHP >= 5.2
without prejudice to PHP 32/64 bits < 5.2 on the 32 bit range
(regardless of architecture)
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -1986,6 +1986,8 @@
19871987 function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
19881988 $uts = 0;
19891989 $da = array();
 1990+ $strtime = '';
 1991+
19901992 if ( $ts === 0 ) {
19911993 $uts = time();
19921994 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
@@ -1997,10 +1999,11 @@
19982000 } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) {
19992001 # TS_UNIX
20002002 $uts = $ts;
 2003+ $strtime = "@$ts"; // Undocumented?
20012004 } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) {
20022005 # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
2003 - $uts = strtotime( preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
2004 - str_replace( '+00:00', 'UTC', $ts ) ) );
 2006+ $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
 2007+ str_replace( '+00:00', 'UTC', $ts ) );
20052008 } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
20062009 # TS_ISO_8601
20072010 } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
@@ -2013,19 +2016,14 @@
20142017 # TS_DB2
20152018 } elseif ( preg_match( '/^[A-Z][a-z]{2}, \d\d [A-Z][a-z]{2} \d{4} \d\d:\d\d:\d\d/', $ts ) ) {
20162019 # TS_RFC2822
2017 - $uts = strtotime( $ts );
 2020+ $strtime = $ts;
20182021 } else {
20192022 # Bogus value; fall back to the epoch...
20202023 wfDebug("wfTimestamp() fed bogus time value: $outputtype; $ts\n");
20212024 $uts = 0;
20222025 }
20232026
2024 - if (count( $da ) ) {
2025 - // Warning! gmmktime() acts oddly if the month or day is set to 0
2026 - // We may want to handle that explicitly at some point
2027 - $uts = gmmktime( (int)$da[4], (int)$da[5], (int)$da[6],
2028 - (int)$da[2], (int)$da[3], (int)$da[1] );
2029 - }
 2027+
20302028
20312029 static $formats = array(
20322030 TS_UNIX => 'U',
@@ -2044,12 +2042,37 @@
20452043 throw new MWException( 'wfTimestamp() called with illegal output type.' );
20462044 }
20472045
2048 - if ( TS_UNIX == $outputtype ) {
2049 - return $uts;
 2046+ if ( function_exists( "date_create" ) ) {
 2047+ if ( count( $da ) ) {
 2048+ $ds = sprintf("%04d-%02d-%02dT%02d:%02d:%02d.00+00:00",
 2049+ (int)$da[1], (int)$da[2], (int)$da[3],
 2050+ (int)$da[4], (int)$da[5], (int)$da[6]);
 2051+
 2052+ $d = date_create( $ds, new DateTimeZone( 'GMT' ) );
 2053+ } else {
 2054+ $d = date_create( $strtime, new DateTimeZone( 'GMT' ) );
 2055+ }
 2056+ $output = $d->format( $formats[$outputtype] );
 2057+ } else {
 2058+ if ( count( $da ) ) {
 2059+ // Warning! gmmktime() acts oddly if the month or day is set to 0
 2060+ // We may want to handle that explicitly at some point
 2061+ $uts = gmmktime( (int)$da[4], (int)$da[5], (int)$da[6],
 2062+ (int)$da[2], (int)$da[3], (int)$da[1] );
 2063+ } elseif ( $strtime ) {
 2064+ $uts = strtotime( $strtime );
 2065+ }
 2066+
 2067+ if ( $uts === false ) {
 2068+ return "Can't handle date";
 2069+ }
 2070+
 2071+ if ( TS_UNIX == $outputtype ) {
 2072+ return $uts;
 2073+ }
 2074+ $output = gmdate( $formats[$outputtype], $uts );
20502075 }
20512076
2052 - $output = gmdate( $formats[$outputtype], $uts );
2053 -
20542077 if ( ( $outputtype == TS_RFC2822 ) || ( $outputtype == TS_POSTGRES ) ) {
20552078 $output .= ' GMT';
20562079 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r77204Release notes for r77171, which was slightly based on t.glaser ...platonides23:30, 23 November 2010
r77332Follow up r77171platonides15:47, 26 November 2010
r77401Fix bug discovered in r77171 from the user data in If-Modified-Since passed b...platonides21:59, 28 November 2010
r77407Fix bug introduced in r77171platonides22:40, 28 November 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r75761Change wfTimestamp() to an array() and add a bunch of timestamp tests hard fo...platonides23:30, 31 October 2010

Comments

#Comment by Hashar (talk | contribs)   07:30, 24 November 2010

Good job!

#Comment by Platonides (talk | contribs)   17:57, 24 November 2010

Thanks. Now you can unfixme r75761 ;)

#Comment by Hashar (talk | contribs)   21:43, 24 November 2010

r75761 is now ok. I will let someone else double check this revision (77171) just to be sure :b

#Comment by Nikerabbit (talk | contribs)   15:38, 26 November 2010

PHP Fatal error: Call to a member function format() on a non-object in /www/w/includes/GlobalFunctions.php on line 2055

#Comment by Platonides (talk | contribs)   15:42, 26 November 2010

With which input?

#Comment by Nikerabbit (talk | contribs)   15:44, 26 November 2010

I have no idea.

#Comment by Nikerabbit (talk | contribs)   11:48, 27 November 2010

More info:

[27-Nov-2010 08:46:41]  Mon, 22 Nov 2010 14:12:42 GMT; length=52626

This is the $strtotime path

#Comment by Nikerabbit (talk | contribs)   17:40, 27 November 2010

And callers are always the same:

ResourceLoader::respond/wfTimestamp
#Comment by Catrope (talk | contribs)   18:21, 27 November 2010
+			return "Can't handle date";

Return something sane, like false or null, or throw an exception, but don't return an English string.

Status & tagging log