@c9up/chronos 0.1.16 → 0.1.17
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/README.md +81 -8
- package/dist/DateTime.d.ts +64 -4
- package/dist/DateTime.d.ts.map +1 -1
- package/dist/DateTime.js +314 -32
- package/dist/DateTime.js.map +1 -1
- package/dist/format.d.ts +10 -0
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +5 -5
- package/dist/format.js.map +1 -1
- package/dist/holidays.d.ts +43 -0
- package/dist/holidays.d.ts.map +1 -0
- package/dist/holidays.js +102 -0
- package/dist/holidays.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/native/generated.d.ts +9 -0
- package/dist/native/generated.d.ts.map +1 -1
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +3 -0
- package/dist/native.js.map +1 -1
- package/dist/parse.d.ts +59 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +404 -0
- package/dist/parse.js.map +1 -0
- package/dist/rrule.d.ts +10 -2
- package/dist/rrule.d.ts.map +1 -1
- package/dist/rrule.js +33 -7
- package/dist/rrule.js.map +1 -1
- package/index.darwin-arm64.node +0 -0
- package/index.darwin-x64.node +0 -0
- package/index.linux-arm64-gnu.node +0 -0
- package/index.linux-x64-gnu.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +2 -3
- package/src/DateTime.ts +448 -32
- package/src/format.ts +6 -6
- package/src/holidays.ts +161 -0
- package/src/index.ts +18 -0
- package/src/native/generated.ts +21 -0
- package/src/native.ts +4 -0
- package/src/parse.ts +559 -0
- package/src/rrule.ts +47 -8
- package/wasm/chronos_engine_wasm.d.ts +9 -0
- package/wasm/chronos_engine_wasm.js +61 -0
- package/wasm/chronos_engine_wasm_bg.wasm +0 -0
- package/wasm/chronos_engine_wasm_bg.wasm.d.ts +3 -0
- package/dist/utils.d.ts +0 -15
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -17
- package/dist/utils.js.map +0 -1
- package/scripts/build-napi-types.mjs +0 -69
- package/scripts/clean-wasm-pack-meta.mjs +0 -32
- package/scripts/copy-napi.mjs +0 -56
- package/scripts/generate-napi-types.mjs +0 -156
- package/scripts/verify-napi.mjs +0 -46
- package/scripts/verify-wasm.mjs +0 -55
- package/src/utils.ts +0 -17
package/README.md
CHANGED
|
@@ -40,13 +40,27 @@ d > DateTime.from('2026-01-01') // true — valueOf is the epoch ms
|
|
|
40
40
|
Built from whatever you have:
|
|
41
41
|
`from` · `now` · `local` · `utc` · `fromISO` · `fromMillis` · `fromSeconds` ·
|
|
42
42
|
`fromUnix` · `fromJSDate` · `fromObject` · `fromRFC2822` · `fromSQL` ·
|
|
43
|
-
`fromHTTP`. `fromISO`, `fromObject
|
|
44
|
-
without an offset is then a wall clock in
|
|
45
|
-
host's zone).
|
|
43
|
+
`fromHTTP` · `fromFormat`. `fromISO`, `fromObject`, `fromSQL` and
|
|
44
|
+
`fromFormat` take `{ zone }`: a time without an offset is then a wall clock in
|
|
45
|
+
that zone (UTC otherwise — never the host's zone).
|
|
46
|
+
|
|
47
|
+
`fromFormat` reads the tokens `toFormat` writes, names in `{ locale }` (en-US
|
|
48
|
+
by default); units the format leaves out come from now for the larger ones and
|
|
49
|
+
start at their minimum for the smaller ones:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
DateTime.fromFormat('10/08/2026 09:30', 'dd/MM/yyyy HH:mm', { zone: 'Europe/Paris' })
|
|
53
|
+
DateTime.fromFormat('10 août 2026', 'd MMMM yyyy', { locale: 'fr' })
|
|
54
|
+
DateTime.fromFormat('14:30', 'HH:mm') // today at 14:30 UTC
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Text that does not match the format, or names a date that does not exist,
|
|
58
|
+
throws.
|
|
46
59
|
|
|
47
60
|
Read out as `toISO` · `toISODate` · `toISOTime` · `toSQL` · `toSQLDate` ·
|
|
48
61
|
`toSQLTime` · `toZonedISO` · `toDate` · `toMillis` · `toSeconds` · `toFormat` ·
|
|
49
|
-
`format` · `toLocaleString` · `toRelative
|
|
62
|
+
`format` · `toLocaleString` · `toRelative` ("1 day ago") · `toRelativeCalendar`
|
|
63
|
+
("yesterday"), compared with `equals` · `isBefore` ·
|
|
50
64
|
`isAfter` · `isSameDay` · `hasSame` · `isWithin` (or `<`, `>`), and moved with
|
|
51
65
|
`plus` · `minus` · `set` · `startOf` · `endOf` · `setZone` (`keepLocalTime`) ·
|
|
52
66
|
`setLocale` · `toUTC`.
|
|
@@ -171,10 +185,69 @@ expandRRule('2026-03-23T08:00:00Z', 'FREQ=WEEKLY;COUNT=2', { tzid: 'Europe/Paris
|
|
|
171
185
|
expandRRule('DTSTART;TZID=Europe/Paris:20260323T090000\nRRULE:FREQ=WEEKLY;COUNT=2')
|
|
172
186
|
```
|
|
173
187
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
188
|
+
### Exceptions: EXDATE and RDATE
|
|
189
|
+
|
|
190
|
+
A series without some of its occurrences, or with a few more, as RFC 5545
|
|
191
|
+
§3.8.5 sets it: the rule's occurrences and the RDATEs, without the EXDATEs.
|
|
192
|
+
COUNT and UNTIL bound the rule alone — an EXDATE does not give its place back.
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
// A Monday slot, not on the 28th, once on Wednesday the 30th instead.
|
|
196
|
+
expandRRule(
|
|
197
|
+
'DTSTART;TZID=Europe/Zurich:20261214T090000\n' +
|
|
198
|
+
'RRULE:FREQ=WEEKLY;BYDAY=MO;COUNT=4\n' +
|
|
199
|
+
'EXDATE;TZID=Europe/Zurich:20261228T090000\n' +
|
|
200
|
+
'RDATE;TZID=Europe/Zurich:20261230T140000',
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
// The same, as options: instants or DateTimes.
|
|
204
|
+
expandRRule(start, { freq: 'WEEKLY', count: 3 }, { exdates: ['2026-12-21T09:00:00+01:00'], rdates: [extra] })
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
A floating value is read on its line's TZID, else on the rule's zone; a `Z`
|
|
208
|
+
value is the instant it names. Date-times only: `VALUE=DATE` would name days
|
|
209
|
+
of a series of times, which RFC 5545 forbids, and an RDATE `PERIOD` is not an
|
|
210
|
+
occurrence — both are refused rather than guessed at.
|
|
211
|
+
|
|
212
|
+
A rule from user input or an imported .ics is safe to expand: the rule is
|
|
213
|
+
capped at 8 KB and the whole text, exceptions included, at 256 KB; EXDATE and
|
|
214
|
+
RDATE name at most 10 000 date-times; BY* lists are sets, at most 10 000
|
|
215
|
+
occurrences come back, and iteration ends at year 9999, so an unsatisfiable
|
|
216
|
+
rule answers `[]` in milliseconds. Unknown properties and malformed parts are
|
|
217
|
+
refused.
|
|
218
|
+
|
|
219
|
+
## Public holidays and Easter
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
import { easterDate, holidays, isHoliday } from '@c9up/chronos'
|
|
223
|
+
|
|
224
|
+
easterDate(2026) // '2026-04-05'
|
|
225
|
+
holidays(2026, { country: 'FR' }) // [{ date: '2026-01-01', key: 'new_year' }, …]
|
|
226
|
+
holidays(2026, { country: 'CH', region: 'VD' }) // federal days + Vaud's
|
|
227
|
+
isHoliday('2026-09-21', { country: 'CH', region: 'VD' }) // true: Federal Fast Monday
|
|
228
|
+
isHoliday(DateTime.now().setZone('Europe/Zurich'), { country: 'CH', region: 'GE' })
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Computed by the engine, in Node and in the browser alike. `country` is `FR`
|
|
232
|
+
(its eleven national days) or `CH`: without a `region` its three federal days
|
|
233
|
+
(1 January, 1 August, 25 December), with one a canton's two-letter code
|
|
234
|
+
(`SWISS_CANTONS`) adding that canton's days: Easter Monday, Ascension and Whit
|
|
235
|
+
Monday everywhere, then its own, fixed (Saint Berchtold, Saint Stephen,
|
|
236
|
+
Assumption, All Saints, …) or moving (Good Friday, Corpus Christi, Federal Fast
|
|
237
|
+
Monday in Vaud and Neuchâtel, the Geneva Fast, the Näfelser Fahrt in Glarus).
|
|
238
|
+
|
|
239
|
+
- Each holiday has a stable `key` (`easter_monday`, `federal_fast_monday`, …)
|
|
240
|
+
for the application to translate; chronos names none in any language.
|
|
241
|
+
- Two holidays on one day are both listed (Ascension on 1 May, 2008).
|
|
242
|
+
- Neuchâtel's 2 January and 26 December count only when 1 January / 25
|
|
243
|
+
December is a Sunday. The Näfelser Fahrt is the first Thursday of April, one
|
|
244
|
+
week later when that Thursday falls in Holy Week.
|
|
245
|
+
- A `DateTime` is read on its own wall clock; a string must be `YYYY-MM-DD`.
|
|
246
|
+
- An unknown country or canton is refused (`E_CHRONOS_UNKNOWN_COUNTRY`,
|
|
247
|
+
`E_CHRONOS_UNKNOWN_REGION`) rather than answered with no holiday; the Easter
|
|
248
|
+
date is computed for 1583-9999 (`E_CHRONOS_INVALID_YEAR`).
|
|
249
|
+
- The cantonal table is today's calendar applied to every year: it does not
|
|
250
|
+
know when a holiday was instituted.
|
|
178
251
|
|
|
179
252
|
## Ranges
|
|
180
253
|
|
package/dist/DateTime.d.ts
CHANGED
|
@@ -48,6 +48,32 @@ export interface CalendarParts {
|
|
|
48
48
|
daysInYear: number;
|
|
49
49
|
isLeapYear: boolean;
|
|
50
50
|
}
|
|
51
|
+
/** A unit `toRelative` and `toRelativeCalendar` can count in. */
|
|
52
|
+
export type RelativeUnit = "years" | "months" | "weeks" | "days" | "hours" | "minutes" | "seconds";
|
|
53
|
+
export type RelativeUnitLike = RelativeUnit | "year" | "month" | "week" | "day" | "hour" | "minute" | "second";
|
|
54
|
+
export type RelativeRounding = "trunc" | "expand" | "round" | "floor" | "ceil";
|
|
55
|
+
interface RelativeFormatOptions {
|
|
56
|
+
/** Compared against; now, in this value's zone, by default. */
|
|
57
|
+
base?: DateInput;
|
|
58
|
+
/** Overrides this value's locale. */
|
|
59
|
+
locale?: Intl.LocalesArgument;
|
|
60
|
+
style?: Intl.RelativeTimeFormatStyle;
|
|
61
|
+
/** Reaches the formatter; the engine may not honour it. */
|
|
62
|
+
numberingSystem?: string;
|
|
63
|
+
}
|
|
64
|
+
export interface ToRelativeOptions extends RelativeFormatOptions {
|
|
65
|
+
/** One unit, or the units to pick the largest non-zero one from. */
|
|
66
|
+
unit?: RelativeUnitLike | readonly RelativeUnitLike[];
|
|
67
|
+
/** Round the count (default), or keep two decimals. */
|
|
68
|
+
round?: boolean;
|
|
69
|
+
/** How the count is rounded; toward zero by default. */
|
|
70
|
+
rounding?: RelativeRounding;
|
|
71
|
+
/** Milliseconds added away from the base before counting. */
|
|
72
|
+
padding?: number;
|
|
73
|
+
}
|
|
74
|
+
export interface ToRelativeCalendarOptions extends RelativeFormatOptions {
|
|
75
|
+
unit?: RelativeUnitLike;
|
|
76
|
+
}
|
|
51
77
|
export declare class DateTime {
|
|
52
78
|
#private;
|
|
53
79
|
/**
|
|
@@ -101,6 +127,27 @@ export declare class DateTime {
|
|
|
101
127
|
zone?: string;
|
|
102
128
|
}): DateTime;
|
|
103
129
|
static fromHTTP(input: string): DateTime;
|
|
130
|
+
/**
|
|
131
|
+
* Read `text` written with Luxon's format tokens (`dd/MM/yyyy HH:mm`,
|
|
132
|
+
* `dd LLL yyyy 'at' h:mm a`, `kkkk-'W'WW-c`, macros such as `D` or `f`), as
|
|
133
|
+
* Luxon's `fromFormat`. Units the format leaves out are filled as Luxon
|
|
134
|
+
* fills them: larger ones from now, smaller ones with their minimum
|
|
135
|
+
* (`HH:mm` is today at that time, `MMMM` the 1st of that month this year).
|
|
136
|
+
*
|
|
137
|
+
* The wall clock is read in the zone the text names (`z`), else at the
|
|
138
|
+
* offset it writes (`Z`, `ZZ`, `ZZZ`), else in `options.zone` (UTC by
|
|
139
|
+
* default); the result is in `options.zone`. Names are matched in
|
|
140
|
+
* `options.locale` (en-US by default), which the result keeps.
|
|
141
|
+
*
|
|
142
|
+
* Two deviations, named: text that does not match the format, or names a
|
|
143
|
+
* date that does not exist, throws instead of returning an invalid value
|
|
144
|
+
* (chronos has no invalid DateTime); and there is no `setZone` option, since
|
|
145
|
+
* a chronos zone is an IANA name and an offset alone names none.
|
|
146
|
+
*/
|
|
147
|
+
static fromFormat(text: string, format: string, options?: {
|
|
148
|
+
zone?: string;
|
|
149
|
+
locale?: string;
|
|
150
|
+
}): DateTime;
|
|
104
151
|
get zoneName(): string;
|
|
105
152
|
get offset(): number;
|
|
106
153
|
/**
|
|
@@ -230,10 +277,22 @@ export declare class DateTime {
|
|
|
230
277
|
valueOf(): number;
|
|
231
278
|
toSeconds(): number;
|
|
232
279
|
toLocaleString(locale?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions): string;
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
280
|
+
/**
|
|
281
|
+
* This value relative to `base` (now by default), as Luxon's `toRelative`:
|
|
282
|
+
* "in 1 day", "48 hours ago". The count is the calendar-aware diff in the
|
|
283
|
+
* largest unit that reaches 1, truncated toward zero, always as a number
|
|
284
|
+
* ("1 day ago", never "yesterday": that is {@link toRelativeCalendar}).
|
|
285
|
+
* Formatted in this value's locale unless `locale` is given.
|
|
286
|
+
*
|
|
287
|
+
* NAMED DEVIATION: no `quarters` unit, as chronos's Duration has none.
|
|
288
|
+
*/
|
|
289
|
+
toRelative(options?: ToRelativeOptions): string;
|
|
290
|
+
/**
|
|
291
|
+
* This value relative to `base`'s calendar, as Luxon's
|
|
292
|
+
* `toRelativeCalendar`: "yesterday", "next month", "3 days ago". Counts the
|
|
293
|
+
* calendar boundaries crossed in years, months, then days, or in `unit`.
|
|
294
|
+
*/
|
|
295
|
+
toRelativeCalendar(options?: ToRelativeCalendarOptions): string;
|
|
237
296
|
get calendar(): CalendarParts;
|
|
238
297
|
get weekday(): number;
|
|
239
298
|
get weekNumber(): number;
|
|
@@ -255,4 +314,5 @@ export declare function inRange(input: DateInput, range: DateRange, options?: Ra
|
|
|
255
314
|
export declare function containsRange(outer: DateRange, inner: DateRange, options?: RangeCompareOptions): boolean;
|
|
256
315
|
export declare function overlapsRange(a: DateRange, b: DateRange, options?: RangeCompareOptions): boolean;
|
|
257
316
|
export declare function analyzeRange(a: DateRange, b: DateRange, options?: RangeCompareOptions): RangeRelation;
|
|
317
|
+
export {};
|
|
258
318
|
//# sourceMappingURL=DateTime.d.ts.map
|
package/dist/DateTime.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DateTime.d.ts","sourceRoot":"","sources":["../src/DateTime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EACN,QAAQ,EACR,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"DateTime.d.ts","sourceRoot":"","sources":["../src/DateTime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EACN,QAAQ,EACR,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,MAAM,eAAe,CAAC;AAmBvB,MAAM,MAAM,QAAQ,GACjB,QAAQ,GACR,QAAQ,GACR,MAAM,GACN,KAAK,GACL,MAAM,GACN,OAAO,GACP,MAAM,CAAC;AACV,MAAM,MAAM,SAAS,GAClB,QAAQ,GACR,QAAQ,GACR,MAAM,GACN,KAAK,GACL,MAAM,GACN,OAAO,GACP,MAAM,CAAC;AACV,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,IAAI,GAAG,QAAQ,CAAC;AACjD,MAAM,WAAW,SAAS;IACzB,KAAK,EAAE,SAAS,CAAC;IACjB,GAAG,EAAE,SAAS,CAAC;CACf;AACD,MAAM,WAAW,mBAAmB;IACnC,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,YAAY,CAAC,EAAE,OAAO,CAAC;CACvB;AACD,MAAM,WAAW,aAAa;IAC7B,QAAQ,EAAE,OAAO,CAAC;IAClB,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,OAAO,CAAC;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xon } from "./format.js";
|
|
16
16
|
import { nativeChronos } from "./native.js";
|
|
17
|
-
import {
|
|
17
|
+
import { dayOfMs, daysInYearOf, dayToIsoWeek, dayToOrdinal, isoWeekday, isoWeeksInYear, isoWeekToDay, ordinalToDay, parseWithFormat, utcDayMs, } from "./parse.js";
|
|
18
18
|
// ─── Internal helpers ────────────────────────────────────────
|
|
19
19
|
const MONTH_NAMES = [
|
|
20
20
|
"January",
|
|
@@ -119,6 +119,130 @@ function isoOfMs(ms, input) {
|
|
|
119
119
|
}
|
|
120
120
|
return new Date(ms).toISOString();
|
|
121
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Fill the units a `fromFormat` text left out, as Luxon's `fromObject` does:
|
|
124
|
+
* the units larger than the largest one written come from `now`, the smaller
|
|
125
|
+
* ones take their minimum. A text naming a week (`kkkk`, `W`) or only a
|
|
126
|
+
* weekday is read on the ISO week calendar, one naming an ordinal (`o`) on the
|
|
127
|
+
* ordinal calendar. Every unit is range-checked; hour 24 is accepted at
|
|
128
|
+
* 24:00:00.000 and is midnight of the next day, as Luxon reads it.
|
|
129
|
+
*/
|
|
130
|
+
function resolveParsedFields(f, now, raw) {
|
|
131
|
+
const hasOrdinal = f.ordinal !== undefined;
|
|
132
|
+
const hasMonthDay = f.month !== undefined || f.day !== undefined;
|
|
133
|
+
const hasGregorian = f.year !== undefined || hasMonthDay;
|
|
134
|
+
const weekDefined = f.weekYear !== undefined || f.weekNumber !== undefined;
|
|
135
|
+
if ((hasGregorian || hasOrdinal) && weekDefined) {
|
|
136
|
+
throw new Error("Can't mix weekYear/weekNumber units with year/month/day or ordinals");
|
|
137
|
+
}
|
|
138
|
+
if (hasMonthDay && hasOrdinal) {
|
|
139
|
+
throw new Error("Can't mix ordinal dates with month/day");
|
|
140
|
+
}
|
|
141
|
+
let found = false;
|
|
142
|
+
const take = (given, current, min) => {
|
|
143
|
+
if (given !== undefined) {
|
|
144
|
+
found = true;
|
|
145
|
+
return given;
|
|
146
|
+
}
|
|
147
|
+
return found ? min : current;
|
|
148
|
+
};
|
|
149
|
+
const range = (unit, value, min, max) => {
|
|
150
|
+
if (!Number.isInteger(value) || value < min || value > max) {
|
|
151
|
+
throw new RangeError(`Invalid date '${raw}': ${unit} ${String(value)} is out of range (${min}-${max}).`);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const nowDay = {
|
|
155
|
+
year: now.year,
|
|
156
|
+
month: now.month,
|
|
157
|
+
day: now.day,
|
|
158
|
+
};
|
|
159
|
+
let day;
|
|
160
|
+
if (weekDefined || (f.weekday !== undefined && !hasGregorian)) {
|
|
161
|
+
const nowWeek = dayToIsoWeek(nowDay);
|
|
162
|
+
const weekYear = take(f.weekYear, nowWeek.weekYear, 1);
|
|
163
|
+
const weekNumber = take(f.weekNumber, nowWeek.weekNumber, 1);
|
|
164
|
+
const weekday = take(f.weekday, nowWeek.weekday, 1);
|
|
165
|
+
range("weekYear", weekYear, 0, 9999);
|
|
166
|
+
range("weekNumber", weekNumber, 1, isoWeeksInYear(weekYear));
|
|
167
|
+
range("weekday", weekday, 1, 7);
|
|
168
|
+
day = isoWeekToDay(weekYear, weekNumber, weekday);
|
|
169
|
+
}
|
|
170
|
+
else if (hasOrdinal) {
|
|
171
|
+
const year = take(f.year, now.year, 1);
|
|
172
|
+
const ordinal = take(f.ordinal, dayToOrdinal(nowDay), 1);
|
|
173
|
+
range("year", year, 0, 9999);
|
|
174
|
+
range("ordinal", ordinal, 1, daysInYearOf(year));
|
|
175
|
+
day = ordinalToDay(year, ordinal);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
day = {
|
|
179
|
+
year: take(f.year, now.year, 1),
|
|
180
|
+
month: take(f.month, now.month, 1),
|
|
181
|
+
day: take(f.day, now.day, 1),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const clock = {
|
|
185
|
+
...day,
|
|
186
|
+
hour: take(f.hour, now.hour, 0),
|
|
187
|
+
minute: take(f.minute, now.minute, 0),
|
|
188
|
+
second: take(f.second, now.second, 0),
|
|
189
|
+
millisecond: take(f.millisecond, now.millisecond, 0),
|
|
190
|
+
};
|
|
191
|
+
const endOfDay = clock.hour === 24 &&
|
|
192
|
+
clock.minute === 0 &&
|
|
193
|
+
clock.second === 0 &&
|
|
194
|
+
clock.millisecond === 0;
|
|
195
|
+
assertWallClock(endOfDay ? { ...clock, hour: 0 } : clock, raw);
|
|
196
|
+
const out = endOfDay
|
|
197
|
+
? {
|
|
198
|
+
...clock,
|
|
199
|
+
...dayOfMs(utcDayMs(clock.year, clock.month, clock.day + 1)),
|
|
200
|
+
hour: 0,
|
|
201
|
+
}
|
|
202
|
+
: clock;
|
|
203
|
+
assertWallClock(out, raw);
|
|
204
|
+
if (f.weekday !== undefined &&
|
|
205
|
+
hasGregorian &&
|
|
206
|
+
isoWeekday(out) !== f.weekday) {
|
|
207
|
+
throw new RangeError(`Invalid date '${raw}': weekday ${f.weekday} does not fall on ${pad(out.year, 4)}-${pad(out.month)}-${pad(out.day)}.`);
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
/** Now as a wall clock in `zone`, or at a fixed `offset` (minutes) when the text gave one. */
|
|
212
|
+
function nowWallClock(zone, offset) {
|
|
213
|
+
if (offset === undefined) {
|
|
214
|
+
const { year, month, day, hour, minute, second, millisecond } = DateTime.now(zone).calendar;
|
|
215
|
+
return { year, month, day, hour, minute, second, millisecond };
|
|
216
|
+
}
|
|
217
|
+
const d = new Date(Date.now() + offset * 60_000);
|
|
218
|
+
return {
|
|
219
|
+
year: d.getUTCFullYear(),
|
|
220
|
+
month: d.getUTCMonth() + 1,
|
|
221
|
+
day: d.getUTCDate(),
|
|
222
|
+
hour: d.getUTCHours(),
|
|
223
|
+
minute: d.getUTCMinutes(),
|
|
224
|
+
second: d.getUTCSeconds(),
|
|
225
|
+
millisecond: d.getUTCMilliseconds(),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* The instant of a parsed wall clock. An offset alone fixes it. A zone and an
|
|
230
|
+
* offset together: the offset settles the hour a zone repeats when its clocks
|
|
231
|
+
* go back, as long as the zone has that offset then; otherwise the zone wins.
|
|
232
|
+
*/
|
|
233
|
+
function instantOfParsed(c, parsed, wallZone) {
|
|
234
|
+
if (parsed.offset !== undefined) {
|
|
235
|
+
const atOffset = utcMs(c) - parsed.offset * 60_000;
|
|
236
|
+
if (parsed.zone === undefined)
|
|
237
|
+
return atOffset;
|
|
238
|
+
const zoneOffset = parsed.zone === "UTC"
|
|
239
|
+
? 0
|
|
240
|
+
: nativeChronos().zoneOffset(isoOfMs(atOffset, () => `'${parsed.zone}'`), parsed.zone);
|
|
241
|
+
if (zoneOffset === parsed.offset)
|
|
242
|
+
return atOffset;
|
|
243
|
+
}
|
|
244
|
+
return wallClockMs(c, wallZone);
|
|
245
|
+
}
|
|
122
246
|
/** Read an ISO 8601 string; an offset-less one is a wall clock in `zone`. */
|
|
123
247
|
function parseIso(raw, zone) {
|
|
124
248
|
const m = ISO_8601.exec(raw);
|
|
@@ -157,12 +281,12 @@ function toIso(value, zone = "UTC") {
|
|
|
157
281
|
if (value instanceof DateTime)
|
|
158
282
|
return value.toISO();
|
|
159
283
|
if (value instanceof Date) {
|
|
160
|
-
return
|
|
284
|
+
return isoOfMs(value.getTime(), () => "(JS Date)");
|
|
161
285
|
}
|
|
162
286
|
if (typeof value !== "string") {
|
|
163
287
|
throw new TypeError(`Invalid date input: expected a string, a Date or a DateTime, got ${typeof value}`);
|
|
164
288
|
}
|
|
165
|
-
return
|
|
289
|
+
return parseIso(value, zone);
|
|
166
290
|
}
|
|
167
291
|
function toMs(value) {
|
|
168
292
|
return new Date(toIso(value)).getTime();
|
|
@@ -202,6 +326,110 @@ const CALENDAR_UNITS = new Set([
|
|
|
202
326
|
"weeks",
|
|
203
327
|
"days",
|
|
204
328
|
]);
|
|
329
|
+
const RELATIVE_UNITS = [
|
|
330
|
+
"years",
|
|
331
|
+
"months",
|
|
332
|
+
"weeks",
|
|
333
|
+
"days",
|
|
334
|
+
"hours",
|
|
335
|
+
"minutes",
|
|
336
|
+
"seconds",
|
|
337
|
+
];
|
|
338
|
+
const TO_RELATIVE_UNITS = [
|
|
339
|
+
"years",
|
|
340
|
+
"months",
|
|
341
|
+
"days",
|
|
342
|
+
"hours",
|
|
343
|
+
"minutes",
|
|
344
|
+
"seconds",
|
|
345
|
+
];
|
|
346
|
+
const TO_RELATIVE_CALENDAR_UNITS = [
|
|
347
|
+
"years",
|
|
348
|
+
"months",
|
|
349
|
+
"days",
|
|
350
|
+
];
|
|
351
|
+
const BOUND_OF = {
|
|
352
|
+
years: "year",
|
|
353
|
+
months: "month",
|
|
354
|
+
weeks: "week",
|
|
355
|
+
days: "day",
|
|
356
|
+
hours: "hour",
|
|
357
|
+
minutes: "minute",
|
|
358
|
+
seconds: "second",
|
|
359
|
+
};
|
|
360
|
+
function relativeUnitOf(unit) {
|
|
361
|
+
const plural = typeof unit === "string" && !unit.endsWith("s") ? `${unit}s` : unit;
|
|
362
|
+
const found = RELATIVE_UNITS.find((u) => u === plural);
|
|
363
|
+
if (found === undefined) {
|
|
364
|
+
throw new RangeError(`Unsupported relative unit: ${String(unit)}`);
|
|
365
|
+
}
|
|
366
|
+
return found;
|
|
367
|
+
}
|
|
368
|
+
/** Round to `digits` decimals the way the reference library's `roundTo` does. */
|
|
369
|
+
function roundTo(value, digits, rounding) {
|
|
370
|
+
const factor = 10 ** digits;
|
|
371
|
+
switch (rounding) {
|
|
372
|
+
case "expand":
|
|
373
|
+
return value > 0
|
|
374
|
+
? Math.ceil(value * factor) / factor
|
|
375
|
+
: Math.floor(value * factor) / factor;
|
|
376
|
+
case "trunc":
|
|
377
|
+
return Math.trunc(value * factor) / factor;
|
|
378
|
+
case "round":
|
|
379
|
+
return Math.round(value * factor) / factor;
|
|
380
|
+
case "floor":
|
|
381
|
+
return Math.floor(value * factor) / factor;
|
|
382
|
+
case "ceil":
|
|
383
|
+
return Math.ceil(value * factor) / factor;
|
|
384
|
+
default:
|
|
385
|
+
throw new RangeError(`Value rounding ${String(rounding)} is out of range`);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* The reference library's `diffRelative`: count `end - start` in the first
|
|
390
|
+
* unit (largest first) that reaches 1, or in the one unit asked for.
|
|
391
|
+
* `calendary` counts calendar boundaries crossed (yesterday is 1 day back at
|
|
392
|
+
* 23:59 as at 00:01) and rounds to the nearest; otherwise the count is the
|
|
393
|
+
* calendar-aware diff, truncated unless `rounding` says otherwise.
|
|
394
|
+
*/
|
|
395
|
+
function diffRelative(start, end, opts) {
|
|
396
|
+
const round = opts.round ?? true;
|
|
397
|
+
const rounding = opts.rounding ?? "trunc";
|
|
398
|
+
// NAMED DEVIATION: the reference library prints English (en, en-US) from a
|
|
399
|
+
// hand-written table and every other locale through Intl. Every locale goes
|
|
400
|
+
// through Intl here, so en-US gets CLDR's own short and narrow forms
|
|
401
|
+
// ("2d ago" in narrow, where the table prints "2 days ago"); the long style,
|
|
402
|
+
// the default, reads the same.
|
|
403
|
+
const intlOptions = {
|
|
404
|
+
numeric: opts.numeric,
|
|
405
|
+
style: opts.style ?? "long",
|
|
406
|
+
};
|
|
407
|
+
if (opts.numberingSystem !== undefined) {
|
|
408
|
+
intlOptions.numberingSystem = opts.numberingSystem;
|
|
409
|
+
}
|
|
410
|
+
const formatter = new Intl.RelativeTimeFormat(opts.locale ?? end.locale, intlOptions);
|
|
411
|
+
const format = (count, unit) => formatter.format(roundTo(count, round || opts.calendary ? 0 : 2, opts.calendary ? "round" : rounding), unit);
|
|
412
|
+
const differ = (unit) => {
|
|
413
|
+
if (opts.calendary) {
|
|
414
|
+
const bound = BOUND_OF[unit];
|
|
415
|
+
if (end.hasSame(start, bound))
|
|
416
|
+
return 0;
|
|
417
|
+
return end.startOf(bound).diff(start.startOf(bound), unit).as(unit);
|
|
418
|
+
}
|
|
419
|
+
return end.diff(start, unit).as(unit);
|
|
420
|
+
};
|
|
421
|
+
if (opts.unit !== undefined)
|
|
422
|
+
return format(differ(opts.unit), opts.unit);
|
|
423
|
+
for (const unit of opts.units) {
|
|
424
|
+
const count = differ(unit);
|
|
425
|
+
if (Math.abs(count) >= 1)
|
|
426
|
+
return format(count, unit);
|
|
427
|
+
}
|
|
428
|
+
const last = opts.units.at(-1);
|
|
429
|
+
if (last === undefined)
|
|
430
|
+
throw new RangeError("No unit to count in");
|
|
431
|
+
return format(start.toMillis() > end.toMillis() ? -0 : 0, last);
|
|
432
|
+
}
|
|
205
433
|
function durationUnitOf(unit) {
|
|
206
434
|
const plural = unit.endsWith("s") ? unit : `${unit}s`;
|
|
207
435
|
const found = DIFF_ORDER.find((u) => u === plural);
|
|
@@ -442,6 +670,11 @@ export class DateTime {
|
|
|
442
670
|
return new _a(new Date(ms));
|
|
443
671
|
}
|
|
444
672
|
static fromSeconds(seconds) {
|
|
673
|
+
// Same guard as fromMillis: `seconds * 1000` would turn "12" into 12 s
|
|
674
|
+
// and null into the epoch.
|
|
675
|
+
if (typeof seconds !== "number") {
|
|
676
|
+
throw new TypeError(`fromSeconds expects a number, got ${seconds === null ? "null" : typeof seconds}`);
|
|
677
|
+
}
|
|
445
678
|
return new _a(new Date(seconds * 1000));
|
|
446
679
|
}
|
|
447
680
|
static fromUnix(seconds) {
|
|
@@ -496,6 +729,44 @@ export class DateTime {
|
|
|
496
729
|
static fromHTTP(input) {
|
|
497
730
|
return new _a(nativeChronos().parseHttp(input));
|
|
498
731
|
}
|
|
732
|
+
/**
|
|
733
|
+
* Read `text` written with Luxon's format tokens (`dd/MM/yyyy HH:mm`,
|
|
734
|
+
* `dd LLL yyyy 'at' h:mm a`, `kkkk-'W'WW-c`, macros such as `D` or `f`), as
|
|
735
|
+
* Luxon's `fromFormat`. Units the format leaves out are filled as Luxon
|
|
736
|
+
* fills them: larger ones from now, smaller ones with their minimum
|
|
737
|
+
* (`HH:mm` is today at that time, `MMMM` the 1st of that month this year).
|
|
738
|
+
*
|
|
739
|
+
* The wall clock is read in the zone the text names (`z`), else at the
|
|
740
|
+
* offset it writes (`Z`, `ZZ`, `ZZZ`), else in `options.zone` (UTC by
|
|
741
|
+
* default); the result is in `options.zone`. Names are matched in
|
|
742
|
+
* `options.locale` (en-US by default), which the result keeps.
|
|
743
|
+
*
|
|
744
|
+
* Two deviations, named: text that does not match the format, or names a
|
|
745
|
+
* date that does not exist, throws instead of returning an invalid value
|
|
746
|
+
* (chronos has no invalid DateTime); and there is no `setZone` option, since
|
|
747
|
+
* a chronos zone is an IANA name and an offset alone names none.
|
|
748
|
+
*/
|
|
749
|
+
static fromFormat(text, format, options = {}) {
|
|
750
|
+
if (typeof text !== "string" || typeof format !== "string") {
|
|
751
|
+
throw new TypeError("fromFormat requires an input string and a format");
|
|
752
|
+
}
|
|
753
|
+
const zone = options.zone ?? "UTC";
|
|
754
|
+
assertZone(zone);
|
|
755
|
+
const locale = options.locale ?? "en-US";
|
|
756
|
+
const parsed = parseWithFormat(text, format, locale);
|
|
757
|
+
if (parsed === null ||
|
|
758
|
+
(Object.keys(parsed.fields).length === 0 && parsed.zone === undefined)) {
|
|
759
|
+
throw new RangeError(`Invalid date '${text}': it does not match the format '${format}'.`);
|
|
760
|
+
}
|
|
761
|
+
if (parsed.zone !== undefined)
|
|
762
|
+
assertZone(parsed.zone);
|
|
763
|
+
const wallZone = parsed.zone ?? zone;
|
|
764
|
+
const clock = resolveParsedFields(parsed.fields, nowWallClock(wallZone, parsed.offset), text);
|
|
765
|
+
const ms = instantOfParsed(clock, parsed, wallZone);
|
|
766
|
+
const out = new _a(isoOfMs(ms, () => `'${text}'`), zone);
|
|
767
|
+
out.#locale = locale;
|
|
768
|
+
return out;
|
|
769
|
+
}
|
|
499
770
|
// ─── Timezone ───────────────────────────────────────────
|
|
500
771
|
get zoneName() {
|
|
501
772
|
return this.#zone;
|
|
@@ -828,39 +1099,50 @@ export class DateTime {
|
|
|
828
1099
|
...options,
|
|
829
1100
|
}).format(this.toDate());
|
|
830
1101
|
}
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
1102
|
+
/**
|
|
1103
|
+
* This value relative to `base` (now by default), as Luxon's `toRelative`:
|
|
1104
|
+
* "in 1 day", "48 hours ago". The count is the calendar-aware diff in the
|
|
1105
|
+
* largest unit that reaches 1, truncated toward zero, always as a number
|
|
1106
|
+
* ("1 day ago", never "yesterday": that is {@link toRelativeCalendar}).
|
|
1107
|
+
* Formatted in this value's locale unless `locale` is given.
|
|
1108
|
+
*
|
|
1109
|
+
* NAMED DEVIATION: no `quarters` unit, as chronos's Duration has none.
|
|
1110
|
+
*/
|
|
1111
|
+
toRelative(options = {}) {
|
|
1112
|
+
const base = options.base === undefined
|
|
1113
|
+
? _a.now(this.#zone)
|
|
1114
|
+
: _a.from(options.base);
|
|
1115
|
+
const padding = options.padding
|
|
1116
|
+
? this.toMillis() < base.toMillis()
|
|
1117
|
+
? -options.padding
|
|
1118
|
+
: options.padding
|
|
1119
|
+
: 0;
|
|
1120
|
+
let units = TO_RELATIVE_UNITS;
|
|
835
1121
|
let unit;
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
unit = "second";
|
|
839
|
-
value = Math.round(diffMs / 1000);
|
|
1122
|
+
if (Array.isArray(options.unit)) {
|
|
1123
|
+
units = options.unit.map(relativeUnitOf);
|
|
840
1124
|
}
|
|
841
|
-
else if (
|
|
842
|
-
unit =
|
|
843
|
-
value = Math.round(diffMs / 60_000);
|
|
1125
|
+
else if (options.unit !== undefined) {
|
|
1126
|
+
unit = relativeUnitOf(options.unit);
|
|
844
1127
|
}
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
unit = "year";
|
|
859
|
-
value = Math.round(diffMs / 31_536_000_000);
|
|
860
|
-
}
|
|
861
|
-
return new Intl.RelativeTimeFormat(options?.locale, {
|
|
1128
|
+
return diffRelative(base, padding === 0 ? this : this.plus({ milliseconds: padding }), { ...options, numeric: "always", units, unit, calendary: false });
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* This value relative to `base`'s calendar, as Luxon's
|
|
1132
|
+
* `toRelativeCalendar`: "yesterday", "next month", "3 days ago". Counts the
|
|
1133
|
+
* calendar boundaries crossed in years, months, then days, or in `unit`.
|
|
1134
|
+
*/
|
|
1135
|
+
toRelativeCalendar(options = {}) {
|
|
1136
|
+
const base = options.base === undefined
|
|
1137
|
+
? _a.now(this.#zone)
|
|
1138
|
+
: _a.from(options.base);
|
|
1139
|
+
return diffRelative(base, this, {
|
|
1140
|
+
...options,
|
|
862
1141
|
numeric: "auto",
|
|
863
|
-
|
|
1142
|
+
units: TO_RELATIVE_CALENDAR_UNITS,
|
|
1143
|
+
unit: options.unit === undefined ? undefined : relativeUnitOf(options.unit),
|
|
1144
|
+
calendary: true,
|
|
1145
|
+
});
|
|
864
1146
|
}
|
|
865
1147
|
// ─── Calendar accessors ─────────────────────────────────
|
|
866
1148
|
get calendar() {
|