* @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;
}
/**
*/
function wfTimestampNow() {
# return NOW
- return wfTimestamp( TS_MW, time() );
+ return MWTimestamp::convert( TS_MW, time() );
}
/**
$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.
*
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