|
2 | 2 | * Contains an assortment of helper constants and functions for working with time, dates, and durations. |
3 | 3 | */ |
4 | 4 |
|
5 | | -export const ONE_MINUTE_IN_MS = 1000 * 60; |
| 5 | +export const ONE_SECOND_IN_MS = 1000; |
| 6 | +export const ONE_MINUTE_IN_MS = ONE_SECOND_IN_MS * 60; |
6 | 7 | export const ONE_HOUR_IN_MS = ONE_MINUTE_IN_MS * 60; |
7 | 8 | export const TWO_HOURS_IN_MS = ONE_HOUR_IN_MS * 2; |
8 | 9 | export const THREE_HOURS_IN_MS = ONE_HOUR_IN_MS * 3; |
@@ -43,20 +44,23 @@ export function humanizeRelativeTime(relativeTimeMillis?: number) { |
43 | 44 |
|
44 | 45 | /** |
45 | 46 | * Converts a number of milliseconds into a human-readable string with units, indicating an amount of time. |
46 | | - * Negative numbers have no meaning and are considered to be "Less than a minute". |
| 47 | + * Negative numbers have no meaning and are considered to be "Less than a second". |
47 | 48 | * |
48 | 49 | * @param millis The number of milliseconds to convert. |
49 | | - * @returns A humanized duration. For example, "2 minutes", "2 hours", "2 days", or "2 months". |
| 50 | + * @returns A humanized duration. For example, "2 seconds", "2 minutes", "2 hours", "2 days", or "2 months". |
50 | 51 | */ |
51 | 52 | export function humanizeUnit(millis?: number): string { |
52 | 53 | // assume a blank or empty string is a zero |
53 | 54 | // assume anything less than 0 is a zero |
54 | | - if (!millis || millis < ONE_MINUTE_IN_MS) { |
55 | | - return 'Less than a minute'; |
| 55 | + if (!millis || millis < ONE_SECOND_IN_MS) { |
| 56 | + return 'Less than a second'; |
56 | 57 | } |
57 | 58 | let unit: string; |
58 | 59 | let unitDiff: number; |
59 | | - if (millis < ONE_HOUR_IN_MS) { |
| 60 | + if (millis < ONE_MINUTE_IN_MS) { |
| 61 | + unit = 'second'; |
| 62 | + unitDiff = Math.floor(millis / ONE_SECOND_IN_MS); |
| 63 | + } else if (millis < ONE_HOUR_IN_MS) { |
60 | 64 | unit = 'minute'; |
61 | 65 | unitDiff = Math.floor(millis / ONE_MINUTE_IN_MS); |
62 | 66 | } else if (millis < ONE_DAY_IN_MS) { |
|
0 commit comments