chronos-ts 1.1.0 → 2.0.1
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/README.md +249 -443
- package/dist/core/chronos.d.ts +460 -0
- package/dist/core/chronos.js +1259 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.js +19 -0
- package/dist/core/interval.d.ts +289 -0
- package/dist/core/interval.js +689 -0
- package/dist/core/period-collection.d.ts +205 -0
- package/dist/core/period-collection.js +562 -0
- package/dist/core/period.d.ts +428 -0
- package/dist/core/period.js +1007 -0
- package/dist/core/timezone.d.ts +289 -0
- package/dist/core/timezone.js +671 -0
- package/dist/index.d.ts +50 -4
- package/dist/index.js +148 -22
- package/dist/locales/index.d.ts +66 -0
- package/dist/locales/index.js +847 -0
- package/dist/types/index.d.ts +428 -0
- package/dist/types/index.js +71 -0
- package/dist/utils/index.d.ts +127 -0
- package/dist/utils/index.js +656 -0
- package/package.json +19 -3
- package/dist/interval.d.ts +0 -61
- package/dist/interval.js +0 -82
- package/dist/period.d.ts +0 -196
- package/dist/period.js +0 -365
- package/dist/precision.d.ts +0 -24
- package/dist/precision.js +0 -46
- package/dist/utils.d.ts +0 -190
- package/dist/utils.js +0 -374
|
@@ -0,0 +1,689 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ChronosInterval - Duration/Interval handling
|
|
4
|
+
* @module ChronosInterval
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.ChronosInterval = void 0;
|
|
8
|
+
const types_1 = require("../types");
|
|
9
|
+
const utils_1 = require("../utils");
|
|
10
|
+
const locales_1 = require("../locales");
|
|
11
|
+
// ============================================================================
|
|
12
|
+
// ChronosInterval Class
|
|
13
|
+
// ============================================================================
|
|
14
|
+
/**
|
|
15
|
+
* ChronosInterval - Represents a duration/interval of time
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // Create intervals
|
|
20
|
+
* const interval = ChronosInterval.create({ days: 5, hours: 3 });
|
|
21
|
+
* const hours = ChronosInterval.hours(24);
|
|
22
|
+
* const fromISO = ChronosInterval.fromISO('P1Y2M3D');
|
|
23
|
+
*
|
|
24
|
+
* // Arithmetic
|
|
25
|
+
* const doubled = interval.multiply(2);
|
|
26
|
+
* const combined = interval.add(hours);
|
|
27
|
+
*
|
|
28
|
+
* // Formatting
|
|
29
|
+
* console.log(interval.forHumans()); // "5 days 3 hours"
|
|
30
|
+
* console.log(interval.toISO()); // "P5DT3H"
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
class ChronosInterval {
|
|
34
|
+
// ============================================================================
|
|
35
|
+
// Constructor
|
|
36
|
+
// ============================================================================
|
|
37
|
+
constructor(duration = {}, inverted = false) {
|
|
38
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
39
|
+
this._years = (_a = duration.years) !== null && _a !== void 0 ? _a : 0;
|
|
40
|
+
this._months = (_b = duration.months) !== null && _b !== void 0 ? _b : 0;
|
|
41
|
+
this._weeks = (_c = duration.weeks) !== null && _c !== void 0 ? _c : 0;
|
|
42
|
+
this._days = (_d = duration.days) !== null && _d !== void 0 ? _d : 0;
|
|
43
|
+
this._hours = (_e = duration.hours) !== null && _e !== void 0 ? _e : 0;
|
|
44
|
+
this._minutes = (_f = duration.minutes) !== null && _f !== void 0 ? _f : 0;
|
|
45
|
+
this._seconds = Math.floor((_g = duration.seconds) !== null && _g !== void 0 ? _g : 0);
|
|
46
|
+
this._milliseconds =
|
|
47
|
+
(_h = duration.milliseconds) !== null && _h !== void 0 ? _h : (((_j = duration.seconds) !== null && _j !== void 0 ? _j : 0) % 1) * 1000;
|
|
48
|
+
this._locale = (0, locales_1.getLocale)('en');
|
|
49
|
+
this._inverted = inverted;
|
|
50
|
+
}
|
|
51
|
+
// ============================================================================
|
|
52
|
+
// Static Factory Methods
|
|
53
|
+
// ============================================================================
|
|
54
|
+
/**
|
|
55
|
+
* Create an interval from a duration object
|
|
56
|
+
*/
|
|
57
|
+
static create(duration) {
|
|
58
|
+
return new ChronosInterval(duration);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Create an interval from years
|
|
62
|
+
*/
|
|
63
|
+
static years(years) {
|
|
64
|
+
return new ChronosInterval({ years });
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Create an interval from months
|
|
68
|
+
*/
|
|
69
|
+
static months(months) {
|
|
70
|
+
return new ChronosInterval({ months });
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Create an interval from weeks
|
|
74
|
+
*/
|
|
75
|
+
static weeks(weeks) {
|
|
76
|
+
return new ChronosInterval({ weeks });
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Create an interval from days
|
|
80
|
+
*/
|
|
81
|
+
static days(days) {
|
|
82
|
+
return new ChronosInterval({ days });
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create an interval from hours
|
|
86
|
+
*/
|
|
87
|
+
static hours(hours) {
|
|
88
|
+
return new ChronosInterval({ hours });
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Create an interval from minutes
|
|
92
|
+
*/
|
|
93
|
+
static minutes(minutes) {
|
|
94
|
+
return new ChronosInterval({ minutes });
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Create an interval from seconds
|
|
98
|
+
*/
|
|
99
|
+
static seconds(seconds) {
|
|
100
|
+
return new ChronosInterval({ seconds });
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Create an interval from milliseconds
|
|
104
|
+
*/
|
|
105
|
+
static milliseconds(milliseconds) {
|
|
106
|
+
return new ChronosInterval({ milliseconds });
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Create a single unit interval
|
|
110
|
+
*/
|
|
111
|
+
static unit(amount, unit) {
|
|
112
|
+
const normalizedUnit = (0, utils_1.normalizeUnit)(unit);
|
|
113
|
+
const duration = {};
|
|
114
|
+
switch (normalizedUnit) {
|
|
115
|
+
case 'millisecond':
|
|
116
|
+
duration.milliseconds = amount;
|
|
117
|
+
break;
|
|
118
|
+
case 'second':
|
|
119
|
+
duration.seconds = amount;
|
|
120
|
+
break;
|
|
121
|
+
case 'minute':
|
|
122
|
+
duration.minutes = amount;
|
|
123
|
+
break;
|
|
124
|
+
case 'hour':
|
|
125
|
+
duration.hours = amount;
|
|
126
|
+
break;
|
|
127
|
+
case 'day':
|
|
128
|
+
duration.days = amount;
|
|
129
|
+
break;
|
|
130
|
+
case 'week':
|
|
131
|
+
duration.weeks = amount;
|
|
132
|
+
break;
|
|
133
|
+
case 'month':
|
|
134
|
+
duration.months = amount;
|
|
135
|
+
break;
|
|
136
|
+
case 'quarter':
|
|
137
|
+
duration.months = amount * 3;
|
|
138
|
+
break;
|
|
139
|
+
case 'year':
|
|
140
|
+
duration.years = amount;
|
|
141
|
+
break;
|
|
142
|
+
case 'decade':
|
|
143
|
+
duration.years = amount * 10;
|
|
144
|
+
break;
|
|
145
|
+
case 'century':
|
|
146
|
+
duration.years = amount * 100;
|
|
147
|
+
break;
|
|
148
|
+
case 'millennium':
|
|
149
|
+
duration.years = amount * 1000;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
return new ChronosInterval(duration);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Create from ISO 8601 duration string (P1Y2M3DT4H5M6S)
|
|
156
|
+
*/
|
|
157
|
+
static fromISO(iso) {
|
|
158
|
+
const duration = (0, utils_1.parseISODuration)(iso);
|
|
159
|
+
return new ChronosInterval(duration);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Create from a human-readable string
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* ChronosInterval.fromString('2 days 3 hours')
|
|
167
|
+
* ChronosInterval.fromString('1 year, 6 months')
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
static fromString(input) {
|
|
171
|
+
const duration = {};
|
|
172
|
+
const patterns = [
|
|
173
|
+
[/(\d+)\s*(?:years?|y)/i, 'years'],
|
|
174
|
+
[/(\d+)\s*(?:months?|mo)/i, 'months'],
|
|
175
|
+
[/(\d+)\s*(?:weeks?|w)/i, 'weeks'],
|
|
176
|
+
[/(\d+)\s*(?:days?|d)/i, 'days'],
|
|
177
|
+
[/(\d+)\s*(?:hours?|hrs?|h)/i, 'hours'],
|
|
178
|
+
[/(\d+)\s*(?:minutes?|mins?|m)(?!\w)/i, 'minutes'],
|
|
179
|
+
[/(\d+)\s*(?:seconds?|secs?|s)(?!\w)/i, 'seconds'],
|
|
180
|
+
[/(\d+)\s*(?:milliseconds?|ms)/i, 'milliseconds'],
|
|
181
|
+
];
|
|
182
|
+
for (const [pattern, key] of patterns) {
|
|
183
|
+
const match = input.match(pattern);
|
|
184
|
+
if (match) {
|
|
185
|
+
duration[key] = parseInt(match[1], 10);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return new ChronosInterval(duration);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Create a zero-length interval
|
|
192
|
+
*/
|
|
193
|
+
static zero() {
|
|
194
|
+
return new ChronosInterval();
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Create an interval from the difference between two dates
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* const start = new Date('2024-01-01');
|
|
202
|
+
* const end = new Date('2024-03-15');
|
|
203
|
+
* const interval = ChronosInterval.between(start, end);
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
static between(start, end) {
|
|
207
|
+
const startDate = start instanceof Date ? start : start.toDate();
|
|
208
|
+
const endDate = end instanceof Date ? end : end.toDate();
|
|
209
|
+
const diffMs = endDate.getTime() - startDate.getTime();
|
|
210
|
+
const inverted = diffMs < 0;
|
|
211
|
+
const absDiffMs = Math.abs(diffMs);
|
|
212
|
+
// Calculate approximate components
|
|
213
|
+
const days = Math.floor(absDiffMs / types_1.MILLISECONDS_PER_DAY);
|
|
214
|
+
const remainingMs = absDiffMs % types_1.MILLISECONDS_PER_DAY;
|
|
215
|
+
const hours = Math.floor(remainingMs / types_1.MILLISECONDS_PER_HOUR);
|
|
216
|
+
const remainingAfterHours = remainingMs % types_1.MILLISECONDS_PER_HOUR;
|
|
217
|
+
const minutes = Math.floor(remainingAfterHours / types_1.MILLISECONDS_PER_MINUTE);
|
|
218
|
+
const remainingAfterMinutes = remainingAfterHours % types_1.MILLISECONDS_PER_MINUTE;
|
|
219
|
+
const seconds = Math.floor(remainingAfterMinutes / types_1.MILLISECONDS_PER_SECOND);
|
|
220
|
+
const milliseconds = remainingAfterMinutes % types_1.MILLISECONDS_PER_SECOND;
|
|
221
|
+
return new ChronosInterval({ days, hours, minutes, seconds, milliseconds }, inverted);
|
|
222
|
+
}
|
|
223
|
+
// ============================================================================
|
|
224
|
+
// Getters
|
|
225
|
+
// ============================================================================
|
|
226
|
+
get years() {
|
|
227
|
+
return this._years;
|
|
228
|
+
}
|
|
229
|
+
get months() {
|
|
230
|
+
return this._months;
|
|
231
|
+
}
|
|
232
|
+
get weeks() {
|
|
233
|
+
return this._weeks;
|
|
234
|
+
}
|
|
235
|
+
get days() {
|
|
236
|
+
return this._days;
|
|
237
|
+
}
|
|
238
|
+
get hours() {
|
|
239
|
+
return this._hours;
|
|
240
|
+
}
|
|
241
|
+
get minutes() {
|
|
242
|
+
return this._minutes;
|
|
243
|
+
}
|
|
244
|
+
get seconds() {
|
|
245
|
+
return this._seconds;
|
|
246
|
+
}
|
|
247
|
+
get milliseconds() {
|
|
248
|
+
return this._milliseconds;
|
|
249
|
+
}
|
|
250
|
+
get inverted() {
|
|
251
|
+
return this._inverted;
|
|
252
|
+
}
|
|
253
|
+
// ============================================================================
|
|
254
|
+
// Total Calculations
|
|
255
|
+
// ============================================================================
|
|
256
|
+
/**
|
|
257
|
+
* Get total duration in a specific unit
|
|
258
|
+
*/
|
|
259
|
+
total(unit) {
|
|
260
|
+
const ms = this.totalMilliseconds();
|
|
261
|
+
const normalizedUnit = (0, utils_1.normalizeUnit)(unit);
|
|
262
|
+
switch (normalizedUnit) {
|
|
263
|
+
case 'millisecond':
|
|
264
|
+
return ms;
|
|
265
|
+
case 'second':
|
|
266
|
+
return ms / types_1.MILLISECONDS_PER_SECOND;
|
|
267
|
+
case 'minute':
|
|
268
|
+
return ms / types_1.MILLISECONDS_PER_MINUTE;
|
|
269
|
+
case 'hour':
|
|
270
|
+
return ms / types_1.MILLISECONDS_PER_HOUR;
|
|
271
|
+
case 'day':
|
|
272
|
+
return ms / types_1.MILLISECONDS_PER_DAY;
|
|
273
|
+
case 'week':
|
|
274
|
+
return ms / types_1.MILLISECONDS_PER_WEEK;
|
|
275
|
+
case 'month':
|
|
276
|
+
return ms / (types_1.AVERAGE_DAYS_PER_MONTH * types_1.MILLISECONDS_PER_DAY);
|
|
277
|
+
case 'year':
|
|
278
|
+
return ms / (types_1.AVERAGE_DAYS_PER_YEAR * types_1.MILLISECONDS_PER_DAY);
|
|
279
|
+
default:
|
|
280
|
+
return ms;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Get total duration in milliseconds
|
|
285
|
+
*/
|
|
286
|
+
totalMilliseconds() {
|
|
287
|
+
let ms = this._milliseconds;
|
|
288
|
+
ms += this._seconds * types_1.MILLISECONDS_PER_SECOND;
|
|
289
|
+
ms += this._minutes * types_1.MILLISECONDS_PER_MINUTE;
|
|
290
|
+
ms += this._hours * types_1.MILLISECONDS_PER_HOUR;
|
|
291
|
+
ms += this._days * types_1.MILLISECONDS_PER_DAY;
|
|
292
|
+
ms += this._weeks * types_1.MILLISECONDS_PER_WEEK;
|
|
293
|
+
ms += this._months * types_1.AVERAGE_DAYS_PER_MONTH * types_1.MILLISECONDS_PER_DAY;
|
|
294
|
+
ms += this._years * types_1.AVERAGE_DAYS_PER_YEAR * types_1.MILLISECONDS_PER_DAY;
|
|
295
|
+
return this._inverted ? -ms : ms;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Get total duration in seconds
|
|
299
|
+
*/
|
|
300
|
+
totalSeconds() {
|
|
301
|
+
return this.total('seconds');
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Get total duration in minutes
|
|
305
|
+
*/
|
|
306
|
+
totalMinutes() {
|
|
307
|
+
return this.total('minutes');
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Get total duration in hours
|
|
311
|
+
*/
|
|
312
|
+
totalHours() {
|
|
313
|
+
return this.total('hours');
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Get total duration in days
|
|
317
|
+
*/
|
|
318
|
+
totalDays() {
|
|
319
|
+
return this.total('days');
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Get total duration in weeks
|
|
323
|
+
*/
|
|
324
|
+
totalWeeks() {
|
|
325
|
+
return this.total('weeks');
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Get total duration in months (approximate)
|
|
329
|
+
*/
|
|
330
|
+
totalMonths() {
|
|
331
|
+
return this.total('months');
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Get total duration in years (approximate)
|
|
335
|
+
*/
|
|
336
|
+
totalYears() {
|
|
337
|
+
return this.total('years');
|
|
338
|
+
}
|
|
339
|
+
// ============================================================================
|
|
340
|
+
// Arithmetic Operations
|
|
341
|
+
// ============================================================================
|
|
342
|
+
/**
|
|
343
|
+
* Add another interval to this one
|
|
344
|
+
*/
|
|
345
|
+
add(other) {
|
|
346
|
+
const otherInterval = other instanceof ChronosInterval ? other : ChronosInterval.create(other);
|
|
347
|
+
return new ChronosInterval({
|
|
348
|
+
years: this._years + otherInterval._years,
|
|
349
|
+
months: this._months + otherInterval._months,
|
|
350
|
+
weeks: this._weeks + otherInterval._weeks,
|
|
351
|
+
days: this._days + otherInterval._days,
|
|
352
|
+
hours: this._hours + otherInterval._hours,
|
|
353
|
+
minutes: this._minutes + otherInterval._minutes,
|
|
354
|
+
seconds: this._seconds + otherInterval._seconds,
|
|
355
|
+
milliseconds: this._milliseconds + otherInterval._milliseconds,
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Subtract another interval from this one
|
|
360
|
+
*/
|
|
361
|
+
subtract(other) {
|
|
362
|
+
const otherInterval = other instanceof ChronosInterval ? other : ChronosInterval.create(other);
|
|
363
|
+
return new ChronosInterval({
|
|
364
|
+
years: this._years - otherInterval._years,
|
|
365
|
+
months: this._months - otherInterval._months,
|
|
366
|
+
weeks: this._weeks - otherInterval._weeks,
|
|
367
|
+
days: this._days - otherInterval._days,
|
|
368
|
+
hours: this._hours - otherInterval._hours,
|
|
369
|
+
minutes: this._minutes - otherInterval._minutes,
|
|
370
|
+
seconds: this._seconds - otherInterval._seconds,
|
|
371
|
+
milliseconds: this._milliseconds - otherInterval._milliseconds,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Multiply the interval by a factor
|
|
376
|
+
*/
|
|
377
|
+
multiply(factor) {
|
|
378
|
+
return new ChronosInterval({
|
|
379
|
+
years: this._years * factor,
|
|
380
|
+
months: this._months * factor,
|
|
381
|
+
weeks: this._weeks * factor,
|
|
382
|
+
days: this._days * factor,
|
|
383
|
+
hours: this._hours * factor,
|
|
384
|
+
minutes: this._minutes * factor,
|
|
385
|
+
seconds: this._seconds * factor,
|
|
386
|
+
milliseconds: this._milliseconds * factor,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Divide the interval by a factor
|
|
391
|
+
*/
|
|
392
|
+
divide(factor) {
|
|
393
|
+
if (factor === 0) {
|
|
394
|
+
throw new Error('Cannot divide by zero');
|
|
395
|
+
}
|
|
396
|
+
return this.multiply(1 / factor);
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Negate the interval
|
|
400
|
+
*/
|
|
401
|
+
negate() {
|
|
402
|
+
return new ChronosInterval({
|
|
403
|
+
years: this._years,
|
|
404
|
+
months: this._months,
|
|
405
|
+
weeks: this._weeks,
|
|
406
|
+
days: this._days,
|
|
407
|
+
hours: this._hours,
|
|
408
|
+
minutes: this._minutes,
|
|
409
|
+
seconds: this._seconds,
|
|
410
|
+
milliseconds: this._milliseconds,
|
|
411
|
+
}, !this._inverted);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Get absolute value of interval
|
|
415
|
+
*/
|
|
416
|
+
abs() {
|
|
417
|
+
return new ChronosInterval({
|
|
418
|
+
years: Math.abs(this._years),
|
|
419
|
+
months: Math.abs(this._months),
|
|
420
|
+
weeks: Math.abs(this._weeks),
|
|
421
|
+
days: Math.abs(this._days),
|
|
422
|
+
hours: Math.abs(this._hours),
|
|
423
|
+
minutes: Math.abs(this._minutes),
|
|
424
|
+
seconds: Math.abs(this._seconds),
|
|
425
|
+
milliseconds: Math.abs(this._milliseconds),
|
|
426
|
+
}, false);
|
|
427
|
+
}
|
|
428
|
+
// ============================================================================
|
|
429
|
+
// Comparison Methods
|
|
430
|
+
// ============================================================================
|
|
431
|
+
/**
|
|
432
|
+
* Check if equal to another interval
|
|
433
|
+
*/
|
|
434
|
+
equals(other) {
|
|
435
|
+
return this.totalMilliseconds() === other.totalMilliseconds();
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Check if greater than another interval
|
|
439
|
+
*/
|
|
440
|
+
greaterThan(other) {
|
|
441
|
+
return this.totalMilliseconds() > other.totalMilliseconds();
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Check if less than another interval
|
|
445
|
+
*/
|
|
446
|
+
lessThan(other) {
|
|
447
|
+
return this.totalMilliseconds() < other.totalMilliseconds();
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Check if greater than or equal to another interval
|
|
451
|
+
*/
|
|
452
|
+
greaterThanOrEqual(other) {
|
|
453
|
+
return this.totalMilliseconds() >= other.totalMilliseconds();
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Check if less than or equal to another interval
|
|
457
|
+
*/
|
|
458
|
+
lessThanOrEqual(other) {
|
|
459
|
+
return this.totalMilliseconds() <= other.totalMilliseconds();
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Check if the interval is zero
|
|
463
|
+
*/
|
|
464
|
+
isZero() {
|
|
465
|
+
return this.totalMilliseconds() === 0;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Check if the interval is positive
|
|
469
|
+
*/
|
|
470
|
+
isPositive() {
|
|
471
|
+
return this.totalMilliseconds() > 0;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Check if the interval is negative
|
|
475
|
+
*/
|
|
476
|
+
isNegative() {
|
|
477
|
+
return this.totalMilliseconds() < 0;
|
|
478
|
+
}
|
|
479
|
+
// ============================================================================
|
|
480
|
+
// Normalization Methods
|
|
481
|
+
// ============================================================================
|
|
482
|
+
/**
|
|
483
|
+
* Cascade units to proper values (normalize overflow)
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* 90 seconds becomes 1 minute 30 seconds
|
|
487
|
+
*/
|
|
488
|
+
cascade() {
|
|
489
|
+
let ms = Math.abs(this.totalMilliseconds());
|
|
490
|
+
const years = Math.floor(ms / (types_1.AVERAGE_DAYS_PER_YEAR * types_1.MILLISECONDS_PER_DAY));
|
|
491
|
+
ms %= types_1.AVERAGE_DAYS_PER_YEAR * types_1.MILLISECONDS_PER_DAY;
|
|
492
|
+
const months = Math.floor(ms / (types_1.AVERAGE_DAYS_PER_MONTH * types_1.MILLISECONDS_PER_DAY));
|
|
493
|
+
ms %= types_1.AVERAGE_DAYS_PER_MONTH * types_1.MILLISECONDS_PER_DAY;
|
|
494
|
+
const weeks = Math.floor(ms / types_1.MILLISECONDS_PER_WEEK);
|
|
495
|
+
ms %= types_1.MILLISECONDS_PER_WEEK;
|
|
496
|
+
const days = Math.floor(ms / types_1.MILLISECONDS_PER_DAY);
|
|
497
|
+
ms %= types_1.MILLISECONDS_PER_DAY;
|
|
498
|
+
const hours = Math.floor(ms / types_1.MILLISECONDS_PER_HOUR);
|
|
499
|
+
ms %= types_1.MILLISECONDS_PER_HOUR;
|
|
500
|
+
const minutes = Math.floor(ms / types_1.MILLISECONDS_PER_MINUTE);
|
|
501
|
+
ms %= types_1.MILLISECONDS_PER_MINUTE;
|
|
502
|
+
const seconds = Math.floor(ms / types_1.MILLISECONDS_PER_SECOND);
|
|
503
|
+
const milliseconds = ms % types_1.MILLISECONDS_PER_SECOND;
|
|
504
|
+
return new ChronosInterval({
|
|
505
|
+
years,
|
|
506
|
+
months,
|
|
507
|
+
weeks,
|
|
508
|
+
days,
|
|
509
|
+
hours,
|
|
510
|
+
minutes,
|
|
511
|
+
seconds,
|
|
512
|
+
milliseconds,
|
|
513
|
+
}, this._inverted);
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Cascade without including weeks
|
|
517
|
+
*/
|
|
518
|
+
cascadeWithoutWeeks() {
|
|
519
|
+
const cascaded = this.cascade();
|
|
520
|
+
return new ChronosInterval({
|
|
521
|
+
years: cascaded._years,
|
|
522
|
+
months: cascaded._months,
|
|
523
|
+
days: cascaded._days + cascaded._weeks * 7,
|
|
524
|
+
hours: cascaded._hours,
|
|
525
|
+
minutes: cascaded._minutes,
|
|
526
|
+
seconds: cascaded._seconds,
|
|
527
|
+
milliseconds: cascaded._milliseconds,
|
|
528
|
+
}, this._inverted);
|
|
529
|
+
}
|
|
530
|
+
// ============================================================================
|
|
531
|
+
// Formatting Methods
|
|
532
|
+
// ============================================================================
|
|
533
|
+
/**
|
|
534
|
+
* Format as ISO 8601 duration
|
|
535
|
+
*/
|
|
536
|
+
toISO() {
|
|
537
|
+
return (0, utils_1.durationToISO)({
|
|
538
|
+
years: this._years,
|
|
539
|
+
months: this._months,
|
|
540
|
+
weeks: this._weeks,
|
|
541
|
+
days: this._days,
|
|
542
|
+
hours: this._hours,
|
|
543
|
+
minutes: this._minutes,
|
|
544
|
+
seconds: this._seconds + this._milliseconds / 1000,
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Format for human reading
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```typescript
|
|
552
|
+
* interval.forHumans() // "2 days 3 hours"
|
|
553
|
+
* interval.forHumans({ short: true }) // "2d 3h"
|
|
554
|
+
* interval.forHumans({ parts: 2 }) // "2 days 3 hours" (max 2 parts)
|
|
555
|
+
* ```
|
|
556
|
+
*/
|
|
557
|
+
forHumans(options = {}) {
|
|
558
|
+
const { short = false, parts = 7, join = ' ', conjunction } = options;
|
|
559
|
+
const cascaded = this.cascade();
|
|
560
|
+
const result = [];
|
|
561
|
+
const units = [
|
|
562
|
+
[cascaded._years, 'year'],
|
|
563
|
+
[cascaded._months, 'month'],
|
|
564
|
+
[cascaded._weeks, 'week'],
|
|
565
|
+
[cascaded._days, 'day'],
|
|
566
|
+
[cascaded._hours, 'hour'],
|
|
567
|
+
[cascaded._minutes, 'minute'],
|
|
568
|
+
[cascaded._seconds, 'second'],
|
|
569
|
+
];
|
|
570
|
+
for (const [value, unit] of units) {
|
|
571
|
+
if (value !== 0 && result.length < parts) {
|
|
572
|
+
const label = short ? unit[0] : ` ${(0, utils_1.pluralizeUnit)(unit, value)}`;
|
|
573
|
+
result.push(`${value}${label}`);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (result.length === 0) {
|
|
577
|
+
return short ? '0s' : '0 seconds';
|
|
578
|
+
}
|
|
579
|
+
if (conjunction && result.length > 1) {
|
|
580
|
+
const last = result.pop();
|
|
581
|
+
return `${result.join(join)}${conjunction}${last}`;
|
|
582
|
+
}
|
|
583
|
+
return result.join(join);
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Format using a format string
|
|
587
|
+
*
|
|
588
|
+
* Tokens:
|
|
589
|
+
* - %y: years
|
|
590
|
+
* - %m: months
|
|
591
|
+
* - %w: weeks
|
|
592
|
+
* - %d: days
|
|
593
|
+
* - %h: hours
|
|
594
|
+
* - %i: minutes
|
|
595
|
+
* - %s: seconds
|
|
596
|
+
* - %f: milliseconds
|
|
597
|
+
* - %R: +/- sign
|
|
598
|
+
* - %r: +/- or empty
|
|
599
|
+
*/
|
|
600
|
+
format(formatStr) {
|
|
601
|
+
const sign = this._inverted ? '-' : '+';
|
|
602
|
+
return formatStr
|
|
603
|
+
.replace(/%y/g, String(Math.abs(this._years)))
|
|
604
|
+
.replace(/%m/g, String(Math.abs(this._months)))
|
|
605
|
+
.replace(/%w/g, String(Math.abs(this._weeks)))
|
|
606
|
+
.replace(/%d/g, String(Math.abs(this._days)))
|
|
607
|
+
.replace(/%h/g, String(Math.abs(this._hours)))
|
|
608
|
+
.replace(/%H/g, (0, utils_1.padStart)(Math.abs(this._hours), 2))
|
|
609
|
+
.replace(/%i/g, String(Math.abs(this._minutes)))
|
|
610
|
+
.replace(/%I/g, (0, utils_1.padStart)(Math.abs(this._minutes), 2))
|
|
611
|
+
.replace(/%s/g, String(Math.abs(this._seconds)))
|
|
612
|
+
.replace(/%S/g, (0, utils_1.padStart)(Math.abs(this._seconds), 2))
|
|
613
|
+
.replace(/%f/g, String(Math.abs(this._milliseconds)))
|
|
614
|
+
.replace(/%R/g, sign)
|
|
615
|
+
.replace(/%r/g, this._inverted ? '-' : '');
|
|
616
|
+
}
|
|
617
|
+
// ============================================================================
|
|
618
|
+
// Conversion Methods
|
|
619
|
+
// ============================================================================
|
|
620
|
+
/**
|
|
621
|
+
* Convert to Duration object
|
|
622
|
+
*/
|
|
623
|
+
toDuration() {
|
|
624
|
+
return {
|
|
625
|
+
years: this._years,
|
|
626
|
+
months: this._months,
|
|
627
|
+
weeks: this._weeks,
|
|
628
|
+
days: this._days,
|
|
629
|
+
hours: this._hours,
|
|
630
|
+
minutes: this._minutes,
|
|
631
|
+
seconds: this._seconds,
|
|
632
|
+
milliseconds: this._milliseconds,
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Convert to array
|
|
637
|
+
*/
|
|
638
|
+
toArray() {
|
|
639
|
+
return [
|
|
640
|
+
this._years,
|
|
641
|
+
this._months,
|
|
642
|
+
this._weeks,
|
|
643
|
+
this._days,
|
|
644
|
+
this._hours,
|
|
645
|
+
this._minutes,
|
|
646
|
+
this._seconds,
|
|
647
|
+
this._milliseconds,
|
|
648
|
+
];
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Clone this interval
|
|
652
|
+
*/
|
|
653
|
+
clone() {
|
|
654
|
+
const cloned = new ChronosInterval(this.toDuration(), this._inverted);
|
|
655
|
+
cloned._locale = this._locale;
|
|
656
|
+
return cloned;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Set locale for this interval
|
|
660
|
+
*/
|
|
661
|
+
locale(code) {
|
|
662
|
+
const cloned = this.clone();
|
|
663
|
+
cloned._locale = (0, locales_1.getLocale)(code);
|
|
664
|
+
return cloned;
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Get primitive value (total milliseconds)
|
|
668
|
+
*/
|
|
669
|
+
valueOf() {
|
|
670
|
+
return this.totalMilliseconds();
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Convert to string
|
|
674
|
+
*/
|
|
675
|
+
toString() {
|
|
676
|
+
return this.forHumans();
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Convert to JSON
|
|
680
|
+
*/
|
|
681
|
+
toJSON() {
|
|
682
|
+
return Object.assign(Object.assign({}, this.toDuration()), { iso: this.toISO() });
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
exports.ChronosInterval = ChronosInterval;
|
|
686
|
+
// ============================================================================
|
|
687
|
+
// Export Default
|
|
688
|
+
// ============================================================================
|
|
689
|
+
exports.default = ChronosInterval;
|