@@ -73,17 +73,18 @@ const timestampFormatterCache = new Map<string, Intl.DateTimeFormat>();
7373function getTimestampFormatter (
7474 timestampFormat : TimestampFormat ,
7575 includeSeconds : boolean ,
76+ timeZone ?: string ,
7677) : Intl . DateTimeFormat {
77- const cacheKey = `${ timestampFormat } :${ includeSeconds ? "seconds" : "minutes" } ` ;
78+ const cacheKey = `${ timestampFormat } :${ includeSeconds ? "seconds" : "minutes" } : ${ timeZone ?? "" } ` ;
7879 const cachedFormatter = timestampFormatterCache . get ( cacheKey ) ;
7980 if ( cachedFormatter ) {
8081 return cachedFormatter ;
8182 }
8283
83- const formatter = new Intl . DateTimeFormat (
84- timestampLocale ,
85- getTimestampFormatOptions ( timestampFormat , includeSeconds ) ,
86- ) ;
84+ const formatter = new Intl . DateTimeFormat ( timestampLocale , {
85+ ... getTimestampFormatOptions ( timestampFormat , includeSeconds ) ,
86+ ... ( timeZone ? { timeZone } : { } ) ,
87+ } ) ;
8788 timestampFormatterCache . set ( cacheKey , formatter ) ;
8889 return formatter ;
8990}
@@ -136,58 +137,88 @@ export function formatShortTimestamp(isoDate: string, timestampFormat: Timestamp
136137 return getTimestampFormatter ( timestampFormat , false ) . format ( date ) ;
137138}
138139
139- const numericDateFormatter = new Intl . DateTimeFormat ( timestampLocale , {
140- month : "numeric" ,
141- day : "numeric" ,
142- } ) ;
143- const numericDateWithYearFormatter = new Intl . DateTimeFormat ( timestampLocale , {
144- month : "numeric" ,
145- day : "numeric" ,
140+ const numericDateOptions : Intl . DateTimeFormatOptions = { month : "numeric" , day : "numeric" } ;
141+ const numericDateWithYearOptions : Intl . DateTimeFormatOptions = {
142+ ...numericDateOptions ,
146143 year : "numeric" ,
147- } ) ;
144+ } ;
145+ const numericDateFormatter = new Intl . DateTimeFormat ( timestampLocale , numericDateOptions ) ;
146+ const numericDateWithYearFormatter = new Intl . DateTimeFormat (
147+ timestampLocale ,
148+ numericDateWithYearOptions ,
149+ ) ;
150+
151+ interface CalendarDay {
152+ year : number ;
153+ /** Days since the epoch, so differences are whole days across DST shifts. */
154+ index : number ;
155+ }
156+
157+ function calendarDay ( date : Date , timeZone : string | undefined ) : CalendarDay {
158+ if ( ! timeZone ) {
159+ const year = date . getFullYear ( ) ;
160+ return { year, index : Date . UTC ( year , date . getMonth ( ) , date . getDate ( ) ) / 86_400_000 } ;
161+ }
162+ const parts = new Intl . DateTimeFormat ( "en-US" , {
163+ timeZone,
164+ year : "numeric" ,
165+ month : "numeric" ,
166+ day : "numeric" ,
167+ } ) . formatToParts ( date ) ;
168+ const part = ( type : Intl . DateTimeFormatPartTypes ) =>
169+ Number ( parts . find ( ( entry ) => entry . type === type ) ?. value ) ;
170+ const year = part ( "year" ) ;
171+ return { year, index : Date . UTC ( year , part ( "month" ) - 1 , part ( "day" ) ) / 86_400_000 } ;
172+ }
173+
174+ function formatNumericDate ( date : Date , sameYear : boolean , timeZone : string | undefined ) : string {
175+ if ( ! timeZone ) {
176+ return ( sameYear ? numericDateFormatter : numericDateWithYearFormatter ) . format ( date ) ;
177+ }
178+ return new Intl . DateTimeFormat ( timestampLocale , {
179+ ...( sameYear ? numericDateOptions : numericDateWithYearOptions ) ,
180+ timeZone,
181+ } ) . format ( date ) ;
182+ }
148183
149184export function formatDayAwareTimestamp (
150185 isoDate : string ,
151186 timestampFormat : TimestampFormat ,
152187 nowMs : number = Date . now ( ) ,
188+ timeZone ?: string ,
153189) : string {
154190 const date = parseTimestampDate ( isoDate ) ;
155191 if ( ! date ) return "" ;
156- const time = getTimestampFormatter ( timestampFormat , false ) . format ( date ) ;
192+ const time = getTimestampFormatter ( timestampFormat , false , timeZone ) . format ( date ) ;
157193
158- const now = new Date ( nowMs ) ;
159- const startOfToday = new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) ) . getTime ( ) ;
160- const startOfMessageDay = new Date ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) ) . getTime ( ) ;
161- // Round so DST-shifted 23/25 hour days still count as whole days.
162- const dayDiff = Math . round ( ( startOfToday - startOfMessageDay ) / 86_400_000 ) ;
194+ const today = calendarDay ( new Date ( nowMs ) , timeZone ) ;
195+ const messageDay = calendarDay ( date , timeZone ) ;
196+ const dayDiff = today . index - messageDay . index ;
163197
164198 if ( dayDiff <= 0 ) return time ;
165199 if ( dayDiff === 1 ) return `yesterday at ${ time } ` ;
166- const dateFormatter =
167- date . getFullYear ( ) === now . getFullYear ( ) ? numericDateFormatter : numericDateWithYearFormatter ;
168- return `${ dateFormatter . format ( date ) } ${ time } ` ;
200+ return `${ formatNumericDate ( date , messageDay . year === today . year , timeZone ) } ${ time } ` ;
169201}
170202
203+ /** `timeZone` overrides the zone the formatters cached when they were built. */
171204export function formatUpcomingTimestamp (
172205 isoDate : string ,
173206 timestampFormat : TimestampFormat ,
174207 nowMs : number = Date . now ( ) ,
208+ timeZone ?: string ,
175209) : string {
176210 const date = parseTimestampDate ( isoDate ) ;
177211 if ( ! date ) return "" ;
178- const time = getTimestampFormatter ( timestampFormat , false ) . format ( date ) ;
212+ const time = getTimestampFormatter ( timestampFormat , false , timeZone ) . format ( date ) ;
179213
180- const now = new Date ( nowMs ) ;
181- const startOfToday = new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) ) . getTime ( ) ;
182- const startOfTargetDay = new Date ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) ) . getTime ( ) ;
183- const dayDiff = Math . round ( ( startOfTargetDay - startOfToday ) / 86_400_000 ) ;
214+ const today = calendarDay ( new Date ( nowMs ) , timeZone ) ;
215+ const targetDay = calendarDay ( date , timeZone ) ;
216+ const dayDiff = targetDay . index - today . index ;
184217
185- if ( dayDiff < 0 ) return formatDayAwareTimestamp ( isoDate , timestampFormat , nowMs ) ;
218+ if ( dayDiff < 0 ) return formatDayAwareTimestamp ( isoDate , timestampFormat , nowMs , timeZone ) ;
186219 if ( dayDiff === 0 ) return time ;
187220 if ( dayDiff === 1 ) return `tomorrow at ${ time } ` ;
188- const dateFormatter =
189- date . getFullYear ( ) === now . getFullYear ( ) ? numericDateFormatter : numericDateWithYearFormatter ;
190- return `${ dateFormatter . format ( date ) } ${ time } ` ;
221+ return `${ formatNumericDate ( date , targetDay . year === today . year , timeZone ) } ${ time } ` ;
191222}
192223
193224type RelativeTimeParts = { value : string ; suffix : string | null } ;
0 commit comments