[MediaWiki-commits] [Gerrit] mediawiki/core[master]: time: Implement ConvertableTimestamp::convert()

2016-09-21 Thread jenkins-bot (Code Review)
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 
Gerrit-Reviewer: Aaron Schulz 
Gerrit-Reviewer: Krinkle 
Gerrit-Reviewer: Legoktm 
Gerrit-Reviewer: jenkins-bot <>

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits


[MediaWiki-commits] [Gerrit] mediawiki/core[master]: time: Implement ConvertableTimestamp::convert()

2016-09-20 Thread Krinkle (Code Review)
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 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits