|
| 1 | +import { getDateDefaults } from '../dateDefaults' |
| 2 | +import { toZonedDateTime } from '../helpers' |
| 3 | +import { buildFinalFormatter } from '../../formatter/buildFinalFormatter' |
| 4 | +import { buildDateFormatter } from '../../formatter/buildDateFormatter' |
| 5 | +import { buildTimeFormatter } from '../../formatter/buildTimeFormatter' |
| 6 | +import { buildDateTimeFormatter } from '../../formatter/buildDateTimeFormatter' |
| 7 | +import type { DateInput, DateOptions } from '../types' |
| 8 | +import type { |
| 9 | + DateFormatterBuildParams, |
| 10 | + DateTimeFormatterBuildParams, |
| 11 | + TimeFormatterBuildParams, |
| 12 | +} from '../../formatter/shared' |
| 13 | + |
| 14 | +export type FormatType = 'date' | 'time' | 'datetime' |
| 15 | + |
| 16 | +export interface FormatDateOptions extends DateOptions { |
| 17 | + type?: FormatType |
| 18 | + locale?: string | Intl.Locale | Array<string> | Array<Intl.Locale> |
| 19 | + options?: |
| 20 | + | string |
| 21 | + | DateFormatterBuildParams['options'] |
| 22 | + | TimeFormatterBuildParams['options'] |
| 23 | + | DateTimeFormatterBuildParams['options'] |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * format |
| 28 | + * Formats a date/time instance using Intl.DateTimeFormat |
| 29 | + * @param date - The date to format |
| 30 | + * @param formatOptions - Formatting options including type, locale, and Intl.DateTimeFormat options |
| 31 | + */ |
| 32 | +export function format( |
| 33 | + date: DateInput, |
| 34 | + formatOptions?: FormatDateOptions, |
| 35 | +): string { |
| 36 | + const defaults = getDateDefaults() |
| 37 | + const { |
| 38 | + type = 'datetime', |
| 39 | + locale = defaults.locale, |
| 40 | + timeZone = defaults.timeZone, |
| 41 | + calendar = defaults.calendar, |
| 42 | + options, |
| 43 | + } = formatOptions ?? {} |
| 44 | + |
| 45 | + const zdt = toZonedDateTime(date, timeZone, calendar) |
| 46 | + const dateObj = new Date(Number(zdt.epochNanoseconds / 1_000_000n)) |
| 47 | + |
| 48 | + const mergedOptions = |
| 49 | + typeof options === 'string' |
| 50 | + ? options |
| 51 | + : { |
| 52 | + ...options, |
| 53 | + timeZone, |
| 54 | + calendar, |
| 55 | + } |
| 56 | + |
| 57 | + let formatter: Intl.DateTimeFormat |
| 58 | + |
| 59 | + switch (type) { |
| 60 | + case 'date': |
| 61 | + formatter = buildDateFormatter({ |
| 62 | + locale, |
| 63 | + options: mergedOptions as DateFormatterBuildParams['options'], |
| 64 | + }) |
| 65 | + break |
| 66 | + case 'time': |
| 67 | + formatter = buildTimeFormatter({ |
| 68 | + locale, |
| 69 | + options: mergedOptions as TimeFormatterBuildParams['options'], |
| 70 | + }) |
| 71 | + break |
| 72 | + case 'datetime': |
| 73 | + default: |
| 74 | + formatter = buildDateTimeFormatter({ |
| 75 | + locale, |
| 76 | + options: mergedOptions as DateTimeFormatterBuildParams['options'], |
| 77 | + }) |
| 78 | + break |
| 79 | + } |
| 80 | + |
| 81 | + const formatFn = buildFinalFormatter({ |
| 82 | + formatter, |
| 83 | + formatterName: 'format', |
| 84 | + }) |
| 85 | + |
| 86 | + return formatFn(dateObj) |
| 87 | +} |
0 commit comments