-
Notifications
You must be signed in to change notification settings - Fork 4k
/
duration.ts
328 lines (290 loc) · 10.2 KB
/
duration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { Token, Tokenization } from './token';
/**
* Represents a length of time.
*
* The amount can be specified either as a literal value (e.g: `10`) which
* cannot be negative, or as an unresolved number token.
*
* When the amount is passed as a token, unit conversion is not possible.
*/
export class Duration {
/**
* Create a Duration representing an amount of milliseconds
*
* @param amount the amount of Milliseconds the `Duration` will represent.
* @returns a new `Duration` representing `amount` ms.
*/
public static millis(amount: number): Duration {
return new Duration(amount, TimeUnit.Milliseconds);
}
/**
* Create a Duration representing an amount of seconds
*
* @param amount the amount of Seconds the `Duration` will represent.
* @returns a new `Duration` representing `amount` Seconds.
*/
public static seconds(amount: number): Duration {
return new Duration(amount, TimeUnit.Seconds);
}
/**
* Create a Duration representing an amount of minutes
*
* @param amount the amount of Minutes the `Duration` will represent.
* @returns a new `Duration` representing `amount` Minutes.
*/
public static minutes(amount: number): Duration {
return new Duration(amount, TimeUnit.Minutes);
}
/**
* Create a Duration representing an amount of hours
*
* @param amount the amount of Hours the `Duration` will represent.
* @returns a new `Duration` representing `amount` Hours.
*/
public static hours(amount: number): Duration {
return new Duration(amount, TimeUnit.Hours);
}
/**
* Create a Duration representing an amount of days
*
* @param amount the amount of Days the `Duration` will represent.
* @returns a new `Duration` representing `amount` Days.
*/
public static days(amount: number): Duration {
return new Duration(amount, TimeUnit.Days);
}
/**
* Parse a period formatted according to the ISO 8601 standard
*
* @see https://www.iso.org/fr/standard/70907.html
* @param duration an ISO-formtted duration to be parsed.
* @returns the parsed `Duration`.
*/
public static parse(duration: string): Duration {
const matches = duration.match(/^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/);
if (!matches) {
throw new Error(`Not a valid ISO duration: ${duration}`);
}
const [, days, hours, minutes, seconds] = matches;
if (!days && !hours && !minutes && !seconds) {
throw new Error(`Not a valid ISO duration: ${duration}`);
}
return Duration.millis(
_toInt(seconds) * TimeUnit.Seconds.inMillis
+ (_toInt(minutes) * TimeUnit.Minutes.inMillis)
+ (_toInt(hours) * TimeUnit.Hours.inMillis)
+ (_toInt(days) * TimeUnit.Days.inMillis),
);
function _toInt(str: string): number {
if (!str) { return 0; }
return Number(str);
}
}
private readonly amount: number;
private readonly unit: TimeUnit;
private constructor(amount: number, unit: TimeUnit) {
if (!Token.isUnresolved(amount) && amount < 0) {
throw new Error(`Duration amounts cannot be negative. Received: ${amount}`);
}
this.amount = amount;
this.unit = unit;
}
/**
* Add two Durations together
*/
public plus(rhs: Duration): Duration {
const targetUnit = finestUnit(this.unit, rhs.unit);
const total = convert(this.amount, this.unit, targetUnit, {}) + convert(rhs.amount, rhs.unit, targetUnit, {});
return new Duration(total, targetUnit);
}
/**
* Return the total number of milliseconds in this Duration
*
* @returns the value of this `Duration` expressed in Milliseconds.
*/
public toMilliseconds(opts: TimeConversionOptions = {}): number {
return convert(this.amount, this.unit, TimeUnit.Milliseconds, opts);
}
/**
* Return the total number of seconds in this Duration
*
* @returns the value of this `Duration` expressed in Seconds.
*/
public toSeconds(opts: TimeConversionOptions = {}): number {
return convert(this.amount, this.unit, TimeUnit.Seconds, opts);
}
/**
* Return the total number of minutes in this Duration
*
* @returns the value of this `Duration` expressed in Minutes.
*/
public toMinutes(opts: TimeConversionOptions = {}): number {
return convert(this.amount, this.unit, TimeUnit.Minutes, opts);
}
/**
* Return the total number of hours in this Duration
*
* @returns the value of this `Duration` expressed in Hours.
*/
public toHours(opts: TimeConversionOptions = {}): number {
return convert(this.amount, this.unit, TimeUnit.Hours, opts);
}
/**
* Return the total number of days in this Duration
*
* @returns the value of this `Duration` expressed in Days.
*/
public toDays(opts: TimeConversionOptions = {}): number {
return convert(this.amount, this.unit, TimeUnit.Days, opts);
}
/**
* Return an ISO 8601 representation of this period
*
* @returns a string starting with 'P' describing the period
* @see https://www.iso.org/fr/standard/70907.html
*/
public toIsoString(): string {
if (this.amount === 0) { return 'PT0S'; }
const ret = ['P'];
let tee = false;
for (const [amount, unit] of this.components(true)) {
if ([TimeUnit.Seconds, TimeUnit.Minutes, TimeUnit.Hours].includes(unit) && !tee) {
ret.push('T');
tee = true;
}
ret.push(`${amount}${unit.isoLabel}`);
}
return ret.join('');
}
/**
* Return an ISO 8601 representation of this period
*
* @returns a string starting with 'P' describing the period
* @see https://www.iso.org/fr/standard/70907.html
* @deprecated Use `toIsoString()` instead.
*/
public toISOString(): string {
return this.toIsoString();
}
/**
* Turn this duration into a human-readable string
*/
public toHumanString(): string {
if (this.amount === 0) { return fmtUnit(0, this.unit); }
if (Token.isUnresolved(this.amount)) { return `<token> ${this.unit.label}`; }
return this.components(false)
// 2 significant parts, that's totally enough for humans
.slice(0, 2)
.map(([amount, unit]) => fmtUnit(amount, unit))
.join(' ');
function fmtUnit(amount: number, unit: TimeUnit) {
if (amount === 1) {
// All of the labels end in 's'
return `${amount} ${unit.label.substring(0, unit.label.length - 1)}`;
}
return `${amount} ${unit.label}`;
}
}
/**
* Returns a string representation of this `Duration` that is also a Token that cannot be successfully resolved. This
* protects users against inadvertently stringifying a `Duration` object, when they should have called one of the
* `to*` methods instead.
*/
public toString(): string {
return Token.asString(
() => {
throw new Error('Duration.toString() was used, but .toSeconds, .toMinutes or .toDays should have been called instead');
},
{ displayHint: `${this.amount} ${this.unit.label}` },
);
}
/**
* Return the duration in a set of whole numbered time components, ordered from largest to smallest
*
* Only components != 0 will be returned.
*
* Can combine millis and seconds together for the benefit of toIsoString,
* makes the logic in there simpler.
*/
private components(combineMillisWithSeconds: boolean): Array<[number, TimeUnit]> {
const ret = new Array<[number, TimeUnit]>();
let millis = convert(this.amount, this.unit, TimeUnit.Milliseconds, { integral: false });
for (const unit of [TimeUnit.Days, TimeUnit.Hours, TimeUnit.Minutes, TimeUnit.Seconds]) {
const count = convert(millis, TimeUnit.Milliseconds, unit, { integral: false });
// Round down to a whole number UNLESS we're combining millis and seconds and we got to the seconds
const wholeCount = unit === TimeUnit.Seconds && combineMillisWithSeconds ? count : Math.floor(count);
if (wholeCount > 0) {
ret.push([wholeCount, unit]);
millis -= wholeCount * unit.inMillis;
}
}
// Remainder in millis
if (millis > 0) {
ret.push([millis, TimeUnit.Milliseconds]);
}
return ret;
}
/**
* Checks if duration is a token or a resolvable object
*/
public isUnresolved() {
return Token.isUnresolved(this.amount);
}
/**
* Returns unit of the duration
*/
public unitLabel() {
return this.unit.label;
}
/**
* Returns stringified number of duration
*/
public formatTokenToNumber(): string {
const number = Tokenization.stringifyNumber(this.amount);
return `${number} ${this.unit.label}`;
}
}
/**
* Options for how to convert time to a different unit.
*/
export interface TimeConversionOptions {
/**
* If `true`, conversions into a larger time unit (e.g. `Seconds` to `Minutes`) will fail if the result is not an
* integer.
*
* @default true
*/
readonly integral?: boolean;
}
class TimeUnit {
public static readonly Milliseconds = new TimeUnit('millis', '', 1);
public static readonly Seconds = new TimeUnit('seconds', 'S', 1_000);
public static readonly Minutes = new TimeUnit('minutes', 'M', 60_000);
public static readonly Hours = new TimeUnit('hours', 'H', 3_600_000);
public static readonly Days = new TimeUnit('days', 'D', 86_400_000);
private constructor(public readonly label: string, public readonly isoLabel: string, public readonly inMillis: number) {
// MAX_SAFE_INTEGER is 2^53, so by representing our duration in millis (the lowest
// common unit) the highest duration we can represent is
// 2^53 / 86*10^6 ~= 104 * 10^6 days (about 100 million days).
}
public toString() {
return this.label;
}
}
function convert(amount: number, fromUnit: TimeUnit, toUnit: TimeUnit, { integral = true }: TimeConversionOptions) {
if (fromUnit.inMillis === toUnit.inMillis) { return amount; }
if (Token.isUnresolved(amount)) {
throw new Error(`Unable to perform time unit conversion on un-resolved token ${amount}.`);
}
const value = (amount * fromUnit.inMillis) / toUnit.inMillis;
if (!Number.isInteger(value) && integral) {
throw new Error(`'${amount} ${fromUnit}' cannot be converted into a whole number of ${toUnit}.`);
}
return value;
}
/**
* Return the time unit with highest granularity
*/
function finestUnit(a: TimeUnit, b: TimeUnit) {
return a.inMillis < b.inMillis ? a : b;
}