Index: trunk/extensions/ParserFunctions/ParserFunctions_body.php |
— | — | @@ -437,19 +437,10 @@ |
438 | 438 | # when errors occur, whereas date_create appears to just output a warning |
439 | 439 | # that can't really be detected from within the code |
440 | 440 | 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 | + |
454 | 445 | # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock |
455 | 446 | if ( preg_match( '/^[0-9]{4}$/', $date ) ) { |
456 | 447 | $date = '00:00 '.$date; |
— | — | @@ -457,43 +448,53 @@ |
458 | 449 | |
459 | 450 | # Parse date |
460 | 451 | if ( $date !== '' ) { |
461 | | - $dateObject = new DateTime( $date, $tz ); |
| 452 | + # UTC is a default input timezone. |
| 453 | + $dateObject = new DateTime( $date, $utc ); |
462 | 454 | } else { |
463 | 455 | # use current date and time |
464 | | - $dateObject = new DateTime( 'now', $tz ); |
| 456 | + $dateObject = new DateTime( 'now', $utc ); |
465 | 457 | } |
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 | + } |
467 | 469 | # Generate timestamp |
468 | 470 | $ts = $dateObject->format( 'YmdHis' ); |
| 471 | + |
469 | 472 | } catch ( Exception $ex ) { |
470 | 473 | $invalidTime = true; |
471 | 474 | } |
472 | 475 | } else { # PHP < 5.2 |
| 476 | + $oldtz = date_default_timezone_get(); |
| 477 | + # UTC is a default inpu timezone. |
| 478 | + date_default_timezone_set( 'UTC' ); |
473 | 479 | if ( $date !== '' ) { |
474 | | - $unix = @strtotime( $date ); |
| 480 | + wfSuppressWarnings(); |
| 481 | + $unix = strtotime( $date ); |
| 482 | + wfRestoreWarnings(); |
475 | 483 | } else { |
476 | 484 | $unix = time(); |
477 | 485 | } |
478 | | - |
479 | 486 | if ( $unix == -1 || $unix == false ) { |
480 | 487 | $invalidTime = true; |
481 | 488 | } 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 ); |
496 | 492 | } |
| 493 | + # Generate timestamp |
| 494 | + wfSuppressWarnings(); // E_STRICT system time bitching |
| 495 | + $ts = date( 'YmdHis', $unix ); |
| 496 | + wfRestoreWarnings(); |
497 | 497 | } |
| 498 | + date_default_timezone_set( $oldtz ); |
498 | 499 | } |
499 | 500 | |
500 | 501 | # format the timestamp and return the result |
Index: trunk/extensions/ParserFunctions/funcsParserTests.txt |
— | — | @@ -22,7 +22,7 @@ |
23 | 23 | !! endarticle |
24 | 24 | |
25 | 25 | !! test |
26 | | -Input times should probably be UTC, not local time |
| 26 | +Input times are UTC, not local time |
27 | 27 | !! input |
28 | 28 | {{#time:c|15 January 2001}} |
29 | 29 | !!result |
— | — | @@ -58,6 +58,33 @@ |
59 | 59 | !! end |
60 | 60 | |
61 | 61 | !! 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 |
62 | 89 | Bug 19093: Default values don't fall through in switch |
63 | 90 | !! input |
64 | 91 | <{{#switch: foo | bar | #default = DEF }}> |