From 3b6acb99bddc6a45e2b83e5c41447c48795cf40c Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Mon, 4 Sep 2023 12:16:29 +0800 Subject: [PATCH] MDL-75928 format_week: improve get_section_dates one week calc --- course/format/weeks/lib.php | 23 ++++++++++++++----- .../format/weeks/tests/format_weeks_test.php | 4 ++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php index 3bd726b4fc35a..79b0b119fb41b 100644 --- a/course/format/weeks/lib.php +++ b/course/format/weeks/lib.php @@ -406,14 +406,25 @@ public function get_section_dates($section, $startdate = false) { } else { $sectionnum = $section; } - $oneweekseconds = 604800; - // Hack alert. We add 2 hours to avoid possible DST problems. (e.g. we go into daylight - // savings and the date changes. - $startdate = $startdate + 7200; + + // Create a DateTime object for the start date. + $startdateobj = new DateTime("@$startdate"); + + // Calculate the interval for one week. + $oneweekinterval = new DateInterval('P7D'); + + // Calculate the interval for the specified number of sections. + for ($i = 1; $i < $sectionnum; $i++) { + $startdateobj->add($oneweekinterval); + } + + // Calculate the end date. + $enddateobj = clone $startdateobj; + $enddateobj->add($oneweekinterval); $dates = new stdClass(); - $dates->start = $startdate + ($oneweekseconds * ($sectionnum - 1)); - $dates->end = $dates->start + $oneweekseconds; + $dates->start = $startdateobj->getTimestamp(); + $dates->end = $enddateobj->getTimestamp(); return $dates; } diff --git a/course/format/weeks/tests/format_weeks_test.php b/course/format/weeks/tests/format_weeks_test.php index dd5eb990a366c..8d6414a2a0c53 100644 --- a/course/format/weeks/tests/format_weeks_test.php +++ b/course/format/weeks/tests/format_weeks_test.php @@ -219,8 +219,8 @@ public function test_default_course_enddate() { $courseform = new \testable_course_edit_form(null, $args); $courseform->definition_after_data(); - // format_weeks::get_section_dates is adding 2h to avoid DST problems, we need to replicate it here. - $enddate = $params['startdate'] + (WEEKSECS * $params['numsections']) + 7200; + // Calculate the expected end date. + $enddate = $params['startdate'] + (WEEKSECS * $params['numsections']); $weeksformat = course_get_format($course->id); $this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form()));