Skip user namespace prefix for rights log entry
[lhc/web/wiklou.git] / includes / MWTimestamp.php
index 26f5e54..639403d 100644 (file)
@@ -56,7 +56,7 @@ class MWTimestamp {
         *
         * @since 1.20
         *
-        * @param bool|string $timestamp Timestamp to set, or false for current time
+        * @param bool|string|int|float $timestamp Timestamp to set, or false for current time
         */
        public function __construct( $timestamp = false ) {
                $this->setTimestamp( $timestamp );
@@ -74,6 +74,7 @@ class MWTimestamp {
         * @throws TimestampException
         */
        public function setTimestamp( $ts = false ) {
+               $m = array();
                $da = array();
                $strtime = '';
 
@@ -87,9 +88,9 @@ class MWTimestamp {
                        # TS_EXIF
                } 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 ) ) {
+               } elseif ( preg_match( '/^(-?\d{1,13})(\.\d+)?$/D', $ts, $m ) ) {
                        # TS_UNIX
-                       $strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php
+                       $strtime = "@{$m[1]}"; // 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 = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
@@ -105,7 +106,7 @@ class MWTimestamp {
                        $ts,
                        $da
                ) ) {
-                       #TS_ISO_8601_BASIC
+                       # TS_ISO_8601_BASIC
                } elseif ( preg_match(
                        '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/',
                        $ts,
@@ -182,6 +183,11 @@ class MWTimestamp {
                        $output .= ' GMT';
                }
 
+               if ( $style == TS_MW && strlen( $output ) !== 14 ) {
+                       throw new TimestampException( __METHOD__ . ': The timestamp cannot be represented in ' .
+                               'the specified format' );
+               }
+
                return $output;
        }
 
@@ -194,42 +200,23 @@ class MWTimestamp {
         *
         * @since 1.20
         * @since 1.22 Uses Language::getHumanTimestamp to produce the timestamp
+        * @deprecated since 1.26 Use Language::getHumanTimestamp directly
         *
-        * @param MWTimestamp|null $relativeTo The base timestamp to compare to
-        *   (defaults to now).
-        * @param User|null $user User the timestamp is being generated for (or null
-        *   to use main context's user).
+        * @param MWTimestamp|null $relativeTo The base timestamp to compare to (defaults to now)
+        * @param User|null $user User the timestamp is being generated for
+        *  (or null to use main context's user)
         * @param Language|null $lang Language to use to make the human timestamp
-        *   (or null to use main context's language).
+        *  (or null to use main context's language)
         * @return string Formatted timestamp
         */
-       public function getHumanTimestamp( MWTimestamp $relativeTo = null,
-               User $user = null, Language $lang = null
+       public function getHumanTimestamp(
+               MWTimestamp $relativeTo = null, User $user = null, Language $lang = null
        ) {
-               if ( $relativeTo === null ) {
-                       $relativeTo = new self();
-               }
-               if ( $user === null ) {
-                       $user = RequestContext::getMain()->getUser();
-               }
                if ( $lang === null ) {
                        $lang = RequestContext::getMain()->getLanguage();
                }
 
-               // Adjust for the user's timezone.
-               $offsetThis = $this->offsetForUser( $user );
-               $offsetRel = $relativeTo->offsetForUser( $user );
-
-               $ts = '';
-               if ( wfRunHooks( 'GetHumanTimestamp', array( &$ts, $this, $relativeTo, $user, $lang ) ) ) {
-                       $ts = $lang->getHumanTimestamp( $this, $relativeTo, $user );
-               }
-
-               // Reset the timezone on the objects.
-               $this->timestamp->sub( $offsetThis );
-               $relativeTo->timestamp->sub( $offsetRel );
-
-               return $ts;
+               return $lang->getHumanTimestamp( $this, $relativeTo, $user );
        }
 
        /**
@@ -326,7 +313,7 @@ class MWTimestamp {
 
                $ts = '';
                $diff = $this->diff( $relativeTo );
-               if ( wfRunHooks(
+               if ( Hooks::run(
                        'GetRelativeTimestamp',
                        array( &$ts, &$diff, $this, $relativeTo, $user, $lang )
                ) ) {
@@ -384,6 +371,26 @@ class MWTimestamp {
                return $this->timestamp->getTimezone();
        }
 
+       /**
+        * Get the localized timezone message, if available.
+        *
+        * Premade translations are not shipped as format() may return whatever the
+        * system uses, localized or not, so translation must be done through wiki.
+        *
+        * @since 1.25
+        * @return Message The localized timezone message
+        */
+       public function getTimezoneMessage() {
+               $tzMsg = $this->format( 'T' );  // might vary on DST changeover!
+               $key = 'timezone-' . strtolower( trim( $tzMsg ) );
+               $msg = wfMessage( $key );
+               if ( $msg->exists() ) {
+                       return $msg;
+               } else {
+                       return new RawMessage( $tzMsg );
+               }
+       }
+
        /**
         * Format the timestamp in a given format.
         *