From c46aa59dadd6d1417ad9ab8a17af492f5c5895a5 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 21 Sep 2016 01:19:00 +0100 Subject: [PATCH] time: Implement ConvertableTimestamp::convert() * This method is analogous to wfTimestamp(). Optimise for the common idiom of just converting a timestamp without having the caller hold on to any object. * Make wfTimestamp() use this (it could already since it didn't use any MWTimestamp methods). Use via MWTimestamp. While this is the same as direct access, it allows future changes. * Add tests covering this new method. Change-Id: I7f9104f1701d92fe25d72c7943581c64e1d093fa --- includes/GlobalFunctions.php | 10 ++++------ includes/libs/time/ConvertableTimestamp.php | 16 ++++++++++++++++ .../libs/time/ConvertableTimestampTest.php | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index e5f518ffd2..0360d19731 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -2003,13 +2003,11 @@ require_once __DIR__ . '/libs/time/defines.php'; * @return string|bool String / false The same date in the format specified in $outputtype or false */ function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) { - try { - $timestamp = new MWTimestamp( $ts ); - return $timestamp->getTimestamp( $outputtype ); - } catch ( TimestampException $e ) { + $ret = MWTimestamp::convert( $outputtype, $ts ); + if ( $ret === false ) { wfDebug( "wfTimestamp() fed bogus time value: TYPE=$outputtype; VALUE=$ts\n" ); - return false; } + return $ret; } /** @@ -2035,7 +2033,7 @@ function wfTimestampOrNull( $outputtype = TS_UNIX, $ts = null ) { */ function wfTimestampNow() { # return NOW - return wfTimestamp( TS_MW, time() ); + return MWTimestamp::convert( TS_MW, time() ); } /** diff --git a/includes/libs/time/ConvertableTimestamp.php b/includes/libs/time/ConvertableTimestamp.php index af7eca6da8..a4c79babb5 100644 --- a/includes/libs/time/ConvertableTimestamp.php +++ b/includes/libs/time/ConvertableTimestamp.php @@ -161,6 +161,22 @@ class ConvertableTimestamp { $this->timestamp = $final; } + /** + * Convert a timestamp string to a given format. + * + * @param int $style Constant Output format for timestamp + * @param string $ts Timestamp + * @return string|bool Formatted timestamp or false on failure + */ + public static function convert( $style = TS_UNIX, $ts ) { + try { + $ct = new static( $ts ); + return $ct->getTimestamp( $style ); + } catch ( TimestampException $e ) { + return false; + } + } + /** * Get the timestamp represented by this object in a certain form. * diff --git a/tests/phpunit/includes/libs/time/ConvertableTimestampTest.php b/tests/phpunit/includes/libs/time/ConvertableTimestampTest.php index 88c29898cb..986dda422f 100644 --- a/tests/phpunit/includes/libs/time/ConvertableTimestampTest.php +++ b/tests/phpunit/includes/libs/time/ConvertableTimestampTest.php @@ -71,6 +71,22 @@ class ConvertableTimestampTest extends PHPUnit_Framework_TestCase { new ConvertableTimestamp( "This is not a timestamp." ); } + /** + * @dataProvider provideValidTimestamps + * @covers ConvertableTimestamp::convert + */ + public function testConvert( $format, $expected, $original ) { + $this->assertSame( $expected, ConvertableTimestamp::convert( $format, $original ) ); + } + + /** + * Format an invalid timestamp. + * @covers ConvertableTimestamp::convert + */ + public function testConvertInvalid() { + $this->assertSame( false, ConvertableTimestamp::convert( 'Not a timestamp', 0 ) ); + } + /** * Test an out of range timestamp * @dataProvider provideOutOfRangeTimestamps -- 2.20.1