-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtime.ts
More file actions
69 lines (61 loc) · 2.17 KB
/
time.ts
File metadata and controls
69 lines (61 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Contains an assortment of helper functions for working with time, dates, and durations.
*/
const durationFormatter = new Intl.RelativeTimeFormat('en', {
numeric: 'auto',
});
// All these are approximate, specifically months and years
const MINUTE_IN_MILLIS = 1000 * 60;
const HOUR_IN_MILLIS = 60 * MINUTE_IN_MILLIS;
const DAY_IN_MILLIS = 24 * HOUR_IN_MILLIS;
const MONTH_IN_MILLIS = 30 * DAY_IN_MILLIS;
const YEAR_IN_MILLIS = 365 * DAY_IN_MILLIS;
export function humanizeDuration(diffInMs?: number) {
if (diffInMs === undefined) {
return '';
}
if (Math.abs(diffInMs) < HOUR_IN_MILLIS) {
return durationFormatter.format(Math.floor(diffInMs / MINUTE_IN_MILLIS), 'minute');
} else if (Math.abs(diffInMs) < DAY_IN_MILLIS) {
return durationFormatter.format(Math.floor(diffInMs / HOUR_IN_MILLIS), 'hour');
} else if (Math.abs(diffInMs) < MONTH_IN_MILLIS) {
return durationFormatter.format(Math.floor(diffInMs / DAY_IN_MILLIS), 'day');
} else if (Math.abs(diffInMs) < YEAR_IN_MILLIS) {
return durationFormatter.format(Math.floor(diffInMs / MONTH_IN_MILLIS), 'month');
} else {
return durationFormatter.format(Math.floor(diffInMs / YEAR_IN_MILLIS), 'year');
}
}
function createFormatter(unit: string) {
return Intl.NumberFormat('en-US', {
style: 'unit',
unit,
unitDisplay: 'long'
});
}
export function humanizeUnit(diffInMs?: number): string {
// assume a blank or empty string is a zero
// assume anything less than 0 is a zero
if (!diffInMs || diffInMs < MINUTE_IN_MILLIS) {
return 'Less than a minute';
}
let unit: string;
let unitDiff: number;
if (diffInMs < HOUR_IN_MILLIS) {
unit = 'minute';
unitDiff = Math.floor(diffInMs / MINUTE_IN_MILLIS);
} else if (diffInMs < DAY_IN_MILLIS) {
unit = 'hour';
unitDiff = Math.floor(diffInMs / HOUR_IN_MILLIS);
} else if (diffInMs < MONTH_IN_MILLIS) {
unit = 'day';
unitDiff = Math.floor(diffInMs / DAY_IN_MILLIS);
} else if (diffInMs < YEAR_IN_MILLIS) {
unit = 'month';
unitDiff = Math.floor(diffInMs / MONTH_IN_MILLIS);
} else {
unit = 'year';
unitDiff = Math.floor(diffInMs / YEAR_IN_MILLIS);
}
return createFormatter(unit).format(unitDiff);
}