Skip to content

Commit

Permalink
front: introduce Duration type
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Ser <[email protected]>
References: #8816
  • Loading branch information
emersion committed Jan 10, 2025
1 parent 86129e0 commit 400ac86
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 8 deletions.
66 changes: 58 additions & 8 deletions front/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions front/src/utils/duration.ts
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();
}
}

0 comments on commit 400ac86

Please sign in to comment.