-
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
2 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,41 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
|
||
import dayjs from 'dayjs'; | ||
import duration from 'dayjs/plugin/duration'; | ||
|
||
dayjs.extend(duration); | ||
|
||
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. */ | ||
toISOString() { | ||
return dayjs.duration(this.ms).toISOString(); | ||
} | ||
|
||
toJSON() { | ||
return this.toISOString(); | ||
} | ||
} |