r75761 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75760‎ | r75761 | r75762 >
Date:23:30, 31 October 2010
Author:platonides
Status:ok (Comments)
Tags:
Comment:
Change wfTimestamp() to an array() and add a bunch of timestamp tests hard for 32 bit php.
Intended to ease transition to DateTime. See r74778 and Bug 25451
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/maintenance/tests/phpunit/includes/GlobalTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tests/phpunit/includes/GlobalTest.php
@@ -146,6 +146,10 @@
147147 wfTimestamp( TS_MW, $t ),
148148 'TS_UNIX to TS_MW' );
149149 $this->assertEquals(
 150+ '19690115123456',
 151+ wfTimestamp( TS_MW, -30281104 ),
 152+ 'Negative TS_UNIX to TS_MW' );
 153+ $this->assertEquals(
150154 979562096,
151155 wfTimestamp( TS_UNIX, $t ),
152156 'TS_UNIX to TS_UNIX' );
@@ -193,6 +197,83 @@
194198 'TS_DB to TS_ISO_8601_BASIC' );
195199 }
196200
 201+ /**
 202+ * This test checks wfTimestamp() with values outside.
 203+ * It needs PHP 64 bits or PHP > 5.1.
 204+ * See r74778 and bug 25451
 205+ */
 206+ function testOldTimestamps() {
 207+ $this->assertEquals( 'Fri, 13 Dec 1901 20:45:54 GMT',
 208+ wfTimestamp( TS_RFC2822, '19011213204554' ),
 209+ 'Earliest time according to php documentation' );
 210+
 211+ $this->assertEquals( 'Tue, 19 Jan 2038 03:14:07 GMT',
 212+ wfTimestamp( TS_RFC2822, '20380119031407' ),
 213+ 'Latest 32 bit time' );
 214+
 215+ $this->assertEquals( '-2147483648',
 216+ wfTimestamp( TS_UNIX, '19011213204552' ),
 217+ 'Earliest 32 bit unix time' );
 218+
 219+ $this->assertEquals( '2147483647',
 220+ wfTimestamp( TS_UNIX, '20380119031407' ),
 221+ 'Latest 32 bit unix time' );
 222+
 223+ $this->assertEquals( 'Fri, 13 Dec 1901 20:45:52 GMT',
 224+ wfTimestamp( TS_RFC2822, '19011213204552' ),
 225+ 'Earliest 32 bit time' );
 226+
 227+ $this->assertEquals( 'Fri, 13 Dec 1901 20:45:51 GMT',
 228+ wfTimestamp( TS_RFC2822, '19011213204551' ),
 229+ 'Earliest 32 bit time - 1' );
 230+
 231+ $this->assertEquals( 'Tue, 19 Jan 2038 03:14:08 GMT',
 232+ wfTimestamp( TS_RFC2822, '20380119031408' ),
 233+ 'Latest 32 bit time + 1' );
 234+
 235+ $this->assertEquals( '19011212000000',
 236+ wfTimestamp(TS_MW, '19011212000000'),
 237+ 'Convert to itself r74778#c10645' );
 238+
 239+ $this->assertEquals( '-2147483649',
 240+ wfTimestamp( TS_UNIX, '19011213204551' ),
 241+ 'Earliest 32 bit unix time - 1' );
 242+
 243+ $this->assertEquals( '2147483648',
 244+ wfTimestamp( TS_UNIX, '20380119031408' ),
 245+ 'Latest 32 bit unix time + 1' );
 246+
 247+ $this->assertEquals( '19011213204551',
 248+ wfTimestamp( TS_MW, '-2147483649' ),
 249+ '1901 negative unix time to MediaWiki' );
 250+
 251+ $this->assertEquals( '18010115123456',
 252+ wfTimestamp( TS_MW, '-5331871504' ),
 253+ '1801 negative unix time to MediaWiki' );
 254+
 255+ $this->assertEquals( 'Tue, 09 Aug 0117 12:34:56 GMT',
 256+ wfTimestamp( TS_RFC2822, '0117-08-09 12:34:56'),
 257+ 'Death of Roman Emperor [[Trajan]]');
 258+
 259+ /* FIXME: 00 to 101 years are taken as being in [1970-2069] */
 260+
 261+ $this->assertEquals( 'Sun, 01 Jan 0101 00:00:00 GMT',
 262+ wfTimestamp( TS_RFC2822, '-58979923200'),
 263+ '1/1/101');
 264+
 265+ $this->assertEquals( 'Mon, 01 Jan 0001 00:00:00 GMT',
 266+ wfTimestamp( TS_RFC2822, '-62135596800'),
 267+ 'Year 1');
 268+
 269+ /* It is not clear if we should generate a year 0 or not
 270+ * We are completely off RFC2822 requirement of year being
 271+ * 1900 or later.
 272+ */
 273+ $this->assertEquals( 'Wed, 18 Oct 0000 00:00:00 GMT',
 274+ wfTimestamp( TS_RFC2822, '-62142076800'),
 275+ 'ISO 8601:2004 [[year 0]], also called [[1 BC]]');
 276+ }
 277+
