Skip to content

Commit 5ec901f

Browse files
authored
Merge pull request #3670 from openstax/fix/interval
fix date interval to always end after start
2 parents feae7d0 + 9315287 commit 5ec901f

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
@@ -99,8 +99,15 @@ export default class Time {
9999
get asDate() { return this._value.toJSDate() }
100100
get asDateTime() { return this._value }
101101

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

105112
toISOString() { return this.asISOString }
106113
toString() { return this.asISOString }

0 commit comments

Comments
 (0)