r50506 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50505‎ | r50506 | r50507 >
Date:06:56, 12 May 2009
Author:tstarling
Status:deferred
Tags:
Comment:
* Fix for PHP 5.3: use the timezone parameter to DateTime::__construct() instead of applying setTimeZone() afterwards. This avoids an exception being thrown when the timezone is not set in php.ini.
* Tweaked formatting
Modified paths:
  • /trunk/extensions/ParserFunctions/ParserFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions.php
@@ -409,22 +409,32 @@
410410 $invalidTime = false;
411411
412412 if ( class_exists( 'DateTime' ) ) { #PHP >= 5.2
413 - try { #the DateTime constructor must be used because it throws exceptions when errors occur, whereas date_create appears to just output a warning that can't really be detected from within the code
414 - if ( $date !== '' ) {
415 - $dateObject = new DateTime( $date );
 413+ # the DateTime constructor must be used because it throws exceptions
 414+ # when errors occur, whereas date_create appears to just output a warning
 415+ # that can't really be detected from within the code
 416+ try {
 417+ # Determine timezone
 418+ if ( $local ) {
 419+ # convert to MediaWiki local timezone if set
 420+ if ( isset( $wgLocaltimezone ) ) {
 421+ $tz = new DateTimeZone( $wgLocaltimezone );
 422+ } else {
 423+ $tz = new DateTimeZone( 'UTC' );
 424+ }
416425 } else {
417 - $dateObject = new DateTime(); #use current date and time
 426+ # if local time was not requested, convert to UTC
 427+ $tz = new DateTimeZone( 'UTC' );
418428 }
419 -
420 - if ( $local ) {
421 - if ( isset( $wgLocaltimezone ) ) { #convert to MediaWiki local timezone if set
422 - $dateObject->setTimeZone( new DateTimeZone( $wgLocaltimezone ) );
423 - } #otherwise leave in PHP default
 429+
 430+ # Parse date
 431+ if ( $date !== '' ) {
 432+ $dateObject = new DateTime( $date, $tz );
424433 } else {
425 - #if local time was not requested, convert to UTC
426 - $dateObject->setTimeZone( new DateTimeZone( 'UTC' ) );
 434+ # use current date and time
 435+ $dateObject = new DateTime( 'now', $tz );
427436 }
428 -
 437+
 438+ # Generate timestamp
429439 $ts = $dateObject->format( 'YmdHis' );
430440 } catch (Exception $ex) {
431441 $invalidTime = true;

Follow-up revisions

RevisionCommit summaryAuthorDate
r50579Backported r50506: make #time work in PHP 5.3.tstarling12:21, 14 May 2009

Status & tagging log