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
package/dist/period.js
DELETED
|
@@ -1,365 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Period = void 0;
|
|
4
|
-
const precision_1 = require("./precision");
|
|
5
|
-
const utils_1 = require("./utils");
|
|
6
|
-
class Period {
|
|
7
|
-
/**
|
|
8
|
-
* Constructs a new Period instance.
|
|
9
|
-
*
|
|
10
|
-
* @param start - The start date of the period, either as a string or a Date object.
|
|
11
|
-
* @param end - The end date of the period, either as a string or a Date object.
|
|
12
|
-
* @param precision - The precision level of the period, defaulting to Precision.DAY.
|
|
13
|
-
* @param interval - An optional interval associated with the period, defaulting to null.
|
|
14
|
-
* @throws {Error} If start date is after end date or if dates are invalid.
|
|
15
|
-
*/
|
|
16
|
-
constructor(start, end, precision = precision_1.Precision.DAY, interval = null) {
|
|
17
|
-
this.startDate = this.validateDate(start);
|
|
18
|
-
this.endDate = this.validateDate(end);
|
|
19
|
-
if (this.startDate > this.endDate) {
|
|
20
|
-
throw new Error('Start date must be before or equal to end date');
|
|
21
|
-
}
|
|
22
|
-
this.precision = precision;
|
|
23
|
-
this.interval = interval;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Validates and converts a date input to a Date object.
|
|
27
|
-
*
|
|
28
|
-
* @param date - The date to validate, either as a string or Date object.
|
|
29
|
-
* @returns A valid Date object.
|
|
30
|
-
* @throws {Error} If the date is invalid.
|
|
31
|
-
*/
|
|
32
|
-
validateDate(date) {
|
|
33
|
-
const result = new Date(date);
|
|
34
|
-
if (isNaN(result.getTime())) {
|
|
35
|
-
throw new Error(`Invalid date: ${date}`);
|
|
36
|
-
}
|
|
37
|
-
return result;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Retrieves the start date of the period.
|
|
41
|
-
*
|
|
42
|
-
* @returns {Date} The start date as a Date object.
|
|
43
|
-
*/
|
|
44
|
-
getStartDate() {
|
|
45
|
-
return new Date(this.startDate);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Retrieves the end date of the period.
|
|
49
|
-
*
|
|
50
|
-
* @returns {Date} The end date as a Date object.
|
|
51
|
-
*/
|
|
52
|
-
getEndDate() {
|
|
53
|
-
return new Date(this.endDate);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Checks if a given date is within the period defined by `startDate` and `endDate`.
|
|
57
|
-
*
|
|
58
|
-
* @param date - The date to check, either as a string or a Date object.
|
|
59
|
-
* @returns `true` if the date is within the period, `false` otherwise.
|
|
60
|
-
*/
|
|
61
|
-
contains(date) {
|
|
62
|
-
const checkDate = this.validateDate(date);
|
|
63
|
-
return checkDate >= this.startDate && checkDate <= this.endDate;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Determines if the current period overlaps with another period.
|
|
67
|
-
*
|
|
68
|
-
* @param other - The other period to compare with.
|
|
69
|
-
* @returns `true` if the periods overlap, otherwise `false`.
|
|
70
|
-
*/
|
|
71
|
-
overlapsWith(other) {
|
|
72
|
-
return this.startDate < other.endDate && this.endDate > other.startDate;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Determines if the current period is adjacent to another period.
|
|
76
|
-
*
|
|
77
|
-
* Two periods are considered adjacent if they do not overlap and the gap
|
|
78
|
-
* between them is within one precision unit of the larger precision of the two periods.
|
|
79
|
-
*
|
|
80
|
-
* @param other - The other period to compare with.
|
|
81
|
-
* @returns `true` if the periods are adjacent, `false` otherwise.
|
|
82
|
-
*/
|
|
83
|
-
isAdjacentTo(other) {
|
|
84
|
-
const thisPrecisionMs = (0, precision_1.getPrecisionInMilliseconds)(this.precision);
|
|
85
|
-
const otherPrecisionMs = (0, precision_1.getPrecisionInMilliseconds)(other.precision);
|
|
86
|
-
const maxPrecisionMs = Math.max(thisPrecisionMs, otherPrecisionMs);
|
|
87
|
-
const thisEndTime = this.endDate.getTime();
|
|
88
|
-
const otherStartTime = other.startDate.getTime();
|
|
89
|
-
const thisStartTime = this.startDate.getTime();
|
|
90
|
-
const otherEndTime = other.endDate.getTime();
|
|
91
|
-
// Check for overlap
|
|
92
|
-
if (thisStartTime <= otherEndTime && otherStartTime <= thisEndTime) {
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
95
|
-
// Check if the gap between periods is within one precision unit
|
|
96
|
-
const gap = Math.abs(otherStartTime - thisEndTime);
|
|
97
|
-
const reverseGap = Math.abs(thisStartTime - otherEndTime);
|
|
98
|
-
return ((gap > 0 && gap <= maxPrecisionMs) ||
|
|
99
|
-
(reverseGap > 0 && reverseGap <= maxPrecisionMs));
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Retrieves an array of dates within the specified interval.
|
|
103
|
-
*
|
|
104
|
-
* @returns {Date[] | null} An array of dates if the interval is defined, otherwise `null`.
|
|
105
|
-
*/
|
|
106
|
-
getDatesInInterval() {
|
|
107
|
-
if (this.interval) {
|
|
108
|
-
return (0, utils_1.getDatesWithInterval)(this.startDate, this.endDate, this.interval);
|
|
109
|
-
}
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Calculates the number of minutes in the interval between the start and end dates.
|
|
114
|
-
*
|
|
115
|
-
* @returns {number} The number of minutes between the start and end dates.
|
|
116
|
-
*/
|
|
117
|
-
getMinutesInInterval() {
|
|
118
|
-
return Math.floor((this.endDate.getTime() - this.startDate.getTime()) / (1000 * 60));
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Calculates the number of whole hours in the interval.
|
|
122
|
-
*
|
|
123
|
-
* @returns {number} The number of whole hours in the interval.
|
|
124
|
-
*/
|
|
125
|
-
getHoursInInterval() {
|
|
126
|
-
return Math.floor(this.getMinutesInInterval() / 60);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Calculates the number of whole days in the interval.
|
|
130
|
-
*
|
|
131
|
-
* @returns The number of whole days in the interval.
|
|
132
|
-
*/
|
|
133
|
-
getDaysInInterval() {
|
|
134
|
-
return Math.floor(this.getHoursInInterval() / 24);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Calculates the number of whole weeks in the interval.
|
|
138
|
-
*
|
|
139
|
-
* @returns The number of whole weeks in the interval.
|
|
140
|
-
*/
|
|
141
|
-
getWeeksInInterval() {
|
|
142
|
-
return Math.floor(this.getDaysInInterval() / 7);
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Calculates the number of whole months in the interval between the start and end dates.
|
|
146
|
-
*
|
|
147
|
-
* This method computes the difference in months between the `startDate` and `endDate` properties.
|
|
148
|
-
* It adjusts for month boundaries by decrementing the month count if the end date's day is less than the start date's day.
|
|
149
|
-
*
|
|
150
|
-
* @returns {number} The number of whole months in the interval.
|
|
151
|
-
*/
|
|
152
|
-
getMonthsInInterval() {
|
|
153
|
-
let months = (this.endDate.getFullYear() - this.startDate.getFullYear()) * 12;
|
|
154
|
-
months += this.endDate.getMonth() - this.startDate.getMonth();
|
|
155
|
-
// Adjust for month boundaries
|
|
156
|
-
if (this.endDate.getDate() < this.startDate.getDate()) {
|
|
157
|
-
months--;
|
|
158
|
-
}
|
|
159
|
-
return Math.max(0, months);
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Calculates the number of whole years in the interval.
|
|
163
|
-
*
|
|
164
|
-
* @returns The number of whole years in the interval.
|
|
165
|
-
*/
|
|
166
|
-
getYearsInInterval() {
|
|
167
|
-
return Math.floor(this.getMonthsInInterval() / 12);
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Calculates the length of the period in the specified precision.
|
|
171
|
-
*
|
|
172
|
-
* @returns {number} The length of the period, rounded down to the nearest whole number.
|
|
173
|
-
*/
|
|
174
|
-
length() {
|
|
175
|
-
return Math.floor((this.endDate.getTime() - this.startDate.getTime()) /
|
|
176
|
-
(0, precision_1.getPrecisionInMilliseconds)(this.precision));
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Determines the overlapping period between this period and another period.
|
|
180
|
-
*
|
|
181
|
-
* @param other - The other period to check for overlap with.
|
|
182
|
-
* @returns The overlapping period as a new `Period` instance, or `null` if there is no overlap.
|
|
183
|
-
*/
|
|
184
|
-
overlap(other) {
|
|
185
|
-
if (!this.overlapsWith(other)) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
const start = new Date(Math.max(this.startDate.getTime(), other.startDate.getTime()));
|
|
189
|
-
const end = new Date(Math.min(this.endDate.getTime(), other.endDate.getTime()));
|
|
190
|
-
return new Period(start, end, this.precision);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Subtracts the given period from the current period and returns the resulting periods.
|
|
194
|
-
* If the periods do not overlap, the current period is returned as a single-element array.
|
|
195
|
-
* If they do overlap, the resulting periods are returned as an array.
|
|
196
|
-
*
|
|
197
|
-
* @param other - The period to subtract from the current period.
|
|
198
|
-
* @returns An array of resulting periods after subtraction.
|
|
199
|
-
*/
|
|
200
|
-
subtract(other) {
|
|
201
|
-
if (!this.overlapsWith(other)) {
|
|
202
|
-
return [this];
|
|
203
|
-
}
|
|
204
|
-
const periods = [];
|
|
205
|
-
// Add period before the overlap (if any)
|
|
206
|
-
if (this.startDate < other.startDate) {
|
|
207
|
-
periods.push(new Period(this.startDate, other.startDate, this.precision));
|
|
208
|
-
}
|
|
209
|
-
// Add period after the overlap (if any)
|
|
210
|
-
if (this.endDate > other.endDate) {
|
|
211
|
-
periods.push(new Period(other.endDate, this.endDate, this.precision));
|
|
212
|
-
}
|
|
213
|
-
return periods;
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Calculates the gap between this period and another period.
|
|
217
|
-
* If the periods overlap or are adjacent, returns null.
|
|
218
|
-
* Otherwise, returns a new Period representing the gap between the two periods.
|
|
219
|
-
*
|
|
220
|
-
* @param other - The other period to compare with.
|
|
221
|
-
* @returns A new Period representing the gap, or null if the periods overlap or are adjacent.
|
|
222
|
-
*/
|
|
223
|
-
gap(other) {
|
|
224
|
-
if (this.overlapsWith(other) || this.isAdjacentTo(other)) {
|
|
225
|
-
return null;
|
|
226
|
-
}
|
|
227
|
-
let start, end;
|
|
228
|
-
if (this.endDate < other.startDate) {
|
|
229
|
-
start = this.endDate;
|
|
230
|
-
end = other.startDate;
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
start = other.endDate;
|
|
234
|
-
end = this.startDate;
|
|
235
|
-
}
|
|
236
|
-
// Ensure start is before end for valid Period creation
|
|
237
|
-
if (start >= end) {
|
|
238
|
-
return null;
|
|
239
|
-
}
|
|
240
|
-
return new Period(start, end, this.precision);
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Computes the symmetric difference between this period and another period.
|
|
244
|
-
* The symmetric difference is defined as the set of periods that are in either
|
|
245
|
-
* of the two periods but not in their intersection.
|
|
246
|
-
*
|
|
247
|
-
* @param other - The other period to compute the symmetric difference with.
|
|
248
|
-
* @returns An array of periods representing the symmetric difference, sorted
|
|
249
|
-
* by start date and merged if adjacent.
|
|
250
|
-
*/
|
|
251
|
-
symmetricDifference(other) {
|
|
252
|
-
const periods = [];
|
|
253
|
-
const overlapPeriod = this.overlap(other);
|
|
254
|
-
if (!overlapPeriod) {
|
|
255
|
-
// No overlap, return both periods
|
|
256
|
-
return [this, other].sort((a, b) => a.startDate.getTime() - b.startDate.getTime());
|
|
257
|
-
}
|
|
258
|
-
const thisSubtracted = this.subtract(overlapPeriod);
|
|
259
|
-
const otherSubtracted = other.subtract(overlapPeriod);
|
|
260
|
-
periods.push(...thisSubtracted, ...otherSubtracted);
|
|
261
|
-
// Sort periods and merge adjacent ones
|
|
262
|
-
return this.mergeAdjacentPeriods(periods);
|
|
263
|
-
}
|
|
264
|
-
mergeAdjacentPeriods(periods) {
|
|
265
|
-
if (periods.length <= 1)
|
|
266
|
-
return periods;
|
|
267
|
-
periods.sort((a, b) => a.startDate.getTime() - b.startDate.getTime());
|
|
268
|
-
const merged = [periods[0]];
|
|
269
|
-
for (let i = 1; i < periods.length; i++) {
|
|
270
|
-
const current = periods[i];
|
|
271
|
-
const previous = merged[merged.length - 1];
|
|
272
|
-
if (previous.isAdjacentTo(current) || previous.overlapsWith(current)) {
|
|
273
|
-
merged[merged.length - 1] = new Period(previous.startDate, current.endDate > previous.endDate
|
|
274
|
-
? current.endDate
|
|
275
|
-
: previous.endDate, this.precision);
|
|
276
|
-
}
|
|
277
|
-
else {
|
|
278
|
-
merged.push(current);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return merged;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Renews the current period by creating a new `Period` instance.
|
|
285
|
-
* The new period starts immediately after the end of the current period
|
|
286
|
-
* and has the same length and precision.
|
|
287
|
-
*
|
|
288
|
-
* @returns {Period} A new `Period` instance with updated start and end dates.
|
|
289
|
-
*/
|
|
290
|
-
renew() {
|
|
291
|
-
const length = this.length();
|
|
292
|
-
const precisionMs = (0, precision_1.getPrecisionInMilliseconds)(this.precision);
|
|
293
|
-
const newStart = new Date(this.endDate.getTime() + precisionMs);
|
|
294
|
-
const newEnd = new Date(newStart.getTime() + length * precisionMs);
|
|
295
|
-
return new Period(newStart, newEnd, this.precision, this.interval);
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Computes the union of this period with another period.
|
|
299
|
-
* If the periods overlap or are adjacent, they are merged into a single period.
|
|
300
|
-
* Otherwise, the periods are returned as separate, sorted periods.
|
|
301
|
-
*
|
|
302
|
-
* @param other - The other period to union with this period.
|
|
303
|
-
* @returns An array of periods resulting from the union operation.
|
|
304
|
-
*/
|
|
305
|
-
union(other) {
|
|
306
|
-
if (this.overlapsWith(other) || this.isAdjacentTo(other)) {
|
|
307
|
-
const start = new Date(Math.min(this.startDate.getTime(), other.startDate.getTime()));
|
|
308
|
-
const end = new Date(Math.max(this.endDate.getTime(), other.endDate.getTime()));
|
|
309
|
-
return [new Period(start, end, this.precision)];
|
|
310
|
-
}
|
|
311
|
-
return [this, other].sort((a, b) => a.startDate.getTime() - b.startDate.getTime());
|
|
312
|
-
}
|
|
313
|
-
// Fluent API methods
|
|
314
|
-
/**
|
|
315
|
-
* Sets the start date for the period.
|
|
316
|
-
*
|
|
317
|
-
* @param start - The start date, which can be a string or a Date object.
|
|
318
|
-
* @returns The current instance for method chaining.
|
|
319
|
-
* @throws {Error} If the new start date is after the current end date.
|
|
320
|
-
*/
|
|
321
|
-
setStart(start) {
|
|
322
|
-
const newStartDate = this.validateDate(start);
|
|
323
|
-
if (newStartDate > this.endDate) {
|
|
324
|
-
throw new Error('Start date must be before or equal to end date');
|
|
325
|
-
}
|
|
326
|
-
this.startDate = newStartDate;
|
|
327
|
-
return this;
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Sets the end date for the period.
|
|
331
|
-
*
|
|
332
|
-
* @param end - The end date as a string or Date object.
|
|
333
|
-
* @returns The current instance for method chaining.
|
|
334
|
-
* @throws {Error} If the new end date is before the current start date.
|
|
335
|
-
*/
|
|
336
|
-
setEnd(end) {
|
|
337
|
-
const newEndDate = this.validateDate(end);
|
|
338
|
-
if (newEndDate < this.startDate) {
|
|
339
|
-
throw new Error('End date must be after or equal to start date');
|
|
340
|
-
}
|
|
341
|
-
this.endDate = newEndDate;
|
|
342
|
-
return this;
|
|
343
|
-
}
|
|
344
|
-
/**
|
|
345
|
-
* Sets the precision for the period.
|
|
346
|
-
*
|
|
347
|
-
* @param precision - The precision to set.
|
|
348
|
-
* @returns The current instance for method chaining.
|
|
349
|
-
*/
|
|
350
|
-
setPrecision(precision) {
|
|
351
|
-
this.precision = precision;
|
|
352
|
-
return this;
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Sets the interval for the period.
|
|
356
|
-
*
|
|
357
|
-
* @param interval - The interval to be set.
|
|
358
|
-
* @returns The current instance of the period.
|
|
359
|
-
*/
|
|
360
|
-
setInterval(interval) {
|
|
361
|
-
this.interval = interval;
|
|
362
|
-
return this;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
exports.Period = Period;
|
package/dist/precision.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export declare enum Precision {
|
|
2
|
-
MINUTE = "minute",
|
|
3
|
-
HOUR = "hour",
|
|
4
|
-
DAY = "day",
|
|
5
|
-
WEEK = "week",
|
|
6
|
-
MONTH = "month",
|
|
7
|
-
YEAR = "year"
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Converts a given precision to its equivalent duration in milliseconds.
|
|
11
|
-
*
|
|
12
|
-
* @param precision - The precision to convert. It can be one of the following:
|
|
13
|
-
* - `Precision.MINUTE`: 1 minute in milliseconds.
|
|
14
|
-
* - `Precision.HOUR`: 1 hour in milliseconds.
|
|
15
|
-
* - `Precision.DAY`: 1 day in milliseconds.
|
|
16
|
-
* - `Precision.WEEK`: 1 week in milliseconds.
|
|
17
|
-
* - `Precision.MONTH`: 1 month (approximated as 30 days) in milliseconds.
|
|
18
|
-
* - `Precision.YEAR`: 1 year (approximated as 365 days) in milliseconds.
|
|
19
|
-
*
|
|
20
|
-
* @returns The duration in milliseconds corresponding to the given precision.
|
|
21
|
-
*
|
|
22
|
-
* @throws Will throw an error if the provided precision is not supported.
|
|
23
|
-
*/
|
|
24
|
-
export declare function getPrecisionInMilliseconds(precision: Precision): number;
|
package/dist/precision.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Precision = void 0;
|
|
4
|
-
exports.getPrecisionInMilliseconds = getPrecisionInMilliseconds;
|
|
5
|
-
var Precision;
|
|
6
|
-
(function (Precision) {
|
|
7
|
-
Precision["MINUTE"] = "minute";
|
|
8
|
-
Precision["HOUR"] = "hour";
|
|
9
|
-
Precision["DAY"] = "day";
|
|
10
|
-
Precision["WEEK"] = "week";
|
|
11
|
-
Precision["MONTH"] = "month";
|
|
12
|
-
Precision["YEAR"] = "year";
|
|
13
|
-
})(Precision || (exports.Precision = Precision = {}));
|
|
14
|
-
/**
|
|
15
|
-
* Converts a given precision to its equivalent duration in milliseconds.
|
|
16
|
-
*
|
|
17
|
-
* @param precision - The precision to convert. It can be one of the following:
|
|
18
|
-
* - `Precision.MINUTE`: 1 minute in milliseconds.
|
|
19
|
-
* - `Precision.HOUR`: 1 hour in milliseconds.
|
|
20
|
-
* - `Precision.DAY`: 1 day in milliseconds.
|
|
21
|
-
* - `Precision.WEEK`: 1 week in milliseconds.
|
|
22
|
-
* - `Precision.MONTH`: 1 month (approximated as 30 days) in milliseconds.
|
|
23
|
-
* - `Precision.YEAR`: 1 year (approximated as 365 days) in milliseconds.
|
|
24
|
-
*
|
|
25
|
-
* @returns The duration in milliseconds corresponding to the given precision.
|
|
26
|
-
*
|
|
27
|
-
* @throws Will throw an error if the provided precision is not supported.
|
|
28
|
-
*/
|
|
29
|
-
function getPrecisionInMilliseconds(precision) {
|
|
30
|
-
switch (precision) {
|
|
31
|
-
case Precision.MINUTE:
|
|
32
|
-
return 1000 * 60;
|
|
33
|
-
case Precision.HOUR:
|
|
34
|
-
return 1000 * 60 * 60;
|
|
35
|
-
case Precision.DAY:
|
|
36
|
-
return 1000 * 60 * 60 * 24;
|
|
37
|
-
case Precision.WEEK:
|
|
38
|
-
return 1000 * 60 * 60 * 24 * 7;
|
|
39
|
-
case Precision.MONTH:
|
|
40
|
-
return 1000 * 60 * 60 * 24 * 30;
|
|
41
|
-
case Precision.YEAR:
|
|
42
|
-
return 1000 * 60 * 60 * 24 * 365;
|
|
43
|
-
default:
|
|
44
|
-
throw new Error('Unsupported precision');
|
|
45
|
-
}
|
|
46
|
-
}
|
package/dist/utils.d.ts
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import { Interval } from './interval';
|
|
2
|
-
import { Precision } from './precision';
|
|
3
|
-
/**
|
|
4
|
-
* Generates an array of dates between the given start and end dates,
|
|
5
|
-
* with a specified interval between each date.
|
|
6
|
-
*
|
|
7
|
-
* @param start - The start date.
|
|
8
|
-
* @param end - The end date.
|
|
9
|
-
* @param interval - An object representing the interval between dates.
|
|
10
|
-
* @returns An array of dates between the start and end dates, inclusive,
|
|
11
|
-
* with the specified interval.
|
|
12
|
-
*/
|
|
13
|
-
export declare function getDatesWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
14
|
-
/**
|
|
15
|
-
* Generates an array of dates representing the start of each week within a given interval.
|
|
16
|
-
*
|
|
17
|
-
* @param start - The start date of the interval.
|
|
18
|
-
* @param end - The end date of the interval.
|
|
19
|
-
* @param interval - An object representing the interval in minutes.
|
|
20
|
-
* @returns An array of dates, each representing the start of a week within the interval.
|
|
21
|
-
*/
|
|
22
|
-
export declare function getWeeksWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
23
|
-
/**
|
|
24
|
-
* Generates an array of dates between the start and end dates, inclusive,
|
|
25
|
-
* with a specified interval.
|
|
26
|
-
*
|
|
27
|
-
* @param start - The start date.
|
|
28
|
-
* @param end - The end date.
|
|
29
|
-
* @param interval - The interval object that provides the interval in minutes.
|
|
30
|
-
* @returns An array of dates with the specified interval.
|
|
31
|
-
*/
|
|
32
|
-
export declare function getDaysWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
33
|
-
/**
|
|
34
|
-
* Generates an array of Date objects representing hours between the start and end dates,
|
|
35
|
-
* incremented by a specified interval.
|
|
36
|
-
*
|
|
37
|
-
* @param start - The start date and time.
|
|
38
|
-
* @param end - The end date and time.
|
|
39
|
-
* @param interval - An object that provides the interval in minutes.
|
|
40
|
-
* @returns An array of Date objects representing the hours within the specified interval.
|
|
41
|
-
*/
|
|
42
|
-
export declare function getHoursWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
43
|
-
/**
|
|
44
|
-
* Generates an array of Date objects representing each minute within a specified interval
|
|
45
|
-
* between a start and end date.
|
|
46
|
-
*
|
|
47
|
-
* @param start - The start date and time.
|
|
48
|
-
* @param end - The end date and time.
|
|
49
|
-
* @param interval - An object that provides the interval in minutes.
|
|
50
|
-
* @returns An array of Date objects, each representing a minute within the specified interval.
|
|
51
|
-
*/
|
|
52
|
-
export declare function getMinutesWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
53
|
-
/**
|
|
54
|
-
* Generates an array of Date objects representing the start of each month
|
|
55
|
-
* within the specified interval between the start and end dates.
|
|
56
|
-
*
|
|
57
|
-
* @param start - The start date of the interval.
|
|
58
|
-
* @param end - The end date of the interval.
|
|
59
|
-
* @param interval - An object representing the interval in minutes.
|
|
60
|
-
* @returns An array of Date objects, each representing the start of a month within the interval.
|
|
61
|
-
*/
|
|
62
|
-
export declare function getMonthsWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
63
|
-
/**
|
|
64
|
-
* Generates an array of Date objects representing the start of each year
|
|
65
|
-
* within the specified interval between the start and end dates.
|
|
66
|
-
*
|
|
67
|
-
* @param start - The start date.
|
|
68
|
-
* @param end - The end date.
|
|
69
|
-
* @param interval - An Interval object that determines the interval in minutes.
|
|
70
|
-
* @returns An array of Date objects representing the start of each year within the interval.
|
|
71
|
-
*/
|
|
72
|
-
export declare function getYearsWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
73
|
-
/**
|
|
74
|
-
* Adds a specified amount of time to a given date based on the provided precision unit.
|
|
75
|
-
*
|
|
76
|
-
* @param date - The original date to which the amount will be added.
|
|
77
|
-
* @param amount - The amount of time to add. Can be positive or negative.
|
|
78
|
-
* @param unit - The unit of time to add (e.g., minute, hour, day, week, month, year).
|
|
79
|
-
*
|
|
80
|
-
* @returns A new `Date` object with the specified amount of time added.
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* ```typescript
|
|
84
|
-
* const originalDate = new Date(Date.UTC(2023, 0, 1));
|
|
85
|
-
* const newDate = addToDate(originalDate, 5, Precision.DAY);
|
|
86
|
-
* console.log(newDate); // Outputs: 2023-01-06T00:00:00.000Z
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
export declare function addToDate(date: Date, amount: number, unit: Precision): Date;
|
|
90
|
-
/**
|
|
91
|
-
* Subtracts a specified amount of time from a given date.
|
|
92
|
-
*
|
|
93
|
-
* @param date - The original date from which to subtract.
|
|
94
|
-
* @param amount - The amount of time to subtract.
|
|
95
|
-
* @param unit - The unit of time to subtract (e.g., days, months, years).
|
|
96
|
-
* @returns A new Date object with the specified amount of time subtracted.
|
|
97
|
-
*/
|
|
98
|
-
export declare function subtractFromDate(date: Date, amount: number, unit: Precision): Date;
|
|
99
|
-
/**
|
|
100
|
-
* Checks if two dates are on the same calendar day.
|
|
101
|
-
*
|
|
102
|
-
* @param date1 - The first date to compare.
|
|
103
|
-
* @param date2 - The second date to compare.
|
|
104
|
-
* @returns `true` if both dates are on the same day, otherwise `false`.
|
|
105
|
-
*/
|
|
106
|
-
export declare function isSameDay(date1: Date, date2: Date): boolean;
|
|
107
|
-
/**
|
|
108
|
-
* Calculates the ISO week number for a given date.
|
|
109
|
-
*
|
|
110
|
-
* The ISO week date system is a leap week calendar system that is part of the ISO 8601 date and time standard.
|
|
111
|
-
* It assigns a week number to each week of the year, where the first week of the year is the week that contains
|
|
112
|
-
* the first Thursday of the year.
|
|
113
|
-
*
|
|
114
|
-
* @param date - The date object for which to calculate the week number.
|
|
115
|
-
* @returns The ISO week number of the given date.
|
|
116
|
-
*/
|
|
117
|
-
export declare function getWeekNumber(date: Date): number;
|
|
118
|
-
/**
|
|
119
|
-
* Returns the quarter of the year for a given date.
|
|
120
|
-
*
|
|
121
|
-
* @param date - The date object for which to determine the quarter.
|
|
122
|
-
* @returns The quarter of the year (1, 2, 3, or 4).
|
|
123
|
-
*/
|
|
124
|
-
export declare function getQuarter(date: Date): number;
|
|
125
|
-
/**
|
|
126
|
-
* Determines whether a given year is a leap year.
|
|
127
|
-
*
|
|
128
|
-
* A leap year is exactly divisible by 4 except for end-of-century years, which must be divisible by 400.
|
|
129
|
-
* This means that the year 2000 was a leap year, although 1900 was not.
|
|
130
|
-
*
|
|
131
|
-
* @param year - The year to be checked.
|
|
132
|
-
* @returns `true` if the year is a leap year, otherwise `false`.
|
|
133
|
-
*/
|
|
134
|
-
export declare function isLeapYear(year: number): boolean;
|
|
135
|
-
/**
|
|
136
|
-
* Returns the number of days in a given month of a specific year.
|
|
137
|
-
*
|
|
138
|
-
* @param year - The year for which to get the number of days in the month.
|
|
139
|
-
* @param month - The month (0-indexed, where 0 = January, 11 = December) for which to get the number of days.
|
|
140
|
-
* @returns The number of days in the specified month of the given year.
|
|
141
|
-
*/
|
|
142
|
-
export declare function getDaysInMonth(year: number, month: number): number;
|
|
143
|
-
/**
|
|
144
|
-
* Formats a given date according to the specified format string.
|
|
145
|
-
*
|
|
146
|
-
* The format string can contain the following placeholders:
|
|
147
|
-
* - `YYYY`: Full year (e.g., 2023)
|
|
148
|
-
* - `MM`: Month (01-12)
|
|
149
|
-
* - `DD`: Day of the month (01-31)
|
|
150
|
-
* - `HH`: Hours (00-23)
|
|
151
|
-
* - `mm`: Minutes (00-59)
|
|
152
|
-
* - `ss`: Seconds (00-59)
|
|
153
|
-
*
|
|
154
|
-
* Example usage:
|
|
155
|
-
* ```typescript
|
|
156
|
-
* const date = new Date(2023, 0, 1, 12, 30, 45);
|
|
157
|
-
* const formattedDate = formatDate(date, 'YYYY-MM-DD HH:mm:ss');
|
|
158
|
-
* console.log(formattedDate); // Output: "2023-01-01 12:30:45"
|
|
159
|
-
* ```
|
|
160
|
-
*
|
|
161
|
-
* @param date - The date object to format.
|
|
162
|
-
* @param format - The format string.
|
|
163
|
-
* @returns The formatted date string.
|
|
164
|
-
*/
|
|
165
|
-
export declare function formatDate(date: Date, format: string): string;
|
|
166
|
-
/**
|
|
167
|
-
* Parses a date string according to the specified format and returns a Date object.
|
|
168
|
-
*
|
|
169
|
-
* @param dateString - The date string to parse.
|
|
170
|
-
* @param format - The format of the date string. Supported format parts are YYYY, MM, DD, HH, mm, ss.
|
|
171
|
-
* @returns A Date object representing the parsed date.
|
|
172
|
-
* @throws {Error} If the date string doesn't match the specified format.
|
|
173
|
-
*
|
|
174
|
-
* @example
|
|
175
|
-
* ```typescript
|
|
176
|
-
* const date = parseDate('2023-10-05 14:30:00', 'YYYY-MM-DD HH:mm:ss');
|
|
177
|
-
* console.log(date); // Outputs: Thu Oct 05 2023 14:30:00 GMT+0000 (Coordinated Universal Time)
|
|
178
|
-
* ```
|
|
179
|
-
*/
|
|
180
|
-
export declare function parseDate(dateString: string, format: string): Date;
|
|
181
|
-
/**
|
|
182
|
-
* Generates an array of numbers within a specified range.
|
|
183
|
-
*
|
|
184
|
-
* @param start - The starting number of the range.
|
|
185
|
-
* @param end - The ending number of the range.
|
|
186
|
-
* @param step - The step between each number in the range. Defaults to 1.
|
|
187
|
-
* @returns An array of numbers from start to end, incremented by step.
|
|
188
|
-
* @throws {Error} If step is zero or has the wrong sign.
|
|
189
|
-
*/
|
|
190
|
-
export declare function range(start: number, end: number, step?: number): number[];
|