From ab0d8fdadd465e19fa7a0f2150ec63d0404ae24c Mon Sep 17 00:00:00 2001 From: campbell-m <87438215+campbell-m@users.noreply.github.com> Date: Fri, 8 Nov 2024 18:50:42 +0000 Subject: [PATCH] Restructure, using constant for 'Y-m-d'. --- web/edit_entry.php | 2 +- web/edit_entry_handler.php | 2 +- web/functions.inc | 6 +++--- web/functions_view.inc | 2 +- web/lib/MRBS/DateTime.php | 7 ++++--- web/lib/MRBS/EntryInterval.php | 3 +-- web/mrbs_sql.inc | 6 +++--- web/report.php | 10 +++++----- web/search.php | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/web/edit_entry.php b/web/edit_entry.php index e2a5167902..e4d9fb7a8a 100644 --- a/web/edit_entry.php +++ b/web/edit_entry.php @@ -1183,7 +1183,7 @@ function getbookingdate(int $t, bool $is_end=false) : array // The end date that came through from the drag select is actually the repeat end // date, and the real end date will actually be the start date. $rep_type = RepeatRule::DAILY; - $rep_end_date = DateTime::createFromFormat('Y-m-d', $end_date); + $rep_end_date = DateTime::createFromFormat(DateTime::ISO8601_DATE, $end_date); $end_date = $start_date; } } diff --git a/web/edit_entry_handler.php b/web/edit_entry_handler.php index 25eb805c1c..da629db29c 100644 --- a/web/edit_entry_handler.php +++ b/web/edit_entry_handler.php @@ -628,7 +628,7 @@ function invalid_booking(string $message) : void } if (isset($rep_end_date)) { - $repeat_end_date = DateTime::createFromFormat('Y-m-d', $rep_end_date); + $repeat_end_date = DateTime::createFromFormat(DateTime::ISO8601_DATE, $rep_end_date); if ($repeat_end_date === false) { throw new Exception("Could not create repeat end date"); diff --git a/web/functions.inc b/web/functions.inc index 153df24b76..28ea5aaa67 100644 --- a/web/functions.inc +++ b/web/functions.inc @@ -1766,7 +1766,7 @@ function times_somewhere(bool $only_enabled=false) : bool function format_iso_date(int $year, int $month, int $day) : string { $date = new DateTime(); - return $date->setDate($year, $month, $day)->format('Y-m-d'); + return $date->setDate($year, $month, $day)->format(DateTime::ISO8601_DATE); } @@ -1775,7 +1775,7 @@ function format_iso_date(int $year, int $month, int $day) : string // on failure. function split_iso_date(string $iso_date) { - if (false === ($date = DateTime::createFromFormat('Y-m-d', $iso_date))) + if (false === ($date = DateTime::createFromFormat(DateTime::ISO8601_DATE, $iso_date))) { return false; } @@ -1795,7 +1795,7 @@ function validate_iso_date(string $date) : bool // the inverse operation gives the original string then it is // valid. (This is to check that, for example, 2021-09-31 hasn't // been converted into 2021-10-01). - $format = 'Y-m-d'; + $format = DateTime::ISO8601_DATE; $datetime = DateTime::createFromFormat($format, $date); return ($datetime && ($datetime->format($format) === $date)); diff --git a/web/functions_view.inc b/web/functions_view.inc index 33f76fe55f..68a831396d 100644 --- a/web/functions_view.inc +++ b/web/functions_view.inc @@ -134,7 +134,7 @@ function create_details_body(array $data, bool $as_html=false, bool $keep_privat // Convert DATE types into a locale friendly form if (isset($column) && ('date' == $column->getType())) { - $date = DateTime::createFromFormat('Y-m-d', $data[$key]); + $date = DateTime::createFromFormat(DateTime::ISO8601_DATE, $data[$key]); if ($date !== false) { $data[$key] = datetime_format($datetime_formats['date'], $date->getTimestamp()); diff --git a/web/lib/MRBS/DateTime.php b/web/lib/MRBS/DateTime.php index 021d23e19e..69acc38b00 100644 --- a/web/lib/MRBS/DateTime.php +++ b/web/lib/MRBS/DateTime.php @@ -11,6 +11,7 @@ class DateTime extends \DateTime private static $isHoliday = array(); private static $validHolidays = array(); + public const ISO8601_DATE = 'Y-m-d'; private const HOLIDAY_RANGE_OPERATOR = '..'; @@ -187,7 +188,7 @@ public function getYear() : int // Returns a date in ISO 8601 format ('yyyy-mm-dd') public function getISODate() : string { - return $this->format('Y-m-d'); + return $this->format(self::ISO8601_DATE); } @@ -329,7 +330,7 @@ private function isHolidayConfig() : bool global $holidays; $year = $this->format('Y'); - $iso_date = $this->format('Y-m-d'); + $iso_date = $this->getISODate(); // Only need to check if a date is a holiday once, so store the answer in a // static property @@ -398,7 +399,7 @@ public function isHoliday() : bool public function isToday() : bool { $today = new DateTime(); - return ($this->format('Y-m-d') == $today->format('Y-m-d')); + return ($this->getISODate() == $today->getISODate()); } diff --git a/web/lib/MRBS/EntryInterval.php b/web/lib/MRBS/EntryInterval.php index 2ed8c6d83b..36e49ec7dc 100644 --- a/web/lib/MRBS/EntryInterval.php +++ b/web/lib/MRBS/EntryInterval.php @@ -166,8 +166,7 @@ public function overlapsWeekend() // Checks whether an entry spans more than one calendar (not booking) day public function spansMultipleDays() : bool { - $format = 'Y-m-d'; - return ($this->start_date->format($format) !== $this->end_date->format($format)); + return ($this->start_date->getISODate() !== $this->end_date->getISODate()); } diff --git a/web/mrbs_sql.inc b/web/mrbs_sql.inc index 1b24813eb5..7bda900667 100644 --- a/web/mrbs_sql.inc +++ b/web/mrbs_sql.inc @@ -153,7 +153,7 @@ function mrbsCheckFree(array $booking, ?int $ignore_entry_id=null, ?int $ignore_ } // enclose the viewday etc. links in a span to make it easier for JavaScript to strip them out - $vars = array('page_date' => $existing_start_date->format('Y-m-d'), + $vars = array('page_date' => $existing_start_date->getISODate(), 'area' => $area, 'room' => $room_id); @@ -602,7 +602,7 @@ function mrbsCheckPolicy(array $booking, ?int $ignore_entry_id=null, ?int $ignor if ($min_booking_date_enabled && !$delete) { // Use DateTimeImmutable because we will need the original date for the error message - $date_immutable = DateTimeImmutable::createFromFormat($format='Y-m-d', $min_booking_date); + $date_immutable = DateTimeImmutable::createFromFormat($format=DateTime::ISO8601_DATE, $min_booking_date); // Check that the date is valid (createFromFormat will allow, for example, months > 12) if (($date_immutable === false) || ($date_immutable->format($format) !== $min_booking_date)) { @@ -621,7 +621,7 @@ function mrbsCheckPolicy(array $booking, ?int $ignore_entry_id=null, ?int $ignor if ($max_booking_date_enabled && !$delete) { // Use DateTimeImmutable because we will need the original date for the error message - $date_immutable = DateTimeImmutable::createFromFormat($format='Y-m-d', $max_booking_date); + $date_immutable = DateTimeImmutable::createFromFormat($format=DateTime::ISO8601_DATE, $max_booking_date); // Check that the date is valid (createFromFormat will allow, for example, months > 12) if (($date_immutable === false) || ($date_immutable->format($format) !== $max_booking_date)) { diff --git a/web/report.php b/web/report.php index 2dbf90844a..2661ce9f9f 100644 --- a/web/report.php +++ b/web/report.php @@ -1477,8 +1477,8 @@ function get_match_condition(string $full_column_name, ?string $match, array &$s $default_from_time = mktime(0, 0, 0, $month, $day, $year); $default_to_time = mktime(0, 0, 0, $month, $day + $default_report_days, $year); -$default_from_date = date('Y-m-d', $default_from_time); -$default_to_date = date('Y-m-d', $default_to_time); +$default_from_date = date(DateTime::ISO8601_DATE, $default_from_time); +$default_to_date = date(DateTime::ISO8601_DATE, $default_to_time); // Get non-standard form variables $from_date = get_form_var('from_date', 'string', $default_from_date); @@ -1632,16 +1632,16 @@ function get_match_condition(string $full_column_name, ?string $match, array &$s if ($phase == 2) { // Start and end times are also used to clip the times for summary info. - // createFromFormat('Y-m-d') gives the current time. We want the report to + // createFromFormat() gives the current time. We want the report to // start at the beginning of the start day and end of the day, so set the // times accordingly. - if (false === ($report_start = DateTime::createFromFormat('Y-m-d', $from_date))) + if (false === ($report_start = DateTime::createFromFormat(DateTime::ISO8601_DATE, $from_date))) { throw new Exception("Invalid from_date '$from_date'"); } $report_start = $report_start->setTime(0, 0)->getTimestamp(); - if (false === ($report_end = DateTime::createFromFormat('Y-m-d', $to_date))) + if (false === ($report_end = DateTime::createFromFormat(DateTime::ISO8601_DATE, $to_date))) { throw new Exception("Invalid to_date '$to_date'"); } diff --git a/web/search.php b/web/search.php index 49e886b3e4..3ea3825d45 100644 --- a/web/search.php +++ b/web/search.php @@ -158,7 +158,7 @@ function output_row($row, $returl) { if (validate_iso_date($from_date)) { - $search_start_time = DateTime::createFromFormat('Y-m-d', $from_date)->setTime(0, 0)->getTimestamp(); + $search_start_time = DateTime::createFromFormat(DateTime::ISO8601_DATE, $from_date)->setTime(0, 0)->getTimestamp(); } else {