* The actual timestamp being wrapped. Either a DateTime
* object or a string with a Unix timestamp depending on
* PHP.
+ * @var string|DateTime
*/
private $timestamp;
* Make a new timestamp and set it to the specified time,
* or the current time if unspecified.
*
- * @param $timestamp Timestamp to set, or false for current time
+ * @since 1.20
+ *
+ * @param $timestamp bool|string Timestamp to set, or false for current time
*/
public function __construct( $timestamp = false ) {
$this->setTimestamp( $timestamp );
/**
* Set the timestamp to the specified time, or the current time if unspecified.
*
- * Parse the given timestamp into either a DateTime object or a Unix teimstamp,
+ * Parse the given timestamp into either a DateTime object or a Unix timestamp,
* and then store it.
*
- * @param $ts Timestamp to store, or false for now
+ * @since 1.20
+ *
+ * @param $ts string|bool Timestamp to store, or false for now
+ * @throws TimestampException
*/
public function setTimestamp( $ts = false ) {
- $uts = 0;
$da = array();
$strtime = '';
- if ( !$ts ) { // We want to catch 0, '', null... but not date strings starting with a letter.
+ if ( !$ts || $ts === "\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ) { // We want to catch 0, '', null... but not date strings starting with a letter.
$uts = time();
$strtime = "@$uts";
} elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
# TS_MW
} elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) {
# TS_UNIX
- $uts = $ts;
$strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php
} elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) {
# TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
$strtime = call_user_func_array( "sprintf", $da );
}
- if( function_exists( "date_create" ) ) {
- try {
- $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) );
- } catch(Exception $e) {
- throw new TimestampException( __METHOD__ . 'Invalid timestamp format.' );
- }
- } else {
- $final = strtotime( $strtime );
+ try {
+ $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) );
+ } catch(Exception $e) {
+ throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' );
}
if( $final === false ) {
- throw new TimestampException( __METHOD__ . 'Invalid timestamp format.' );
+ throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' );
}
$this->timestamp = $final;
}
* Convert the internal timestamp to the specified format and then
* return it.
*
- * @param $style Output format for timestamp
+ * @since 1.20
+ *
+ * @param $style int Constant Output format for timestamp
+ * @throws TimestampException
* @return string The formatted timestamp
*/
public function getTimestamp( $style = TS_UNIX ) {
throw new TimestampException( __METHOD__ . ' : Illegal timestamp output type.' );
}
- if( is_object( $this->timestamp ) ) {
+ if( is_object( $this->timestamp ) ) {
// DateTime object was used, call DateTime::format.
$output = $this->timestamp->format( self::$formats[$style] );
} elseif( TS_UNIX == $style ) {
* generate a readable timestamp by returning "<N> <units> ago", where the
* largest possible unit is used.
*
- * @return string Formatted timestamp
+ * @since 1.20
+ *
+ * @return Message Formatted timestamp
*/
public function getHumanTimestamp() {
$then = $this->getTimestamp( TS_UNIX );
}
}
+ /**
+ * @since 1.20
+ *
+ * @return string
+ */
public function __toString() {
return $this->getTimestamp();
}
}
+/**
+ * @since 1.20
+ */
class TimestampException extends MWException {}