UNPKG

1.91 kBPlain TextView Raw
1/**
2 * [seconds, nanoseconds]
3 */
4export declare type HrDuration = [number, number];
5
6export namespace HrTime {
7 export const MS = 1e6;
8 export const SECOND = 1e9;
9 export const MINUTE = 60 * HrTime.SECOND;
10 export const HOUR = 60 * HrTime.MINUTE;
11 export const DAY = 24 * HrTime.HOUR;
12
13 export const past = (interval: HrDuration): Date => new Date(Date.now() - toMs(interval));
14 export const future = (interval: HrDuration): Date => new Date(Date.now() + toMs(interval));
15
16 export const ns = (nanoSeconds: number): HrDuration => [Math.round(nanoSeconds / HrTime.SECOND), Math.round(nanoSeconds % HrTime.SECOND)];
17 export const ms = (milliSeconds: number): HrDuration => ns(milliSeconds * HrTime.MS);
18 export const seconds = (s: number): HrDuration => ns(s * HrTime.SECOND);
19 export const minutes = (m: number): HrDuration => ns(m * HrTime.MINUTE);
20 export const hours = (h: number): HrDuration => ns(h * HrTime.HOUR);
21 export const days = (d: number): HrDuration => ns(d * HrTime.DAY);
22
23 export const wholeMs = (d: HrDuration): number => Math.round(toNs(d) / HrTime.MS);
24 export const wholeSeconds = (d: HrDuration): number => Math.round(toNs(d) / HrTime.SECOND);
25 export const wholeMinutes = (d: HrDuration): number => Math.round(toNs(d) / HrTime.MINUTE);
26 export const wholeHours = (d: HrDuration): number => Math.round(toNs(d) / HrTime.HOUR);
27 export const wholeDays = (d: HrDuration): number => Math.round(toNs(d) / HrTime.DAY);
28
29 export const toNs = (d: HrDuration): number => d[0] * HrTime.SECOND + d[1];
30 export const toMs = (d: HrDuration): number => toNs(d) / HrTime.MS;
31 export const toSeconds = (d: HrDuration): number => toNs(d) / HrTime.SECOND;
32 export const toMinutes = (d: HrDuration): number => toNs(d) / HrTime.MINUTE;
33 export const toHours = (d: HrDuration): number => toNs(d) / HrTime.HOUR;
34 export const toDays = (d: HrDuration): number => toNs(d) / HrTime.DAY;
35}