r106640 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106639‎ | r106640 | r106641 >
Date:10:51, 19 December 2011
Author:maxsem
Status:ok
Tags:
Comment:
ParserFunctions: ditch PHP pre 5.2 compatibility
Modified paths:
  • /trunk/extensions/ParserFunctions/ParserFunctions_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions_body.php
@@ -439,73 +439,46 @@
440440
441441 # compute the timestamp string $ts
442442 # PHP >= 5.2 can handle dates before 1970 or after 2038 using the DateTime object
443 - # PHP < 5.2 is limited to dates between 1970 and 2038
444443
445444 $invalidTime = false;
446445
447 - if ( class_exists( 'DateTime' ) ) { # PHP >= 5.2
448 - # the DateTime constructor must be used because it throws exceptions
449 - # when errors occur, whereas date_create appears to just output a warning
450 - # that can't really be detected from within the code
451 - try {
 446+ # the DateTime constructor must be used because it throws exceptions
 447+ # when errors occur, whereas date_create appears to just output a warning
 448+ # that can't really be detected from within the code
 449+ try {
452450
453 - # Default input timezone is UTC.
454 - $utc = new DateTimeZone( 'UTC' );
 451+ # Default input timezone is UTC.
 452+ $utc = new DateTimeZone( 'UTC' );
455453
456 - # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock
457 - if ( preg_match( '/^[0-9]{4}$/', $date ) ) {
458 - $date = '00:00 '.$date;
459 - }
460 -
461 - # Parse date
462 - if ( $date !== '' ) {
463 - # UTC is a default input timezone.
464 - $dateObject = new DateTime( $date, $utc );
465 - } else {
466 - # use current date and time
467 - $dateObject = new DateTime( 'now', $utc );
468 - }
469 - # Set output timezone.
470 - if ( $local ) {
471 - if ( isset( $wgLocaltimezone ) ) {
472 - $tz = new DateTimeZone( $wgLocaltimezone );
473 - } else {
474 - $tz = new DateTimeZone( date_default_timezone_get() );
475 - }
476 - $dateObject->setTimezone( $tz );
477 - } else {
478 - $dateObject->setTimezone( $utc );
479 - }
480 - # Generate timestamp
481 - $ts = $dateObject->format( 'YmdHis' );
482 -
483 - } catch ( Exception $ex ) {
484 - $invalidTime = true;
 454+ # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock
 455+ if ( preg_match( '/^[0-9]{4}$/', $date ) ) {
 456+ $date = '00:00 '.$date;
485457 }
486 - } else { # PHP < 5.2
487 - $oldtz = date_default_timezone_get();
488 - # UTC is a default inpu timezone.
489 - date_default_timezone_set( 'UTC' );
 458+
 459+ # Parse date
490460 if ( $date !== '' ) {
491 - wfSuppressWarnings();
492 - $unix = strtotime( $date );
493 - wfRestoreWarnings();
 461+ # UTC is a default input timezone.
 462+ $dateObject = new DateTime( $date, $utc );
494463 } else {
495 - $unix = time();
 464+ # use current date and time
 465+ $dateObject = new DateTime( 'now', $utc );
496466 }
497 - if ( $unix == -1 || $unix == false ) {
498 - $invalidTime = true;
499 - } else {
500 - # Set output timezone.
501 - if ( $local && isset( $wgLocaltimezone ) ) {
502 - date_default_timezone_set( $wgLocaltimezone );
 467+ # Set output timezone.
 468+ if ( $local ) {
 469+ if ( isset( $wgLocaltimezone ) ) {
 470+ $tz = new DateTimeZone( $wgLocaltimezone );
 471+ } else {
 472+ $tz = new DateTimeZone( date_default_timezone_get() );
503473 }
504 - # Generate timestamp
505 - wfSuppressWarnings(); // E_STRICT system time bitching
506 - $ts = date( 'YmdHis', $unix );
507 - wfRestoreWarnings();
 474+ $dateObject->setTimezone( $tz );
 475+ } else {
 476+ $dateObject->setTimezone( $utc );
508477 }
509 - date_default_timezone_set( $oldtz );
 478+ # Generate timestamp
 479+ $ts = $dateObject->format( 'YmdHis' );
 480+
 481+ } catch ( Exception $ex ) {
 482+ $invalidTime = true;
510483 }
511484
512485 # format the timestamp and return the result

Status & tagging log