Skip to content

Commit

Permalink
Fix Interval#count counting the endpoint as part of the interval (#1308)
Browse files Browse the repository at this point in the history
* Fix Interval#count counting the endpoint as part of the interval
Fixes #1306

* Run formatter
  • Loading branch information
diesieben07 authored Mar 4, 2023
1 parent 304bddc commit 33f7957
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export default class Interval {
if (!this.isValid) return NaN;
const start = this.start.startOf(unit),
end = this.end.startOf(unit);
return Math.floor(end.diff(start, unit).get(unit)) + 1;
return Math.floor(end.diff(start, unit).get(unit)) + (end.valueOf() !== this.end.valueOf());
}

/**
Expand Down
7 changes: 6 additions & 1 deletion test/interval/info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ test("Interval#count('years') returns 2 if the interval crosses the new year", (
expect(i.count("years")).toBe(2);
});

test("Interval#count() does not count the endpoint of the interval", () => {
const i = DateTime.fromISO("2022-10-01").until(DateTime.fromISO("2022-10-03"));
expect(i.count("days")).toBe(2);
});

test("Interval#count() uses milliseconds by default", () => {
const i = DateTime.fromISO("2016-05-25T03:00").until(DateTime.fromISO("2016-05-25T14:00"));
expect(i.count()).toBe(39600001);
expect(i.count()).toBe(39600000);
});

test("Interval#count() returns NaN for invalid intervals", () => {
Expand Down

0 comments on commit 33f7957

Please sign in to comment.