mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 02:16:41 +02:00
Merge branch 'MDL-44608-master' of git://github.com/damyon/moodle
This commit is contained in:
commit
9ea57c8bc3
2 changed files with 56 additions and 0 deletions
|
@ -272,9 +272,17 @@ abstract class scheduled_task extends task_base {
|
|||
* @return int $nextruntime.
|
||||
*/
|
||||
public function get_next_scheduled_time() {
|
||||
global $CFG;
|
||||
|
||||
$validminutes = $this->eval_cron_field($this->minute, 0, 59);
|
||||
$validhours = $this->eval_cron_field($this->hour, 0, 23);
|
||||
|
||||
// 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) {
|
||||
date_default_timezone_set($CFG->timezone);
|
||||
}
|
||||
|
||||
$daysinmonth = date("t");
|
||||
$validdays = $this->eval_cron_field($this->day, 1, $daysinmonth);
|
||||
$validdaysofweek = $this->eval_cron_field($this->dayofweek, 0, 7);
|
||||
|
@ -342,6 +350,11 @@ abstract class scheduled_task extends task_base {
|
|||
$nextvaliddayofmonth,
|
||||
$nextvalidyear);
|
||||
|
||||
// We need to change the timezone back so other date functions in moodle do not get confused.
|
||||
if (!empty($CFG->timezone) && $CFG->timezone != 99) {
|
||||
date_default_timezone_set($origtz);
|
||||
}
|
||||
|
||||
return $nexttime;
|
||||
}
|
||||
|
||||
|
|
|
@ -82,6 +82,49 @@ class scheduled_task_testcase extends advanced_testcase {
|
|||
$this->assertEquals($nexttenminutes, $nexttime, 'Next scheduled time is in 10 minutes.');
|
||||
}
|
||||
|
||||
public function test_timezones() {
|
||||
global $CFG, $USER;
|
||||
|
||||
// The timezones used in this test are chosen because they do not use DST - that would break the test.
|
||||
|
||||
$currenttimezonephp = date_default_timezone_get();
|
||||
$currenttimezonecfg = null;
|
||||
if (!empty($CFG->timezone)) {
|
||||
$currenttimezonecfg = $CFG->timezone;
|
||||
}
|
||||
$userstimezone = null;
|
||||
if (!empty($USER->timezone)) {
|
||||
$userstimezone = $USER->timezone;
|
||||
}
|
||||
|
||||
// We are testing a difference between $CFG->timezone and the php.ini timezone.
|
||||
// GMT+8.
|
||||
date_default_timezone_set('Australia/Perth');
|
||||
// GMT-04:30.
|
||||
$CFG->timezone = 'America/Caracas';
|
||||
|
||||
$testclass = new testable_scheduled_task();
|
||||
|
||||
// Scheduled tasks should always use servertime - so this is 03:30 GMT.
|
||||
$testclass->set_hour('1');
|
||||
$testclass->set_minute('0');
|
||||
|
||||
// Next valid time should be 1am of the next day.
|
||||
$nexttime = $testclass->get_next_scheduled_time();
|
||||
|
||||
// GMT+05:45.
|
||||
$USER->timezone = 'Asia/Kathmandu';
|
||||
$userdate = userdate($nexttime);
|
||||
|
||||
// Should be displayed in user timezone.
|
||||
// I used http://www.timeanddate.com/worldclock/fixedtime.html?msg=Moodle+Test&iso=20140314T01&p1=58
|
||||
// to verify this time.
|
||||
$this->assertContains('11:15 AM', $userdate);
|
||||
|
||||
$CFG->timezone = $currenttimezonecfg;
|
||||
date_default_timezone_set($currenttimezonephp);
|
||||
}
|
||||
|
||||
public function test_get_next_scheduled_task() {
|
||||
global $DB;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue