native-dates 0.0.0-stage → 0.0.6
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 +7 -0
- package/README.md +41 -2
- package/package.json +46 -4
- package/src/calendar-date.js +393 -0
- package/src/date-time.js +433 -0
- package/src/duration.js +341 -0
- package/src/helpers/compare.js +126 -0
- package/src/helpers/errors.js +89 -0
- package/src/helpers/fields.js +194 -0
- package/src/helpers/format.js +93 -0
- package/src/helpers/identity.js +10 -0
- package/src/helpers/intl.js +45 -0
- package/src/helpers/types.js +20 -0
- package/src/helpers/units.js +147 -0
- package/src/helpers/zones.js +113 -0
- package/src/index.js +30 -0
- package/src/time.js +357 -0
- package/types/index.d.ts +538 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Temporal value. Temporal's own declarations ship with newer TypeScript libs and with the polyfills,
|
|
3
|
+
* so the values this library hands back from `toTemporal()` are typed loosely until they are everywhere
|
|
4
|
+
*/
|
|
5
|
+
export type TemporalValue = any;
|
|
6
|
+
|
|
7
|
+
/** A unit name, singular or plural, or an abbreviation like `h`, `min`, `d`, `mo` */
|
|
8
|
+
export type Unit =
|
|
9
|
+
| 'year'
|
|
10
|
+
| 'years'
|
|
11
|
+
| 'quarter'
|
|
12
|
+
| 'quarters'
|
|
13
|
+
| 'month'
|
|
14
|
+
| 'months'
|
|
15
|
+
| 'week'
|
|
16
|
+
| 'weeks'
|
|
17
|
+
| 'day'
|
|
18
|
+
| 'days'
|
|
19
|
+
| 'hour'
|
|
20
|
+
| 'hours'
|
|
21
|
+
| 'minute'
|
|
22
|
+
| 'minutes'
|
|
23
|
+
| 'second'
|
|
24
|
+
| 'seconds'
|
|
25
|
+
| 'millisecond'
|
|
26
|
+
| 'milliseconds'
|
|
27
|
+
| 'microsecond'
|
|
28
|
+
| 'microseconds'
|
|
29
|
+
| 'nanosecond'
|
|
30
|
+
| 'nanoseconds'
|
|
31
|
+
| (string & {});
|
|
32
|
+
|
|
33
|
+
/** A unit a calendar date can start, end or step by */
|
|
34
|
+
export type DateUnit = 'year' | 'quarter' | 'month' | 'week' | 'day' | (string & {});
|
|
35
|
+
|
|
36
|
+
/** A weekday by name (`'monday'`, `'Fri'`) or ISO number, 1 for monday through 7 for sunday */
|
|
37
|
+
export type Weekday =
|
|
38
|
+
| 'monday'
|
|
39
|
+
| 'tuesday'
|
|
40
|
+
| 'wednesday'
|
|
41
|
+
| 'thursday'
|
|
42
|
+
| 'friday'
|
|
43
|
+
| 'saturday'
|
|
44
|
+
| 'sunday'
|
|
45
|
+
| 1
|
|
46
|
+
| 2
|
|
47
|
+
| 3
|
|
48
|
+
| 4
|
|
49
|
+
| 5
|
|
50
|
+
| 6
|
|
51
|
+
| 7
|
|
52
|
+
| (string & {});
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* An IANA zone (`'America/New_York'`), a fixed offset (`'+05:30'`), or `'UTC'`. With `@native-dates/plugins/zones`,
|
|
56
|
+
* also cities (`'Berlin'`, `'los angeles'`), abbreviations (`'PT'`, `'CET'`), and `zoneAliases`
|
|
57
|
+
*/
|
|
58
|
+
export type Zone = string;
|
|
59
|
+
|
|
60
|
+
/** The options form of a factory's second argument */
|
|
61
|
+
export interface ReadOptions {
|
|
62
|
+
/** The zone to read the value in (default: the configured zone, else the machine's) */
|
|
63
|
+
zone?: Zone;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Duration fields, plural, as Temporal spells them */
|
|
67
|
+
export interface DurationFields {
|
|
68
|
+
years?: number;
|
|
69
|
+
months?: number;
|
|
70
|
+
weeks?: number;
|
|
71
|
+
days?: number;
|
|
72
|
+
hours?: number;
|
|
73
|
+
minutes?: number;
|
|
74
|
+
seconds?: number;
|
|
75
|
+
milliseconds?: number;
|
|
76
|
+
microseconds?: number;
|
|
77
|
+
nanoseconds?: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Anything that reads as a duration: a `Duration`, a `Temporal.Duration`, a fields object, a phrase like
|
|
82
|
+
* `'1h 30m'` or `'2 weeks and 3 days'`, an ISO string like `'PT1H30M'`, or a number of milliseconds
|
|
83
|
+
*/
|
|
84
|
+
export type DurationInput = Duration | DurationFields | string | number | TemporalValue;
|
|
85
|
+
|
|
86
|
+
/** Calendar fields for a datetime or date, singular, as Temporal's `with()` spells them */
|
|
87
|
+
export interface DateTimeFields {
|
|
88
|
+
year?: number;
|
|
89
|
+
month?: number;
|
|
90
|
+
day?: number;
|
|
91
|
+
hour?: number;
|
|
92
|
+
minute?: number;
|
|
93
|
+
second?: number;
|
|
94
|
+
millisecond?: number;
|
|
95
|
+
microsecond?: number;
|
|
96
|
+
nanosecond?: number;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Anything that reads as a datetime: an ISO string, a `Date`, epoch milliseconds, a fields object, a
|
|
101
|
+
* `DateTime`, a `CalendarDate` (its midnight in the zone), or a Temporal value
|
|
102
|
+
*/
|
|
103
|
+
export type DateTimeInput = DateTime | CalendarDate | Date | number | string | DateTimeFields | TemporalValue;
|
|
104
|
+
|
|
105
|
+
/** Anything that reads as a calendar date: `'2026-09-06'`, a fields object, a `DateTime`, a `Date`, or a Temporal value */
|
|
106
|
+
export type CalendarDateInput = CalendarDate | DateTime | Date | string | DateTimeFields | TemporalValue;
|
|
107
|
+
|
|
108
|
+
/** Anything that reads as a time of day: `'09:00'`, `'5:30pm'`, a fields object, a `DateTime`, a `Date`, or a Temporal value */
|
|
109
|
+
export type TimeInput = Time | DateTime | Date | string | DateTimeFields | TemporalValue;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* How to format: a preset (`'short'`, `'medium'`, `'long'`, `'full'`, `'month'`, and for a datetime
|
|
113
|
+
* `'date'`, `'time'` or `'text'`), an `Intl.DateTimeFormat` options bag, or a token pattern with `@native-dates/plugins/tokens`
|
|
114
|
+
*/
|
|
115
|
+
export type FormatSpec =
|
|
116
|
+
| 'short'
|
|
117
|
+
| 'medium'
|
|
118
|
+
| 'long'
|
|
119
|
+
| 'full'
|
|
120
|
+
| 'month'
|
|
121
|
+
| 'date'
|
|
122
|
+
| 'time'
|
|
123
|
+
| 'text'
|
|
124
|
+
| Intl.DateTimeFormatOptions
|
|
125
|
+
| (string & {});
|
|
126
|
+
|
|
127
|
+
/** A BCP 47 locale tag, overriding the configured locale for one call */
|
|
128
|
+
export type Locale = string;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* An exact moment with a zone to read it in. One type for timestamps, appointments and deadlines.
|
|
132
|
+
* The zone is a view: `toString()` and `toJSON()` print the instant in UTC.
|
|
133
|
+
*/
|
|
134
|
+
export class DateTime {
|
|
135
|
+
constructor(input: DateTimeInput, zone?: Zone);
|
|
136
|
+
constructor(input: DateTimeInput, options?: ReadOptions);
|
|
137
|
+
|
|
138
|
+
/** The value in the configured locale's common form, `'Sep 6, 2026, 2:30 PM EDT'`, what `format('text')` prints */
|
|
139
|
+
readonly text: string;
|
|
140
|
+
readonly month: number;
|
|
141
|
+
readonly day: number;
|
|
142
|
+
readonly year: number;
|
|
143
|
+
/** ISO, 1 for monday through 7 for sunday */
|
|
144
|
+
readonly weekday: number;
|
|
145
|
+
readonly hour: number;
|
|
146
|
+
readonly minute: number;
|
|
147
|
+
readonly second: number;
|
|
148
|
+
readonly millisecond: number;
|
|
149
|
+
readonly microsecond: number;
|
|
150
|
+
readonly nanosecond: number;
|
|
151
|
+
/** The canonical zone id */
|
|
152
|
+
readonly zone: string;
|
|
153
|
+
/** The UTC offset in effect, `'+05:30'` */
|
|
154
|
+
readonly offset: string;
|
|
155
|
+
/** Epoch milliseconds */
|
|
156
|
+
readonly epoch: number;
|
|
157
|
+
/** The exact instant as epoch nanoseconds, the key two datetimes a nanosecond apart still tell apart on */
|
|
158
|
+
readonly epochNanoseconds: bigint;
|
|
159
|
+
readonly quarter: number;
|
|
160
|
+
readonly dayOfYear: number;
|
|
161
|
+
/** ISO week number */
|
|
162
|
+
readonly weekOfYear: number;
|
|
163
|
+
readonly daysInMonth: number;
|
|
164
|
+
readonly daysInYear: number;
|
|
165
|
+
/** 23, 24 or 25 across a daylight saving change */
|
|
166
|
+
readonly hoursInDay: number;
|
|
167
|
+
isLeapYear(): boolean;
|
|
168
|
+
isWeekend(): boolean;
|
|
169
|
+
isWeekday(): boolean;
|
|
170
|
+
/** The calendar date in this zone */
|
|
171
|
+
readonly date: CalendarDate;
|
|
172
|
+
/** The time of day in this zone */
|
|
173
|
+
readonly time: Time;
|
|
174
|
+
|
|
175
|
+
/** Adding days keeps the wall clock across a daylight saving change, adding hours counts hours */
|
|
176
|
+
plus(amount: DurationInput, unit?: Unit): DateTime;
|
|
177
|
+
/** Alias for `plus` */
|
|
178
|
+
add(amount: DurationInput, unit?: Unit): DateTime;
|
|
179
|
+
minus(amount: DurationInput, unit?: Unit): DateTime;
|
|
180
|
+
/** Alias for `minus` */
|
|
181
|
+
subtract(amount: DurationInput, unit?: Unit): DateTime;
|
|
182
|
+
/** Alias for `minus` */
|
|
183
|
+
remove(amount: DurationInput, unit?: Unit): DateTime;
|
|
184
|
+
|
|
185
|
+
set(fields: DateTimeFields): DateTime;
|
|
186
|
+
set(unit: Unit, value: number): DateTime;
|
|
187
|
+
/** The same date and zone at another time of day */
|
|
188
|
+
at(time: TimeInput): DateTime;
|
|
189
|
+
/** The same instant read in another zone */
|
|
190
|
+
in(zone: Zone): DateTime;
|
|
191
|
+
/** The first instant of the unit around this value. A week starts on the configured first day, or on `firstDay` */
|
|
192
|
+
startOf(unit: Unit, firstDay?: Weekday): DateTime;
|
|
193
|
+
/** The last millisecond of the unit */
|
|
194
|
+
endOf(unit: Unit, firstDay?: Weekday): DateTime;
|
|
195
|
+
round(unit: Unit): DateTime;
|
|
196
|
+
round(increment: number, unit: Unit): DateTime;
|
|
197
|
+
floor(unit: Unit): DateTime;
|
|
198
|
+
floor(increment: number, unit: Unit): DateTime;
|
|
199
|
+
ceil(unit: Unit): DateTime;
|
|
200
|
+
ceil(increment: number, unit: Unit): DateTime;
|
|
201
|
+
/** The next such weekday strictly after this one, at the same time of day */
|
|
202
|
+
next(weekday: Weekday): DateTime;
|
|
203
|
+
previous(weekday: Weekday): DateTime;
|
|
204
|
+
|
|
205
|
+
/** The same instant, whatever zone each side reads it in */
|
|
206
|
+
equals(other: DateTimeInput): boolean;
|
|
207
|
+
isBefore(other: DateTimeInput): boolean;
|
|
208
|
+
isAfter(other: DateTimeInput): boolean;
|
|
209
|
+
/** Same to the unit, judged in this datetime's zone */
|
|
210
|
+
isSame(other: DateTimeInput, unit?: Unit, firstDay?: Weekday): boolean;
|
|
211
|
+
isPast(): boolean;
|
|
212
|
+
isFuture(): boolean;
|
|
213
|
+
isToday(): boolean;
|
|
214
|
+
isTomorrow(): boolean;
|
|
215
|
+
isYesterday(): boolean;
|
|
216
|
+
|
|
217
|
+
/** A duration balanced from years down that remembers this moment, or the whole length as a number in a unit */
|
|
218
|
+
until(other: DateTimeInput): Duration;
|
|
219
|
+
until(other: DateTimeInput, unit: Unit): number;
|
|
220
|
+
since(other: DateTimeInput): Duration;
|
|
221
|
+
since(other: DateTimeInput, unit: Unit): number;
|
|
222
|
+
|
|
223
|
+
/** A preset ('short', 'medium', 'long', 'full', 'date', 'time', 'month', 'text') or an Intl options bag */
|
|
224
|
+
format(spec?: FormatSpec, locale?: Locale): string;
|
|
225
|
+
/** The instant in UTC, `'2026-09-06T14:30:00.000Z'` */
|
|
226
|
+
toString(): string;
|
|
227
|
+
toJSON(): string;
|
|
228
|
+
toJSDate(): Date;
|
|
229
|
+
toTemporal(): TemporalValue;
|
|
230
|
+
/** Epoch milliseconds, so datetimes sort and subtract with `<` and `-` */
|
|
231
|
+
valueOf(): number;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Reads an ISO string (`'2026-09-06T14:30Z'`, `'2026-09-06T14:30'` as a wall clock in the zone,
|
|
236
|
+
* `'2026-09-06'` as midnight), a Date, epoch milliseconds, a fields object, a date, or a Temporal value.
|
|
237
|
+
* A missing value refuses, `now()` reads the current moment
|
|
238
|
+
*/
|
|
239
|
+
export function datetime(input: DateTimeInput, zone?: Zone): DateTime;
|
|
240
|
+
export function datetime(input: DateTimeInput, options?: ReadOptions): DateTime;
|
|
241
|
+
|
|
242
|
+
/** The current moment at millisecond precision, in the zone or the configured default */
|
|
243
|
+
export function now(zone?: Zone): DateTime;
|
|
244
|
+
/** `now(zone).startOf('day')`, the bound a query for today writes */
|
|
245
|
+
export function startOfToday(zone?: Zone): DateTime;
|
|
246
|
+
/** `now(zone).endOf('day')`, the last millisecond of today */
|
|
247
|
+
export function endOfToday(zone?: Zone): DateTime;
|
|
248
|
+
|
|
249
|
+
export function isDateTime(value: unknown): value is DateTime;
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* A calendar date with no time and no zone: a due date, a birthday, a booking night. It is not a
|
|
253
|
+
* moment, so it never shifts when read from another zone.
|
|
254
|
+
*/
|
|
255
|
+
export class CalendarDate {
|
|
256
|
+
constructor(input: CalendarDateInput, options?: ReadOptions);
|
|
257
|
+
constructor(year: number, month: number, day?: number);
|
|
258
|
+
|
|
259
|
+
/** The day in the configured locale's common form, `'Sep 6, 2026'`, what `format()` prints */
|
|
260
|
+
readonly text: string;
|
|
261
|
+
readonly month: number;
|
|
262
|
+
readonly day: number;
|
|
263
|
+
readonly year: number;
|
|
264
|
+
/** ISO, 1 for monday through 7 for sunday */
|
|
265
|
+
readonly weekday: number;
|
|
266
|
+
readonly quarter: number;
|
|
267
|
+
readonly dayOfYear: number;
|
|
268
|
+
/** ISO week number */
|
|
269
|
+
readonly weekOfYear: number;
|
|
270
|
+
readonly daysInMonth: number;
|
|
271
|
+
readonly daysInYear: number;
|
|
272
|
+
isLeapYear(): boolean;
|
|
273
|
+
isWeekend(): boolean;
|
|
274
|
+
isWeekday(): boolean;
|
|
275
|
+
|
|
276
|
+
/** Adds years, months, weeks or days. A clock unit refuses */
|
|
277
|
+
plus(amount: DurationInput, unit?: Unit): CalendarDate;
|
|
278
|
+
/** Alias for `plus` */
|
|
279
|
+
add(amount: DurationInput, unit?: Unit): CalendarDate;
|
|
280
|
+
minus(amount: DurationInput, unit?: Unit): CalendarDate;
|
|
281
|
+
/** Alias for `minus` */
|
|
282
|
+
subtract(amount: DurationInput, unit?: Unit): CalendarDate;
|
|
283
|
+
/** Alias for `minus` */
|
|
284
|
+
remove(amount: DurationInput, unit?: Unit): CalendarDate;
|
|
285
|
+
|
|
286
|
+
set(fields: Pick<DateTimeFields, 'year' | 'month' | 'day'>): CalendarDate;
|
|
287
|
+
set(unit: Unit, value: number): CalendarDate;
|
|
288
|
+
/** The first day of the unit around this value. A week starts on the configured first day, or on `firstDay` */
|
|
289
|
+
startOf(unit: DateUnit, firstDay?: Weekday): CalendarDate;
|
|
290
|
+
/** The last day of the unit, so `endOf('month')` is the 28th, 30th or 31st */
|
|
291
|
+
endOf(unit: DateUnit, firstDay?: Weekday): CalendarDate;
|
|
292
|
+
/** The next such weekday strictly after this one */
|
|
293
|
+
next(weekday: Weekday): CalendarDate;
|
|
294
|
+
previous(weekday: Weekday): CalendarDate;
|
|
295
|
+
|
|
296
|
+
equals(other: CalendarDateInput): boolean;
|
|
297
|
+
isBefore(other: CalendarDateInput): boolean;
|
|
298
|
+
isAfter(other: CalendarDateInput): boolean;
|
|
299
|
+
isSame(other: CalendarDateInput, unit?: DateUnit, firstDay?: Weekday): boolean;
|
|
300
|
+
/** Today depends on where you stand, so these take the zone to judge from */
|
|
301
|
+
isPast(zone?: Zone): boolean;
|
|
302
|
+
isFuture(zone?: Zone): boolean;
|
|
303
|
+
isToday(zone?: Zone): boolean;
|
|
304
|
+
isTomorrow(zone?: Zone): boolean;
|
|
305
|
+
isYesterday(zone?: Zone): boolean;
|
|
306
|
+
|
|
307
|
+
/** A duration balanced from years down that remembers this date, or the whole length as a number in a unit */
|
|
308
|
+
until(other: CalendarDateInput): Duration;
|
|
309
|
+
until(other: CalendarDateInput, unit: Unit): number;
|
|
310
|
+
since(other: CalendarDateInput): Duration;
|
|
311
|
+
since(other: CalendarDateInput, unit: Unit): number;
|
|
312
|
+
/** This date at a time of day in a zone, the moment an appointment happens. No time means midnight */
|
|
313
|
+
at(time?: TimeInput, zone?: Zone): DateTime;
|
|
314
|
+
|
|
315
|
+
format(spec?: FormatSpec, locale?: Locale): string;
|
|
316
|
+
/** ISO 8601, `'2026-09-06'` */
|
|
317
|
+
toString(): string;
|
|
318
|
+
toJSON(): string;
|
|
319
|
+
/** Midnight of this date in the zone, as a JS Date */
|
|
320
|
+
toJSDate(zone?: Zone): Date;
|
|
321
|
+
toTemporal(): TemporalValue;
|
|
322
|
+
/** Throws. A date is not a point on the number line */
|
|
323
|
+
valueOf(): never;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Reads `'2026-09-06'`, three numbers, a fields object, a datetime (its date in its zone), a Date (its
|
|
328
|
+
* date in the zone), or a Temporal value. A wall-clock string keeps its day
|
|
329
|
+
*/
|
|
330
|
+
export function date(input: CalendarDateInput, options?: ReadOptions): CalendarDate;
|
|
331
|
+
export function date(year: number, month: number, day?: number): CalendarDate;
|
|
332
|
+
export function date(input: CalendarDateInput, zone: Zone): CalendarDate;
|
|
333
|
+
|
|
334
|
+
export function today(zone?: Zone): CalendarDate;
|
|
335
|
+
export function tomorrow(zone?: Zone): CalendarDate;
|
|
336
|
+
export function yesterday(zone?: Zone): CalendarDate;
|
|
337
|
+
|
|
338
|
+
export function isCalendarDate(value: unknown): value is CalendarDate;
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* A time of day with no date: opening hours, a daily reminder. Arithmetic wraps at midnight.
|
|
342
|
+
*/
|
|
343
|
+
export class Time {
|
|
344
|
+
constructor(input: TimeInput, options?: ReadOptions);
|
|
345
|
+
constructor(hour: number, minute?: number, second?: number);
|
|
346
|
+
|
|
347
|
+
/** The clock in the configured locale's common form, `'5:30 PM'`, what `format()` prints */
|
|
348
|
+
readonly text: string;
|
|
349
|
+
readonly hour: number;
|
|
350
|
+
readonly minute: number;
|
|
351
|
+
readonly second: number;
|
|
352
|
+
readonly millisecond: number;
|
|
353
|
+
readonly microsecond: number;
|
|
354
|
+
readonly nanosecond: number;
|
|
355
|
+
|
|
356
|
+
plus(amount: DurationInput, unit?: Unit): Time;
|
|
357
|
+
/** Alias for `plus` */
|
|
358
|
+
add(amount: DurationInput, unit?: Unit): Time;
|
|
359
|
+
minus(amount: DurationInput, unit?: Unit): Time;
|
|
360
|
+
/** Alias for `minus` */
|
|
361
|
+
subtract(amount: DurationInput, unit?: Unit): Time;
|
|
362
|
+
/** Alias for `minus` */
|
|
363
|
+
remove(amount: DurationInput, unit?: Unit): Time;
|
|
364
|
+
|
|
365
|
+
set(fields: Partial<Record<'hour' | 'minute' | 'second' | 'millisecond', number>>): Time;
|
|
366
|
+
set(unit: Unit, value: number): Time;
|
|
367
|
+
startOf(unit: Unit): Time;
|
|
368
|
+
endOf(unit: Unit): Time;
|
|
369
|
+
round(unit: Unit): Time;
|
|
370
|
+
round(increment: number, unit: Unit): Time;
|
|
371
|
+
floor(unit: Unit): Time;
|
|
372
|
+
floor(increment: number, unit: Unit): Time;
|
|
373
|
+
ceil(unit: Unit): Time;
|
|
374
|
+
ceil(increment: number, unit: Unit): Time;
|
|
375
|
+
|
|
376
|
+
equals(other: TimeInput): boolean;
|
|
377
|
+
isBefore(other: TimeInput): boolean;
|
|
378
|
+
isAfter(other: TimeInput): boolean;
|
|
379
|
+
isSame(other: TimeInput, unit?: Unit): boolean;
|
|
380
|
+
|
|
381
|
+
/** The signed length until another time within the day, or that length as a number in a unit */
|
|
382
|
+
until(other: TimeInput): Duration;
|
|
383
|
+
until(other: TimeInput, unit: Unit): number;
|
|
384
|
+
since(other: TimeInput): Duration;
|
|
385
|
+
since(other: TimeInput, unit: Unit): number;
|
|
386
|
+
/** This time on a date in a zone, the mirror of `date.at(time, zone)` */
|
|
387
|
+
on(day: CalendarDateInput, zone?: Zone): DateTime;
|
|
388
|
+
|
|
389
|
+
format(spec?: FormatSpec, locale?: Locale): string;
|
|
390
|
+
/** ISO 8601, `'17:30:00'` */
|
|
391
|
+
toString(): string;
|
|
392
|
+
toJSON(): string;
|
|
393
|
+
toTemporal(): TemporalValue;
|
|
394
|
+
/** Throws. A time of day is not a point on the number line */
|
|
395
|
+
valueOf(): never;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/** Reads `'09:00'`, `'5:30pm'`, `'17:30:15.250'`, numbers, a fields object, a datetime or a Date */
|
|
399
|
+
export function time(input: TimeInput, options?: ReadOptions): Time;
|
|
400
|
+
export function time(hour: number, minute?: number, second?: number): Time;
|
|
401
|
+
export function time(input: TimeInput, zone: Zone): Time;
|
|
402
|
+
|
|
403
|
+
export function isTime(value: unknown): value is Time;
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* A length of time. Fields stay as written until balanced, and a duration measured with `until()` or
|
|
407
|
+
* `since()` remembers where it started, so months and years can total in any unit.
|
|
408
|
+
*/
|
|
409
|
+
export class Duration {
|
|
410
|
+
constructor(input: DurationInput, unit?: Unit);
|
|
411
|
+
|
|
412
|
+
/** The length in the configured locale's common form, `'1 hour, 30 minutes'`, what `format('long')` prints */
|
|
413
|
+
readonly text: string;
|
|
414
|
+
readonly years: number;
|
|
415
|
+
readonly months: number;
|
|
416
|
+
readonly weeks: number;
|
|
417
|
+
readonly days: number;
|
|
418
|
+
readonly hours: number;
|
|
419
|
+
readonly minutes: number;
|
|
420
|
+
readonly seconds: number;
|
|
421
|
+
readonly milliseconds: number;
|
|
422
|
+
readonly microseconds: number;
|
|
423
|
+
readonly nanoseconds: number;
|
|
424
|
+
/** 1, 0 or -1 */
|
|
425
|
+
readonly sign: number;
|
|
426
|
+
isZero(): boolean;
|
|
427
|
+
isNegative(): boolean;
|
|
428
|
+
/** The point this duration was measured from, when it came from `until()` or `since()` */
|
|
429
|
+
readonly anchor: TemporalValue | undefined;
|
|
430
|
+
|
|
431
|
+
/** The nonzero fields as a plain object */
|
|
432
|
+
toFields(): DurationFields;
|
|
433
|
+
|
|
434
|
+
plus(other: DurationInput, unit?: Unit): Duration;
|
|
435
|
+
/** Alias for `plus` */
|
|
436
|
+
add(other: DurationInput, unit?: Unit): Duration;
|
|
437
|
+
minus(other: DurationInput, unit?: Unit): Duration;
|
|
438
|
+
/** Alias for `minus` */
|
|
439
|
+
subtract(other: DurationInput, unit?: Unit): Duration;
|
|
440
|
+
/** Alias for `minus` */
|
|
441
|
+
remove(other: DurationInput, unit?: Unit): Duration;
|
|
442
|
+
|
|
443
|
+
times(factor: number): Duration;
|
|
444
|
+
negated(): Duration;
|
|
445
|
+
abs(): Duration;
|
|
446
|
+
/** Carry overflow upward. A length stops at hours, an anchored duration at days, an explicit unit is taken as given */
|
|
447
|
+
balance(largest?: Unit): Duration;
|
|
448
|
+
/** Rounds to the nearest whole unit */
|
|
449
|
+
round(smallest: Unit): Duration;
|
|
450
|
+
|
|
451
|
+
/** The whole duration in one unit, fractional. Months and years need the anchor */
|
|
452
|
+
total(unit: Unit): number;
|
|
453
|
+
/** -1, 0 or 1 by length */
|
|
454
|
+
compare(other: DurationInput): number;
|
|
455
|
+
equals(other: DurationInput): boolean;
|
|
456
|
+
|
|
457
|
+
/** Words in the locale: `'2 hours, 30 minutes'`, `'2 hr, 30 min'`, `'2h 30m'`, `'2:30:00'` */
|
|
458
|
+
format(style?: 'long' | 'short' | 'narrow' | 'digital', locale?: Locale): string;
|
|
459
|
+
/** ISO 8601, `'PT1H30M'` */
|
|
460
|
+
toString(): string;
|
|
461
|
+
toJSON(): string;
|
|
462
|
+
toTemporal(): TemporalValue;
|
|
463
|
+
/** The whole length in milliseconds, months and years only when anchored */
|
|
464
|
+
toMilliseconds(): number;
|
|
465
|
+
valueOf(): number;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Reads a phrase, an ISO string, a fields object, or a number with a unit. A bare number is milliseconds */
|
|
469
|
+
export function duration(input: DurationInput, unit?: Unit): Duration;
|
|
470
|
+
|
|
471
|
+
export function years(count: number | string): Duration;
|
|
472
|
+
export function months(count: number | string): Duration;
|
|
473
|
+
export function weeks(count: number | string): Duration;
|
|
474
|
+
export function days(count: number | string): Duration;
|
|
475
|
+
export function hours(count: number | string): Duration;
|
|
476
|
+
export function minutes(count: number | string): Duration;
|
|
477
|
+
export function seconds(count: number | string): Duration;
|
|
478
|
+
export function milliseconds(count: number | string): Duration;
|
|
479
|
+
|
|
480
|
+
export function isDuration(value: unknown): value is Duration;
|
|
481
|
+
|
|
482
|
+
/** A datetime, a date or a time, or a string that reads as one */
|
|
483
|
+
export type Point = DateTime | CalendarDate | Time | Date | string;
|
|
484
|
+
|
|
485
|
+
/** Augmented by `@native-dates/plugins/ranges` with `DateRange | DateTimeRange | TimeRange` */
|
|
486
|
+
export interface RangeMap {}
|
|
487
|
+
|
|
488
|
+
export type Range = RangeMap[keyof RangeMap];
|
|
489
|
+
|
|
490
|
+
/** A sort comparator over points of one kind, durations, or ranges of one kind. A raw value reads as the first's kind */
|
|
491
|
+
export function compare(a: Point | Duration | Range, b: Point | Duration | Range): -1 | 0 | 1;
|
|
492
|
+
|
|
493
|
+
/** The point that comes first, from several arguments or one array */
|
|
494
|
+
export function earliest<T extends Point>(
|
|
495
|
+
...points: T[] | [T[]]
|
|
496
|
+
): T extends string ? DateTime | CalendarDate | Time : T;
|
|
497
|
+
/** The point that comes last, from several arguments or one array */
|
|
498
|
+
export function latest<T extends Point>(...points: T[] | [T[]]): T extends string ? DateTime | CalendarDate | Time : T;
|
|
499
|
+
|
|
500
|
+
/** The kind of a value from this library, or undefined */
|
|
501
|
+
export function kindOf(
|
|
502
|
+
value: unknown,
|
|
503
|
+
): 'datetime' | 'date' | 'time' | 'duration' | 'dateRange' | 'datetimeRange' | 'timeRange' | undefined;
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* The brands, keyed with `Symbol.for` so a value is recognised across bundles and realms without
|
|
507
|
+
* importing the class. `value[IS_DATE_TIME]` is true for a DateTime from any copy of this package.
|
|
508
|
+
*/
|
|
509
|
+
export const IS_DATE_TIME: unique symbol;
|
|
510
|
+
export const IS_CALENDAR_DATE: unique symbol;
|
|
511
|
+
export const IS_TIME: unique symbol;
|
|
512
|
+
export const IS_DURATION: unique symbol;
|
|
513
|
+
/** Every range kind */
|
|
514
|
+
export const IS_RANGE: unique symbol;
|
|
515
|
+
export const IS_DATE_RANGE: unique symbol;
|
|
516
|
+
export const IS_DATE_TIME_RANGE: unique symbol;
|
|
517
|
+
export const IS_TIME_RANGE: unique symbol;
|
|
518
|
+
|
|
519
|
+
/** The ISO number of a weekday from its name, `weekday('sunday')` is 7, a number passing through */
|
|
520
|
+
export function weekday(day: Weekday): number;
|
|
521
|
+
|
|
522
|
+
export interface Settings {
|
|
523
|
+
/** The default zone every factory reads in. Null resets to the machine's zone */
|
|
524
|
+
zone?: Zone | null;
|
|
525
|
+
/** The default locale for formatting. Null resets to the runtime's */
|
|
526
|
+
locale?: Locale | null;
|
|
527
|
+
/** The first day of the week for `startOf('week')` (default: monday) */
|
|
528
|
+
weekStart?: Weekday;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Sets package-wide defaults once at app boot. Returns the settings in effect */
|
|
532
|
+
export function configure(settings?: Settings): {
|
|
533
|
+
zone: string | undefined;
|
|
534
|
+
locale: string | undefined;
|
|
535
|
+
weekStart: number;
|
|
536
|
+
dayFirst: boolean;
|
|
537
|
+
zoneAliases: Record<string, string>;
|
|
538
|
+
};
|