1 | import { DateTimeFormatOptions, NumberingSystem } from './misc';
|
2 | import { ConversionAccuracy } from './datetime';
|
3 |
|
4 | export interface DurationOptions {
|
5 | locale?: string;
|
6 | numberingSystem?: NumberingSystem;
|
7 | conversionAccuracy?: ConversionAccuracy;
|
8 | }
|
9 |
|
10 | export interface DurationObjectUnits {
|
11 | year?: number;
|
12 | years?: number;
|
13 | quarter?: number;
|
14 | quarters?: number;
|
15 | month?: number;
|
16 | months?: number;
|
17 | week?: number;
|
18 | weeks?: number;
|
19 | day?: number;
|
20 | days?: number;
|
21 | hour?: number;
|
22 | hours?: number;
|
23 | minute?: number;
|
24 | minutes?: number;
|
25 | second?: number;
|
26 | seconds?: number;
|
27 | millisecond?: number;
|
28 | milliseconds?: number;
|
29 | }
|
30 |
|
31 | export interface DurationObject extends DurationObjectUnits, DurationOptions {}
|
32 |
|
33 | export type DurationUnit = keyof DurationObjectUnits;
|
34 | export type DurationUnits = DurationUnit | DurationUnit[];
|
35 |
|
36 | export interface DurationToFormatOptions extends DateTimeFormatOptions {
|
37 | floor?: boolean;
|
38 | round?: boolean;
|
39 | }
|
40 |
|
41 | export type ToISOFormat = 'basic' | 'extended';
|
42 |
|
43 | export interface ToISOTimeDurationOptions {
|
44 | /**
|
45 | * Include the `T` prefix
|
46 | * @default false
|
47 | */
|
48 | includePrefix?: boolean;
|
49 | /**
|
50 | * Exclude milliseconds from the format if they're 0
|
51 | * @default false
|
52 | */
|
53 | suppressMilliseconds?: boolean;
|
54 | /**
|
55 | * Exclude seconds from the format if they're 0
|
56 | * @default false
|
57 | */
|
58 | suppressSeconds?: boolean;
|
59 | /**
|
60 | * Choose between the basic and extended format
|
61 | * @default 'extended'
|
62 | */
|
63 | format?: ToISOFormat;
|
64 | }
|
65 |
|
66 | /**
|
67 | * Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject()
|
68 | */
|
69 | export type DurationInput = Duration | number | DurationObject;
|
70 |
|
71 | /**
|
72 | * A Duration object represents a period of time, like "2 months" or "1 day, 1 hour".
|
73 | * Conceptually, it's just a map of units to their quantities, accompanied by some additional configuration and methods
|
74 | * for creating, parsing, interrogating, transforming, and formatting them.
|
75 | * They can be used on their own or in conjunction with other Luxon types;
|
76 | * for example, you can use {@link DateTime.plus} to add a Duration object to a DateTime, producing another DateTime.
|
77 | *
|
78 | * Here is a brief overview of commonly used methods and getters in Duration:
|
79 | *
|
80 | * * **Creation** To create a Duration, use {@link Duration.fromMillis}, {@link Duration.fromObject}, or {@link Duration.fromISO}.
|
81 | * * **Unit values** See the {@link Duration.years}, {@link Duration.months}, {@link Duration.weeks}, {@link Duration.days},
|
82 | * {@link Duration.hours}, {@link Duration.minutes}, {@link Duration.seconds}, {@link Duration.milliseconds} accessors.
|
83 | * * **Configuration** See {@link Duration.locale} and {@link Duration.numberingSystem} accessors.
|
84 | * * **Transformation** To create new Durations out of old ones use {@link Duration.plus}, {@link Duration.minus}, {@link Duration.normalize},
|
85 | * {@link Duration.set}, {@link Duration.reconfigure}, {@link Duration.shiftTo}, and {@link Duration.negate}.
|
86 | * * **Output** To convert the Duration into other representations, see {@link Duration.as}, {@link Duration.toISO}, {@link Duration.toFormat}, and {@link Duration.toJSON}
|
87 | *
|
88 | * There's are more methods documented below. In addition, for more information on subtler topics like internationalization and validity, see the external documentation.
|
89 | */
|
90 | export class Duration {
|
91 | /**
|
92 | * Create a Duration from an ISO 8601 duration string.
|
93 | * @see https://en.wikipedia.org/wiki/ISO_8601#Durations
|
94 | * @example
|
95 | * Duration.fromISO('P3Y6M1W4DT12H30M5S').toObject() //=> { years: 3, months: 6, weeks: 1, days: 4, hours: 12, minutes: 30, seconds: 5 }
|
96 | * @example
|
97 | * Duration.fromISO('PT23H').toObject() //=> { hours: 23 }
|
98 | * @example
|
99 | * Duration.fromISO('P5Y3M').toObject() //=> { years: 5, months: 3 }
|
100 | */
|
101 | static fromISO(text: string, options?: DurationOptions): Duration;
|
102 |
|
103 | /**
|
104 | * Create a Duration from an ISO 8601 time string.
|
105 | * @see https://en.wikipedia.org/wiki/ISO_8601#Times
|
106 | * @example
|
107 | * Duration.fromISOTime('11:22:33.444').toObject() //=> { hours: 11, minutes: 22, seconds: 33, milliseconds: 444 }
|
108 | * @example
|
109 | * Duration.fromISOTime('11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
|
110 | * @example
|
111 | * Duration.fromISOTime('T11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
|
112 | * @example
|
113 | * Duration.fromISOTime('1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
|
114 | * @example
|
115 | * Duration.fromISOTime('T1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
|
116 | */
|
117 | static fromISOTime(text: string, options?: DurationOptions): Duration;
|
118 |
|
119 | /**
|
120 | * Create Duration from a number of milliseconds.
|
121 | * @param count of milliseconds
|
122 | * @param [options] - options for parsing
|
123 | * @param [options.locale='en-US'] - the locale to use
|
124 | * @param [options.numberingSystem] - the numbering system to use
|
125 | * @param [options.conversionAccuracy='casual'] - the conversion system to use
|
126 | */
|
127 | static fromMillis(count: number, options?: DurationOptions): Duration;
|
128 |
|
129 | /**
|
130 | * Create a Duration from a JavaScript object with keys like 'years' and 'hours'.
|
131 | * If this object is empty then a zero milliseconds duration is returned.
|
132 | */
|
133 | static fromObject(obj: DurationObject): Duration;
|
134 |
|
135 | /**
|
136 | * Create an invalid Duration.
|
137 | * @param reason - simple string of why this datetime is invalid. Should not contain parameters or anything else data-dependent
|
138 | * @param [explanation] - longer explanation, may include parameters and other useful debugging information
|
139 | */
|
140 | static invalid(reason: string, explanation?: string): Duration;
|
141 |
|
142 | /**
|
143 | * Check if an object is a Duration. Works across context boundaries
|
144 | */
|
145 | static isDuration(o: any): o is Duration;
|
146 |
|
147 | days: number;
|
148 | hours: number;
|
149 | /**
|
150 | * Returns an error code if this Duration became invalid, or null if the Duration is valid
|
151 | */
|
152 | invalidReason: string | null;
|
153 | /**
|
154 | * Returns an explanation of why this Duration became invalid, or null if the Duration is valid
|
155 | */
|
156 | invalidExplanation: string | null;
|
157 |
|
158 | /**
|
159 | * Returns whether the Duration is invalid. Invalid durations are returned by diff operations
|
160 | * on invalid DateTimes or Intervals.
|
161 | */
|
162 | isValid: boolean;
|
163 |
|
164 | /**
|
165 | * Get the locale of a Duration, such 'en-GB'
|
166 | */
|
167 | locale: string;
|
168 | milliseconds: number;
|
169 | minutes: number;
|
170 | months: number;
|
171 |
|
172 | /**
|
173 | * Get the numbering system of a Duration, such 'beng'. The numbering system is used when formatting the Duration
|
174 | */
|
175 | numberingSystem: string;
|
176 | quarters: number;
|
177 | seconds: number;
|
178 | weeks: number;
|
179 | years: number;
|
180 |
|
181 | /**
|
182 | * Return the length of the duration in the specified unit.
|
183 | * @example
|
184 | * Duration.fromObject({years: 1}).as('days') //=> 365
|
185 | * @example
|
186 | * Duration.fromObject({years: 1}).as('months') //=> 12
|
187 | * @example
|
188 | * Duration.fromObject({hours: 60}).as('days') //=> 2.5
|
189 | */
|
190 | as(unit: DurationUnit): number;
|
191 |
|
192 | /**
|
193 | * Equality check
|
194 | * Two Durations are equal if they have the same units and the same values for each unit.
|
195 | */
|
196 | equals(other: Duration): boolean;
|
197 |
|
198 | /**
|
199 | * Get the value of unit.
|
200 | * @example
|
201 | * Duration.fromObject({years: 2, days: 3}).years //=> 2
|
202 | * @example
|
203 | * Duration.fromObject({years: 2, days: 3}).months //=> 0
|
204 | * @example
|
205 | * Duration.fromObject({years: 2, days: 3}).days //=> 3
|
206 | */
|
207 | get(unit: DurationUnit): number;
|
208 |
|
209 | /**
|
210 | * Make this Duration shorter by the specified amount. Return a newly-constructed Duration.
|
211 | */
|
212 | minus(duration: DurationInput): Duration;
|
213 |
|
214 | /**
|
215 | * Return the negative of this Duration.
|
216 | * @example
|
217 | * Duration.fromObject({ hours: 1, seconds: 30 }).negate().toObject() //=> { hours: -1, seconds: -30 }
|
218 | */
|
219 | negate(): Duration;
|
220 |
|
221 | /**
|
222 | * Reduce this Duration to its canonical representation in its current units.
|
223 | * @example
|
224 | * Duration.fromObject({ years: 2, days: 5000 }).normalize().toObject() //=> { years: 15, days: 255 }
|
225 | * @example
|
226 | * Duration.fromObject({ hours: 12, minutes: -45 }).normalize().toObject() //=> { hours: 11, minutes: 15 }
|
227 | */
|
228 | normalize(): Duration;
|
229 |
|
230 | /**
|
231 | * Make this Duration longer by the specified amount. Return a newly-constructed Duration.
|
232 | */
|
233 | plus(duration: DurationInput): Duration;
|
234 |
|
235 | /**
|
236 | * "Set" the Duration's options. Returns a newly-constructed Duration.
|
237 | */
|
238 | reconfigure(options: DurationOptions): Duration;
|
239 |
|
240 | /**
|
241 | * "Set" the values of specified units. Return a newly-constructed Duration.
|
242 | */
|
243 | set(values: DurationObjectUnits): Duration;
|
244 |
|
245 | /**
|
246 | * Convert this Duration into its representation in a different set of units.
|
247 | * @example
|
248 | * Duration.fromObject({ hours: 1, seconds: 30 }).shiftTo('minutes', 'milliseconds').toObject() //=> { minutes: 60, milliseconds: 30000 }
|
249 | */
|
250 | shiftTo(...units: DurationUnit[]): Duration;
|
251 |
|
252 | /**
|
253 | * Scale this Duration by the specified amount. Return a newly-constructed Duration.
|
254 | * @example
|
255 | * Duration.fromObject({ hours: 1, minutes: 30 }).mapUnit(x => x * 2) //=> { hours: 2, minutes: 60 }
|
256 | * @example
|
257 | * Duration.fromObject({ hours: 1, minutes: 30 }).mapUnit((x, u) => u === "hour" ? x * 2 : x) //=> { hours: 2, minutes: 30 }
|
258 | */
|
259 | mapUnits(fn: (x: number, u: DurationUnit) => number): Duration;
|
260 |
|
261 | /**
|
262 | * Returns a string representation of this Duration formatted according to the specified format string.
|
263 | *
|
264 | * You may use these tokens:
|
265 | * * `S` for milliseconds
|
266 | * * `s` for seconds
|
267 | * * `m` for minutes
|
268 | * * `h` for hours
|
269 | * * `d` for days
|
270 | * * `M` for months
|
271 | * * `y` for years
|
272 | *
|
273 | * Notes:
|
274 | * * Add padding by repeating the token, e.g. `yy` pads the years to two digits, `hhhh` pads the hours out to four digits
|
275 | * * The duration will be converted to the set of units in the format string using {@link Duration.shiftTo} and the Duration's conversion accuracy setting.
|
276 | *
|
277 | * @param format - the format string
|
278 | * @param [options] - options
|
279 | * @param [options.floor=true] - floor numerical values
|
280 | * @example
|
281 | * Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("y d s") //=> "1 6 2"
|
282 | * @example
|
283 | * Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("yy dd sss") //=> "01 06 002"
|
284 | * @example
|
285 | * Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("M S") //=> "12 518402000"
|
286 | */
|
287 | toFormat(format: string, options?: DurationToFormatOptions): string;
|
288 |
|
289 | /**
|
290 | * Returns an ISO 8601-compliant string representation of this Duration.
|
291 | * @see https://en.wikipedia.org/wiki/ISO_8601#Durations
|
292 | * @example
|
293 | * Duration.fromObject({ years: 3, seconds: 45 }).toISO() //=> 'P3YT45S'
|
294 | * @example
|
295 | * Duration.fromObject({ months: 4, seconds: 45 }).toISO() //=> 'P4MT45S'
|
296 | * @example
|
297 | * Duration.fromObject({ months: 5 }).toISO() //=> 'P5M'
|
298 | * @example
|
299 | * Duration.fromObject({ minutes: 5 }).toISO() //=> 'PT5M'
|
300 | * @example
|
301 | * Duration.fromObject({ milliseconds: 6 }).toISO() //=> 'PT0.006S'
|
302 | */
|
303 | toISO(): string;
|
304 |
|
305 | /**
|
306 | * Returns an ISO 8601-compliant string representation of this Duration, formatted as a time of day.
|
307 | * Note that this will return null if the duration is invalid, negative, or equal to or greater than 24 hours.
|
308 | * @see https://en.wikipedia.org/wiki/ISO_8601#Times
|
309 | * @example
|
310 | * Duration.fromObject({ hours: 11 }).toISOTime() //=> '11:00:00.000'
|
311 | * @example
|
312 | * Duration.fromObject({ hours: 11 }).toISOTime({ suppressMilliseconds: true }) //=> '11:00:00'
|
313 | * @example
|
314 | * Duration.fromObject({ hours: 11 }).toISOTime({ suppressSeconds: true }) //=> '11:00'
|
315 | * @example
|
316 | * Duration.fromObject({ hours: 11 }).toISOTime({ includePrefix: true }) //=> 'T11:00:00.000'
|
317 | * @example
|
318 | * Duration.fromObject({ hours: 11 }).toISOTime({ format: 'basic' }) //=> '110000.000'
|
319 | */
|
320 | toISOTime(options?: ToISOTimeDurationOptions): string; // | null
|
321 |
|
322 | /**
|
323 | * Returns an ISO 8601 representation of this Duration appropriate for use in JSON.
|
324 | * Called implicitly via {@link JSON.stringify}
|
325 | */
|
326 | toJSON(): string;
|
327 |
|
328 | /**
|
329 | * Returns a milliseconds value of this Duration.
|
330 | */
|
331 | toMillis(): number;
|
332 |
|
333 | /**
|
334 | * Returns a JavaScript object with this Duration's values.
|
335 | * @param [options] - options for generating the object
|
336 | * @param [options.includeConfig=false] - include configuration attributes in the output
|
337 | * @example
|
338 | * Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toObject() //=> { years: 1, days: 6, seconds: 2 }
|
339 | */
|
340 | toObject(options?: {
|
341 | /**
|
342 | * include configuration attributes in the output
|
343 | */
|
344 | includeConfig?: boolean
|
345 | }): DurationObject;
|
346 |
|
347 | /**
|
348 | * Returns an ISO 8601 representation of this Duration appropriate for use in debugging.
|
349 | */
|
350 | toString(): string;
|
351 |
|
352 | /**
|
353 | * Returns an milliseconds value of this Duration. Alias of {@link toMillis}
|
354 | * Called implicitly when coercing types.
|
355 | */
|
356 | valueOf(): number;
|
357 | }
|