Merge "Add a test for named vs. positional parameter whitespace stripping"
[lhc/web/wiklou.git] / includes / Timestamp.php
index 41665bc..56ce46c 100644 (file)
@@ -61,6 +61,7 @@ class MWTimestamp {
         * The actual timestamp being wrapped. Either a DateTime
         * object or a string with a Unix timestamp depending on
         * PHP.
+        * @var string|DateTime
         */
        private $timestamp;
 
@@ -68,7 +69,9 @@ class MWTimestamp {
         * 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 );
@@ -77,17 +80,19 @@ class MWTimestamp {
        /**
         * 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 ) ) {
@@ -98,7 +103,6 @@ class MWTimestamp {
                        # 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
@@ -136,18 +140,14 @@ class MWTimestamp {
                        $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;
        }
@@ -158,7 +158,10 @@ class MWTimestamp {
         * 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 ) {
@@ -166,7 +169,7 @@ class MWTimestamp {
                        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 ) {
@@ -191,7 +194,9 @@ class MWTimestamp {
         * 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 );
@@ -217,9 +222,17 @@ class MWTimestamp {
                }
        }
 
+       /**
+        * @since 1.20
+        *
+        * @return string
+        */
        public function __toString() {
                return $this->getTimestamp();
        }
 }
 
+/**
+ * @since 1.20
+ */
 class TimestampException extends MWException {}