Index: trunk/phase3/maintenance/tests/phpunit/includes/GlobalTest.php |
— | — | @@ -146,6 +146,10 @@ |
147 | 147 | wfTimestamp( TS_MW, $t ), |
148 | 148 | 'TS_UNIX to TS_MW' ); |
149 | 149 | $this->assertEquals( |
| 150 | + '19690115123456', |
| 151 | + wfTimestamp( TS_MW, -30281104 ), |
| 152 | + 'Negative TS_UNIX to TS_MW' ); |
| 153 | + $this->assertEquals( |
150 | 154 | 979562096, |
151 | 155 | wfTimestamp( TS_UNIX, $t ), |
152 | 156 | 'TS_UNIX to TS_UNIX' ); |
— | — | @@ -193,6 +197,83 @@ |
194 | 198 | 'TS_DB to TS_ISO_8601_BASIC' ); |
195 | 199 | } |
196 | 200 | |
| 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 | + |
197 | 278 | function testBasename() { |
198 | 279 | $sets = array( |
199 | 280 | '' => '', |
Index: trunk/phase3/includes/GlobalFunctions.php |
— | — | @@ -2017,32 +2017,33 @@ |
2018 | 2018 | (int)$da[2], (int)$da[3], (int)$da[1] ); |
2019 | 2019 | } |
2020 | 2020 | |
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 ) |
2023 | 2039 | 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'; |
2046 | 2045 | } |
| 2046 | + |
| 2047 | + return $output; |
2047 | 2048 | } |
2048 | 2049 | |
2049 | 2050 | /** |