Skip to content

Commit

Permalink
Restructure, using constant for 'Y-m-d'.
Browse files Browse the repository at this point in the history
  • Loading branch information
campbell-m committed Nov 8, 2024
1 parent c433327 commit ab0d8fd
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion web/edit_entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/edit_entry_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions web/functions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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;
}
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion web/functions_view.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
7 changes: 4 additions & 3 deletions web/lib/MRBS/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '..';


Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}


Expand Down
3 changes: 1 addition & 2 deletions web/lib/MRBS/EntryInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}


Expand Down
6 changes: 3 additions & 3 deletions web/mrbs_sql.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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))
{
Expand All @@ -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))
{
Expand Down
10 changes: 5 additions & 5 deletions web/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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'");
}
Expand Down
2 changes: 1 addition & 1 deletion web/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit ab0d8fd

Please sign in to comment.