jenkins-bot has submitted this change and it was merged. 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, 36 insertions(+), 6 deletions(-) Approvals: Aaron Schulz: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index e5f518f..0360d19 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; } /** @@ -2035,7 +2033,7 @@ */ 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 af7eca6..a4c79ba 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 $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. * * 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..986dda4 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', 0 ) ); + } + + /** * 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: merged Gerrit-Change-Id: I7f9104f1701d92fe25d72c7943581c64e1d093fa Gerrit-PatchSet: 9 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Aaron Schulz <asch...@wikimedia.org> Gerrit-Reviewer: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits