Skip to content

Commit

Permalink
MDL-49684 timezones: rewrite timezone support
Browse files Browse the repository at this point in the history
This patch replaces all homegrown timezone
stuff with standard PHP date/time code.

The main change is the introduction of core_date
class that returns normalised user and server
timezones. From now on nobody should be using
$CFG->timezone or $user->timezone directly!

Other new features and fixes:
* admins are prompted for timezone during install
* editing of other users is finally fixed
* timezones are displayed in user profile
* new $this->setTimezone() in phpunit
* time locale is now automatically reset in phpunit
* timezone is now automatically reset in phpunit
* phpunit has Australia/Perth as default timezone
  • Loading branch information
Petr Skoda committed Apr 6, 2015
1 parent e19b371 commit d6e7a63
Show file tree
Hide file tree
Showing 60 changed files with 2,341 additions and 4,620 deletions.
7 changes: 4 additions & 3 deletions admin/cli/install.php
Expand Up @@ -121,10 +121,11 @@
chdir(dirname($_SERVER['argv'][0]));

// Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
// This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this.
if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
@date_default_timezone_set(@date_default_timezone_get());
if (!function_exists('date_default_timezone_set') or !function_exists('date_default_timezone_get')) {
fwrite(STDERR, "Timezone functions are not available.\n");
exit(1);
}
date_default_timezone_set(@date_default_timezone_get());

// make sure PHP errors are displayed - helps with diagnosing of problems
@error_reporting(E_ALL);
Expand Down
1 change: 1 addition & 0 deletions admin/index.php
Expand Up @@ -521,6 +521,7 @@
// probably new installation - lets return to frontpage after this step
// remove settings that we want uninitialised
unset_config('registerauth');
unset_config('timezone'); // Force admin to select timezone!
redirect('upgradesettings.php?return=site');
}

Expand Down
23 changes: 19 additions & 4 deletions admin/settings/location.php
Expand Up @@ -4,11 +4,26 @@

// "locations" settingpage
$temp = new admin_settingpage('locationsettings', new lang_string('locationsettings', 'admin'));
$options = get_list_of_timezones();
$options[99] = new lang_string('serverlocaltime');
$temp->add(new admin_setting_configselect('timezone', new lang_string('timezone','admin'), new lang_string('configtimezone', 'admin'), 99, $options));

$current = isset($CFG->timezone) ? $CFG->timezone : null;
$options = core_date::get_list_of_timezones($current, false);
$default = core_date::get_default_php_timezone();
if ($current == 99) {
// Do not show 99 unless it is current value, we want to get rid of it over time.
$options['99'] = new lang_string('timezonephpdefault', 'core_admin', $default);
}
if ($default === 'UTC') {
// Nobody really wants UTC, so instead default selection to the country that is confused by the UTC the most.
$default = 'Europe/London';
}
$temp->add(new admin_setting_configselect('timezone', new lang_string('timezone', 'admin'),
new lang_string('configtimezone', 'admin'), $default, $options));

$options = core_date::get_list_of_timezones(isset($CFG->forcetimezone) ? $CFG->forcetimezone : null, true);
$options[99] = new lang_string('timezonenotforced', 'admin');
$temp->add(new admin_setting_configselect('forcetimezone', new lang_string('forcetimezone', 'admin'), new lang_string('helpforcetimezone', 'admin'), 99, $options));
$temp->add(new admin_setting_configselect('forcetimezone', new lang_string('forcetimezone', 'admin'),
new lang_string('helpforcetimezone', 'admin'), 99, $options));

$temp->add(new admin_settings_country_select('country', new lang_string('country', 'admin'), new lang_string('configcountry', 'admin'), 0));
$temp->add(new admin_setting_configtext('defaultcity', new lang_string('defaultcity', 'admin'), new lang_string('defaultcity_help', 'admin'), ''));

Expand Down
1 change: 1 addition & 0 deletions admin/settings/users.php
Expand Up @@ -157,6 +157,7 @@
array('description' => new lang_string('description'),
'city' => new lang_string('city'),
'country' => new lang_string('country'),
'timezone' => new lang_string('timezone'),
'webpage' => new lang_string('webpage'),
'icqnumber' => new lang_string('icqnumber'),
'skypeid' => new lang_string('skypeid'),
Expand Down
4 changes: 2 additions & 2 deletions admin/timezone.php
Expand Up @@ -41,11 +41,11 @@
}

require_once($CFG->dirroot.'/calendar/lib.php');
$timezones = get_list_of_timezones();
$timezones = core_date::get_list_of_timezones(null, true);

echo '<center><form action="timezone.php" method="post">';
echo html_writer::label($strusers . ' (' . $strall . '): ', 'menuzone');
echo html_writer::select($timezones, "zone", $current, array('99'=>get_string("serverlocaltime")));
echo html_writer::select($timezones, "zone", $current);
echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
echo '<input type="submit" value="'.s($strsavechanges).'" />';
echo "</form></center>";
Expand Down
157 changes: 0 additions & 157 deletions admin/tool/timezoneimport/index.php

This file was deleted.

31 changes: 0 additions & 31 deletions admin/tool/timezoneimport/lang/en/tool_timezoneimport.php

This file was deleted.

31 changes: 0 additions & 31 deletions admin/tool/timezoneimport/settings.php

This file was deleted.

31 changes: 0 additions & 31 deletions admin/tool/timezoneimport/version.php

This file was deleted.

3 changes: 1 addition & 2 deletions admin/tool/uploaduser/user_form.php
Expand Up @@ -255,8 +255,7 @@ function definition () {
}
$mform->setAdvanced('country');

$choices = get_list_of_timezones();
$choices['99'] = get_string('serverlocaltime');
$choices = core_date::get_list_of_timezones($templateuser->timezone, true);
$mform->addElement('select', 'timezone', get_string('timezone'), $choices);
$mform->setDefault('timezone', $templateuser->timezone);
$mform->setAdvanced('timezone');
Expand Down
9 changes: 6 additions & 3 deletions availability/condition/date/tests/condition_test.php
Expand Up @@ -47,13 +47,13 @@ public function setUp() {
* Tests constructing and using date condition as part of tree.
*/
public function test_in_tree() {
global $SITE, $USER;
global $SITE, $USER, $CFG;
$this->resetAfterTest();
$this->setAdminUser();

// Set server timezone for test. (Important as otherwise the timezone
// could be anything - this is modified by other unit tests, too.)
date_default_timezone_set('UTC');
$this->setTimezone('UTC');

// SEt user to GMT+5.
$USER->timezone = 5;
Expand Down Expand Up @@ -179,7 +179,10 @@ public function test_is_available() {
* Tests the get_description and get_standalone_description functions.
*/
public function test_get_description() {
global $SITE;
global $SITE, $CFG;

$this->resetAfterTest();
$this->setTimezone('UTC');

$modinfo = get_fast_modinfo($SITE);
$info = new \core_availability\mock_info();
Expand Down

0 comments on commit d6e7a63

Please sign in to comment.