Avoid date()/gmdate() for date formatting
authorumherirrender <umherirrender_de.wp@web.de>
Sat, 6 Jul 2013 19:59:35 +0000 (21:59 +0200)
committerTim Starling <tstarling@wikimedia.org>
Mon, 8 Jul 2013 04:14:12 +0000 (04:14 +0000)
Added a MWTimestamp::format method to format a timestamp object in the
given pattern. This avoids date(), which needs the local time
zone corretly set on the server, which is assumed at the moment in
Setup.php

Added MWTimestamp::getInstance for gmdate() and
MWTimestamp::getLocalInstance for date() to create a MWTimestamp object
and use it as inline statement.
Also MWTimestamp::setTimezone and MWTimestamp::getTimzone was added for
timezone handling.

Change-Id: I812aa013be2f4380e0cf10dc465202756fe8347b

includes/Pager.php
includes/Preferences.php
includes/Timestamp.php
includes/UserMailer.php
includes/Xml.php
includes/actions/HistoryAction.php
includes/db/DatabaseOracle.php
includes/filerepo/FileRepo.php
includes/parser/Parser.php
includes/specials/SpecialContributions.php
includes/specials/SpecialVersion.php

index c7f51a3..89930e2 100644 (file)
@@ -872,9 +872,10 @@ abstract class ReverseChronologicalPager extends IndexPager {
                        $year = $this->mYear;
                } else {
                        // If no year given, assume the current one
-                       $year = gmdate( 'Y' );
+                       $timestamp = MWTimestamp::getInstance();
+                       $year = $timestamp->format( 'Y' );
                        // If this month hasn't happened yet this year, go back to last year's month
-                       if ( $this->mMonth > gmdate( 'n' ) ) {
+                       if ( $this->mMonth > $timestamp->format( 'n' ) ) {
                                $year--;
                        }
                }
index 6b5b5eb..d5c0470 100644 (file)
@@ -1290,10 +1290,11 @@ class Preferences {
        static function getTimezoneOptions( IContextSource $context ) {
                $opt = array();
 
-               global $wgLocalTZoffset, $wgLocaltimezone;
-               // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
-               if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
-                       $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $wgLocaltimezone )->text();
+               global $wgLocalTZoffset;
+               $timestamp = MWTimestamp::getLocalInstance();
+               // Check that $wgLocalTZoffset is the same as the local time zone offset
+               if ( $wgLocalTZoffset == $timestamp->format( 'Z' ) / 60 ) {
+                       $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $timestamp->getTimezone()->getName() )->text();
                } else {
                        $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) % 60 );
                        $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
