-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtime.ts
More file actions
83 lines (75 loc) · 2.97 KB
/
time.ts
File metadata and controls
83 lines (75 loc) · 2.97 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Contains an assortment of helper functions for working with time, dates, and durations.
*/
const durationFormatter = new Intl.RelativeTimeFormat('en', {
numeric: 'auto',
});
// Months and years are approximate
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;
/**
* Converts a number of milliseconds into a human-readable string with units, indicating a relative time in the past or future.
*
* @param relativeTimeMillis The duration in milliseconds. A negative number indicates a duration in the past. And a positive number is
* the future.
* @returns A humanized duration. For example, "in 2 minutes", "2 minutes ago", "yesterday", or "tomorrow".
*/
export function humanizeRelativeTime(relativeTimeMillis?: number) {
if (relativeTimeMillis === undefined) {
return '';
}
if (Math.abs(relativeTimeMillis) < HOUR_IN_MILLIS) {
return durationFormatter.format(Math.floor(relativeTimeMillis / MINUTE_IN_MILLIS), 'minute');
} else if (Math.abs(relativeTimeMillis) < DAY_IN_MILLIS) {
return durationFormatter.format(Math.floor(relativeTimeMillis / HOUR_IN_MILLIS), 'hour');
} else if (Math.abs(relativeTimeMillis) < MONTH_IN_MILLIS) {
return durationFormatter.format(Math.floor(relativeTimeMillis / DAY_IN_MILLIS), 'day');
} else if (Math.abs(relativeTimeMillis) < YEAR_IN_MILLIS) {
return durationFormatter.format(Math.floor(relativeTimeMillis / MONTH_IN_MILLIS), 'month');
} else {
return durationFormatter.format(Math.floor(relativeTimeMillis / YEAR_IN_MILLIS), 'year');
}
}
/**
* Converts a number of milliseconds into a human-readable string with units, indicating an amount of time.
* Negative numbers have no meaning and are considered to be "Less than a minute".
*
* @param millis The number of milliseconds to convert.
* @returns A humanized duration. For example, "2 minutes", "2 hours", "2 days", or "2 months".
*/
export function humanizeUnit(millis?: number): string {
// assume a blank or empty string is a zero
// assume anything less than 0 is a zero
if (!millis || millis < MINUTE_IN_MILLIS) {
return 'Less than a minute';
}
let unit: string;
let unitDiff: number;
if (millis < HOUR_IN_MILLIS) {
unit = 'minute';
unitDiff = Math.floor(millis / MINUTE_IN_MILLIS);
} else if (millis < DAY_IN_MILLIS) {
unit = 'hour';
unitDiff = Math.floor(millis / HOUR_IN_MILLIS);
} else if (millis < MONTH_IN_MILLIS) {
unit = 'day';
unitDiff = Math.floor(millis / DAY_IN_MILLIS);
} else if (millis < YEAR_IN_MILLIS) {
unit = 'month';
unitDiff = Math.floor(millis / MONTH_IN_MILLIS);
} else {
unit = 'year';
unitDiff = Math.floor(millis / YEAR_IN_MILLIS);
}
return createFormatter(unit).format(unitDiff);
}
function createFormatter(unit: string) {
return Intl.NumberFormat('en-US', {
style: 'unit',
unit,
unitDisplay: 'long'
});
}