( isoString: string, now: Date = new Date(), )
| 14 | * `now` is injectable for tests. |
| 15 | */ |
| 16 | export function formatBriefTimestamp( |
| 17 | isoString: string, |
| 18 | now: Date = new Date(), |
| 19 | ): string { |
| 20 | const d = new Date(isoString) |
| 21 | if (Number.isNaN(d.getTime())) { |
| 22 | return '' |
| 23 | } |
| 24 | |
| 25 | const locale = getLocale() |
| 26 | const dayDiff = startOfDay(now) - startOfDay(d) |
| 27 | const daysAgo = Math.round(dayDiff / 86_400_000) |
| 28 | |
| 29 | if (daysAgo === 0) { |
| 30 | return d.toLocaleTimeString(locale, { |
| 31 | hour: 'numeric', |
| 32 | minute: '2-digit', |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | if (daysAgo > 0 && daysAgo < 7) { |
| 37 | return d.toLocaleString(locale, { |
| 38 | weekday: 'long', |
| 39 | hour: 'numeric', |
| 40 | minute: '2-digit', |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | return d.toLocaleString(locale, { |
| 45 | weekday: 'long', |
| 46 | month: 'short', |
| 47 | day: 'numeric', |
| 48 | hour: 'numeric', |
| 49 | minute: '2-digit', |
| 50 | }) |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Derive a BCP 47 locale tag from POSIX env vars. |
no test coverage detected