UNPKG

4.96 kBTypeScriptView Raw
1/**
2 * Represents a length of time.
3 *
4 * The amount can be specified either as a literal value (e.g: `10`) which
5 * cannot be negative, or as an unresolved number token.
6 *
7 * When the amount is passed as a token, unit conversion is not possible.
8 */
9export declare class Duration {
10 /**
11 * Create a Duration representing an amount of milliseconds
12 *
13 * @param amount the amount of Milliseconds the `Duration` will represent.
14 * @returns a new `Duration` representing `amount` ms.
15 */
16 static millis(amount: number): Duration;
17 /**
18 * Create a Duration representing an amount of seconds
19 *
20 * @param amount the amount of Seconds the `Duration` will represent.
21 * @returns a new `Duration` representing `amount` Seconds.
22 */
23 static seconds(amount: number): Duration;
24 /**
25 * Create a Duration representing an amount of minutes
26 *
27 * @param amount the amount of Minutes the `Duration` will represent.
28 * @returns a new `Duration` representing `amount` Minutes.
29 */
30 static minutes(amount: number): Duration;
31 /**
32 * Create a Duration representing an amount of hours
33 *
34 * @param amount the amount of Hours the `Duration` will represent.
35 * @returns a new `Duration` representing `amount` Hours.
36 */
37 static hours(amount: number): Duration;
38 /**
39 * Create a Duration representing an amount of days
40 *
41 * @param amount the amount of Days the `Duration` will represent.
42 * @returns a new `Duration` representing `amount` Days.
43 */
44 static days(amount: number): Duration;
45 /**
46 * Parse a period formatted according to the ISO 8601 standard
47 *
48 * @see https://www.iso.org/fr/standard/70907.html
49 * @param duration an ISO-formtted duration to be parsed.
50 * @returns the parsed `Duration`.
51 */
52 static parse(duration: string): Duration;
53 private readonly amount;
54 private readonly unit;
55 private constructor();
56 /**
57 * Add two Durations together
58 */
59 plus(rhs: Duration): Duration;
60 /**
61 * Substract two Durations together
62 */
63 minus(rhs: Duration): Duration;
64 /**
65 * Return the total number of milliseconds in this Duration
66 *
67 * @returns the value of this `Duration` expressed in Milliseconds.
68 */
69 toMilliseconds(opts?: TimeConversionOptions): number;
70 /**
71 * Return the total number of seconds in this Duration
72 *
73 * @returns the value of this `Duration` expressed in Seconds.
74 */
75 toSeconds(opts?: TimeConversionOptions): number;
76 /**
77 * Return the total number of minutes in this Duration
78 *
79 * @returns the value of this `Duration` expressed in Minutes.
80 */
81 toMinutes(opts?: TimeConversionOptions): number;
82 /**
83 * Return the total number of hours in this Duration
84 *
85 * @returns the value of this `Duration` expressed in Hours.
86 */
87 toHours(opts?: TimeConversionOptions): number;
88 /**
89 * Return the total number of days in this Duration
90 *
91 * @returns the value of this `Duration` expressed in Days.
92 */
93 toDays(opts?: TimeConversionOptions): number;
94 /**
95 * Return an ISO 8601 representation of this period
96 *
97 * @returns a string starting with 'P' describing the period
98 * @see https://www.iso.org/fr/standard/70907.html
99 */
100 toIsoString(): string;
101 /**
102 * Return an ISO 8601 representation of this period
103 *
104 * @returns a string starting with 'P' describing the period
105 * @see https://www.iso.org/fr/standard/70907.html
106 * @deprecated Use `toIsoString()` instead.
107 */
108 toISOString(): string;
109 /**
110 * Turn this duration into a human-readable string
111 */
112 toHumanString(): string;
113 /**
114 * Returns a string representation of this `Duration`
115 *
116 * This is is never the right function to use when you want to use the `Duration`
117 * object in a template. Use `toSeconds()`, `toMinutes()`, `toDays()`, etc. instead.
118 */
119 toString(): string;
120 /**
121 * Return the duration in a set of whole numbered time components, ordered from largest to smallest
122 *
123 * Only components != 0 will be returned.
124 *
125 * Can combine millis and seconds together for the benefit of toIsoString,
126 * makes the logic in there simpler.
127 */
128 private components;
129 /**
130 * Checks if duration is a token or a resolvable object
131 */
132 isUnresolved(): boolean;
133 /**
134 * Returns unit of the duration
135 */
136 unitLabel(): string;
137 /**
138 * Returns stringified number of duration
139 */
140 formatTokenToNumber(): string;
141}
142/**
143 * Options for how to convert time to a different unit.
144 */
145export interface TimeConversionOptions {
146 /**
147 * If `true`, conversions into a larger time unit (e.g. `Seconds` to `Minutes`) will fail if the result is not an
148 * integer.
149 *
150 * @default true
151 */
152 readonly integral?: boolean;
153}