Skip to content

Commit

Permalink
Formatted empty interval should parse back to empty interval
Browse files Browse the repository at this point in the history
  • Loading branch information
VladaHejda committed Jun 23, 2023
1 parent 5ed8a35 commit e2060c8
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/Time/Interval/DateTimeInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public static function createFromString(string $string): self

$start = new DateTime($start);
$end = new DateTime($end);
if ($start > $end) {
return self::empty();
}

return new static($start, $end);
}
Expand Down
12 changes: 3 additions & 9 deletions src/Time/Interval/NightInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,11 @@ public static function createFromString(string $string): self
if ($openEnd) {
$end = $end->subtractDay();
}

$startJd = $start->getJulianDay();
$endJd = $end->getJulianDay();

if ($startJd > $endJd) {
throw new InvalidIntervalStartEndOrderException($start, $end);
} elseif ($startJd === $endJd) {
if ($start->getJulianDay() >= $end->getJulianDay()) {
return self::empty();
} else {
return new static($start, $end);
}

return new static($start, $end);
}

public static function createFromStartAndLength(Date $start, DateTimeUnit $unit, int $amount): self
Expand Down
1 change: 1 addition & 0 deletions tests/src/Time/Interval/DateInterval.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Assert::equal(DateInterval::createFromString('[2000-01-10,2000-01-20]'), $interv
Assert::equal(DateInterval::createFromString('[2000-01-10,2000-01-21)'), $interval);
Assert::equal(DateInterval::createFromString('(2000-01-09,2000-01-21)'), $interval);
Assert::equal(DateInterval::createFromString('(2000-01-09,2000-01-20]'), $interval);
Assert::equal(DateInterval::createFromString($empty->format()), $empty);
Assert::exception(static function (): void {
DateInterval::createFromString('foo|bar|baz');
}, InvalidIntervalStringFormatException::class);
Expand Down
1 change: 1 addition & 0 deletions tests/src/Time/Interval/DateTimeInterval.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Assert::equal(DateTimeInterval::createFromString('2000-01-10 00:00,2000-01-20 00
Assert::equal(DateTimeInterval::createFromString('2000-01-10 00:00|2000-01-20 00:00'), $interval);
Assert::equal(DateTimeInterval::createFromString('2000-01-10 00:00/2000-01-20 00:00'), $interval);
Assert::equal(DateTimeInterval::createFromString('2000-01-10 00:00 - 2000-01-20 00:00'), $interval);
Assert::equal(DateTimeInterval::createFromString($empty->format()), $empty);
Assert::exception(static function (): void {
DateTimeInterval::createFromString('foo|bar|baz');
}, InvalidIntervalStringFormatException::class);
Expand Down
1 change: 1 addition & 0 deletions tests/src/Time/Interval/NightInterval.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Assert::equal(NightInterval::createFromString('[2000-01-10,2000-01-21]'), $inter
Assert::equal(NightInterval::createFromString('[2000-01-10,2000-01-22)'), $interval);
Assert::equal(NightInterval::createFromString('(2000-01-09,2000-01-22)'), $interval);
Assert::equal(NightInterval::createFromString('(2000-01-09,2000-01-21]'), $interval);
Assert::equal(NightInterval::createFromString($empty->format()), $empty);
Assert::exception(static function (): void {
NightInterval::createFromString('foo|bar|baz');
}, InvalidIntervalStringFormatException::class);
Expand Down
1 change: 1 addition & 0 deletions tests/src/Time/Interval/TimeInterval.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Assert::equal(TimeInterval::createFromString('00:00 - 00:00'), new TimeInterval(
Assert::equal(TimeInterval::createFromString('00:00 - 00:00')->getLengthInMicroseconds(), 0);
Assert::equal(TimeInterval::createFromString('00:00 - 24:00'), new TimeInterval($t(0), $t(24)));
Assert::equal(TimeInterval::createFromString('00:00 - 24:00')->getLengthInMicroseconds(), Microseconds::DAY);
Assert::equal(TimeInterval::createFromString($empty->format()), $empty);
Assert::exception(static function (): void {
TimeInterval::createFromString('foo|bar|baz');
}, InvalidIntervalStringFormatException::class);
Expand Down

0 comments on commit e2060c8

Please sign in to comment.