index 5296122..c77f9a4 100644 (file)
@@ -289,6 +289,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.' );
+               }
+       }
+
+       /**
+        * 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 );
+       }
 }
 
 /**
index 8e3f048..049db6a 100644 (file)
@@ -241,7 +241,7 @@ class UserMailer {
                        $headers['Reply-To'] = $replyto->toString();
                }
 
-               $headers['Date'] = date( 'r' );
+               $headers['Date'] = MWTimestamp::getLocalInstance()->format( 'r' );
                $headers['Message-ID'] = self::makeMsgId();
                $headers['X-Mailer'] = 'MediaWiki mailer';
 
index d5501fe..ac0539d 100644 (file)
@@ -195,8 +195,9 @@ class Xml {
                if ( $year ) {
                        $encYear = intval( $year );
                } elseif ( $encMonth ) {
-                       $thisMonth = intval( gmdate( 'n' ) );
-                       $thisYear = intval( gmdate( 'Y' ) );
+                       $timestamp = MWTimestamp::getInstance();
+                       $thisMonth = intval( $timestamp->format( 'n' ) );
+                       $thisYear = intval( $timestamp->format( 'Y' ) );
                        if ( intval( $encMonth ) > $thisMonth ) {
                                $thisYear--;
                        }
index f43736b..ddcaa10 100644 (file)
@@ -178,7 +178,7 @@ class HistoryAction extends FormlessAction {
                        ) .
                        Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n" .
                        Html::hidden( 'action', 'history' ) . "\n" .
-                       Xml::dateMenu( ( $year == null ? date( "Y" ) : $year ), $month ) . '&#160;' .
+                       Xml::dateMenu( ( $year == null ? MWTimestamp::getLocalInstance()->format( 'Y' ) : $year ), $month ) . '&#160;' .
                        ( $tagSelector ? ( implode( '&#160;', $tagSelector ) . '&#160;' ) : '' ) .
                        $checkDeleted .
                        Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
index c0d3805..2ccc056 100644 (file)
@@ -341,7 +341,7 @@ class DatabaseOracle extends DatabaseBase {
                $union_unique = ( preg_match( '/\/\* UNION_UNIQUE \*\/ /', $sql ) != 0 );
                // EXPLAIN syntax in Oracle is EXPLAIN PLAN FOR and it return nothing
                // you have to select data from plan table after explain
-               $explain_id = date( 'dmYHis' );
+               $explain_id = MWTimestamp::getLocalInstance()->format( 'dmYHis' );
 
                $sql = preg_replace( '/^EXPLAIN /', 'EXPLAIN PLAN SET STATEMENT_ID = \'' . $explain_id . '\' FOR', $sql, 1, $explain_count );
 
index b82a0b8..bc1ad91 100644 (file)
@@ -965,7 +965,7 @@ class FileRepo {
        public function storeTemp( $originalName, $srcPath ) {
                $this->assertWritableRepo(); // fail out if read-only
 
-               $date = gmdate( "YmdHis" );
+               $date = MWTimestamp::getInstance()->format( 'YmdHis' );
                $hashPath = $this->getHashPath( $originalName );
                $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
                $virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
index fbde56d..813aaca 100644 (file)
@@ -2685,46 +2685,46 @@ class Parser {
 
                switch ( $index ) {
                        case 'currentmonth':
-                               $value = $pageLang->formatNum( gmdate( 'm', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'm' ) );
                                break;
                        case 'currentmonth1':
-                               $value = $pageLang->formatNum( gmdate( 'n', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'n' ) );
                                break;
                        case 'currentmonthname':
-                               $value = $pageLang->getMonthName( gmdate( 'n', $ts ) );
+                               $value = $pageLang->getMonthName( MWTimestamp::getInstance( $ts )->format( 'n' ) );
                                break;
                        case 'currentmonthnamegen':
-                               $value = $pageLang->getMonthNameGen( gmdate( 'n', $ts ) );
+                               $value = $pageLang->getMonthNameGen( MWTimestamp::getInstance( $ts )->format( 'n' ) );
                                break;
                        case 'currentmonthabbrev':
-                               $value = $pageLang->getMonthAbbreviation( gmdate( 'n', $ts ) );
+                               $value = $pageLang->getMonthAbbreviation( MWTimestamp::getInstance( $ts )->format( 'n' ) );
                                break;
                        case 'currentday':
-                               $value = $pageLang->formatNum( gmdate( 'j', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'j' ) );
                                break;
                        case 'currentday2':
-                               $value = $pageLang->formatNum( gmdate( 'd', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'd' ) );
                                break;
                        case 'localmonth':
-                               $value = $pageLang->formatNum( date( 'm', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'm' ) );
                                break;
                        case 'localmonth1':
-                               $value = $pageLang->formatNum( date( 'n', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
                                break;
                        case 'localmonthname':
-                               $value = $pageLang->getMonthName( date( 'n', $ts ) );
+                               $value = $pageLang->getMonthName( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
                                break;
                        case 'localmonthnamegen':
-                               $value = $pageLang->getMonthNameGen( date( 'n', $ts ) );
+                               $value = $pageLang->getMonthNameGen( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
                                break;
                        case 'localmonthabbrev':
-                               $value = $pageLang->getMonthAbbreviation( date( 'n', $ts ) );
+                               $value = $pageLang->getMonthAbbreviation( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
                                break;
                        case 'localday':
-                               $value = $pageLang->formatNum( date( 'j', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'j' ) );
                                break;
                        case 'localday2':
-                               $value = $pageLang->formatNum( date( 'd', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'd' ) );
                                break;
                        case 'pagename':
                                $value = wfEscapeWikiText( $this->mTitle->getText() );
@@ -2870,44 +2870,44 @@ class Parser {
                                $value = ( wfUrlencode( $this->mTitle->getSubjectNsText() ) );
                                break;
                        case 'currentdayname':
-                               $value = $pageLang->getWeekdayName( gmdate( 'w', $ts ) + 1 );
+                               $value = $pageLang->getWeekdayName( MWTimestamp::getInstance( $ts )->format( 'w' ) + 1 );
                                break;
                        case 'currentyear':
-                               $value = $pageLang->formatNum( gmdate( 'Y', $ts ), true );
+                               $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'Y' ), true );
                                break;
                        case 'currenttime':
                                $value = $pageLang->time( wfTimestamp( TS_MW, $ts ), false, false );
                                break;
                        case 'currenthour':
-                               $value = $pageLang->formatNum( gmdate( 'H', $ts ), true );
+                               $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'H' ), true );
                                break;
                        case 'currentweek':
                                # @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
                                # int to remove the padding
-                               $value = $pageLang->formatNum( (int)gmdate( 'W', $ts ) );
+                               $value = $pageLang->formatNum( (int)MWTimestamp::getInstance( $ts )->format( 'W' ) );
                                break;
                        case 'currentdow':
-                               $value = $pageLang->formatNum( gmdate( 'w', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'w' ) );
                                break;
                        case 'localdayname':
-                               $value = $pageLang->getWeekdayName( date( 'w', $ts ) + 1 );
+                               $value = $pageLang->getWeekdayName( MWTimestamp::getLocalInstance( $ts )->format( 'w' ) + 1 );
                                break;
                        case 'localyear':
-                               $value = $pageLang->formatNum( date( 'Y', $ts ), true );
+                               $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'Y' ), true );
                                break;
                        case 'localtime':
-                               $value = $pageLang->time( date( 'YmdHis', $ts ), false, false );
+                               $value = $pageLang->time( MWTimestamp::getLocalInstance( $ts )->format( 'YmdHis' ), false, false );
                                break;
                        case 'localhour':
-                               $value = $pageLang->formatNum( date( 'H', $ts ), true );
+                               $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'H' ), true );
                                break;
                        case 'localweek':
                                # @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
                                # int to remove the padding
-                               $value = $pageLang->formatNum( (int)date( 'W', $ts ) );
+                               $value = $pageLang->formatNum( (int)MWTimestamp::getLocalInstance( $ts )->format( 'W' ) );
                                break;
                        case 'localdow':
-                               $value = $pageLang->formatNum( date( 'w', $ts ) );
+                               $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'w' ) );
                                break;
                        case 'numberofarticles':
                                $value = $pageLang->formatNum( SiteStats::articles() );
@@ -2938,7 +2938,7 @@ class Parser {
                                $value = wfTimestamp( TS_MW, $ts );
                                break;
                        case 'localtimestamp':
-                               $value = date( 'YmdHis', $ts );
+                               $value = MWTimestamp::getLocalInstance( $ts )->format( 'YmdHis' );
                                break;
                        case 'currentversion':
                                $value = SpecialVersion::getVersion();
@@ -4540,11 +4540,11 @@ class Parser {
                # than the one selected in each user's preferences.
                # (see also bug 12815)
                $ts = $this->mOptions->getTimestamp();
-               $unixts = wfTimestamp( TS_UNIX, $ts );
-               $ts = date( 'YmdHis', $unixts );
-               $tzMsg = date( 'T', $unixts );  # might vary on DST changeover!
+               $timestamp = MWTimestamp::getLocalInstance( $ts );
+               $ts = $timestamp->format( 'YmdHis' );
+               $tzMsg = $timestamp->format( 'T' );  # might vary on DST changeover!
 
-               # Allow translation of timezones through wiki. date() can return
+               # Allow translation of timezones through wiki. format() can return
                # whatever crap the system uses, localised or not, so we cannot
                # ship premade translations.
                $key = 'timezone-' . strtolower( trim( $tzMsg ) );
index 2f37cb4..7a3e7c8 100644 (file)
@@ -567,7 +567,7 @@ class SpecialContributions extends SpecialPage {
 
                $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
                        Xml::dateMenu(
-                               $this->opts['year'] === '' ? gmdate( 'Y' ) : $this->opts['year'],
+                               $this->opts['year'] === '' ? MWTimestamp::getInstance()->format( 'Y' ) : $this->opts['year'],
                                $this->opts['month']
                        ) . ' ' .
                                Xml::submitButton(
index c257dd4..beb7a1c 100644 (file)
@@ -129,7 +129,7 @@ class SpecialVersion extends SpecialPage {
                        'Timo Tijhof', 'Daniel Kinzler', 'Jeroen De Dauw', $othersLink
                );
 
-               return wfMessage( 'version-poweredby-credits', date( 'Y' ),
+               return wfMessage( 'version-poweredby-credits', MWTimestamp::getLocalInstance()->format( 'Y' ),
                        $wgLang->listToText( $authorList ) )->text();
        }