fix: return 0 length for empty periods instead of a positive count

Period::length() derived its count from diff()->days (day precision) and
abs() (hour/minute/second precision), both of which discard sign
information. For a period whose boundary exclusions push includedStart()
past includedEnd() (e.g. a single-day period with Boundaries::EXCLUDE_ALL),
the period is empty and iterating it yields zero dates, yet length()
still reported a positive number, disagreeing with iterator_count().

Add an explicit emptiness check so length() returns 0 whenever
includedStart() is after includedEnd(), matching the iterator's behaviour
across all precisions.
This commit is contained in:
dualfroz committed 2026-09-06 00:58:50 +02:00
1 parent dd39a1b841
commit 67bae2d40e
3 files changed
+28 -1

No files matched your search

+6
View File
@@ -11,6 +11,12 @@ it('can determine the period length', function () {
expect($period->length())->toEqual(15);
});
it('returns a length of 0 for an empty period, consistent with its iterator', function (Period $period) {
expect($period->includedStart())->toBeGreaterThan($period->includedEnd());
expect($period->length())->toEqual(0);
expect($period->length())->toEqual(iterator_count($period));
})->with('empty_period_lengths');
it('can renew a period', function () {
$period = Period::make('2018-01-01', '2018-01-15');