-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Simon Ser <[email protected]> References: #8816
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
|
||
import dayjs from 'dayjs'; | ||
|
||
export class Duration { | ||
/** Number of milliseconds */ | ||
readonly ms: number; | ||
|
||
constructor(ms: number) { | ||
this.ms = ms; | ||
} | ||
|
||
static zero = new Duration(0); | ||
|
||
/** Parse an ISO 8601 duration string. */ | ||
static parse(str: string) { | ||
return new Duration(dayjs.duration(str).asMilliseconds()); | ||
} | ||
|
||
/** Subtract two dates. */ | ||
static subtractDate(a: Date, b: Date) { | ||
return new Duration(a.getTime() - b.getTime()); | ||
} | ||
|
||
// Return the number of milliseconds, so that comparison operators work as expected. | ||
valueOf() { | ||
return this.ms; | ||
} | ||
|
||
/** Format this duration as an ISO 8601 string. */ | ||
toString() { | ||
return dayjs.duration(this.ms).toISOString(); | ||
} | ||
} |