2021-07-04 20:19:04 -07:00
|
|
|
import { LOCALE } from '../_static/intl.js'
|
|
|
|
import { thunk } from './thunk.js'
|
2020-11-29 14:13:27 -08:00
|
|
|
|
2022-05-14 11:27:32 -07:00
|
|
|
const safeFormatter = (formatter) => {
|
|
|
|
return {
|
|
|
|
format (date) {
|
|
|
|
if (typeof date !== 'number') {
|
|
|
|
return 'intl.never' // null means "never" in Misskey
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return formatter.format(date)
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof RangeError) {
|
|
|
|
// 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 'intl.never'
|
|
|
|
}
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const absoluteDateFormatter = thunk(() => safeFormatter(new Intl.DateTimeFormat(LOCALE, {
|
2019-01-13 15:56:39 -08:00
|
|
|
year: 'numeric',
|
|
|
|
month: 'long',
|
|
|
|
day: 'numeric',
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit'
|
2022-05-14 11:27:32 -07:00
|
|
|
})))
|
2018-08-26 15:38:45 -07:00
|
|
|
|
2022-05-14 11:27:32 -07:00
|
|
|
export const shortAbsoluteDateFormatter = thunk(() => safeFormatter(new Intl.DateTimeFormat(LOCALE, {
|
2019-01-13 15:56:39 -08:00
|
|
|
year: 'numeric',
|
|
|
|
month: 'short',
|
|
|
|
day: 'numeric',
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit'
|
2022-05-14 11:27:32 -07:00
|
|
|
})))
|
2021-03-21 18:03:53 -07:00
|
|
|
|
2022-05-14 11:27:32 -07:00
|
|
|
export const dayOnlyAbsoluteDateFormatter = thunk(() => safeFormatter(new Intl.DateTimeFormat(LOCALE, {
|
2021-03-21 18:03:53 -07:00
|
|
|
year: 'numeric',
|
|
|
|
month: 'short',
|
|
|
|
day: 'numeric'
|
2022-05-14 11:27:32 -07:00
|
|
|
})))
|