Skip to content

Commit 9315287

Browse files
committed
fix date interval to always end after start
Otherwise luxon complains (as it should) and humanize fails
1 parent e79e2cf commit 9315287

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

shared/specs/model/time.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,13 @@ describe('time class', () => {
5757
expect(future.isAfter(past, 'hour')).toBe(false)
5858
})
5959

60+
it('converts to interval with human display', () => {
61+
const past = new Time('2021-01-15T10:00:00.000Z')
62+
const future = new Time('2021-01-15T10:58:00.000Z')
63+
const interval = future.intervalTo(past)
64+
// it flipped start/end so start always comes first
65+
expect(interval.start.isSame(past, 'millisecond')).toBe(true)
66+
expect(interval.humanized).toEqual('58 minutes')
67+
})
68+
6069
})

shared/src/model/time.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,15 @@ export default class Time {
9696
get asDate() { return this._value.toJSDate() }
9797
get asDateTime() { return this._value }
9898

99-
get intervalToNow() { return new Interval({ start: this, end: Time.now }) }
100-
intervalTo(other: TimeInputs) { return new Interval({ start: this, end: toLDT(other) }) }
99+
get intervalToNow() { return this.intervalTo(Time.now) }
100+
intervalTo(other: TimeInputs) {
101+
const end = toLDT(other)
102+
if (this.isBefore(end)) {
103+
return new Interval({ start: this, end })
104+
}
105+
return new Interval({ start: end, end: this })
106+
}
107+
101108

102109
toISOString() { return this.asISOString }
103110
toString() { return this.asISOString }

0 commit comments

Comments
 (0)