Merge "Proper handling of invalid/unknown time zones"

This commit is contained in:
jenkins-bot 2017-01-05 15:11:02 +00:00 committed by Gerrit Code Review
commit 6f19bac695
2 changed files with 39 additions and 19 deletions

View file

@ -696,18 +696,22 @@ class Preferences {
$tzOptions = self::getTimezoneOptions( $context );
$tzSetting = $tzOffset;
if ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
!in_array( $tzOffset, HTMLFormField::flattenOptions( $tzOptions ) )
) {
// Timezone offset can vary with DST
try {
$userTZ = new DateTimeZone( $tz[2] );
$minDiff = floor( $userTZ->getOffset( new DateTime( 'now' ) ) / 60 );
$tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
} catch ( Exception $e ) {
// User has an invalid time zone set. Fall back to just using the offset
$tz[0] = 'Offset';
}
}
if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
$minDiff = $tz[1];
$tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) % 60 );
} elseif ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
!in_array( $tzOffset, HTMLFormField::flattenOptions( $tzOptions ) )
) {
# Timezone offset can vary with DST
$userTZ = timezone_open( $tz[2] );
if ( $userTZ !== false ) {
$minDiff = floor( timezone_offset_get( $userTZ, date_create( 'now' ) ) / 60 );
$tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
}
}
$defaultPreferences['timecorrection'] = [
@ -1391,6 +1395,24 @@ class Preferences {
$data = explode( '|', $tz, 3 );
switch ( $data[0] ) {
case 'ZoneInfo':
$valid = false;
if ( count( $data ) === 3 ) {
// Make sure this timezone exists
try {
new DateTimeZone( $data[2] );
// If the constructor didn't throw, we know it's valid
$valid = true;
} catch ( Exception $e ) {
// Not a valid timezone
}
}
if ( !$valid ) {
// If the supplied timezone doesn't exist, fall back to the encoded offset
return 'Offset|' . intval( $tz[1] );
}
return $tz;
case 'System':
return $tz;
default:

View file

@ -2100,17 +2100,15 @@ class Language {
$data = explode( '|', $tz, 3 );
if ( $data[0] == 'ZoneInfo' ) {
MediaWiki\suppressWarnings();
$userTZ = timezone_open( $data[2] );
MediaWiki\restoreWarnings();
if ( $userTZ !== false ) {
$date = date_create( $ts, timezone_open( 'UTC' ) );
date_timezone_set( $date, $userTZ );
$date = date_format( $date, 'YmdHis' );
return $date;
try {
$userTZ = new DateTimeZone( $data[2] );
$date = new DateTime( $ts, new DateTimeZone( 'UTC' ) );
$date->setTimezone( $userTZ );
return $date->format( 'YmdHis' );
} catch ( Exception $e ) {
// Unrecognized timezone, default to 'Offset' with the stored offset.
$data[0] = 'Offset';
}
# Unrecognized timezone, default to 'Offset' with the stored offset.
$data[0] = 'Offset';
}
if ( $data[0] == 'System' || $tz == '' ) {