Krinkle has uploaded a new change for review. https://gerrit.wikimedia.org/r/311881
Change subject: time: Implement ConvertableTimestamp::convert() ...................................................................... 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 --- M includes/GlobalFunctions.php M includes/libs/time/ConvertableTimestamp.php M tests/phpunit/includes/libs/time/ConvertableTimestampTest.php 3 files changed, 35 insertions(+), 5 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/81/311881/1 diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index e5f518f..7c86db2 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -2003,13 +2003,11 @@ * @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; } /** diff --git a/includes/libs/time/ConvertableTimestamp.php b/includes/libs/time/ConvertableTimestamp.php index af7eca6..deb1225 100644 --- a/includes/libs/time/ConvertableTimestamp.php +++ b/includes/libs/time/ConvertableTimestamp.php @@ -162,6 +162,22 @@ } /** + * Convert a timestamp string to a given format. + * + * @param int $style Constant Output format for timestamp + * @param string|bool $ts Timestamp, or false for now + * @return string|bool Formatted timestamp or false on failure + */ + public static function convert( $style = TS_UNIX, $ts = false ) { + 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. * * Convert the internal timestamp to the specified format and then diff --git a/tests/phpunit/includes/libs/time/ConvertableTimestampTest.php b/tests/phpunit/includes/libs/time/ConvertableTimestampTest.php index 88c2989..26ed708 100644 --- a/tests/phpunit/includes/libs/time/ConvertableTimestampTest.php +++ b/tests/phpunit/includes/libs/time/ConvertableTimestampTest.php @@ -72,6 +72,22 @@ } /** + * @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' ) ); + } + + /** * Test an out of range timestamp * @dataProvider provideOutOfRangeTimestamps * @expectedException TimestampException -- To view, visit https://gerrit.wikimedia.org/r/311881 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I7f9104f1701d92fe25d72c7943581c64e1d093fa Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Krinkle <krinklem...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits