https://www.mediawiki.org/wiki/Special:Code/MediaWiki/105459

Revision: 105459
Author:   bawolff
Date:     2011-12-07 20:52:41 +0000 (Wed, 07 Dec 2011)
Log Message:
-----------
(bug 32351) Make #time[l] support explicitly specified timezones. Patch by  Van 
de Bugger.

This would make something like {{#time:H:i:s| 9:30 January 1, 2012 MST}} 
convert the time from 9:30 mountatin standard time to whatever it is in UTC.

I made one minor change from the patch on bugzilla in changing an @ to a 
wfSuppressWarnings. (The @ was already in the code, it wasn't introduced by the 
patch).

Modified Paths:
--------------
    trunk/extensions/ParserFunctions/ParserFunctions_body.php
    trunk/extensions/ParserFunctions/funcsParserTests.txt

Modified: trunk/extensions/ParserFunctions/ParserFunctions_body.php
===================================================================
--- trunk/extensions/ParserFunctions/ParserFunctions_body.php   2011-12-07 
20:06:04 UTC (rev 105458)
+++ trunk/extensions/ParserFunctions/ParserFunctions_body.php   2011-12-07 
20:52:41 UTC (rev 105459)
@@ -437,19 +437,10 @@
                        # when errors occur, whereas date_create appears to 
just output a warning
                        # that can't really be detected from within the code
                        try {
-                               # Determine timezone
-                               if ( $local ) {
-                                        # convert to MediaWiki local timezone 
if set
-                                       if ( isset( $wgLocaltimezone ) ) {
-                                               $tz = new DateTimeZone( 
$wgLocaltimezone );
-                                       } else {
-                                               $tz = new DateTimeZone( 
date_default_timezone_get() );
-                                       }
-                               } else {
-                                       # if local time was not requested, 
convert to UTC
-                                       $tz = new DateTimeZone( 'UTC' );
-                               }
-                               
+
+                               # Default input timezone is UTC.
+                               $utc = new DateTimeZone( 'UTC' );
+
                                # Correct for DateTime interpreting 'XXXX' as 
XX:XX o'clock
                                if ( preg_match( '/^[0-9]{4}$/', $date ) ) {
                                        $date = '00:00 '.$date;
@@ -457,43 +448,53 @@
 
                                # Parse date
                                if ( $date !== '' ) {
-                                       $dateObject = new DateTime( $date, $tz 
);
+                                       # UTC is a default input timezone.
+                                       $dateObject = new DateTime( $date, $utc 
);
                                } else {
                                        # use current date and time
-                                       $dateObject = new DateTime( 'now', $tz 
);
+                                       $dateObject = new DateTime( 'now', $utc 
);
                                }
-
+                               # Set output timezone.
+                               if ( $local ) {
+                                       if ( isset( $wgLocaltimezone ) ) {
+                                               $tz = new DateTimeZone( 
$wgLocaltimezone );
+                                       } else {
+                                               $tz = new DateTimeZone( 
date_defaulttimezone_get() );
+                                       }
+                                       $dateObject->setTimezone( $tz );
+                               } else {
+                                       $dateObject->setTimezone( $utc );
+                               }
                                # Generate timestamp
                                $ts = $dateObject->format( 'YmdHis' );
+
                        } catch ( Exception $ex ) {
                                $invalidTime = true;
                        }
                } else { # PHP < 5.2
+                       $oldtz = date_default_timezone_get();
+                       # UTC is a default inpu timezone.
+                       date_default_timezone_set( 'UTC' );
                        if ( $date !== '' ) {
-                               $unix = @strtotime( $date );
+                               wfSuppressWarnings();
+                               $unix = strtotime( $date );
+                               wfRestoreWarnings();
                        } else {
                                $unix = time();
                        }
-
                        if ( $unix == -1 || $unix == false ) {
                                $invalidTime = true;
                        } else {
-                               if ( $local ) {
-                                       # Use the time zone
-                                       if ( isset( $wgLocaltimezone ) ) {
-                                               $oldtz = 
date_default_timezone_get();
-                                               date_default_timezone_set( 
$wgLocaltimezone );
-                                       }
-                                       wfSuppressWarnings(); // E_STRICT 
system time bitching
-                                       $ts = date( 'YmdHis', $unix );
-                                       wfRestoreWarnings();
-                                       if ( isset( $wgLocaltimezone ) ) {
-                                               date_default_timezone_set( 
$oldtz );
-                                       }
-                               } else {
-                                       $ts = wfTimestamp( TS_MW, $unix );
+                               # Set output timezone.
+                               if ( $local && isset( $wgLocaltimezone ) ) {
+                                       date_default_timezone_set( 
$wgLocaltimezone );
                                }
+                               # Generate timestamp
+                               wfSuppressWarnings(); // E_STRICT system time 
bitching
+                               $ts = date( 'YmdHis', $unix );
+                               wfRestoreWarnings();
                        }
+                       date_default_timezone_set( $oldtz );
                }
 
                # format the timestamp and return the result

Modified: trunk/extensions/ParserFunctions/funcsParserTests.txt
===================================================================
--- trunk/extensions/ParserFunctions/funcsParserTests.txt       2011-12-07 
20:06:04 UTC (rev 105458)
+++ trunk/extensions/ParserFunctions/funcsParserTests.txt       2011-12-07 
20:52:41 UTC (rev 105459)
@@ -22,7 +22,7 @@
 !! endarticle
 
 !! test
-Input times should probably be UTC, not local time
+Input times are UTC, not local time
 !! input
 {{#time:c|15 January 2001}}
 !!result
@@ -58,6 +58,33 @@
 !! end
 
 !! test
+Explicitly specified timezone: UTC
+!! input
+{{#time:Y-m-d H:i| 2011-11-12 23:00 UTC }}
+!! result
+<p>2011-11-12 23:00
+</p>
+!! end
+
+!! test
+Explicitly specified timezone: Europe/Paris (UTC+1)
+!! input
+{{#time:Y-m-d H:i| 2011-11-12 23:00 Europe/Paris }}
+!! result
+<p>2011-11-12 22:00
+</p>
+!! end
+
+!! test
+Explicitly specified timezone: America/New_York (UTC-5)
+!! input
+{{#time:Y-m-d H:i| 2011-11-12 23:00 America/New_York }}
+!! result
+<p>2011-11-13 04:00
+</p>
+!! end
+
+!! test
 Bug 19093: Default values don't fall through in switch
 !! input
 <{{#switch: foo | bar | #default = DEF }}>


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to