From a3e6315b3258858c928bf40695619ac81b903740 Mon Sep 17 00:00:00 2001 From: Vlasta Neubauer Date: Wed, 31 May 2023 12:21:36 +0200 Subject: [PATCH] Fix default format in SimpleDateTimeIntervalFormatter --- .../Interval/SimpleDateTimeIntervalFormatter.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Time/Interval/SimpleDateTimeIntervalFormatter.php b/src/Time/Interval/SimpleDateTimeIntervalFormatter.php index c33b4855..11b8f3c4 100644 --- a/src/Time/Interval/SimpleDateTimeIntervalFormatter.php +++ b/src/Time/Interval/SimpleDateTimeIntervalFormatter.php @@ -9,8 +9,12 @@ namespace Dogma\Time\Interval; +use Dogma\ShouldNotHappenException; use Dogma\StrictBehaviorMixin; +use Dogma\Time\Date; +use Dogma\Time\DateTime; use Dogma\Time\InvalidFormattingStringException; +use Dogma\Time\Time; use function count; use function explode; @@ -32,9 +36,17 @@ public function format(DateOrTimeInterval $interval, ?string $format = null): st } [$startFormat, $endFormat] = $parts; $separator = ''; - } else { - $startFormat = $endFormat = null; + } elseif ($interval instanceof TimeInterval) { + $startFormat = $endFormat = Time::DEFAULT_FORMAT; + $separator = ' - '; + } elseif ($interval instanceof DateTimeInterval) { + $startFormat = $endFormat = DateTime::DEFAULT_FORMAT; $separator = ' - '; + } elseif ($interval instanceof DateInterval || $interval instanceof NightInterval) { + $startFormat = $endFormat = Date::DEFAULT_FORMAT; + $separator = ' - '; + } else { + throw new ShouldNotHappenException('Default format for ' . get_class($interval) . ' is not defined.'); } if ($interval instanceof DateInterval) {