@day-kit/date-engine 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +9 -2
- package/dist/create-temporal-engine-CJDDY96Y.js +451 -0
- package/dist/create-temporal-engine-CJDDY96Y.js.map +1 -0
- package/dist/date-engine-CaLe_SPQ.d.ts +235 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/instant-h91-6kZL.js +169 -0
- package/dist/instant-h91-6kZL.js.map +1 -0
- package/dist/temporal-gregorian.d.ts +6 -0
- package/dist/temporal-gregorian.js +16 -0
- package/dist/temporal-gregorian.js.map +1 -0
- package/dist/temporal.d.ts +6 -0
- package/dist/temporal.js +16 -0
- package/dist/temporal.js.map +1 -0
- package/package.json +52 -3
- package/src/contracts/calendar-system.ts +48 -0
- package/src/contracts/date-engine.ts +140 -0
- package/src/contracts/date-formatter.ts +47 -0
- package/src/contracts/date-types.ts +144 -0
- package/src/contracts/errors.ts +104 -0
- package/src/formatting/date-style-fallback.ts +27 -0
- package/src/formatting/intl-formatters.ts +138 -0
- package/src/formatting/locale-info.ts +73 -0
- package/src/index.ts +72 -0
- package/src/operations/instant.ts +49 -0
- package/src/operations/interval.ts +45 -0
- package/src/temporal/create-temporal-engine.ts +437 -0
- package/src/temporal/full-engine.ts +11 -0
- package/src/temporal/gregorian-engine.ts +11 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { CalendarSystem } from '../contracts/calendar-system'
|
|
2
|
+
import type {
|
|
3
|
+
CalendarDateFormatOptions,
|
|
4
|
+
CalendarDateFormatter,
|
|
5
|
+
CalendarDateFormatterOptions,
|
|
6
|
+
InstantFormatter,
|
|
7
|
+
InstantFormatterOptions,
|
|
8
|
+
ResolvedFormatInfo,
|
|
9
|
+
} from '../contracts/date-formatter'
|
|
10
|
+
import type { CalendarDate, IsoDateFields } from '../contracts/date-types'
|
|
11
|
+
import { FormattingNotSupportedError } from '../contracts/errors'
|
|
12
|
+
import { hasUnexpectedTimeParts, withoutDateStyle } from './date-style-fallback'
|
|
13
|
+
|
|
14
|
+
/** The subset of a date engine that Intl-based formatting depends on. */
|
|
15
|
+
export interface IntlFormattingHost {
|
|
16
|
+
readonly name: string
|
|
17
|
+
readonly requireCalendarSystem: (calendarSystem: string) => CalendarSystem
|
|
18
|
+
readonly canonicalizeTimeZone: (timeZone: string) => string
|
|
19
|
+
readonly toIsoDate: (date: CalendarDate) => IsoDateFields
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface IntlFormatters {
|
|
23
|
+
readonly createCalendarDateFormatter: (
|
|
24
|
+
options: CalendarDateFormatterOptions,
|
|
25
|
+
) => CalendarDateFormatter
|
|
26
|
+
readonly createInstantFormatter: (options: InstantFormatterOptions) => InstantFormatter
|
|
27
|
+
readonly isFormattingSupported: (calendarSystem: CalendarSystem) => boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const maxCachedFormatters = 256
|
|
31
|
+
/** A fixed date used to validate formatter patterns. */
|
|
32
|
+
const probeDate = new Date(Date.UTC(2024, 0, 15, 12))
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Noon UTC of an ISO date. Formatting a calendar date at noon UTC with `timeZone: 'UTC'`
|
|
36
|
+
* makes labels independent of the configured time zone, including skipped civil days.
|
|
37
|
+
*/
|
|
38
|
+
function isoNoonUtc(iso: IsoDateFields): Date {
|
|
39
|
+
const date = new Date(0)
|
|
40
|
+
date.setUTCFullYear(iso.year, iso.month - 1, iso.day)
|
|
41
|
+
date.setUTCHours(12, 0, 0, 0)
|
|
42
|
+
return date
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function stableKey(value: object): string {
|
|
46
|
+
const entries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b, 'en'))
|
|
47
|
+
return JSON.stringify(entries)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Creates cached Intl formatters bound to an engine.
|
|
52
|
+
* Throws `FormattingNotSupportedError` when Intl cannot format the requested calendar:
|
|
53
|
+
* labels of another calendar are never shown as if they belonged to it.
|
|
54
|
+
*/
|
|
55
|
+
export function createIntlFormatters(host: IntlFormattingHost): IntlFormatters {
|
|
56
|
+
const cache = new Map<string, Intl.DateTimeFormat>()
|
|
57
|
+
const support = new Map<CalendarSystem, boolean>()
|
|
58
|
+
|
|
59
|
+
function getDateTimeFormat(
|
|
60
|
+
locale: string | undefined,
|
|
61
|
+
options: Intl.DateTimeFormatOptions & { calendar: CalendarSystem; timeZone: string },
|
|
62
|
+
): Intl.DateTimeFormat {
|
|
63
|
+
const key = `${locale ?? ''}|${stableKey(options)}`
|
|
64
|
+
const cached = cache.get(key)
|
|
65
|
+
if (cached !== undefined) return cached
|
|
66
|
+
const format = new Intl.DateTimeFormat(locale, options)
|
|
67
|
+
const resolvedCalendar = format.resolvedOptions().calendar
|
|
68
|
+
if (resolvedCalendar !== options.calendar) {
|
|
69
|
+
throw new FormattingNotSupportedError(
|
|
70
|
+
`Intl.DateTimeFormat in this runtime cannot format the "${options.calendar}" calendar ` +
|
|
71
|
+
`(it resolved to "${resolvedCalendar}").`,
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
if (cache.size >= maxCachedFormatters) cache.clear()
|
|
75
|
+
cache.set(key, format)
|
|
76
|
+
return format
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function resolvedInfo(
|
|
80
|
+
format: Intl.DateTimeFormat,
|
|
81
|
+
calendarSystem: CalendarSystem,
|
|
82
|
+
): ResolvedFormatInfo {
|
|
83
|
+
const resolved = format.resolvedOptions()
|
|
84
|
+
return { locale: resolved.locale, calendarSystem, numberingSystem: resolved.numberingSystem }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
isFormattingSupported(calendarSystem) {
|
|
89
|
+
const known = support.get(calendarSystem)
|
|
90
|
+
if (known !== undefined) return known
|
|
91
|
+
const supported =
|
|
92
|
+
new Intl.DateTimeFormat('en', { calendar: calendarSystem }).resolvedOptions().calendar ===
|
|
93
|
+
calendarSystem
|
|
94
|
+
support.set(calendarSystem, supported)
|
|
95
|
+
return supported
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
createCalendarDateFormatter(options) {
|
|
99
|
+
const calendarSystem = host.requireCalendarSystem(options.calendarSystem)
|
|
100
|
+
const create = (format: CalendarDateFormatOptions) =>
|
|
101
|
+
getDateTimeFormat(options.locale, { ...format, calendar: calendarSystem, timeZone: 'UTC' })
|
|
102
|
+
let format = create(options.format)
|
|
103
|
+
if (hasUnexpectedTimeParts(format.formatToParts(probeDate))) {
|
|
104
|
+
// The runtime lacks this style pattern for the locale and calendar: use components.
|
|
105
|
+
format = create(withoutDateStyle(options.format))
|
|
106
|
+
if (hasUnexpectedTimeParts(format.formatToParts(probeDate))) {
|
|
107
|
+
throw new FormattingNotSupportedError(
|
|
108
|
+
`Intl.DateTimeFormat in this runtime cannot format "${calendarSystem}" dates for ` +
|
|
109
|
+
`locale "${options.locale ?? 'default'}" with ${JSON.stringify(options.format)}.`,
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
resolved: resolvedInfo(format, calendarSystem),
|
|
115
|
+
format: (date) => format.format(isoNoonUtc(host.toIsoDate(date))),
|
|
116
|
+
formatToParts: (date) => format.formatToParts(isoNoonUtc(host.toIsoDate(date))),
|
|
117
|
+
formatRange: (start, end) =>
|
|
118
|
+
format.formatRange(isoNoonUtc(host.toIsoDate(start)), isoNoonUtc(host.toIsoDate(end))),
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
createInstantFormatter(options) {
|
|
123
|
+
const calendarSystem = host.requireCalendarSystem(options.calendarSystem)
|
|
124
|
+
const timeZone = host.canonicalizeTimeZone(options.timeZone)
|
|
125
|
+
const format = getDateTimeFormat(options.locale, {
|
|
126
|
+
...options.format,
|
|
127
|
+
calendar: calendarSystem,
|
|
128
|
+
timeZone,
|
|
129
|
+
})
|
|
130
|
+
return {
|
|
131
|
+
resolved: resolvedInfo(format, calendarSystem),
|
|
132
|
+
format: (instant) => format.format(instant),
|
|
133
|
+
formatToParts: (instant) => format.formatToParts(instant),
|
|
134
|
+
formatRange: (start, end) => format.formatRange(start, end),
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { Weekday } from '../contracts/date-types'
|
|
2
|
+
|
|
3
|
+
export interface LocaleWeekInfo {
|
|
4
|
+
readonly firstDay: Weekday
|
|
5
|
+
readonly weekend: readonly Weekday[]
|
|
6
|
+
/** Minimal days in the first week; undefined when the runtime does not provide it. */
|
|
7
|
+
readonly minimalDays?: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type TextDirection = 'ltr' | 'rtl'
|
|
11
|
+
|
|
12
|
+
interface RawWeekInfo {
|
|
13
|
+
readonly firstDay: number
|
|
14
|
+
readonly weekend: readonly number[]
|
|
15
|
+
readonly minimalDays?: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** `Intl.Locale` with week and text info, which TypeScript's standard library does not declare yet. */
|
|
19
|
+
interface LocaleWithInfo {
|
|
20
|
+
readonly baseName: string
|
|
21
|
+
getWeekInfo?: () => RawWeekInfo
|
|
22
|
+
weekInfo?: RawWeekInfo
|
|
23
|
+
getTextInfo?: () => { direction?: string }
|
|
24
|
+
textInfo?: { direction?: string }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Converts Intl/ISO weekday numbering (1 = Monday … 7 = Sunday) to the public convention. */
|
|
28
|
+
export function weekdayFromIsoNumber(isoWeekday: number): Weekday {
|
|
29
|
+
switch (isoWeekday) {
|
|
30
|
+
case 7:
|
|
31
|
+
return 0
|
|
32
|
+
case 1:
|
|
33
|
+
return 1
|
|
34
|
+
case 2:
|
|
35
|
+
return 2
|
|
36
|
+
case 3:
|
|
37
|
+
return 3
|
|
38
|
+
case 4:
|
|
39
|
+
return 4
|
|
40
|
+
case 5:
|
|
41
|
+
return 5
|
|
42
|
+
case 6:
|
|
43
|
+
return 6
|
|
44
|
+
default:
|
|
45
|
+
throw new RangeError(`Invalid ISO weekday number: ${isoWeekday}`)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function localeInfo(locale: string): LocaleWithInfo {
|
|
50
|
+
return new Intl.Locale(locale)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Week conventions of a locale from `Intl.Locale` week info.
|
|
55
|
+
* Returns undefined when the runtime does not expose week info; callers choose an explicit fallback.
|
|
56
|
+
*/
|
|
57
|
+
export function getLocaleWeekInfo(locale: string): LocaleWeekInfo | undefined {
|
|
58
|
+
const info = localeInfo(locale)
|
|
59
|
+
const raw = info.getWeekInfo?.() ?? info.weekInfo
|
|
60
|
+
if (raw === undefined) return undefined
|
|
61
|
+
const base = {
|
|
62
|
+
firstDay: weekdayFromIsoNumber(raw.firstDay),
|
|
63
|
+
weekend: raw.weekend.map(weekdayFromIsoNumber),
|
|
64
|
+
}
|
|
65
|
+
return raw.minimalDays === undefined ? base : { ...base, minimalDays: raw.minimalDays }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Text direction of a locale, or undefined when the runtime does not expose it. */
|
|
69
|
+
export function getLocaleTextDirection(locale: string): TextDirection | undefined {
|
|
70
|
+
const info = localeInfo(locale)
|
|
71
|
+
const direction = (info.getTextInfo?.() ?? info.textInfo)?.direction
|
|
72
|
+
return direction === 'rtl' || direction === 'ltr' ? direction : undefined
|
|
73
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export {
|
|
2
|
+
isKnownCalendarSystem,
|
|
3
|
+
knownCalendarSystems,
|
|
4
|
+
type CalendarSystem,
|
|
5
|
+
} from './contracts/calendar-system'
|
|
6
|
+
export type { DateEngine, IsoWeek } from './contracts/date-engine'
|
|
7
|
+
export type {
|
|
8
|
+
CalendarDateFormatOptions,
|
|
9
|
+
CalendarDateFormatter,
|
|
10
|
+
CalendarDateFormatterOptions,
|
|
11
|
+
InstantFormatOptions,
|
|
12
|
+
InstantFormatter,
|
|
13
|
+
InstantFormatterOptions,
|
|
14
|
+
ResolvedFormatInfo,
|
|
15
|
+
} from './contracts/date-formatter'
|
|
16
|
+
export type {
|
|
17
|
+
CalendarDate,
|
|
18
|
+
CalendarDateChange,
|
|
19
|
+
CalendarDateFields,
|
|
20
|
+
CalendarDateInput,
|
|
21
|
+
CalendarDuration,
|
|
22
|
+
CalendarPeriod,
|
|
23
|
+
CalendarPeriodUnit,
|
|
24
|
+
DayStart,
|
|
25
|
+
Disambiguation,
|
|
26
|
+
Instant,
|
|
27
|
+
InstantInterval,
|
|
28
|
+
IsoDateFields,
|
|
29
|
+
LocalDateTime,
|
|
30
|
+
LocalTimeResolution,
|
|
31
|
+
Overflow,
|
|
32
|
+
WallClockTime,
|
|
33
|
+
Weekday,
|
|
34
|
+
ZonedDateTimeFields,
|
|
35
|
+
} from './contracts/date-types'
|
|
36
|
+
export {
|
|
37
|
+
AmbiguousLocalTimeError,
|
|
38
|
+
CalendarConfigurationError,
|
|
39
|
+
CalendarError,
|
|
40
|
+
CalendarSystemNotSupportedError,
|
|
41
|
+
DateRangeError,
|
|
42
|
+
FormattingNotSupportedError,
|
|
43
|
+
InvalidCalendarFieldsError,
|
|
44
|
+
InvalidDateError,
|
|
45
|
+
InvalidTimeZoneError,
|
|
46
|
+
NonexistentLocalTimeError,
|
|
47
|
+
type CalendarErrorCode,
|
|
48
|
+
} from './contracts/errors'
|
|
49
|
+
export {
|
|
50
|
+
getLocaleTextDirection,
|
|
51
|
+
getLocaleWeekInfo,
|
|
52
|
+
weekdayFromIsoNumber,
|
|
53
|
+
type LocaleWeekInfo,
|
|
54
|
+
type TextDirection,
|
|
55
|
+
} from './formatting/locale-info'
|
|
56
|
+
export {
|
|
57
|
+
addMilliseconds,
|
|
58
|
+
compareInstants,
|
|
59
|
+
dateFromInstant,
|
|
60
|
+
instantFromDate,
|
|
61
|
+
instantFromEpochMilliseconds,
|
|
62
|
+
isValidDate,
|
|
63
|
+
maxEpochMilliseconds,
|
|
64
|
+
} from './operations/instant'
|
|
65
|
+
export {
|
|
66
|
+
createInterval,
|
|
67
|
+
intersectIntervals,
|
|
68
|
+
intervalContains,
|
|
69
|
+
intervalDuration,
|
|
70
|
+
intervalsOverlap,
|
|
71
|
+
isEmptyInterval,
|
|
72
|
+
} from './operations/interval'
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Instant } from '../contracts/date-types'
|
|
2
|
+
import { DateRangeError, InvalidDateError } from '../contracts/errors'
|
|
3
|
+
|
|
4
|
+
/** The largest absolute epoch-millisecond value representable by `Date`. */
|
|
5
|
+
export const maxEpochMilliseconds = 8.64e15
|
|
6
|
+
|
|
7
|
+
export function isValidDate(value: unknown): value is Date {
|
|
8
|
+
return value instanceof Date && !Number.isNaN(value.getTime())
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Converts a native Date into an instant. Never mutates or retains the Date.
|
|
13
|
+
* Throws `InvalidDateError` for non-Date values and Invalid Date.
|
|
14
|
+
*/
|
|
15
|
+
export function instantFromDate(date: Date, label = 'Date'): Instant {
|
|
16
|
+
if (!isValidDate(date)) throw new InvalidDateError(label)
|
|
17
|
+
return date.getTime() as Instant
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Validates epoch milliseconds and brands them as an instant. */
|
|
21
|
+
export function instantFromEpochMilliseconds(epochMilliseconds: number): Instant {
|
|
22
|
+
if (!Number.isInteger(epochMilliseconds)) {
|
|
23
|
+
throw new DateRangeError(
|
|
24
|
+
`Epoch milliseconds must be an integer, received ${String(epochMilliseconds)}.`,
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
if (Math.abs(epochMilliseconds) > maxEpochMilliseconds) {
|
|
28
|
+
throw new DateRangeError(
|
|
29
|
+
`Epoch milliseconds ${epochMilliseconds} is outside the supported range.`,
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
return epochMilliseconds as Instant
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Creates a new Date for an instant. Each call returns a fresh object. */
|
|
36
|
+
export function dateFromInstant(instant: Instant): Date {
|
|
37
|
+
return new Date(instant)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Adds elapsed (not wall-clock) milliseconds. */
|
|
41
|
+
export function addMilliseconds(instant: Instant, milliseconds: number): Instant {
|
|
42
|
+
return instantFromEpochMilliseconds(instant + milliseconds)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function compareInstants(a: Instant, b: Instant): -1 | 0 | 1 {
|
|
46
|
+
if (a < b) return -1
|
|
47
|
+
if (a > b) return 1
|
|
48
|
+
return 0
|
|
49
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Instant, InstantInterval } from '../contracts/date-types'
|
|
2
|
+
import { DateRangeError } from '../contracts/errors'
|
|
3
|
+
|
|
4
|
+
export function createInterval(start: Instant, end: Instant): InstantInterval {
|
|
5
|
+
if (end < start) {
|
|
6
|
+
throw new DateRangeError(`Interval end (${end}) is before its start (${start}).`)
|
|
7
|
+
}
|
|
8
|
+
return { start, end }
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Elapsed milliseconds of a half-open interval. */
|
|
12
|
+
export function intervalDuration(interval: InstantInterval): number {
|
|
13
|
+
return interval.end - interval.start
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isEmptyInterval(interval: InstantInterval): boolean {
|
|
17
|
+
return interval.end === interval.start
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Whether two half-open intervals share at least one instant.
|
|
22
|
+
* Adjacent intervals (`a.end === b.start`) do not overlap.
|
|
23
|
+
* A zero-duration interval overlaps an interval that contains its instant.
|
|
24
|
+
*/
|
|
25
|
+
export function intervalsOverlap(a: InstantInterval, b: InstantInterval): boolean {
|
|
26
|
+
if (isEmptyInterval(a)) return intervalContains(b, a.start)
|
|
27
|
+
if (isEmptyInterval(b)) return intervalContains(a, b.start)
|
|
28
|
+
return a.start < b.end && b.start < a.end
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The common part of two intervals, or `undefined` when they do not overlap. */
|
|
32
|
+
export function intersectIntervals(
|
|
33
|
+
a: InstantInterval,
|
|
34
|
+
b: InstantInterval,
|
|
35
|
+
): InstantInterval | undefined {
|
|
36
|
+
if (!intervalsOverlap(a, b)) return undefined
|
|
37
|
+
const start = a.start > b.start ? a.start : b.start
|
|
38
|
+
const end = a.end < b.end ? a.end : b.end
|
|
39
|
+
return { start, end: end < start ? start : end }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Whether `instant` lies in `[start, end)`. */
|
|
43
|
+
export function intervalContains(interval: InstantInterval, instant: Instant): boolean {
|
|
44
|
+
return instant >= interval.start && instant < interval.end
|
|
45
|
+
}
|