File tree Expand file tree Collapse file tree 2 files changed +18
-2
lines changed
Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Original file line number Diff line number Diff 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} )
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments