|
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | 5 |
|
6 | | -type DateFormat = ((date: Date) => string) |
7 | | -type DateRangeFormat = ((dates: Date[]) => string) |
8 | | -type LocaleCode = string |
| 6 | +import type { Locale } from 'date-fns/locale' |
9 | 7 |
|
10 | | -export function getDateFormat(locale: LocaleCode): DateFormat { |
11 | | - const formatter = new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }) |
12 | | - return (date) => { |
13 | | - return formatter.format(date) |
| 8 | +type DateFormat = string |
| 9 | + |
| 10 | +/** |
| 11 | + * Provide the format string for dates. |
| 12 | + * |
| 13 | + * @param locale locale to get the format string for |
| 14 | + */ |
| 15 | +export function getDateFormat(locale: Locale): DateFormat { |
| 16 | + if (locale.code.split('-')[0] === 'de') { |
| 17 | + // For german 'P' aka. 'short' is similiar to `{ dateStyle: 'medium' }: Intl.DateTimeFormatOptions` |
| 18 | + // See https://github.com/date-fns/date-fns/blob/v4.1.0/src/locale/de/_lib/formatLong/index.ts#L8-L9 |
| 19 | + return 'P' |
14 | 20 | } |
| 21 | + // Usually 'PP' aka. 'medium' is similiar to `{ dateStyle: 'medium' }: Intl.DateTimeFormatOptions` |
| 22 | + return 'PP' |
15 | 23 | } |
16 | 24 |
|
17 | | -export function getDateRangeFormat(locale: LocaleCode): DateRangeFormat { |
18 | | - const formatter = new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }) |
19 | | - return (dates) => { |
20 | | - return formatter.formatRange(dates[0], dates[1]) |
21 | | - } |
| 25 | +/** |
| 26 | + * Provide the format string for times. |
| 27 | + */ |
| 28 | +export function getTimeFormat(): DateFormat { |
| 29 | + // 'p' aka. 'medium' is similiar to `{ timeStyle: 'medium' }: Intl.DateTimeFormatOptions` |
| 30 | + return 'p' |
22 | 31 | } |
23 | 32 |
|
24 | | -export function getTimeFormat(locale: LocaleCode): DateFormat { |
25 | | - const formatter = new Intl.DateTimeFormat(locale, { timeStyle: 'short' }) |
26 | | - return (date) => { |
27 | | - return formatter.format(date) |
28 | | - } |
| 33 | +/** |
| 34 | + * Provide the format string for date times. |
| 35 | + * |
| 36 | + * @param locale locale to get the format string for |
| 37 | + */ |
| 38 | +export function getDateTimeFormat(locale: Locale): DateFormat { |
| 39 | + return getDateFormat(locale) + getTimeFormat() |
29 | 40 | } |
30 | 41 |
|
31 | | -export function getTimeRangeFormat(locale: LocaleCode): DateRangeFormat { |
32 | | - const formatter = new Intl.DateTimeFormat(locale, { timeStyle: 'short' }) |
33 | | - return (dates) => { |
34 | | - return formatter.formatRange(dates[0], dates[1]) |
35 | | - } |
| 42 | +/** |
| 43 | + * Provide the format string for weeks. |
| 44 | + */ |
| 45 | +export function getWeekFormat(): DateFormat { |
| 46 | + // cannot format weeks with Intl. |
| 47 | + // Do not use RR (or RRR) because what is formated with RR cannot be parsed with RR. |
| 48 | + // e.g. RR formats "2025" to "2025", but expects "25" when parsing. |
| 49 | + return 'RRRR-II' |
36 | 50 | } |
37 | 51 |
|
38 | | -export function getDateTimeFormat(locale: LocaleCode): DateFormat { |
39 | | - const formatter = new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short' }) |
40 | | - return (date) => { |
41 | | - return formatter.format(date) |
42 | | - } |
| 52 | +/** |
| 53 | + * Provide the format string for month. |
| 54 | + * |
| 55 | + * @param locale locale to get the format string for |
| 56 | + */ |
| 57 | +export function getMonthFormat(locale: Locale): DateFormat { |
| 58 | + return guessFormat(locale.code, true) |
43 | 59 | } |
44 | 60 |
|
45 | | -export function getDateTimeRangeFormat(locale: LocaleCode): DateRangeFormat { |
46 | | - const formatter = new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short' }) |
47 | | - return (dates) => { |
48 | | - return formatter.formatRange(dates[0], dates[1]) |
49 | | - } |
| 61 | +/** |
| 62 | + * Provide the format string for year. |
| 63 | + * |
| 64 | + * @param locale locale to get the format string for |
| 65 | + */ |
| 66 | +export function getYearFormat(locale: Locale): DateFormat { |
| 67 | + return guessFormat(locale.code, false) |
50 | 68 | } |
51 | 69 |
|
52 | | -export function getMonthFormat(locale: LocaleCode): DateFormat { |
53 | | - const formatter = new Intl.DateTimeFormat(locale, { year: 'numeric', month: '2-digit' }) |
54 | | - return (date) => { |
55 | | - return formatter.format(date) |
| 70 | +/** |
| 71 | + * Guess the format string for a numeric year with optionally a 2-digit month. |
| 72 | + * |
| 73 | + * @param localeCode locale to guess the format string for |
| 74 | + * @param includeMonth whether to include the month |
| 75 | + */ |
| 76 | +function guessFormat(localeCode: string, includeMonth: boolean): string { |
| 77 | + const sampleDate = new Date(2026, 0, 1) |
| 78 | + const formatOptions: Intl.DateTimeFormatOptions = { |
| 79 | + year: 'numeric', |
| 80 | + // Specify because other calendar systems are not supported |
| 81 | + calendar: 'gregory', |
| 82 | + } |
| 83 | + if (includeMonth) { |
| 84 | + formatOptions.month = '2-digit' |
56 | 85 | } |
| 86 | + // Format extraction using `Intl.DateTime.formatToParts` is inspired from |
| 87 | + // `formatStr` in `@formkit/tempo` and `parseFormatForOpts` in `luxon`. |
| 88 | + const formatter = Intl.DateTimeFormat(localeCode, formatOptions) |
| 89 | + const parts = formatter.formatToParts(sampleDate) |
| 90 | + const partStrings = parts.map((part) => { |
| 91 | + switch (part.type) { |
| 92 | + case 'month': |
| 93 | + return 'MM' |
| 94 | + case 'year': |
| 95 | + return 'yyyy' |
| 96 | + case 'literal': |
| 97 | + return escapeLiteraForFormatString(part.value) |
| 98 | + // Everything else is not expected and supported. |
| 99 | + default: |
| 100 | + return '' |
| 101 | + } |
| 102 | + }) |
| 103 | + return partStrings.join('') |
57 | 104 | } |
58 | 105 |
|
59 | | -export function getYearFormat(locale: LocaleCode): DateFormat { |
60 | | - const formatter = new Intl.DateTimeFormat(locale, { year: 'numeric' }) |
61 | | - return (date) => { |
62 | | - return formatter.format(date) |
63 | | - } |
| 106 | +/** |
| 107 | + * Escape literal value as required for by `date-fns/format`. |
| 108 | + * |
| 109 | + * > The characters wrapped between two single quotes characters (') are escaped. |
| 110 | + * > Two single quotes in a row, whether inside or outside a quoted sequence, |
| 111 | + * > represent a 'real' single quote. (see the last example). |
| 112 | + * https://date-fns.org/v4.4.0/docs/format |
| 113 | + * |
| 114 | + * @param literalValue literal value to be escaped |
| 115 | + */ |
| 116 | +function escapeLiteraForFormatString(literalValue): string { |
| 117 | + // > The characters wrapped between two single quotes characters (') are escaped. |
| 118 | + // > Two single quotes in a row, whether inside or outside a quoted sequence, |
| 119 | + // > represent a 'real' single quote. (see the last example). |
| 120 | + // https://date-fns.org/v4.1.0/docs/format |
| 121 | + literalValue = literalValue.replaceAll(/'/g, '\'\'') |
| 122 | + return literalValue.replaceAll(/[A-Za-z]+/g, (latinCharSeq) => `'${latinCharSeq}'`) |
64 | 123 | } |
0 commit comments