100 lines
2.4 KiB
TypeScript
100 lines
2.4 KiB
TypeScript
import { BERLIN_TZ } from '#shared/staffing'
|
||
|
||
const dateFormatter = new Intl.DateTimeFormat('en-GB', {
|
||
timeZone: BERLIN_TZ,
|
||
day: 'numeric',
|
||
month: 'short',
|
||
year: 'numeric',
|
||
})
|
||
|
||
const shortDateFormatter = new Intl.DateTimeFormat('en-GB', {
|
||
timeZone: BERLIN_TZ,
|
||
day: 'numeric',
|
||
month: 'short',
|
||
})
|
||
|
||
const weekdayFormatter = new Intl.DateTimeFormat('en-GB', {
|
||
timeZone: BERLIN_TZ,
|
||
weekday: 'long',
|
||
})
|
||
|
||
const weekdayShortFormatter = new Intl.DateTimeFormat('en-GB', {
|
||
timeZone: BERLIN_TZ,
|
||
weekday: 'short',
|
||
})
|
||
|
||
const dateTimeFormatter = new Intl.DateTimeFormat('en-GB', {
|
||
timeZone: BERLIN_TZ,
|
||
day: 'numeric',
|
||
month: 'short',
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
hourCycle: 'h23',
|
||
})
|
||
|
||
const timeFormatter = new Intl.DateTimeFormat('en-GB', {
|
||
timeZone: BERLIN_TZ,
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
hourCycle: 'h23',
|
||
})
|
||
|
||
const euroFormatter = new Intl.NumberFormat('en-GB', {
|
||
style: 'currency',
|
||
currency: 'EUR',
|
||
maximumFractionDigits: 0,
|
||
})
|
||
|
||
const numberFormatter = new Intl.NumberFormat('en-GB')
|
||
|
||
/** Date-only strings are parsed at noon UTC so the local date never shifts. */
|
||
function toDate(value: string): Date {
|
||
return value.length > 10 ? new Date(value) : new Date(`${value}T12:00:00Z`)
|
||
}
|
||
|
||
function formatWith(formatter: Intl.DateTimeFormat, value: string): string {
|
||
const date = toDate(value)
|
||
return Number.isNaN(date.getTime()) ? '—' : formatter.format(date)
|
||
}
|
||
|
||
export function formatDate(date: string): string {
|
||
return formatWith(dateFormatter, date)
|
||
}
|
||
|
||
export function formatShortDate(date: string): string {
|
||
return formatWith(shortDateFormatter, date)
|
||
}
|
||
|
||
export function formatWeekday(date: string): string {
|
||
return formatWith(weekdayFormatter, date)
|
||
}
|
||
|
||
export function formatWeekdayShort(date: string): string {
|
||
return formatWith(weekdayShortFormatter, date)
|
||
}
|
||
|
||
/** Format a wire timestamp in Heilbronn local time. */
|
||
export function formatDateTime(iso: string): string {
|
||
return formatWith(dateTimeFormatter, iso)
|
||
}
|
||
|
||
export function formatTime(iso: string): string {
|
||
return formatWith(timeFormatter, iso)
|
||
}
|
||
|
||
export function formatEuro(value: number): string {
|
||
return euroFormatter.format(value)
|
||
}
|
||
|
||
export function formatNumber(value: number): string {
|
||
return numberFormatter.format(value)
|
||
}
|
||
|
||
export function formatPercent(value: number, digits = 0): string {
|
||
return `${value.toFixed(digits)}%`
|
||
}
|
||
|
||
export function weekLabel(start: string, end: string): string {
|
||
return `${formatShortDate(start)} – ${formatDate(end)}`
|
||
}
|