2012-01-09 16:30:32 +00:00
|
|
|
<?php
|
2014-07-18 19:35:43 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group GlobalFunctions
|
2013-10-24 09:53:24 +00:00
|
|
|
* @covers ::wfTimestamp
|
2012-01-09 16:30:32 +00:00
|
|
|
*/
|
2013-02-21 23:58:19 +00:00
|
|
|
class WfTimestampTest extends MediaWikiTestCase {
|
2012-01-09 16:30:32 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideNormalTimestamps
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testNormalTimestamps( $input, $format, $output, $desc ) {
|
2012-01-09 16:30:32 +00:00
|
|
|
$this->assertEquals( $output, wfTimestamp( $format, $input ), $desc );
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-22 02:12:37 +00:00
|
|
|
public static function provideNormalTimestamps() {
|
2012-01-09 16:30:32 +00:00
|
|
|
$t = gmmktime( 12, 34, 56, 1, 15, 2001 );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2012-01-09 16:30:32 +00:00
|
|
|
// TS_UNIX
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $t, TS_MW, '20010115123456', 'TS_UNIX to TS_MW' ],
|
|
|
|
|
[ -30281104, TS_MW, '19690115123456', 'Negative TS_UNIX to TS_MW' ],
|
|
|
|
|
[ $t, TS_UNIX, 979562096, 'TS_UNIX to TS_UNIX' ],
|
|
|
|
|
[ $t, TS_DB, '2001-01-15 12:34:56', 'TS_UNIX to TS_DB' ],
|
2017-09-10 19:11:37 +00:00
|
|
|
[ $t + 0.01, TS_MW, '20010115123456', 'TS_UNIX float to TS_MW' ],
|
2012-01-09 16:30:32 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $t, TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_ISO_8601_BASIC to TS_DB' ],
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2012-01-09 16:30:32 +00:00
|
|
|
// TS_MW
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '20010115123456', TS_MW, '20010115123456', 'TS_MW to TS_MW' ],
|
|
|
|
|
[ '20010115123456', TS_UNIX, 979562096, 'TS_MW to TS_UNIX' ],
|
|
|
|
|
[ '20010115123456', TS_DB, '2001-01-15 12:34:56', 'TS_MW to TS_DB' ],
|
|
|
|
|
[ '20010115123456', TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_MW to TS_ISO_8601_BASIC' ],
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2012-01-09 16:30:32 +00:00
|
|
|
// TS_DB
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '2001-01-15 12:34:56', TS_MW, '20010115123456', 'TS_DB to TS_MW' ],
|
|
|
|
|
[ '2001-01-15 12:34:56', TS_UNIX, 979562096, 'TS_DB to TS_UNIX' ],
|
|
|
|
|
[ '2001-01-15 12:34:56', TS_DB, '2001-01-15 12:34:56', 'TS_DB to TS_DB' ],
|
|
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
'2001-01-15 12:34:56',
|
|
|
|
|
TS_ISO_8601_BASIC,
|
|
|
|
|
'20010115T123456Z',
|
|
|
|
|
'TS_DB to TS_ISO_8601_BASIC'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-01-09 16:30:32 +00:00
|
|
|
|
|
|
|
|
# rfc2822 section 3.3
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '20010115123456', TS_RFC2822, 'Mon, 15 Jan 2001 12:34:56 GMT', 'TS_MW to TS_RFC2822' ],
|
|
|
|
|
[ 'Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ],
|
|
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
' Mon, 15 Jan 2001 12:34:56 GMT',
|
|
|
|
|
TS_MW,
|
|
|
|
|
'20010115123456',
|
|
|
|
|
'TS_RFC2822 with leading space to TS_MW'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
'15 Jan 2001 12:34:56 GMT',
|
|
|
|
|
TS_MW,
|
|
|
|
|
'20010115123456',
|
|
|
|
|
'TS_RFC2822 without optional day-of-week to TS_MW'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-01-09 16:30:32 +00:00
|
|
|
|
|
|
|
|
# FWS = ([*WSP CRLF] 1*WSP) / obs-FWS ; Folding white space
|
|
|
|
|
# obs-FWS = 1*WSP *(CRLF 1*WSP) ; Section 4.2
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ],
|
2012-01-09 16:30:32 +00:00
|
|
|
|
|
|
|
|
# WSP = SP / HTAB ; rfc2234
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
"Mon, 15 Jan\x092001 12:34:56 GMT",
|
|
|
|
|
TS_MW,
|
|
|
|
|
'20010115123456',
|
|
|
|
|
'TS_RFC2822 with HTAB to TS_MW'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
"Mon, 15 Jan\x09 \x09 2001 12:34:56 GMT",
|
|
|
|
|
TS_MW,
|
|
|
|
|
'20010115123456',
|
|
|
|
|
'TS_RFC2822 with HTAB and SP to TS_MW'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
'Sun, 6 Nov 94 08:49:37 GMT',
|
|
|
|
|
TS_MW,
|
|
|
|
|
'19941106084937',
|
|
|
|
|
'TS_RFC2822 with obsolete year to TS_MW'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2012-01-09 16:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This test checks wfTimestamp() with values outside.
|
|
|
|
|
* It needs PHP 64 bits or PHP > 5.1.
|
2017-02-20 23:45:58 +00:00
|
|
|
* See r74778 and T27451
|
2012-01-09 16:30:32 +00:00
|
|
|
* @dataProvider provideOldTimestamps
|
|
|
|
|
*/
|
2014-05-06 09:35:18 +00:00
|
|
|
public function testOldTimestamps( $input, $outputType, $output, $message ) {
|
|
|
|
|
$timestamp = wfTimestamp( $outputType, $input );
|
|
|
|
|
if ( substr( $output, 0, 1 ) === '/' ) {
|
2017-02-20 23:45:58 +00:00
|
|
|
// T66946: Day of the week calculations for very old
|
2014-05-06 09:35:18 +00:00
|
|
|
// timestamps varies from system to system.
|
2014-07-19 21:12:10 +00:00
|
|
|
$this->assertRegExp( $output, $timestamp, $message );
|
2014-05-06 09:35:18 +00:00
|
|
|
} else {
|
|
|
|
|
$this->assertEquals( $output, $timestamp, $message );
|
|
|
|
|
}
|
2012-01-09 16:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-22 02:12:37 +00:00
|
|
|
public static function provideOldTimestamps() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
'19011213204554',
|
|
|
|
|
TS_RFC2822,
|
|
|
|
|
'Fri, 13 Dec 1901 20:45:54 GMT',
|
|
|
|
|
'Earliest time according to PHP documentation'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ '20380119031407', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:07 GMT', 'Latest 32 bit time' ],
|
|
|
|
|
[ '19011213204552', TS_UNIX, '-2147483648', 'Earliest 32 bit unix time' ],
|
|
|
|
|
[ '20380119031407', TS_UNIX, '2147483647', 'Latest 32 bit unix time' ],
|
|
|
|
|
[ '19011213204552', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:52 GMT', 'Earliest 32 bit time' ],
|
|
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
'19011213204551',
|
|
|
|
|
TS_RFC2822,
|
|
|
|
|
'Fri, 13 Dec 1901 20:45:51 GMT', 'Earliest 32 bit time - 1'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ '20380119031408', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:08 GMT', 'Latest 32 bit time + 1' ],
|
|
|
|
|
[ '19011212000000', TS_MW, '19011212000000', 'Convert to itself r74778#c10645' ],
|
|
|
|
|
[ '19011213204551', TS_UNIX, '-2147483649', 'Earliest 32 bit unix time - 1' ],
|
|
|
|
|
[ '20380119031408', TS_UNIX, '2147483648', 'Latest 32 bit unix time + 1' ],
|
|
|
|
|
[ '-2147483649', TS_MW, '19011213204551', '1901 negative unix time to MediaWiki' ],
|
|
|
|
|
[ '-5331871504', TS_MW, '18010115123456', '1801 negative unix time to MediaWiki' ],
|
|
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
'0117-08-09 12:34:56',
|
|
|
|
|
TS_RFC2822,
|
2014-05-06 09:35:18 +00:00
|
|
|
'/, 09 Aug 0117 12:34:56 GMT$/',
|
2014-04-24 09:47:06 +00:00
|
|
|
'Death of Roman Emperor [[Trajan]]'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-01-09 16:30:32 +00:00
|
|
|
|
|
|
|
|
/* @todo FIXME: 00 to 101 years are taken as being in [1970-2069] */
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '-58979923200', TS_RFC2822, '/, 01 Jan 0101 00:00:00 GMT$/', '1/1/101' ],
|
|
|
|
|
[ '-62135596800', TS_RFC2822, 'Mon, 01 Jan 0001 00:00:00 GMT', 'Year 1' ],
|
2012-01-09 16:30:32 +00:00
|
|
|
|
|
|
|
|
/* It is not clear if we should generate a year 0 or not
|
|
|
|
|
* We are completely off RFC2822 requirement of year being
|
|
|
|
|
* 1900 or later.
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
'-62142076800',
|
|
|
|
|
TS_RFC2822,
|
|
|
|
|
'Wed, 18 Oct 0000 00:00:00 GMT',
|
|
|
|
|
'ISO 8601:2004 [[year 0]], also called [[1 BC]]'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2012-01-09 16:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
|
|
|
|
|
* @dataProvider provideHttpDates
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testHttpDate( $input, $output, $desc ) {
|
2012-01-09 16:30:32 +00:00
|
|
|
$this->assertEquals( $output, wfTimestamp( TS_MW, $input ), $desc );
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-22 02:12:37 +00:00
|
|
|
public static function provideHttpDates() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'Sun, 06 Nov 1994 08:49:37 GMT', '19941106084937', 'RFC 822 date' ],
|
|
|
|
|
[ 'Sunday, 06-Nov-94 08:49:37 GMT', '19941106084937', 'RFC 850 date' ],
|
|
|
|
|
[ 'Sun Nov 6 08:49:37 1994', '19941106084937', "ANSI C's asctime() format" ],
|
2012-01-09 16:30:32 +00:00
|
|
|
// See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html and r77171
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 09:47:06 +00:00
|
|
|
'Mon, 22 Nov 2010 14:12:42 GMT; length=52626',
|
|
|
|
|
'20101122141242',
|
|
|
|
|
'Netscape extension to HTTP/1.0'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2012-01-09 16:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* There are a number of assumptions in our codebase where wfTimestamp()
|
|
|
|
|
* should give the current date but it is not given a 0 there. See r71751 CR
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTimestampParameter() {
|
2012-01-09 16:30:32 +00:00
|
|
|
$now = wfTimestamp( TS_UNIX );
|
2013-08-24 15:06:25 +00:00
|
|
|
// We check that wfTimestamp doesn't return false (error) and use a LessThan assert
|
2012-01-09 16:30:32 +00:00
|
|
|
// for the cases where the test is run in a second boundary.
|
|
|
|
|
|
|
|
|
|
$zero = wfTimestamp( TS_UNIX, 0 );
|
|
|
|
|
$this->assertNotEquals( false, $zero );
|
|
|
|
|
$this->assertLessThan( 5, $zero - $now );
|
|
|
|
|
|
|
|
|
|
$empty = wfTimestamp( TS_UNIX, '' );
|
|
|
|
|
$this->assertNotEquals( false, $empty );
|
|
|
|
|
$this->assertLessThan( 5, $empty - $now );
|
|
|
|
|
|
|
|
|
|
$null = wfTimestamp( TS_UNIX, null );
|
|
|
|
|
$this->assertNotEquals( false, $null );
|
|
|
|
|
$this->assertLessThan( 5, $null - $now );
|
|
|
|
|
}
|
|
|
|
|
}
|