Remove useless method overriding
[lhc/web/wiklou.git] / includes / Timestamp.php
index 5296122..c1c6455 100644 (file)
@@ -93,9 +93,9 @@ class MWTimestamp {
                        # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
                        $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
                                        str_replace( '+00:00', 'UTC', $ts ) );
-               } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
+               } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z?$/', $ts, $da ) ) {
                        # TS_ISO_8601
-               } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
+               } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z?$/', $ts, $da ) ) {
                        #TS_ISO_8601_BASIC
                } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', $ts, $da ) ) {
                        # TS_POSTGRES
@@ -114,7 +114,7 @@ class MWTimestamp {
                        # asctime
                        $strtime = $ts;
                } else {
-                       throw new TimestampException( __METHOD__ . " : Invalid timestamp - $ts" );
+                       throw new TimestampException( __METHOD__ . ": Invalid timestamp - $ts" );
                }
 
                if ( !$strtime ) {
@@ -126,11 +126,11 @@ class MWTimestamp {
                try {
                        $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) );
                } catch ( Exception $e ) {
-                       throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' );
+                       throw new TimestampException( __METHOD__ . ': Invalid timestamp format.', $e->getCode(), $e );
                }
 
                if ( $final === false ) {
-                       throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' );
+                       throw new TimestampException( __METHOD__ . ': Invalid timestamp format.' );
                }
                $this->timestamp = $final;
        }
@@ -149,7 +149,7 @@ class MWTimestamp {
         */
        public function getTimestamp( $style = TS_UNIX ) {
                if ( !isset( self::$formats[$style] ) ) {
-                       throw new TimestampException( __METHOD__ . ' : Illegal timestamp output type.' );
+                       throw new TimestampException( __METHOD__ . ': Illegal timestamp output type.' );
                }
 
                $output = $this->timestamp->format( self::$formats[$style] );
@@ -252,7 +252,7 @@ class MWTimestamp {
                        if ( count( $data ) >= 2 ) {
                                // Combination hours and minutes.
                                $diff = abs( (int)$data[0] ) * 60 + (int)$data[1];
-                               if ( (int) $data[0] < 0 ) {
+                               if ( (int)$data[0] < 0 ) {
                                        $diff *= -1;
                                }
                        } else {
@@ -270,6 +270,44 @@ class MWTimestamp {
                return $interval;
        }
 
+       /**
+        * Generate a purely relative timestamp, i.e., represent the time elapsed between
+        * the given base timestamp and this object.
+        *
+        * @param MWTimestamp $relativeTo Relative base timestamp (defaults to now)
+        * @param User $user Use to use offset for
+        * @param Language $lang Language to use
+        * @param array $chosenIntervals Intervals to use to represent it
+        * @return string Relative timestamp
+        */
+       public function getRelativeTimestamp(
+               MWTimestamp $relativeTo = null,
+               User $user = null,
+               Language $lang = null,
+               array $chosenIntervals = array()
+       ) {
+               if ( $relativeTo === null ) {
+                       $relativeTo = new self;
+               }
+               if ( $user === null ) {
+                       $user = RequestContext::getMain()->getUser();
+               }
+               if ( $lang === null ) {
+                       $lang = RequestContext::getMain()->getLanguage();
+               }
+
+               $ts = '';
+               $diff = $this->diff( $relativeTo );
+               if ( wfRunHooks( 'GetRelativeTimestamp', array( &$ts, &$diff, $this, $relativeTo, $user, $lang ) ) ) {
+                       $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
+                       $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
+                               ->inLanguage( $lang )
+                               ->text();
+               }
+
+               return $ts;
+       }
+
        /**
         * @since 1.20
         *
@@ -289,6 +327,67 @@ class MWTimestamp {
        public function diff( MWTimestamp $relativeTo ) {
                return $this->timestamp->diff( $relativeTo->timestamp );
        }
+
+       /**
+        * Set the timezone of this timestamp to the specified timezone.
+        *
+        * @since 1.22
+        * @param String $timezone Timezone to set
+        * @throws TimestampException
+        */
+       public function setTimezone( $timezone ) {
+               try {
+                       $this->timestamp->setTimezone( new DateTimeZone( $timezone ) );
+               } catch ( Exception $e ) {
+                       throw new TimestampException( __METHOD__ . ': Invalid timezone.', $e->getCode(), $e );
+               }
+       }
+
+       /**
+        * Get the timezone of this timestamp.
+        *
+        * @since 1.22
+        * @return DateTimeZone The timezone
+        */
+       public function getTimezone() {
+               return $this->timestamp->getTimezone();
+       }
+
+       /**
+        * Format the timestamp in a given format.
+        *
+        * @since 1.22
+        * @param string $format Pattern to format in
+        * @return string The formatted timestamp
+        */
+       public function format( $format ) {
+               return $this->timestamp->format( $format );
+       }
+
+       /**
+        * Get a timestamp instance in the server local timezone ($wgLocaltimezone)
+        *
+        * @since 1.22
+        * @param bool|string $ts Timestamp to set, or false for current time
+        * @return MWTimestamp the local instance
+        */
+       public static function getLocalInstance( $ts = false ) {
+               global $wgLocaltimezone;
+               $timestamp = new self( $ts );
+               $timestamp->setTimezone( $wgLocaltimezone );
+               return $timestamp;
+       }
+
+       /**
+        * Get a timestamp instance in GMT
+        *
+        * @since 1.22
+        * @param bool|string $ts Timestamp to set, or false for current time
+        * @return MWTimestamp the instance
+        */
+       public static function getInstance( $ts = false ) {
+               return new self( $ts );
+       }
 }
 
 /**