parent
78687479df
commit
e3a7b783ed
|
@ -1,24 +1,41 @@
|
|||
import { LOCALE } from '../_static/intl.js'
|
||||
import { thunk } from './thunk.js'
|
||||
|
||||
export const absoluteDateFormatter = thunk(() => new Intl.DateTimeFormat(LOCALE, {
|
||||
const safeFormatter = (formatter) => {
|
||||
// The fediverse is wild, so invalid dates may exist. Don't fail with a fatal error in that case.
|
||||
// https://github.com/nolanlawson/pinafore/issues/2113
|
||||
return {
|
||||
format (str) {
|
||||
try {
|
||||
return formatter.format(str)
|
||||
} catch (e) {
|
||||
if (e instanceof RangeError) {
|
||||
return 'N/A'
|
||||
}
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const absoluteDateFormatter = thunk(() => safeFormatter(new Intl.DateTimeFormat(LOCALE, {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
}))
|
||||
})))
|
||||
|
||||
export const shortAbsoluteDateFormatter = thunk(() => new Intl.DateTimeFormat(LOCALE, {
|
||||
export const shortAbsoluteDateFormatter = thunk(() => safeFormatter(new Intl.DateTimeFormat(LOCALE, {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
}))
|
||||
})))
|
||||
|
||||
export const dayOnlyAbsoluteDateFormatter = thunk(() => new Intl.DateTimeFormat(LOCALE, {
|
||||
export const dayOnlyAbsoluteDateFormatter = thunk(() => safeFormatter(new Intl.DateTimeFormat(LOCALE, {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric'
|
||||
}))
|
||||
})))
|
||||
|
|
Loading…
Reference in New Issue