chronos-ts 1.1.0 → 2.0.0
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/utils.js
DELETED
|
@@ -1,374 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getDatesWithInterval = getDatesWithInterval;
|
|
4
|
-
exports.getWeeksWithInterval = getWeeksWithInterval;
|
|
5
|
-
exports.getDaysWithInterval = getDaysWithInterval;
|
|
6
|
-
exports.getHoursWithInterval = getHoursWithInterval;
|
|
7
|
-
exports.getMinutesWithInterval = getMinutesWithInterval;
|
|
8
|
-
exports.getMonthsWithInterval = getMonthsWithInterval;
|
|
9
|
-
exports.getYearsWithInterval = getYearsWithInterval;
|
|
10
|
-
exports.addToDate = addToDate;
|
|
11
|
-
exports.subtractFromDate = subtractFromDate;
|
|
12
|
-
exports.isSameDay = isSameDay;
|
|
13
|
-
exports.getWeekNumber = getWeekNumber;
|
|
14
|
-
exports.getQuarter = getQuarter;
|
|
15
|
-
exports.isLeapYear = isLeapYear;
|
|
16
|
-
exports.getDaysInMonth = getDaysInMonth;
|
|
17
|
-
exports.formatDate = formatDate;
|
|
18
|
-
exports.parseDate = parseDate;
|
|
19
|
-
exports.range = range;
|
|
20
|
-
const precision_1 = require("./precision");
|
|
21
|
-
/**
|
|
22
|
-
* Generates an array of dates between the given start and end dates,
|
|
23
|
-
* with a specified interval between each date.
|
|
24
|
-
*
|
|
25
|
-
* @param start - The start date.
|
|
26
|
-
* @param end - The end date.
|
|
27
|
-
* @param interval - An object representing the interval between dates.
|
|
28
|
-
* @returns An array of dates between the start and end dates, inclusive,
|
|
29
|
-
* with the specified interval.
|
|
30
|
-
*/
|
|
31
|
-
function getDatesWithInterval(start, end, interval) {
|
|
32
|
-
const dates = [];
|
|
33
|
-
const current = new Date(start);
|
|
34
|
-
while (current <= end) {
|
|
35
|
-
dates.push(new Date(current));
|
|
36
|
-
current.setMinutes(current.getMinutes() + interval.getMinutesInterval());
|
|
37
|
-
}
|
|
38
|
-
return dates;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Generates an array of dates representing the start of each week within a given interval.
|
|
42
|
-
*
|
|
43
|
-
* @param start - The start date of the interval.
|
|
44
|
-
* @param end - The end date of the interval.
|
|
45
|
-
* @param interval - An object representing the interval in minutes.
|
|
46
|
-
* @returns An array of dates, each representing the start of a week within the interval.
|
|
47
|
-
*/
|
|
48
|
-
function getWeeksWithInterval(start, end, interval) {
|
|
49
|
-
const weeks = [];
|
|
50
|
-
const current = new Date(Date.UTC(start.getUTCFullYear(), start.getUTCMonth(), start.getUTCDate()));
|
|
51
|
-
// Move to the start of the week (Sunday)
|
|
52
|
-
current.setUTCDate(current.getUTCDate() - current.getUTCDay());
|
|
53
|
-
const weekIncrement = Math.max(1, Math.round(interval.getMinutesInterval() / (7 * 24 * 60)));
|
|
54
|
-
while (current <= end) {
|
|
55
|
-
weeks.push(new Date(current));
|
|
56
|
-
// Add weeks based on the interval
|
|
57
|
-
current.setUTCDate(current.getUTCDate() + weekIncrement * 7);
|
|
58
|
-
}
|
|
59
|
-
return weeks;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Generates an array of dates between the start and end dates, inclusive,
|
|
63
|
-
* with a specified interval.
|
|
64
|
-
*
|
|
65
|
-
* @param start - The start date.
|
|
66
|
-
* @param end - The end date.
|
|
67
|
-
* @param interval - The interval object that provides the interval in minutes.
|
|
68
|
-
* @returns An array of dates with the specified interval.
|
|
69
|
-
*/
|
|
70
|
-
function getDaysWithInterval(start, end, interval) {
|
|
71
|
-
const days = [];
|
|
72
|
-
const current = new Date(start);
|
|
73
|
-
current.setUTCHours(0, 0, 0, 0);
|
|
74
|
-
const dayIncrement = Math.max(1, Math.round(interval.getMinutesInterval() / (24 * 60)));
|
|
75
|
-
while (current <= end) {
|
|
76
|
-
days.push(new Date(current));
|
|
77
|
-
current.setUTCDate(current.getUTCDate() + dayIncrement);
|
|
78
|
-
}
|
|
79
|
-
return days;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Generates an array of Date objects representing hours between the start and end dates,
|
|
83
|
-
* incremented by a specified interval.
|
|
84
|
-
*
|
|
85
|
-
* @param start - The start date and time.
|
|
86
|
-
* @param end - The end date and time.
|
|
87
|
-
* @param interval - An object that provides the interval in minutes.
|
|
88
|
-
* @returns An array of Date objects representing the hours within the specified interval.
|
|
89
|
-
*/
|
|
90
|
-
function getHoursWithInterval(start, end, interval) {
|
|
91
|
-
const hours = [];
|
|
92
|
-
const current = new Date(start);
|
|
93
|
-
current.setMinutes(0, 0, 0);
|
|
94
|
-
const hourIncrement = Math.max(1, Math.round(interval.getMinutesInterval() / 60));
|
|
95
|
-
while (current <= end) {
|
|
96
|
-
hours.push(new Date(current));
|
|
97
|
-
current.setHours(current.getHours() + hourIncrement);
|
|
98
|
-
}
|
|
99
|
-
return hours;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Generates an array of Date objects representing each minute within a specified interval
|
|
103
|
-
* between a start and end date.
|
|
104
|
-
*
|
|
105
|
-
* @param start - The start date and time.
|
|
106
|
-
* @param end - The end date and time.
|
|
107
|
-
* @param interval - An object that provides the interval in minutes.
|
|
108
|
-
* @returns An array of Date objects, each representing a minute within the specified interval.
|
|
109
|
-
*/
|
|
110
|
-
function getMinutesWithInterval(start, end, interval) {
|
|
111
|
-
const minutes = [];
|
|
112
|
-
const current = new Date(start);
|
|
113
|
-
current.setSeconds(0, 0);
|
|
114
|
-
const minuteIncrement = Math.max(1, interval.getMinutesInterval());
|
|
115
|
-
while (current <= end) {
|
|
116
|
-
minutes.push(new Date(current));
|
|
117
|
-
current.setMinutes(current.getMinutes() + minuteIncrement);
|
|
118
|
-
}
|
|
119
|
-
return minutes;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Generates an array of Date objects representing the start of each month
|
|
123
|
-
* within the specified interval between the start and end dates.
|
|
124
|
-
*
|
|
125
|
-
* @param start - The start date of the interval.
|
|
126
|
-
* @param end - The end date of the interval.
|
|
127
|
-
* @param interval - An object representing the interval in minutes.
|
|
128
|
-
* @returns An array of Date objects, each representing the start of a month within the interval.
|
|
129
|
-
*/
|
|
130
|
-
function getMonthsWithInterval(start, end, interval) {
|
|
131
|
-
const months = [];
|
|
132
|
-
const current = new Date(Date.UTC(start.getUTCFullYear(), start.getUTCMonth(), 1));
|
|
133
|
-
// Use approximation of 30 days per month for interval calculation
|
|
134
|
-
const monthIncrement = Math.max(1, Math.round(interval.getMinutesInterval() / (30 * 24 * 60)));
|
|
135
|
-
while (current <= end) {
|
|
136
|
-
months.push(new Date(current));
|
|
137
|
-
current.setUTCMonth(current.getUTCMonth() + monthIncrement);
|
|
138
|
-
}
|
|
139
|
-
return months;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Generates an array of Date objects representing the start of each year
|
|
143
|
-
* within the specified interval between the start and end dates.
|
|
144
|
-
*
|
|
145
|
-
* @param start - The start date.
|
|
146
|
-
* @param end - The end date.
|
|
147
|
-
* @param interval - An Interval object that determines the interval in minutes.
|
|
148
|
-
* @returns An array of Date objects representing the start of each year within the interval.
|
|
149
|
-
*/
|
|
150
|
-
function getYearsWithInterval(start, end, interval) {
|
|
151
|
-
const years = [];
|
|
152
|
-
const current = new Date(Date.UTC(start.getUTCFullYear(), 0, 1));
|
|
153
|
-
// Calculate the number of years to add based on the interval
|
|
154
|
-
const yearsToAdd = Math.max(1, Math.round(interval.getMinutesInterval() / 525600)); // 525600 minutes in a year
|
|
155
|
-
while (current <= end) {
|
|
156
|
-
years.push(new Date(current));
|
|
157
|
-
current.setUTCFullYear(current.getUTCFullYear() + yearsToAdd);
|
|
158
|
-
}
|
|
159
|
-
return years;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Adds a specified amount of time to a given date based on the provided precision unit.
|
|
163
|
-
*
|
|
164
|
-
* @param date - The original date to which the amount will be added.
|
|
165
|
-
* @param amount - The amount of time to add. Can be positive or negative.
|
|
166
|
-
* @param unit - The unit of time to add (e.g., minute, hour, day, week, month, year).
|
|
167
|
-
*
|
|
168
|
-
* @returns A new `Date` object with the specified amount of time added.
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ```typescript
|
|
172
|
-
* const originalDate = new Date(Date.UTC(2023, 0, 1));
|
|
173
|
-
* const newDate = addToDate(originalDate, 5, Precision.DAY);
|
|
174
|
-
* console.log(newDate); // Outputs: 2023-01-06T00:00:00.000Z
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
|
-
function addToDate(date, amount, unit) {
|
|
178
|
-
const newDate = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds()));
|
|
179
|
-
switch (unit) {
|
|
180
|
-
case precision_1.Precision.MINUTE:
|
|
181
|
-
newDate.setUTCMinutes(newDate.getUTCMinutes() + amount);
|
|
182
|
-
break;
|
|
183
|
-
case precision_1.Precision.HOUR:
|
|
184
|
-
newDate.setUTCHours(newDate.getUTCHours() + amount);
|
|
185
|
-
break;
|
|
186
|
-
case precision_1.Precision.DAY:
|
|
187
|
-
newDate.setUTCDate(newDate.getUTCDate() + amount);
|
|
188
|
-
break;
|
|
189
|
-
case precision_1.Precision.WEEK:
|
|
190
|
-
newDate.setUTCDate(newDate.getUTCDate() + amount * 7);
|
|
191
|
-
break;
|
|
192
|
-
case precision_1.Precision.MONTH:
|
|
193
|
-
newDate.setUTCMonth(newDate.getUTCMonth() + amount);
|
|
194
|
-
// Handle month overflow for dates that don't exist in the target month
|
|
195
|
-
if (newDate.getUTCDate() !== date.getUTCDate()) {
|
|
196
|
-
newDate.setUTCDate(0); // Set to last day of previous month
|
|
197
|
-
}
|
|
198
|
-
break;
|
|
199
|
-
case precision_1.Precision.YEAR:
|
|
200
|
-
newDate.setUTCFullYear(newDate.getUTCFullYear() + amount);
|
|
201
|
-
// Handle leap year edge case (Feb 29 -> Feb 28)
|
|
202
|
-
if (newDate.getUTCMonth() !== date.getUTCMonth()) {
|
|
203
|
-
newDate.setUTCDate(0); // Set to last day of previous month
|
|
204
|
-
}
|
|
205
|
-
break;
|
|
206
|
-
default:
|
|
207
|
-
throw new Error(`Unsupported precision unit: ${unit}`);
|
|
208
|
-
}
|
|
209
|
-
return newDate;
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Subtracts a specified amount of time from a given date.
|
|
213
|
-
*
|
|
214
|
-
* @param date - The original date from which to subtract.
|
|
215
|
-
* @param amount - The amount of time to subtract.
|
|
216
|
-
* @param unit - The unit of time to subtract (e.g., days, months, years).
|
|
217
|
-
* @returns A new Date object with the specified amount of time subtracted.
|
|
218
|
-
*/
|
|
219
|
-
function subtractFromDate(date, amount, unit) {
|
|
220
|
-
return addToDate(date, -amount, unit);
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Checks if two dates are on the same calendar day.
|
|
224
|
-
*
|
|
225
|
-
* @param date1 - The first date to compare.
|
|
226
|
-
* @param date2 - The second date to compare.
|
|
227
|
-
* @returns `true` if both dates are on the same day, otherwise `false`.
|
|
228
|
-
*/
|
|
229
|
-
function isSameDay(date1, date2) {
|
|
230
|
-
return (date1.getFullYear() === date2.getFullYear() &&
|
|
231
|
-
date1.getMonth() === date2.getMonth() &&
|
|
232
|
-
date1.getDate() === date2.getDate());
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Calculates the ISO week number for a given date.
|
|
236
|
-
*
|
|
237
|
-
* The ISO week date system is a leap week calendar system that is part of the ISO 8601 date and time standard.
|
|
238
|
-
* It assigns a week number to each week of the year, where the first week of the year is the week that contains
|
|
239
|
-
* the first Thursday of the year.
|
|
240
|
-
*
|
|
241
|
-
* @param date - The date object for which to calculate the week number.
|
|
242
|
-
* @returns The ISO week number of the given date.
|
|
243
|
-
*/
|
|
244
|
-
function getWeekNumber(date) {
|
|
245
|
-
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
|
246
|
-
const dayNum = d.getUTCDay() || 7;
|
|
247
|
-
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
|
|
248
|
-
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
|
|
249
|
-
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Returns the quarter of the year for a given date.
|
|
253
|
-
*
|
|
254
|
-
* @param date - The date object for which to determine the quarter.
|
|
255
|
-
* @returns The quarter of the year (1, 2, 3, or 4).
|
|
256
|
-
*/
|
|
257
|
-
function getQuarter(date) {
|
|
258
|
-
return Math.floor(date.getMonth() / 3) + 1;
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Determines whether a given year is a leap year.
|
|
262
|
-
*
|
|
263
|
-
* A leap year is exactly divisible by 4 except for end-of-century years, which must be divisible by 400.
|
|
264
|
-
* This means that the year 2000 was a leap year, although 1900 was not.
|
|
265
|
-
*
|
|
266
|
-
* @param year - The year to be checked.
|
|
267
|
-
* @returns `true` if the year is a leap year, otherwise `false`.
|
|
268
|
-
*/
|
|
269
|
-
function isLeapYear(year) {
|
|
270
|
-
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Returns the number of days in a given month of a specific year.
|
|
274
|
-
*
|
|
275
|
-
* @param year - The year for which to get the number of days in the month.
|
|
276
|
-
* @param month - The month (0-indexed, where 0 = January, 11 = December) for which to get the number of days.
|
|
277
|
-
* @returns The number of days in the specified month of the given year.
|
|
278
|
-
*/
|
|
279
|
-
function getDaysInMonth(year, month) {
|
|
280
|
-
return new Date(year, month + 1, 0).getDate();
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Formats a given date according to the specified format string.
|
|
284
|
-
*
|
|
285
|
-
* The format string can contain the following placeholders:
|
|
286
|
-
* - `YYYY`: Full year (e.g., 2023)
|
|
287
|
-
* - `MM`: Month (01-12)
|
|
288
|
-
* - `DD`: Day of the month (01-31)
|
|
289
|
-
* - `HH`: Hours (00-23)
|
|
290
|
-
* - `mm`: Minutes (00-59)
|
|
291
|
-
* - `ss`: Seconds (00-59)
|
|
292
|
-
*
|
|
293
|
-
* Example usage:
|
|
294
|
-
* ```typescript
|
|
295
|
-
* const date = new Date(2023, 0, 1, 12, 30, 45);
|
|
296
|
-
* const formattedDate = formatDate(date, 'YYYY-MM-DD HH:mm:ss');
|
|
297
|
-
* console.log(formattedDate); // Output: "2023-01-01 12:30:45"
|
|
298
|
-
* ```
|
|
299
|
-
*
|
|
300
|
-
* @param date - The date object to format.
|
|
301
|
-
* @param format - The format string.
|
|
302
|
-
* @returns The formatted date string.
|
|
303
|
-
*/
|
|
304
|
-
function formatDate(date, format) {
|
|
305
|
-
const pad = (n) => {
|
|
306
|
-
const str = n.toString();
|
|
307
|
-
return str.length < 2 ? '0' + str : str;
|
|
308
|
-
};
|
|
309
|
-
const map = {
|
|
310
|
-
YYYY: date.getFullYear().toString(),
|
|
311
|
-
MM: pad(date.getMonth() + 1),
|
|
312
|
-
DD: pad(date.getDate()),
|
|
313
|
-
HH: pad(date.getHours()),
|
|
314
|
-
mm: pad(date.getMinutes()),
|
|
315
|
-
ss: pad(date.getSeconds()),
|
|
316
|
-
};
|
|
317
|
-
return format.replace(/YYYY|MM|DD|HH|mm|ss/gi, (matched) => map[matched]);
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Parses a date string according to the specified format and returns a Date object.
|
|
321
|
-
*
|
|
322
|
-
* @param dateString - The date string to parse.
|
|
323
|
-
* @param format - The format of the date string. Supported format parts are YYYY, MM, DD, HH, mm, ss.
|
|
324
|
-
* @returns A Date object representing the parsed date.
|
|
325
|
-
* @throws {Error} If the date string doesn't match the specified format.
|
|
326
|
-
*
|
|
327
|
-
* @example
|
|
328
|
-
* ```typescript
|
|
329
|
-
* const date = parseDate('2023-10-05 14:30:00', 'YYYY-MM-DD HH:mm:ss');
|
|
330
|
-
* console.log(date); // Outputs: Thu Oct 05 2023 14:30:00 GMT+0000 (Coordinated Universal Time)
|
|
331
|
-
* ```
|
|
332
|
-
*/
|
|
333
|
-
function parseDate(dateString, format) {
|
|
334
|
-
const formatParts = format.match(/YYYY|MM|DD|HH|mm|ss/gi) || [];
|
|
335
|
-
const dateParts = dateString.match(/\d+/g) || [];
|
|
336
|
-
if (formatParts.length !== dateParts.length) {
|
|
337
|
-
throw new Error(`Date string "${dateString}" does not match the specified format "${format}"`);
|
|
338
|
-
}
|
|
339
|
-
const map = {};
|
|
340
|
-
formatParts.forEach((part, index) => {
|
|
341
|
-
map[part] = parseInt(dateParts[index], 10);
|
|
342
|
-
});
|
|
343
|
-
const year = map['YYYY'] || 0;
|
|
344
|
-
const month = (map['MM'] || 1) - 1; // JavaScript months are 0-indexed
|
|
345
|
-
const day = map['DD'] || 1;
|
|
346
|
-
const hour = map['HH'] || 0;
|
|
347
|
-
const minute = map['mm'] || 0;
|
|
348
|
-
const second = map['ss'] || 0;
|
|
349
|
-
const result = new Date(year, month, day, hour, minute, second);
|
|
350
|
-
// Validate the resulting date
|
|
351
|
-
if (isNaN(result.getTime())) {
|
|
352
|
-
throw new Error(`Invalid date components parsed from "${dateString}"`);
|
|
353
|
-
}
|
|
354
|
-
return result;
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Generates an array of numbers within a specified range.
|
|
358
|
-
*
|
|
359
|
-
* @param start - The starting number of the range.
|
|
360
|
-
* @param end - The ending number of the range.
|
|
361
|
-
* @param step - The step between each number in the range. Defaults to 1.
|
|
362
|
-
* @returns An array of numbers from start to end, incremented by step.
|
|
363
|
-
* @throws {Error} If step is zero or has the wrong sign.
|
|
364
|
-
*/
|
|
365
|
-
function range(start, end, step = 1) {
|
|
366
|
-
if (step === 0) {
|
|
367
|
-
throw new Error('Step cannot be zero');
|
|
368
|
-
}
|
|
369
|
-
if ((end > start && step < 0) || (end < start && step > 0)) {
|
|
370
|
-
throw new Error('Step direction must match the range direction');
|
|
371
|
-
}
|
|
372
|
-
const length = Math.floor(Math.abs(end - start) / Math.abs(step)) + 1;
|
|
373
|
-
return Array.from({ length }, (_, i) => start + i * step);
|
|
374
|
-
}
|