Index: lib/classes/task/scheduled_task.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- lib/classes/task/scheduled_task.php (revision bb5135afa27ef56187f545c8700939c456778290) +++ lib/classes/task/scheduled_task.php (revision ) @@ -291,7 +291,7 @@ * @return int $nextruntime. */ public function get_next_scheduled_time() { - global $CFG; + global $CFG, $DB; $validminutes = $this->eval_cron_field($this->minute, 0, 59); $validhours = $this->eval_cron_field($this->hour, 0, 23); @@ -299,7 +299,24 @@ // We need to change to the server timezone before using php date() functions. $origtz = date_default_timezone_get(); if (!empty($CFG->timezone) && $CFG->timezone != 99) { + if (is_numeric($CFG->timezone)) { + $timezonerow = $DB->get_record('timezone', array('gmtoff' => $CFG->timezone * 60), '*', IGNORE_MULTIPLE); + + // Some of the timezones with a .5 do not map to anything. For those that do not + // map to anything, try rounding down and re-fetching the timezone record. + $rounded = round($CFG->timezone, 0, PHP_ROUND_HALF_DOWN); + if (empty($timezonerow) && $rounded != $CFG->timezone) { + $timezonerow = $DB->get_record('timezone', array('gmtoff' => $rounded * 60), '*', IGNORE_MULTIPLE); + } + if (!empty($timezonerow)) { + date_default_timezone_set($timezonerow->name); + } else { + debugging("Failed map the site's timezone ($CFG->timezone) to a PHP timezone ID. ". + 'Scheduled task next run time might be off from your timezone preference.'); + } + } else { - date_default_timezone_set($CFG->timezone); + date_default_timezone_set($CFG->timezone); + } } $daysinmonth = date("t"); Index: lib/tests/scheduled_task_test.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- lib/tests/scheduled_task_test.php (revision bb5135afa27ef56187f545c8700939c456778290) +++ lib/tests/scheduled_task_test.php (revision ) @@ -281,4 +281,26 @@ $this->assertDebuggingCalled(); $this->assertNull($task); } + + public function test_all_timezones() { + global $CFG; + + $this->resetAfterTest(true); + + $timezones = get_list_of_timezones(); + + foreach (array_keys($timezones) as $timezone) { + $CFG->timezone = $timezone; + + $testclass = new \core\task\scheduled_test_task(); + $testclass->set_hour('1'); + $testclass->set_minute('0'); + + try { + $testclass->get_next_scheduled_time(); + } catch (Exception $e) { + echo "Invalid timezone: $CFG->timezone with error: {$e->getMessage()}\n"; + } + } + } }