@@ -212,49 +212,53 @@ export default function createFormatter({
212
212
}
213
213
}
214
214
215
- function extractNowDate (
216
- nowOrOptions ?: RelativeTimeFormatOptions [ 'now' ] | RelativeTimeFormatOptions
217
- ) {
218
- if ( nowOrOptions instanceof Date || typeof nowOrOptions === 'number' ) {
219
- return new Date ( nowOrOptions ) ;
220
- }
221
- if ( nowOrOptions ?. now !== undefined ) {
222
- return new Date ( nowOrOptions . now ) ;
223
- }
224
- return getGlobalNow ( ) ;
225
- }
226
-
227
215
function relativeTime (
228
216
/** The date time that needs to be formatted. */
229
217
date : number | Date ,
230
218
/** The reference point in time to which `date` will be formatted in relation to. */
231
219
nowOrOptions ?: RelativeTimeFormatOptions [ 'now' ] | RelativeTimeFormatOptions
232
220
) {
233
221
try {
222
+ let nowDate : Date | undefined ,
223
+ unit : Intl . RelativeTimeFormatUnit | undefined ;
224
+ const opts : Intl . RelativeTimeFormatOptions = { } ;
225
+ if ( nowOrOptions instanceof Date || typeof nowOrOptions === 'number' ) {
226
+ nowDate = new Date ( nowOrOptions ) ;
227
+ } else if ( nowOrOptions ) {
228
+ if ( nowOrOptions . now != null ) {
229
+ nowDate = new Date ( nowOrOptions . now ) ;
230
+ } else {
231
+ nowDate = getGlobalNow ( ) ;
232
+ }
233
+ unit = nowOrOptions . unit ;
234
+ opts . style = nowOrOptions . style ;
235
+ // @ts -expect-error -- Types are slightly outdated
236
+ opts . numberingSystem = nowOrOptions . numberingSystem ;
237
+ }
238
+
239
+ if ( ! nowDate ) {
240
+ nowDate = getGlobalNow ( ) ;
241
+ }
242
+
234
243
const dateDate = new Date ( date ) ;
235
- const nowDate = extractNowDate ( nowOrOptions ) ;
236
244
const seconds = ( dateDate . getTime ( ) - nowDate . getTime ( ) ) / 1000 ;
237
245
238
- const unit =
239
- typeof nowOrOptions === 'number' ||
240
- nowOrOptions instanceof Date ||
241
- nowOrOptions ?. unit === undefined
242
- ? resolveRelativeTimeUnit ( seconds )
243
- : nowOrOptions . unit ;
246
+ if ( ! unit ) {
247
+ unit = resolveRelativeTimeUnit ( seconds ) ;
248
+ }
244
249
245
- const value = calculateRelativeTimeValue ( seconds , unit ) ;
250
+ // `numeric: 'auto'` can theoretically produce output like "yesterday",
251
+ // but it only works with integers. E.g. -1 day will produce "yesterday",
252
+ // but -1.1 days will produce "-1.1 days". Rounding before formatting is
253
+ // not desired, as the given dates might cross a threshold were the
254
+ // output isn't correct anymore. Example: 2024-01-08T23:00:00.000Z and
255
+ // 2024-01-08T01:00:00.000Z would produce "yesterday", which is not the
256
+ // case. By using `always` we can ensure correct output. The only exception
257
+ // is the formatting of times <1 second as "now".
258
+ opts . numeric = unit === 'second' ? 'auto' : 'always' ;
246
259
247
- return new Intl . RelativeTimeFormat ( locale , {
248
- // `numeric: 'auto'` can theoretically produce output like "yesterday",
249
- // but it only works with integers. E.g. -1 day will produce "yesterday",
250
- // but -1.1 days will produce "-1.1 days". Rounding before formatting is
251
- // not desired, as the given dates might cross a threshold were the
252
- // output isn't correct anymore. Example: 2024-01-08T23:00:00.000Z and
253
- // 2024-01-08T01:00:00.000Z would produce "yesterday", which is not the
254
- // case. By using `always` we can ensure correct output. The only exception
255
- // is the formatting of times <1 second as "now".
256
- numeric : unit === 'second' ? 'auto' : 'always'
257
- } ) . format ( value , unit ) ;
260
+ const value = calculateRelativeTimeValue ( seconds , unit ) ;
261
+ return new Intl . RelativeTimeFormat ( locale , opts ) . format ( value , unit ) ;
258
262
} catch ( error ) {
259
263
onError (
260
264
new IntlError ( IntlErrorCode . FORMATTING_ERROR , ( error as Error ) . message )
0 commit comments