time: Implement ConvertableTimestamp::convert()
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 21 Sep 2016 00:19:00 +0000 (01:19 +0100)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 22 Sep 2016 03:38:07 +0000 (03:38 +0000)
* 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
includes/libs/time/ConvertableTimestamp.php
tests/phpunit/includes/libs/time/ConvertableTimestampTest.php

index e5f518f..0360d19 100644 (file)
@@ -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() );
 }
 
 /**
index af7eca6..a4c79ba 100644 (file)
@@ -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.
         *
index 88c2989..986dda4 100644 (file)
@@ -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