r105459 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105458‎ | r105459 | r105460 >
Date:20:52, 7 December 2011
Author:bawolff
Status:ok (Comments)
Tags:
Comment:
(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 (modified) (history)
  • /trunk/extensions/ParserFunctions/funcsParserTests.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions_body.php
@@ -437,19 +437,10 @@
438438 # when errors occur, whereas date_create appears to just output a warning
439439 # that can't really be detected from within the code
440440 try {
441 - # Determine timezone
442 - if ( $local ) {
443 - # convert to MediaWiki local timezone if set
444 - if ( isset( $wgLocaltimezone ) ) {
445 - $tz = new DateTimeZone( $wgLocaltimezone );
446 - } else {
447 - $tz = new DateTimeZone( date_default_timezone_get() );
448 - }
449 - } else {
450 - # if local time was not requested, convert to UTC
451 - $tz = new DateTimeZone( 'UTC' );
452 - }
453 -
 441+
 442+ # Default input timezone is UTC.
 443+ $utc = new DateTimeZone( 'UTC' );
 444+
454445 # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock
455446 if ( preg_match( '/^[0-9]{4}$/', $date ) ) {
456447 $date = '00:00 '.$date;
@@ -457,43 +448,53 @@
458449
459450 # Parse date
460451 if ( $date !== '' ) {
461 - $dateObject = new DateTime( $date, $tz );
 452+ # UTC is a default input timezone.
 453+ $dateObject = new DateTime( $date, $utc );
462454 } else {
463455 # use current date and time
464 - $dateObject = new DateTime( 'now', $tz );
 456+ $dateObject = new DateTime( 'now', $utc );
465457 }
466 -
 458+ # Set output timezone.
 459+ if ( $local ) {
 460+ if ( isset( $wgLocaltimezone ) ) {
 461+ $tz = new DateTimeZone( $wgLocaltimezone );
 462+ } else {
 463+ $tz = new DateTimeZone( date_defaulttimezone_get() );
 464+ }
 465+ $dateObject->setTimezone( $tz );
 466+ } else {
 467+ $dateObject->setTimezone( $utc );
 468+ }
467469 # Generate timestamp
468470 $ts = $dateObject->format( 'YmdHis' );
 471+
469472 } catch ( Exception $ex ) {
470473 $invalidTime = true;
471474 }
472475 } else { # PHP < 5.2
 476+ $oldtz = date_default_timezone_get();
 477+ # UTC is a default inpu timezone.
 478+ date_default_timezone_set( 'UTC' );
473479 if ( $date !== '' ) {
474 - $unix = @strtotime( $date );
 480+ wfSuppressWarnings();
 481+ $unix = strtotime( $date );
 482+ wfRestoreWarnings();
475483 } else {
476484 $unix = time();
477485 }
478 -
479486 if ( $unix == -1 || $unix == false ) {
480487 $invalidTime = true;
481488 } else {
482 - if ( $local ) {
483 - # Use the time zone
484 - if ( isset( $wgLocaltimezone ) ) {
485 - $oldtz = date_default_timezone_get();
486 - date_default_timezone_set( $wgLocaltimezone );
487 - }
488 - wfSuppressWarnings(); // E_STRICT system time bitching
489 - $ts = date( 'YmdHis', $unix );
490 - wfRestoreWarnings();
491 - if ( isset( $wgLocaltimezone ) ) {
492 - date_default_timezone_set( $oldtz );
493 - }
494 - } else {
495 - $ts = wfTimestamp( TS_MW, $unix );
 489+ # Set output timezone.
 490+ if ( $local && isset( $wgLocaltimezone ) ) {
 491+ date_default_timezone_set( $wgLocaltimezone );
496492 }
 493+ # Generate timestamp
 494+ wfSuppressWarnings(); // E_STRICT system time bitching
 495+ $ts = date( 'YmdHis', $unix );
 496+ wfRestoreWarnings();
497497 }
 498+ date_default_timezone_set( $oldtz );
498499 }
499500
500501 # format the timestamp and return the result
Index: trunk/extensions/ParserFunctions/funcsParserTests.txt
@@ -22,7 +22,7 @@
2323 !! endarticle
2424
2525 !! test
26 -Input times should probably be UTC, not local time
 26+Input times are UTC, not local time
2727 !! input
2828 {{#time:c|15 January 2001}}
2929 !!result
@@ -58,6 +58,33 @@
5959 !! end
6060
6161 !! test
 62+Explicitly specified timezone: UTC
 63+!! input
 64+{{#time:Y-m-d H:i| 2011-11-12 23:00 UTC }}
 65+!! result
 66+<p>2011-11-12 23:00
 67+</p>
 68+!! end
 69+
 70+!! test
 71+Explicitly specified timezone: Europe/Paris (UTC+1)
 72+!! input
 73+{{#time:Y-m-d H:i| 2011-11-12 23:00 Europe/Paris }}
 74+!! result
 75+<p>2011-11-12 22:00
 76+</p>
 77+!! end
 78+
 79+!! test
 80+Explicitly specified timezone: America/New_York (UTC-5)
 81+!! input
 82+{{#time:Y-m-d H:i| 2011-11-12 23:00 America/New_York }}
 83+!! result
 84+<p>2011-11-13 04:00
 85+</p>
 86+!! end
 87+
 88+!! test
6289 Bug 19093: Default values don't fall through in switch
6390 !! input
6491 <{{#switch: foo | bar | #default = DEF }}>

Follow-up revisions

RevisionCommit summaryAuthorDate
r106617Fix function name typo "date_defaulttimezone_get()" to "date_default_timezone...reedy00:51, 19 December 2011

Comments

#Comment by GWicke (talk | contribs)   19:01, 12 January 2012

Minor nitpick: s/default inpu /default input /. Ran the parser tests, but did not test with PHP < 5.2.

#Comment by Bawolff (talk | contribs)   21:19, 12 January 2012

Whoops. However php < 5.2 support was removed in r106640 so the comment is no longer there

#Comment by GWicke (talk | contribs)   21:41, 12 January 2012

Ahh- good that I did not test it then ;)

Status & tagging log