197278 function testBasename() {
198279 $sets = array(
199280 '' => '',
Index: trunk/phase3/includes/GlobalFunctions.php
@@ -2017,32 +2017,33 @@
20182018 (int)$da[2], (int)$da[3], (int)$da[1] );
20192019 }
20202020
2021 - switch( $outputtype ) {
2022 - case TS_UNIX:
 2021+ static $formats = array (
 2022+ TS_UNIX => 'U',
 2023+ TS_MW => 'YmdHis',
 2024+ TS_DB => 'Y-m-d H:i:s',
 2025+ TS_ISO_8601 => 'Y-m-d\TH:i:s\Z',
 2026+ TS_ISO_8601_BASIC => 'Ymd\THis\Z',
 2027+ TS_EXIF => 'Y:m:d H:i:s', // This shouldn't ever be used, but is included for completeness
 2028+ TS_RFC2822 => 'D, d M Y H:i:s',
 2029+ TS_ORACLE => 'd-m-Y H:i:s.000000', //Was 'd-M-y h.i.s A' . ' +00:00' before r51500
 2030+ TS_POSTGRES => 'Y-m-d H:i:s',
 2031+ TS_DB2 => 'Y-m-d H:i:s',
 2032+ );
 2033+
 2034+ if ( !isset( $formats[$outputtype] ) ) {
 2035+ throw new MWException( 'wfTimestamp() called with illegal output type.' );
 2036+ }
 2037+
 2038+ if ( TS_UNIX == $outputtype )
20232039 return $uts;
2024 - case TS_MW:
2025 - return gmdate( 'YmdHis', $uts );
2026 - case TS_DB:
2027 - return gmdate( 'Y-m-d H:i:s', $uts );
2028 - case TS_ISO_8601:
2029 - return gmdate( 'Y-m-d\TH:i:s\Z', $uts );
2030 - case TS_ISO_8601_BASIC:
2031 - return gmdate( 'Ymd\THis\Z', $uts );
2032 - // This shouldn't ever be used, but is included for completeness
2033 - case TS_EXIF:
2034 - return gmdate( 'Y:m:d H:i:s', $uts );
2035 - case TS_RFC2822:
2036 - return gmdate( 'D, d M Y H:i:s', $uts ) . ' GMT';
2037 - case TS_ORACLE:
2038 - return gmdate( 'd-m-Y H:i:s.000000', $uts );
2039 - //return gmdate( 'd-M-y h.i.s A', $uts ) . ' +00:00';
2040 - case TS_POSTGRES:
2041 - return gmdate( 'Y-m-d H:i:s', $uts ) . ' GMT';
2042 - case TS_DB2:
2043 - return gmdate( 'Y-m-d H:i:s', $uts );
2044 - default:
2045 - throw new MWException( 'wfTimestamp() called with illegal output type.' );
 2040+
 2041+ $output = gmdate( $formats[$outputtype], $uts );
 2042+
 2043+ if ( ( $outputtype == TS_RFC2822 ) || ( $outputtype == TS_POSTGRES ) ) {
 2044+ $output .= ' GMT';
20462045 }
 2046+
 2047+ return $output;
20472048 }
20482049
20492050 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r75764follow-up to r75761: coding style tweaksashley23:48, 31 October 2010
r77171Bug 25451: time and date calculation in 32-bit ...platonides18:25, 23 November 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r74778* (Bug 25451) Language::time() and ::date() misfunction when given timestamps...nikerabbit15:59, 14 October 2010

Comments

#Comment by Hashar (talk | contribs)   12:18, 21 November 2010

fixme, there is one assertion failing (Mac OS x 10.5, aaron has the same issue under Vista)

1) GlobalTest::testOldTimestamps Earliest 32 bit time - 1 Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -Fri, 13 Dec 1901 20:45:51 GMT +Thu, 01 Jan 1970 00:00:00 GMT

#Comment by Hashar (talk | contribs)   12:18, 21 November 2010

fixme, there is one assertion failing (Mac OS x 10.5, aaron has the same issue under Vista)

1) GlobalTest::testOldTimestamps Earliest 32 bit time - 1 Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -Fri, 13 Dec 1901 20:45:51 GMT +Thu, 01 Jan 1970 00:00:00 GMT

#Comment by Hashar (talk | contribs)   12:22, 21 November 2010

fixme, there is one assertion failing (Mac OS x 10.5, aaron has the same issue under Vista)

1) GlobalTest::testOldTimestamps
Earliest 32 bit time - 1
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-Fri, 13 Dec 1901 20:45:51 GMT
+Thu, 01 Jan 1970 00:00:00 GMT
#Comment by Hashar (talk | contribs)   21:42, 24 November 2010

marking ok since r77171 solved the issues

Status & tagging log