Line data Source code
1 2355 : const isoFractionalSecondsPattern = /\.(\d+)(?=Z|[+-]\d{2}:?\d{2}|$)/
2 :
3 : /**
4 : * Parse an ISO 8601 date string tolerantly. Some JavaScript Date parsers reject
5 : * fractional seconds unless they are exactly 3 digits, which turns values like
6 : * "2026-04-18T10:04:55.6Z" or "2026-04-18T10:04:55.6513Z" into Invalid Date.
7 : * This helper normalizes the fractional portion to millisecond precision first.
8 : *
9 : * @returns Parsed Date, or null if the string is missing or unparseable.
10 : */
11 4 : export function parseIsoDateSafe(
12 : input: string | null | undefined
13 : ): Date | null {
14 4 : if (!input) return null
15 1 : const normalized = input.replace(
16 : isoFractionalSecondsPattern,
17 : (_, fractionalSeconds: string) =>
18 : `.${fractionalSeconds.slice(0, 3).padEnd(3, '0')}`
19 : )
20 1 : const date = new Date(normalized)
21 4 : return Number.isNaN(date.getTime()) ? null : date
22 : }
23 :
24 : /**
25 : * Return a local date key in YYYY-MM-DD format for grouping.
26 : *
27 : * @param ts Unix timestamp in milliseconds
28 : * @returns Local date key string
29 : */
30 2355 : export const dateKey = (ts: number): string => {
31 88 : const d = new Date(ts)
32 : const y = d.getFullYear()
33 : const m = String(d.getMonth() + 1).padStart(2, '0')
34 : const day = String(d.getDate()).padStart(2, '0')
35 88 : return `${y}-${m}-${day}`
36 : }
37 :
38 : /**
39 : * Check if a timestamp is on the same local day as today.
40 : *
41 : * @param ts Unix timestamp in milliseconds
42 : * @returns True if today
43 : */
44 2355 : export const isToday = (ts: number): boolean => {
45 24 : const d = new Date(ts)
46 24 : const now = new Date()
47 24 : return (
48 : d.getFullYear() === now.getFullYear() &&
49 : d.getMonth() === now.getMonth() &&
50 : d.getDate() === now.getDate()
51 : )
52 : }
53 :
54 : /**
55 : * Check if a timestamp corresponds to yesterday in local time.
56 : *
57 : * @param ts Unix timestamp in milliseconds
58 : * @returns True if yesterday
59 : */
60 2355 : export const isYesterday = (ts: number): boolean => {
61 23 : const d = new Date(ts)
62 23 : const now = new Date()
63 23 : const yest = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1)
64 23 : return (
65 : d.getFullYear() === yest.getFullYear() &&
66 : d.getMonth() === yest.getMonth() &&
67 : d.getDate() === yest.getDate()
68 : )
69 : }
70 :
71 : /**
72 : * Localized short month + day label, e.g. "Jan 2".
73 : *
74 : * @param ts Unix timestamp in milliseconds
75 : * @param locale BCP-47 locale string
76 : * @returns Localized month/day label
77 : */
78 2355 : export const formatShortMonthDay = (ts: number, locale: string): string => {
79 23 : const d = new Date(ts)
80 23 : return new Intl.DateTimeFormat(locale, {
81 : month: 'short',
82 : day: 'numeric'
83 : }).format(d)
84 : }
85 :
86 : /**
87 : * Localized clock time, e.g. "10:05:06" with the app locale for language and
88 : * the browser/system locale preference for 12/24-hour formatting.
89 : *
90 : * @param ts Unix timestamp in milliseconds
91 : * @param locale BCP-47 locale string
92 : * @param clockPreferenceLocale Optional locale source for hour-cycle preference
93 : * @returns Localized time string
94 : */
95 2355 : export const formatClockTime = (
96 : ts: number,
97 : locale: string,
98 : clockPreferenceLocale?: string
99 : ): string => {
100 9 : const d = new Date(ts)
101 9 : const { hourCycle } = new Intl.DateTimeFormat(clockPreferenceLocale, {
102 : hour: 'numeric'
103 : }).resolvedOptions()
104 9 : return new Intl.DateTimeFormat(locale, {
105 : hour: 'numeric',
106 : minute: '2-digit',
107 : second: '2-digit',
108 : hourCycle
109 : }).format(d)
110 : }
|