1 | export declare type Duration = number;
|
2 |
|
3 | export namespace Time {
|
4 | export const SECOND = 1000;
|
5 | export const MINUTE = 60 * Time.SECOND;
|
6 | export const HOUR = 60 * Time.MINUTE;
|
7 | export const DAY = 24 * Time.HOUR;
|
8 |
|
9 | export const past = (interval: Duration): Date => new Date(Date.now() - toMs(interval));
|
10 | export const future = (interval: Duration): Date => new Date(Date.now() + toMs(interval));
|
11 |
|
12 | export const ms = (milliSeconds: number): Duration => milliSeconds;
|
13 | export const seconds = (s: number): Duration => s * Time.SECOND;
|
14 | export const minutes = (m: number): Duration => m * Time.MINUTE;
|
15 | export const hours = (h: number): Duration => h * Time.HOUR;
|
16 | export const days = (d: number): Duration => d * Time.DAY;
|
17 |
|
18 | export const wholeMs = (d: Duration): number => Math.round(toMs(d));
|
19 | export const wholeSeconds = (d: Duration): number => Math.round(toMs(d) / Time.SECOND);
|
20 | export const wholeMinutes = (d: Duration): number => Math.round(toMs(d) / Time.MINUTE);
|
21 | export const wholeHours = (d: Duration): number => Math.round(toMs(d) / Time.HOUR);
|
22 | export const wholeDays = (d: Duration): number => Math.round(toMs(d) / Time.DAY);
|
23 |
|
24 | export const toMs = (d: Duration): number => d;
|
25 | export const toSeconds = (d: Duration): number => toMs(d) / Time.SECOND;
|
26 | export const toMinutes = (d: Duration): number => toMs(d) / Time.MINUTE;
|
27 | export const toHours = (d: Duration): number => toMs(d) / Time.HOUR;
|
28 | export const toDays = (d: Duration): number => toMs(d) / Time.DAY;
|
29 | }
|