Debian Codegraph @

types-luxon/src/interval.d.ts

raw
1 import { CanBeInvalid, DefaultValidity, IfValid, Invalid, Valid } from "./_util";
2 import {
3 _UseLocaleWeekOption,
4 DateObjectUnits,
5 DateTime,
6 DateTimeOptions,
7 DiffOptions,
8 LocaleOptions,
9 ToISOTimeOptions,
10 } from "./datetime";
11 import { Duration, DurationLike, DurationMaybeValid, DurationUnit } from "./duration";
12
13 export interface IntervalObject {
14 start?: DateTime | undefined;
15 end?: DateTime | undefined;
16 }
17
18 export type DateInput = DateTime | DateObjectUnits | Date;
19
20 export type IntervalMaybeValid = CanBeInvalid extends true ? (Interval<Valid> | Interval<Invalid>) : Interval;
21
22 export type CountOptions = _UseLocaleWeekOption;
23
24 /**
25 * An Interval object represents a half-open interval of time, where each endpoint is a {@link DateTime}.
26 * Conceptually, it is a container for those two endpoints, accompanied by methods for
27 * creating, parsing, interrogating, comparing, transforming, and formatting them.
28 *
29 * Here is a brief overview of the most commonly used methods and getters in Interval:
30 *
31 * * **Creation** To create an Interval, use {@link Interval.fromDateTimes}, {@link Interval.after}, {@link Interval.before}, or {@link Interval.fromISO}.
32 * * **Accessors** Use {@link Interval#start} and {@link Interval#end} to get the start and end.
33 * * **Interrogation** To analyze the Interval, use {@link Interval#count}, {@link Interval#length}, {@link Interval#hasSame},
34 * * {@link Interval#contains}, {@link Interval#isAfter}, or {@link Interval#isBefore}.
35 * * **Transformation** To create other Intervals out of this one, use {@link Interval#set}, {@link Interval#splitAt}, {@link Interval#splitBy}, {@link Interval#divideEqually},
36 * * {@link Interval.merge}, {@link Interval.xor}, {@link Interval#union}, {@link Interval#intersection}, or {@link Interval#difference}.
37 * * **Comparison** To compare this Interval to another one, use {@link Interval#equals}, {@link Interval#overlaps}, {@link Interval#abutsStart}, {@link Interval#abutsEnd}, {@link Interval#engulfs}
38 * * **Output** To convert the Interval into other representations, see {@link Interval#toString}, {@link Interval#toLocaleString}, {@link Interval#toISO}, {@link Interval#toISODate},
39 * * {@link Interval#toISOTime}, {@link Interval#toFormat}, and {@link Interval#toDuration}.
40 */
41 export class Interval<IsValid extends boolean = DefaultValidity> {
42 /**
43 * Create an invalid Interval.
44 *
45 * @param reason - simple string of why this Interval is invalid. Should not contain parameters or anything else data-dependent
46 * @param explanation - longer explanation, may include parameters and other useful debugging information.
47 */
48 static invalid(reason: string, explanation?: string): Interval<Invalid>;
49
50 /**
51 * Create an Interval from a start DateTime and an end DateTime. Inclusive of the start but not the end.
52 *
53 * @param start
54 * @param end
55 */
56 static fromDateTimes(start: DateInput, end: DateInput): IntervalMaybeValid;
57
58 /**
59 * Create an Interval from a start DateTime and a Duration to extend to.
60 *
61 * @param start
62 * @param duration - the length of the Interval.
63 */
64 static after(start: DateInput, duration: DurationLike): IntervalMaybeValid;
65
66 /**
67 * Create an Interval from an end DateTime and a Duration to extend backwards to.
68 *
69 * @param end
70 * @param duration - the length of the Interval.
71 */
72 static before(end: DateInput, duration: DurationLike): IntervalMaybeValid;
73
74 /**
75 * Create an Interval from an ISO 8601 string.
76 * Accepts `<start>/<end>`, `<start>/<duration>`, and `<duration>/<end>` formats.
77 * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
78 *
79 * @param text - the ISO string to parse
80 * @param opts - options to pass {@link DateTime.fromISO} and optionally {@link Duration.fromISO}
81 */
82 static fromISO(text: string, opts?: DateTimeOptions): IntervalMaybeValid;
83
84 /**
85 * Check if an object is an Interval. Works across context boundaries
86 *
87 * @param o
88 */
89 static isInterval(o: unknown): o is IntervalMaybeValid;
90
91 private constructor(config: unknown);
92
93 /**
94 * Returns the start of the Interval
95 */
96 get start(): IfValid<DateTime<Valid>, null, IsValid>;
97
98 /**
99 * Returns the end of the Interval. This is the first instant which is not part of the interval.
100 * (Interval is half-open).
101 */
102 get end(): IfValid<DateTime<Valid>, null, IsValid>;
103
104 /**
105 * Returns the last DateTime included in the interval (since end is not part of the interval)
106 */
107 get lastDateTime(): IfValid<DateTime<Valid>, null, IsValid>;
108
109 /**
110 * Returns whether this Interval's end is at least its start, meaning that the Interval isn't 'backwards'.
111 */
112 get isValid(): IfValid<true, false, IsValid>;
113
114 /**
115 * Returns an error code if this Interval is invalid, or null if the Interval is valid
116 */
117 get invalidReason(): IfValid<null, string, IsValid>;
118
119 /**
120 * Returns an explanation of why this Interval became invalid, or null if the Interval is valid
121 */
122 get invalidExplanation(): IfValid<null, string | null, IsValid>;
123
124 /**
125 * Returns the length of the Interval in the specified unit.
126 *
127 * @param unit - the unit (such as 'hours' or 'days') to return the length in.
128 */
129 length(unit?: DurationUnit): IfValid<number, typeof NaN, IsValid>;
130
131 /**
132 * Returns the count of minutes, hours, days, months, or years included in the Interval, even in part.
133 * Unlike {@link Interval#length} this counts sections of the calendar, not periods of time, e.g. specifying 'day'
134 * asks 'what dates are included in this interval?', not 'how many days long is this interval?'
135 *
136 * @param unit - the unit of time to count. Defaults to 'milliseconds'.
137 */
138 count(unit?: DurationUnit, opts?: CountOptions): IfValid<number, typeof NaN, IsValid>;
139
140 /**
141 * Returns whether this Interval's start and end are both in the same unit of time
142 *
143 * @param unit - the unit of time to check sameness on
144 */
145 hasSame(unit: DurationUnit): IfValid<boolean, false, IsValid>;
146
147 /**
148 * Return whether this Interval has the same start and end DateTimes.
149 */
150 isEmpty(): boolean;
151
152 /**
153 * Return whether this Interval's start is after the specified DateTime.
154 *
155 * @param dateTime
156 */
157 isAfter(dateTime: DateTime): IfValid<boolean, false, IsValid>;
158
159 /**
160 * Return whether this Interval's end is before the specified DateTime.
161 *
162 * @param dateTime
163 */
164 isBefore(dateTime: DateTime): IfValid<boolean, false, IsValid>;
165
166 /**
167 * Return whether this Interval contains the specified DateTime.
168 *
169 * @param dateTime
170 */
171 contains(dateTime: DateTime): IfValid<boolean, false, IsValid>;
172
173 /**
174 * "Sets" the start and/or end dates. Returns a newly-constructed Interval.
175 *
176 * @param values - the values to set
177 * @param values.start - the starting DateTime
178 * @param values.end - the ending DateTime
179 */
180 set(values?: IntervalObject): IntervalMaybeValid;
181
182 /**
183 * Split this Interval at each of the specified DateTimes
184 *
185 * @param dateTimes - the unit of time to count.
186 */
187 splitAt(...dateTimes: DateTime[]): IfValid<Interval[], [], IsValid>;
188
189 /**
190 * Split this Interval into smaller Intervals, each of the specified length.
191 * Left over time is grouped into a smaller interval
192 *
193 * @param duration - The length of each resulting interval.
194 */
195 splitBy(duration: DurationLike): IfValid<Interval[], [], IsValid>;
196
197 /**
198 * Split this Interval into the specified number of smaller intervals.
199 *
200 * @param numberOfParts - The number of Intervals to divide the Interval into.
201 */
202 divideEqually(numberOfParts: number): IfValid<Interval[], [], IsValid>;
203
204 /**
205 * Return whether this Interval overlaps with the specified Interval
206 */
207 overlaps(other: Interval): boolean;
208
209 /**
210 * Return whether this Interval's end is adjacent to the specified Interval's start.
211 */
212 abutsStart(other: Interval): IfValid<boolean, false, IsValid>;
213
214 /**
215 * Return whether this Interval's start is adjacent to the specified Interval's end.
216 */
217 abutsEnd(other: Interval): IfValid<boolean, false, IsValid>;
218
219 /**
220 * Return whether this Interval engulfs the start and end of the specified Interval.
221 */
222 engulfs(other: Interval): IfValid<boolean, false, IsValid>;
223
224 /**
225 * Return whether this Interval has the same start and end as the specified Interval.
226 */
227 equals(other: Interval): IfValid<boolean, false, IsValid>;
228
229 /**
230 * Return an Interval representing the intersection of this Interval and the specified Interval.
231 * Specifically, the resulting Interval has the maximum start time and the minimum end time of the two Intervals.
232 * Returns null if the intersection is empty, meaning the intervals do not intersect.
233 */
234 intersection(other: Interval): Interval | null;
235
236 /**
237 * Return an Interval representing the union of this Interval and the specified Interval.
238 * Specifically, the resulting Interval has the minimum start time and the maximum end time of the two Intervals.
239 */
240 union(other: Interval): IntervalMaybeValid;
241
242 /**
243 * Merge an array of Intervals into an equivalent minimal set of Intervals.
244 * Combines overlapping and adjacent Intervals.
245 */
246 static merge(intervals: Interval[]): IntervalMaybeValid[];
247
248 /**
249 * Return an array of Intervals representing the spans of time that only appear in one of the specified Intervals.
250 */
251 static xor(intervals: Interval[]): IntervalMaybeValid[];
252
253 /**
254 * Return Intervals representing the spans of time in this Interval that not overlap with any of the specified Intervals.
255 */
256 difference(...intervals: Interval[]): IntervalMaybeValid[];
257
258 /**
259 * Returns a string representation of this Interval appropriate for debugging.
260 */
261 toString(): IfValid<string, "Invalid Interval", IsValid>;
262
263 /**
264 * Returns a localized string representing this Interval. Accepts the same options as the
265 * Intl.DateTimeFormat constructor and any presets defined by Luxon, such as
266 * {@link DateTime.DATE_FULL} or {@link DateTime.TIME_SIMPLE}. The exact behavior of this method
267 * is browser-specific, but in general it will return an appropriate representation of the
268 * Interval in the assigned locale. Defaults to the system's locale if no locale has been
269 * specified.
270 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
271 * @param formatOpts - Either a DateTime preset or Intl.DateTimeFormat constructor options. Defaults to DateTime.DATE_SHORT
272 * @param opts - Options to override the configuration of the start DateTime.
273 *
274 * @example
275 * Interval.fromISO('2022-11-07T09:00Z/2022-11-08T09:00Z').toLocaleString(); //=> 11/7/2022 – 11/8/2022
276 * @example
277 * Interval.fromISO('2022-11-07T09:00Z/2022-11-08T09:00Z').toLocaleString(DateTime.DATE_FULL); //=> November 7 – 8, 2022
278 * @example
279 * Interval.fromISO('2022-11-07T09:00Z/2022-11-08T09:00Z').toLocaleString(DateTime.DATE_FULL, { locale: 'fr-FR' }); //=> 7–8 novembre 2022
280 * @example
281 * Interval.fromISO('2022-11-07T17:00Z/2022-11-07T19:00Z').toLocaleString(DateTime.TIME_SIMPLE); //=> 6:00 – 8:00 PM
282 * @example
283 * Interval.fromISO("2022-11-07T17:00Z/2022-11-07T19:00Z").toLocaleString({
284 * weekday: "short",
285 * month: "short",
286 * day: "2-digit",
287 * hour: "2-digit",
288 * minute: "2-digit",
289 * }); //=> Mon, Nov 07, 6:00 – 8:00 p
290 */
291 toLocaleString(
292 formatOpts?: Intl.DateTimeFormatOptions,
293 opts?: LocaleOptions,
294 ): IfValid<string, "Invalid Interval", IsValid>;
295
296 /**
297 * Returns an ISO 8601-compliant string representation of this Interval.
298 * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
299 *
300 * @param opts - The same options as {@link DateTime#toISO}
301 */
302 toISO(opts?: ToISOTimeOptions): IfValid<string, "Invalid Interval", IsValid>;
303
304 /**
305 * Returns an ISO 8601-compliant string representation of the dates in this Interval.
306 * The time components are ignored.
307 * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
308 */
309 toISODate(): IfValid<string, "Invalid Interval", IsValid>;
310
311 /**
312 * Returns an ISO 8601-compliant string representation of the times in this Interval.
313 * The date components are ignored.
314 * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
315 *
316 * @param opts - The same options as {@link DateTime.toISO}
317 */
318 toISOTime(opts?: ToISOTimeOptions): IfValid<string, "Invalid Interval", IsValid>;
319
320 /**
321 * Returns a string representation of this Interval formatted according to the specified format string.
322 *
323 * @param dateFormat - the format string. This string formats the start and end time. See {@link DateTime.toFormat} for details.
324 * @param opts - options
325 * @param opts.separator - a separator to place between the start and end representations. Defaults to ' - '.
326 */
327 toFormat(
328 dateFormat: string,
329 opts?: {
330 separator?: string | undefined;
331 },
332 ): IfValid<string, "Invalid Interval", IsValid>;
333
334 /**
335 * Return a Duration representing the time spanned by this interval.
336 *
337 * @param unit - the unit or units (such as 'hours' or 'days') to include in the duration. Defaults to ['milliseconds'].
338 * @param opts - options that affect the creation of the Duration
339 * @param opts.conversionAccuracy - the conversion system to use. Defaults to 'casual'.
340 *
341 * @example
342 * Interval.fromDateTimes(dt1, dt2).toDuration().toObject() //=> { milliseconds: 88489257 }
343 * @example
344 * Interval.fromDateTimes(dt1, dt2).toDuration('days').toObject() //=> { days: 1.0241812152777778 }
345 * @example
346 * Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes']).toObject() //=> { hours: 24, minutes: 34.82095 }
347 * @example
348 * Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes', 'seconds']).toObject() //=> { hours: 24, minutes: 34, seconds: 49.257 }
349 * @example
350 * Interval.fromDateTimes(dt1, dt2).toDuration('seconds').toObject() //=> { seconds: 88489.257 }
351 */
352 toDuration(unit?: DurationUnit | DurationUnit[], opts?: DiffOptions): DurationMaybeValid;
353
354 /**
355 * Run mapFn on the interval start and end, returning a new Interval from the resulting DateTimes
356 *
357 * @example
358 * Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.toUTC())
359 * @example
360 * Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.plus({ hours: 2 }))
361 */
362 mapEndpoints(mapFn: (d: DateTime) => DateTime): IntervalMaybeValid;
363 }