Skip to content

Commit

Permalink
Fix Interval#count counting the endpoint as part of the interval
Browse files Browse the repository at this point in the history
Fixes #1306
  • Loading branch information
diesieben07 committed Oct 19, 2022
1 parent 7fe3eb0 commit 1cc8497
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 @@ -238,7 +238,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 1cc8497

Please sign in to comment.