chronal 0.0.1-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/esm/core/dtf.d.ts +2 -0
- package/esm/core/dtf.d.ts.map +1 -0
- package/esm/core/dtf.js +20 -0
- package/esm/lib/add.d.ts +24 -0
- package/esm/lib/add.d.ts.map +1 -0
- package/esm/lib/add.js +47 -0
- package/esm/lib/difference.d.ts +15 -0
- package/esm/lib/difference.d.ts.map +1 -0
- package/esm/lib/difference.js +47 -0
- package/esm/lib/end-of.d.ts +14 -0
- package/esm/lib/end-of.d.ts.map +1 -0
- package/esm/lib/end-of.js +53 -0
- package/esm/lib/format.d.ts +21 -0
- package/esm/lib/format.d.ts.map +1 -0
- package/esm/lib/format.js +184 -0
- package/esm/lib/is-after.d.ts +13 -0
- package/esm/lib/is-after.d.ts.map +1 -0
- package/esm/lib/is-after.js +15 -0
- package/esm/lib/is-before.d.ts +13 -0
- package/esm/lib/is-before.d.ts.map +1 -0
- package/esm/lib/is-before.js +14 -0
- package/esm/lib/months.d.ts +13 -0
- package/esm/lib/months.d.ts.map +1 -0
- package/esm/lib/months.js +25 -0
- package/esm/lib/start-of.d.ts +14 -0
- package/esm/lib/start-of.d.ts.map +1 -0
- package/esm/lib/start-of.js +50 -0
- package/esm/lib/sub.d.ts +24 -0
- package/esm/lib/sub.d.ts.map +1 -0
- package/esm/lib/sub.js +32 -0
- package/esm/lib/weekdays.d.ts +13 -0
- package/esm/lib/weekdays.d.ts.map +1 -0
- package/esm/lib/weekdays.js +25 -0
- package/esm/mod.d.ts +12 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +11 -0
- package/esm/package.json +3 -0
- package/esm/types/unit.d.ts +3 -0
- package/esm/types/unit.d.ts.map +1 -0
- package/esm/types/unit.js +1 -0
- package/package.json +26 -0
- package/script/core/dtf.d.ts +2 -0
- package/script/core/dtf.d.ts.map +1 -0
- package/script/core/dtf.js +23 -0
- package/script/lib/add.d.ts +24 -0
- package/script/lib/add.d.ts.map +1 -0
- package/script/lib/add.js +50 -0
- package/script/lib/difference.d.ts +15 -0
- package/script/lib/difference.d.ts.map +1 -0
- package/script/lib/difference.js +50 -0
- package/script/lib/end-of.d.ts +14 -0
- package/script/lib/end-of.d.ts.map +1 -0
- package/script/lib/end-of.js +56 -0
- package/script/lib/format.d.ts +21 -0
- package/script/lib/format.d.ts.map +1 -0
- package/script/lib/format.js +187 -0
- package/script/lib/is-after.d.ts +13 -0
- package/script/lib/is-after.d.ts.map +1 -0
- package/script/lib/is-after.js +18 -0
- package/script/lib/is-before.d.ts +13 -0
- package/script/lib/is-before.d.ts.map +1 -0
- package/script/lib/is-before.js +17 -0
- package/script/lib/months.d.ts +13 -0
- package/script/lib/months.d.ts.map +1 -0
- package/script/lib/months.js +28 -0
- package/script/lib/start-of.d.ts +14 -0
- package/script/lib/start-of.d.ts.map +1 -0
- package/script/lib/start-of.js +53 -0
- package/script/lib/sub.d.ts +24 -0
- package/script/lib/sub.d.ts.map +1 -0
- package/script/lib/sub.js +35 -0
- package/script/lib/weekdays.d.ts +13 -0
- package/script/lib/weekdays.d.ts.map +1 -0
- package/script/lib/weekdays.js +28 -0
- package/script/mod.d.ts +12 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +27 -0
- package/script/package.json +3 -0
- package/script/types/unit.d.ts +3 -0
- package/script/types/unit.d.ts.map +1 -0
- package/script/types/unit.js +2 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dtf.d.ts","sourceRoot":"","sources":["../../src/core/dtf.ts"],"names":[],"mappings":"AAEA,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,uBAiBhD"}
|
package/esm/core/dtf.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const dtfCache = new Map();
|
|
2
|
+
export function getDTF(locale, tz) {
|
|
3
|
+
const key = `${locale}|${tz}`;
|
|
4
|
+
let dtf = dtfCache.get(key);
|
|
5
|
+
if (!dtf) {
|
|
6
|
+
dtf = new Intl.DateTimeFormat(locale, {
|
|
7
|
+
year: 'numeric',
|
|
8
|
+
month: '2-digit',
|
|
9
|
+
day: '2-digit',
|
|
10
|
+
hour: '2-digit',
|
|
11
|
+
minute: '2-digit',
|
|
12
|
+
second: '2-digit',
|
|
13
|
+
hour12: false,
|
|
14
|
+
timeZone: tz,
|
|
15
|
+
});
|
|
16
|
+
dtfCache.set(key, dtf);
|
|
17
|
+
}
|
|
18
|
+
return dtf;
|
|
19
|
+
}
|
|
20
|
+
;
|
package/esm/lib/add.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
type AddOptions = {
|
|
2
|
+
years?: number;
|
|
3
|
+
months?: number;
|
|
4
|
+
weeks?: number;
|
|
5
|
+
days?: number;
|
|
6
|
+
hours?: number;
|
|
7
|
+
minutes?: number;
|
|
8
|
+
seconds?: number;
|
|
9
|
+
milliseconds?: number;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Adds specified time units to the given date.
|
|
13
|
+
*
|
|
14
|
+
* @param date - The original date.
|
|
15
|
+
* @param opt - The time units to add.
|
|
16
|
+
* @returns A new Date object with the specified time units added.
|
|
17
|
+
* @example
|
|
18
|
+
* const date = new Date('2024-01-31T12:00:00Z');
|
|
19
|
+
* const newDate = add(date, { years: 1, months: 2, days: 3 });
|
|
20
|
+
* console.log(newDate.toISOString()); // '2025-04-03T12:00:00.000Z'
|
|
21
|
+
*/
|
|
22
|
+
export declare function add(date: Date, opt: AddOptions): Date;
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=add.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"add.d.ts","sourceRoot":"","sources":["../../src/lib/add.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GAAG;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;GAUG;AAEH,wBAAgB,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,CA8CrD"}
|
package/esm/lib/add.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds specified time units to the given date.
|
|
3
|
+
*
|
|
4
|
+
* @param date - The original date.
|
|
5
|
+
* @param opt - The time units to add.
|
|
6
|
+
* @returns A new Date object with the specified time units added.
|
|
7
|
+
* @example
|
|
8
|
+
* const date = new Date('2024-01-31T12:00:00Z');
|
|
9
|
+
* const newDate = add(date, { years: 1, months: 2, days: 3 });
|
|
10
|
+
* console.log(newDate.toISOString()); // '2025-04-03T12:00:00.000Z'
|
|
11
|
+
*/
|
|
12
|
+
export function add(date, opt) {
|
|
13
|
+
let d = new Date(date.getTime());
|
|
14
|
+
// ===== calendar-based (UTC-safe) =====
|
|
15
|
+
if (opt.years || opt.months) {
|
|
16
|
+
const years = opt.years ?? 0;
|
|
17
|
+
const months = opt.months ?? 0;
|
|
18
|
+
const year = d.getUTCFullYear();
|
|
19
|
+
const month = d.getUTCMonth(); // 0-based
|
|
20
|
+
const day = d.getUTCDate();
|
|
21
|
+
// target year/month (allow overflow, normalize manually)
|
|
22
|
+
let targetMonth = month + months;
|
|
23
|
+
const targetYear = year + years + Math.floor(targetMonth / 12);
|
|
24
|
+
targetMonth = ((targetMonth % 12) + 12) % 12;
|
|
25
|
+
// last valid day of target month
|
|
26
|
+
const lastDayOfTargetMonth = new Date(Date.UTC(targetYear, targetMonth + 1, 0)).getUTCDate();
|
|
27
|
+
const safeDay = Math.min(day, lastDayOfTargetMonth);
|
|
28
|
+
d = new Date(Date.UTC(targetYear, targetMonth, safeDay, d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds()));
|
|
29
|
+
}
|
|
30
|
+
// ===== time-based =====
|
|
31
|
+
let delta = 0;
|
|
32
|
+
if (opt.milliseconds)
|
|
33
|
+
delta += opt.milliseconds;
|
|
34
|
+
if (opt.seconds)
|
|
35
|
+
delta += opt.seconds * 1_000;
|
|
36
|
+
if (opt.minutes)
|
|
37
|
+
delta += opt.minutes * 60_000;
|
|
38
|
+
if (opt.hours)
|
|
39
|
+
delta += opt.hours * 3_600_000;
|
|
40
|
+
if (opt.days)
|
|
41
|
+
delta += opt.days * 86_400_000;
|
|
42
|
+
if (opt.weeks)
|
|
43
|
+
delta += opt.weeks * 604_800_000;
|
|
44
|
+
if (delta)
|
|
45
|
+
d = new Date(d.getTime() + delta);
|
|
46
|
+
return d;
|
|
47
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PluralUnit } from "../types/unit.js";
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the difference between two dates in the specified unit.
|
|
4
|
+
*
|
|
5
|
+
* @param dateLeft - The first date.
|
|
6
|
+
* @param dateRight - The second date to subtract from the first.
|
|
7
|
+
* @param unit - The unit to express the difference in ('years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds').
|
|
8
|
+
* @returns The difference as a number (can be negative if dateLeft is before dateRight).
|
|
9
|
+
* @example
|
|
10
|
+
* const date1 = new Date('2024-01-20T12:00:00Z');
|
|
11
|
+
* const date2 = new Date('2024-01-15T12:00:00Z');
|
|
12
|
+
* console.log(difference(date1, date2, 'days')); // 5
|
|
13
|
+
*/
|
|
14
|
+
export declare function difference(dateLeft: Date, dateRight: Date, unit: PluralUnit): number;
|
|
15
|
+
//# sourceMappingURL=difference.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"difference.d.ts","sourceRoot":"","sources":["../../src/lib/difference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;;;;;;;;;GAWG;AAEH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM,CAoDpF"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the difference between two dates in the specified unit.
|
|
3
|
+
*
|
|
4
|
+
* @param dateLeft - The first date.
|
|
5
|
+
* @param dateRight - The second date to subtract from the first.
|
|
6
|
+
* @param unit - The unit to express the difference in ('years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds').
|
|
7
|
+
* @returns The difference as a number (can be negative if dateLeft is before dateRight).
|
|
8
|
+
* @example
|
|
9
|
+
* const date1 = new Date('2024-01-20T12:00:00Z');
|
|
10
|
+
* const date2 = new Date('2024-01-15T12:00:00Z');
|
|
11
|
+
* console.log(difference(date1, date2, 'days')); // 5
|
|
12
|
+
*/
|
|
13
|
+
export function difference(dateLeft, dateRight, unit) {
|
|
14
|
+
const delta = dateLeft.getTime() - dateRight.getTime();
|
|
15
|
+
switch (unit) {
|
|
16
|
+
case 'years': {
|
|
17
|
+
const yearDiff = dateLeft.getUTCFullYear() - dateRight.getUTCFullYear();
|
|
18
|
+
const adjustedDateRight = new Date(Date.UTC(dateRight.getUTCFullYear() + yearDiff, dateRight.getUTCMonth(), dateRight.getUTCDate(), dateRight.getUTCHours(), dateRight.getUTCMinutes(), dateRight.getUTCSeconds(), dateRight.getUTCMilliseconds()));
|
|
19
|
+
if (adjustedDateRight > dateLeft) {
|
|
20
|
+
return yearDiff - 1;
|
|
21
|
+
}
|
|
22
|
+
return yearDiff;
|
|
23
|
+
}
|
|
24
|
+
case 'months': {
|
|
25
|
+
const yearDiff = dateLeft.getUTCFullYear() - dateRight.getUTCFullYear();
|
|
26
|
+
const monthDiff = dateLeft.getUTCMonth() - dateRight.getUTCMonth();
|
|
27
|
+
let totalMonths = yearDiff * 12 + monthDiff;
|
|
28
|
+
const adjustedDateRight = new Date(Date.UTC(dateRight.getUTCFullYear() + Math.floor(totalMonths / 12), (dateRight.getUTCMonth() + (totalMonths % 12) + 12) % 12, dateRight.getUTCDate(), dateRight.getUTCHours(), dateRight.getUTCMinutes(), dateRight.getUTCSeconds(), dateRight.getUTCMilliseconds()));
|
|
29
|
+
if (adjustedDateRight > dateLeft) {
|
|
30
|
+
totalMonths -= 1;
|
|
31
|
+
}
|
|
32
|
+
return totalMonths;
|
|
33
|
+
}
|
|
34
|
+
case 'weeks':
|
|
35
|
+
return Math.floor(delta / 604800000); // 7 * 24 * 60 * 60 * 1000
|
|
36
|
+
case 'days':
|
|
37
|
+
return Math.floor(delta / 86400000); // 24 * 60 * 60 * 1000
|
|
38
|
+
case 'hours':
|
|
39
|
+
return Math.floor(delta / 3600000); // 60 * 60 * 1000
|
|
40
|
+
case 'minutes':
|
|
41
|
+
return Math.floor(delta / 60000); // 60 * 1000
|
|
42
|
+
case 'seconds':
|
|
43
|
+
return Math.floor(delta / 1000);
|
|
44
|
+
default:
|
|
45
|
+
throw new Error(`Unsupported unit: ${unit}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Unit } from "../types/unit.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the end of the specified time unit for the given date.
|
|
4
|
+
*
|
|
5
|
+
* @param date - The original date.
|
|
6
|
+
* @param unit - The time unit to get the end of ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second').
|
|
7
|
+
* @returns A new Date object set to the end of the specified unit (last millisecond).
|
|
8
|
+
* @example
|
|
9
|
+
* const date = new Date('2024-06-15T14:35:22.500Z');
|
|
10
|
+
* const endOfDay = endOf(date, 'day');
|
|
11
|
+
* console.log(endOfDay.toISOString()); // '2024-06-15T23:59:59.999Z'
|
|
12
|
+
*/
|
|
13
|
+
export declare function endOf(date: Date, unit: Unit): Date;
|
|
14
|
+
//# sourceMappingURL=end-of.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"end-of.d.ts","sourceRoot":"","sources":["../../src/lib/end-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;;GAUG;AAEH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CA2ClD"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the end of the specified time unit for the given date.
|
|
3
|
+
*
|
|
4
|
+
* @param date - The original date.
|
|
5
|
+
* @param unit - The time unit to get the end of ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second').
|
|
6
|
+
* @returns A new Date object set to the end of the specified unit (last millisecond).
|
|
7
|
+
* @example
|
|
8
|
+
* const date = new Date('2024-06-15T14:35:22.500Z');
|
|
9
|
+
* const endOfDay = endOf(date, 'day');
|
|
10
|
+
* console.log(endOfDay.toISOString()); // '2024-06-15T23:59:59.999Z'
|
|
11
|
+
*/
|
|
12
|
+
export function endOf(date, unit) {
|
|
13
|
+
const time = new Date(date.getTime());
|
|
14
|
+
switch (unit) {
|
|
15
|
+
case 'year': {
|
|
16
|
+
time.setUTCMonth(11, 31);
|
|
17
|
+
time.setUTCHours(23, 59, 59, 999);
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
case 'month': {
|
|
21
|
+
const year = time.getUTCFullYear();
|
|
22
|
+
const month = time.getUTCMonth();
|
|
23
|
+
const lastDay = new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
|
|
24
|
+
time.setUTCDate(lastDay);
|
|
25
|
+
time.setUTCHours(23, 59, 59, 999);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
case 'week': {
|
|
29
|
+
const day = time.getUTCDay();
|
|
30
|
+
const diff = (day === 0 ? 0 : 7) - day; // Adjust when day is Sunday
|
|
31
|
+
time.setUTCDate(time.getUTCDate() + diff);
|
|
32
|
+
time.setUTCHours(23, 59, 59, 999);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
case 'day': {
|
|
36
|
+
time.setUTCHours(23, 59, 59, 999);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case 'hour': {
|
|
40
|
+
time.setUTCMinutes(59, 59, 999);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
case 'minute': {
|
|
44
|
+
time.setUTCSeconds(59, 999);
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case 'second': {
|
|
48
|
+
time.setUTCMilliseconds(999);
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return time;
|
|
53
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type FormatOptions = {
|
|
2
|
+
tz?: string;
|
|
3
|
+
locale?: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Formats a date into a string using the specified format pattern.
|
|
7
|
+
*
|
|
8
|
+
* @param date - The date to format.
|
|
9
|
+
* @param fmt - The format string with tokens (YYYY, MM, DD, HH, mm, ss, etc.).
|
|
10
|
+
* @param options - Optional formatting options (locale and timezone).
|
|
11
|
+
* @returns The formatted date string.
|
|
12
|
+
* @example
|
|
13
|
+
* const date = new Date('2024-06-15T14:35:22Z');
|
|
14
|
+
* console.log(format(date, 'YYYY-MM-DD')); // '2024-06-15'
|
|
15
|
+
* console.log(format(date, 'YYYY-MM-DD HH:mm:ss')); // '2024-06-15 14:35:22'
|
|
16
|
+
* console.log(format(date, 'DD/MM/YYYY [at] HH:mm')); // '15/06/2024 at 14:35'
|
|
17
|
+
* console.log(format(date, 'MMMM D, YYYY', { locale: 'en-US' })); // 'June 15, 2024'
|
|
18
|
+
*/
|
|
19
|
+
export declare function format(date: Date, fmt: string, options?: FormatOptions): string;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/lib/format.ts"],"names":[],"mappings":"AAGA,KAAK,aAAa,GAAG;IACnB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAkLF;;;;;;;;;;;;;GAaG;AAEH,wBAAgB,MAAM,CACpB,IAAI,EAAE,IAAI,EACV,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAqBR"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { getDTF } from "../core/dtf.js";
|
|
2
|
+
import { months } from "./months.js";
|
|
3
|
+
// Pre-compute pad functions to avoid repeated padStart calls
|
|
4
|
+
const pad2 = (n) => n < 10 ? '0' + n : String(n);
|
|
5
|
+
const tokenMap = {
|
|
6
|
+
YYYY: p => String(p.year),
|
|
7
|
+
YY: p => String(p.year).slice(-2),
|
|
8
|
+
M: p => String(p.month),
|
|
9
|
+
MM: p => pad2(p.month),
|
|
10
|
+
MMM: (p, l) => months(l, 'short')[p.month - 1],
|
|
11
|
+
MMMM: (p, l) => months(l, 'long')[p.month - 1],
|
|
12
|
+
D: p => String(p.day),
|
|
13
|
+
DD: p => pad2(p.day),
|
|
14
|
+
H: p => String(p.hour),
|
|
15
|
+
HH: p => pad2(p.hour),
|
|
16
|
+
m: p => String(p.minute),
|
|
17
|
+
mm: p => pad2(p.minute),
|
|
18
|
+
s: p => String(p.second),
|
|
19
|
+
ss: p => pad2(p.second),
|
|
20
|
+
};
|
|
21
|
+
const tokenRegex = /YYYY|MMMM|MMM|YY|MM|M|DD|D|HH|H|mm|m|ss|s/g;
|
|
22
|
+
const formatCache = new Map();
|
|
23
|
+
function compileFormat(fmt) {
|
|
24
|
+
const cached = formatCache.get(fmt);
|
|
25
|
+
if (cached)
|
|
26
|
+
return cached;
|
|
27
|
+
const hasLiterals = fmt.includes('[');
|
|
28
|
+
const parts = [];
|
|
29
|
+
if (!hasLiterals) {
|
|
30
|
+
// Fast path: no literals, just parse tokens
|
|
31
|
+
let lastIndex = 0;
|
|
32
|
+
tokenRegex.lastIndex = 0;
|
|
33
|
+
let match;
|
|
34
|
+
while ((match = tokenRegex.exec(fmt)) !== null) {
|
|
35
|
+
if (match.index > lastIndex) {
|
|
36
|
+
parts.push({ type: 'literal', value: fmt.slice(lastIndex, match.index) });
|
|
37
|
+
}
|
|
38
|
+
parts.push({ type: 'token', value: match[0] });
|
|
39
|
+
lastIndex = match.index + match[0].length;
|
|
40
|
+
}
|
|
41
|
+
if (lastIndex < fmt.length) {
|
|
42
|
+
parts.push({ type: 'literal', value: fmt.slice(lastIndex) });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// Slow path: handle escaped literals
|
|
47
|
+
let i = 0;
|
|
48
|
+
let currentLiteral = '';
|
|
49
|
+
let inBracket = false;
|
|
50
|
+
while (i < fmt.length) {
|
|
51
|
+
if (fmt[i] === '[' && !inBracket) {
|
|
52
|
+
if (currentLiteral) {
|
|
53
|
+
// Process current literal for tokens
|
|
54
|
+
let lastIndex = 0;
|
|
55
|
+
tokenRegex.lastIndex = 0;
|
|
56
|
+
let match;
|
|
57
|
+
while ((match = tokenRegex.exec(currentLiteral)) !== null) {
|
|
58
|
+
if (match.index > lastIndex) {
|
|
59
|
+
parts.push({ type: 'literal', value: currentLiteral.slice(lastIndex, match.index) });
|
|
60
|
+
}
|
|
61
|
+
parts.push({ type: 'token', value: match[0] });
|
|
62
|
+
lastIndex = match.index + match[0].length;
|
|
63
|
+
}
|
|
64
|
+
if (lastIndex < currentLiteral.length) {
|
|
65
|
+
parts.push({ type: 'literal', value: currentLiteral.slice(lastIndex) });
|
|
66
|
+
}
|
|
67
|
+
currentLiteral = '';
|
|
68
|
+
}
|
|
69
|
+
inBracket = true;
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
else if (fmt[i] === ']' && inBracket) {
|
|
73
|
+
// Escaped literal content - add as-is
|
|
74
|
+
if (currentLiteral) {
|
|
75
|
+
parts.push({ type: 'literal', value: currentLiteral });
|
|
76
|
+
currentLiteral = '';
|
|
77
|
+
}
|
|
78
|
+
inBracket = false;
|
|
79
|
+
i++;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
currentLiteral += fmt[i];
|
|
83
|
+
i++;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (currentLiteral) {
|
|
87
|
+
let lastIndex = 0;
|
|
88
|
+
tokenRegex.lastIndex = 0;
|
|
89
|
+
let match;
|
|
90
|
+
while ((match = tokenRegex.exec(currentLiteral)) !== null) {
|
|
91
|
+
if (match.index > lastIndex) {
|
|
92
|
+
parts.push({ type: 'literal', value: currentLiteral.slice(lastIndex, match.index) });
|
|
93
|
+
}
|
|
94
|
+
parts.push({ type: 'token', value: match[0] });
|
|
95
|
+
lastIndex = match.index + match[0].length;
|
|
96
|
+
}
|
|
97
|
+
if (lastIndex < currentLiteral.length) {
|
|
98
|
+
parts.push({ type: 'literal', value: currentLiteral.slice(lastIndex) });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const compiled = { hasLiterals, parts };
|
|
103
|
+
formatCache.set(fmt, compiled);
|
|
104
|
+
return compiled;
|
|
105
|
+
}
|
|
106
|
+
// Fast UTC date parts extraction
|
|
107
|
+
function getUTCParts(date) {
|
|
108
|
+
return {
|
|
109
|
+
year: date.getUTCFullYear(),
|
|
110
|
+
month: date.getUTCMonth() + 1,
|
|
111
|
+
day: date.getUTCDate(),
|
|
112
|
+
hour: date.getUTCHours(),
|
|
113
|
+
minute: date.getUTCMinutes(),
|
|
114
|
+
second: date.getUTCSeconds(),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
// Fallback for non-UTC timezones
|
|
118
|
+
function getPartsWithTZ(date, locale, tz) {
|
|
119
|
+
const partsRaw = getDTF(locale, tz).formatToParts(date);
|
|
120
|
+
const p = {
|
|
121
|
+
year: 0,
|
|
122
|
+
month: 0,
|
|
123
|
+
day: 0,
|
|
124
|
+
hour: 0,
|
|
125
|
+
minute: 0,
|
|
126
|
+
second: 0,
|
|
127
|
+
};
|
|
128
|
+
for (const part of partsRaw) {
|
|
129
|
+
switch (part.type) {
|
|
130
|
+
case "year":
|
|
131
|
+
p.year = +part.value;
|
|
132
|
+
break;
|
|
133
|
+
case "month":
|
|
134
|
+
p.month = +part.value;
|
|
135
|
+
break;
|
|
136
|
+
case "day":
|
|
137
|
+
p.day = +part.value;
|
|
138
|
+
break;
|
|
139
|
+
case "hour":
|
|
140
|
+
p.hour = +part.value;
|
|
141
|
+
break;
|
|
142
|
+
case "minute":
|
|
143
|
+
p.minute = +part.value;
|
|
144
|
+
break;
|
|
145
|
+
case "second":
|
|
146
|
+
p.second = +part.value;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return p;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Formats a date into a string using the specified format pattern.
|
|
154
|
+
*
|
|
155
|
+
* @param date - The date to format.
|
|
156
|
+
* @param fmt - The format string with tokens (YYYY, MM, DD, HH, mm, ss, etc.).
|
|
157
|
+
* @param options - Optional formatting options (locale and timezone).
|
|
158
|
+
* @returns The formatted date string.
|
|
159
|
+
* @example
|
|
160
|
+
* const date = new Date('2024-06-15T14:35:22Z');
|
|
161
|
+
* console.log(format(date, 'YYYY-MM-DD')); // '2024-06-15'
|
|
162
|
+
* console.log(format(date, 'YYYY-MM-DD HH:mm:ss')); // '2024-06-15 14:35:22'
|
|
163
|
+
* console.log(format(date, 'DD/MM/YYYY [at] HH:mm')); // '15/06/2024 at 14:35'
|
|
164
|
+
* console.log(format(date, 'MMMM D, YYYY', { locale: 'en-US' })); // 'June 15, 2024'
|
|
165
|
+
*/
|
|
166
|
+
export function format(date, fmt, options = {}) {
|
|
167
|
+
const locale = options.locale ?? "en-US";
|
|
168
|
+
const tz = options.tz ?? "UTC";
|
|
169
|
+
// Get compiled format (cached)
|
|
170
|
+
const compiled = compileFormat(fmt);
|
|
171
|
+
// Fast path for UTC
|
|
172
|
+
const p = tz === "UTC" ? getUTCParts(date) : getPartsWithTZ(date, locale, tz);
|
|
173
|
+
// Build result string from compiled parts
|
|
174
|
+
let result = '';
|
|
175
|
+
for (const part of compiled.parts) {
|
|
176
|
+
if (part.type === 'literal') {
|
|
177
|
+
result += part.value;
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
result += tokenMap[part.value](p, locale);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the first date is after the second date.
|
|
3
|
+
*
|
|
4
|
+
* @param dateLeft - The date to compare.
|
|
5
|
+
* @param dateRight - The date to compare against.
|
|
6
|
+
* @returns True if dateLeft is after dateRight, false otherwise.
|
|
7
|
+
* @example
|
|
8
|
+
* const date1 = new Date('2024-01-20T12:00:00Z');
|
|
9
|
+
* const date2 = new Date('2024-01-15T12:00:00Z');
|
|
10
|
+
* console.log(isAfter(date1, date2)); // true
|
|
11
|
+
*/
|
|
12
|
+
export declare function isAfter(dateLeft: Date, dateRight: Date): boolean;
|
|
13
|
+
//# sourceMappingURL=is-after.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-after.d.ts","sourceRoot":"","sources":["../../src/lib/is-after.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,GAAG,OAAO,CAEhE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the first date is after the second date.
|
|
3
|
+
*
|
|
4
|
+
* @param dateLeft - The date to compare.
|
|
5
|
+
* @param dateRight - The date to compare against.
|
|
6
|
+
* @returns True if dateLeft is after dateRight, false otherwise.
|
|
7
|
+
* @example
|
|
8
|
+
* const date1 = new Date('2024-01-20T12:00:00Z');
|
|
9
|
+
* const date2 = new Date('2024-01-15T12:00:00Z');
|
|
10
|
+
* console.log(isAfter(date1, date2)); // true
|
|
11
|
+
*/
|
|
12
|
+
export function isAfter(dateLeft, dateRight) {
|
|
13
|
+
return dateLeft.getTime() > dateRight.getTime();
|
|
14
|
+
}
|
|
15
|
+
;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the first date is before the second date.
|
|
3
|
+
*
|
|
4
|
+
* @param dateLeft - The date to compare.
|
|
5
|
+
* @param dateRight - The date to compare against.
|
|
6
|
+
* @returns True if dateLeft is before dateRight, false otherwise.
|
|
7
|
+
* @example
|
|
8
|
+
* const date1 = new Date('2024-01-15T12:00:00Z');
|
|
9
|
+
* const date2 = new Date('2024-01-20T12:00:00Z');
|
|
10
|
+
* console.log(isBefore(date1, date2)); // true
|
|
11
|
+
*/
|
|
12
|
+
export declare function isBefore(dateLeft: Date, dateRight: Date): boolean;
|
|
13
|
+
//# sourceMappingURL=is-before.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-before.d.ts","sourceRoot":"","sources":["../../src/lib/is-before.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,GAAG,OAAO,CAEjE"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the first date is before the second date.
|
|
3
|
+
*
|
|
4
|
+
* @param dateLeft - The date to compare.
|
|
5
|
+
* @param dateRight - The date to compare against.
|
|
6
|
+
* @returns True if dateLeft is before dateRight, false otherwise.
|
|
7
|
+
* @example
|
|
8
|
+
* const date1 = new Date('2024-01-15T12:00:00Z');
|
|
9
|
+
* const date2 = new Date('2024-01-20T12:00:00Z');
|
|
10
|
+
* console.log(isBefore(date1, date2)); // true
|
|
11
|
+
*/
|
|
12
|
+
export function isBefore(dateLeft, dateRight) {
|
|
13
|
+
return dateLeft.getTime() < dateRight.getTime();
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an array of month names for the specified locale and format.
|
|
3
|
+
*
|
|
4
|
+
* @param locale - The locale to use (default: 'en-US').
|
|
5
|
+
* @param format - The format of month names: 'long', 'short', or 'narrow' (default: 'long').
|
|
6
|
+
* @returns An array of 12 month names starting from January.
|
|
7
|
+
* @example
|
|
8
|
+
* console.log(months('en-US', 'long')); // ['January', 'February', 'March', ...]
|
|
9
|
+
* console.log(months('en-US', 'short')); // ['Jan', 'Feb', 'Mar', ...]
|
|
10
|
+
* console.log(months('pt-BR', 'long')); // ['janeiro', 'fevereiro', 'março', ...]
|
|
11
|
+
*/
|
|
12
|
+
export declare function months(locale?: string, format?: "long" | "short" | "narrow"): string[];
|
|
13
|
+
//# sourceMappingURL=months.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"months.d.ts","sourceRoot":"","sources":["../../src/lib/months.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH,wBAAgB,MAAM,CACpB,MAAM,SAAU,EAChB,MAAM,GAAE,MAAM,GAAG,OAAO,GAAG,QAAiB,GAC3C,MAAM,EAAE,CAiBV"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const monthsCache = new Map();
|
|
2
|
+
/**
|
|
3
|
+
* Returns an array of month names for the specified locale and format.
|
|
4
|
+
*
|
|
5
|
+
* @param locale - The locale to use (default: 'en-US').
|
|
6
|
+
* @param format - The format of month names: 'long', 'short', or 'narrow' (default: 'long').
|
|
7
|
+
* @returns An array of 12 month names starting from January.
|
|
8
|
+
* @example
|
|
9
|
+
* console.log(months('en-US', 'long')); // ['January', 'February', 'March', ...]
|
|
10
|
+
* console.log(months('en-US', 'short')); // ['Jan', 'Feb', 'Mar', ...]
|
|
11
|
+
* console.log(months('pt-BR', 'long')); // ['janeiro', 'fevereiro', 'março', ...]
|
|
12
|
+
*/
|
|
13
|
+
export function months(locale = "en-US", format = "long") {
|
|
14
|
+
const key = `${locale}|${format}`;
|
|
15
|
+
const cached = monthsCache.get(key);
|
|
16
|
+
if (cached)
|
|
17
|
+
return cached;
|
|
18
|
+
const fmt = new Intl.DateTimeFormat(locale, {
|
|
19
|
+
month: format,
|
|
20
|
+
timeZone: "UTC",
|
|
21
|
+
});
|
|
22
|
+
const result = Array.from({ length: 12 }, (_, i) => fmt.format(new Date(Date.UTC(2024, i, 1))));
|
|
23
|
+
monthsCache.set(key, result);
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Unit } from "../types/unit.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the start of the specified time unit for the given date.
|
|
4
|
+
*
|
|
5
|
+
* @param date - The original date.
|
|
6
|
+
* @param unit - The time unit to get the start of ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second').
|
|
7
|
+
* @returns A new Date object set to the start of the specified unit.
|
|
8
|
+
* @example
|
|
9
|
+
* const date = new Date('2024-06-15T14:35:22.500Z');
|
|
10
|
+
* const startOfDay = startOf(date, 'day');
|
|
11
|
+
* console.log(startOfDay.toISOString()); // '2024-06-15T00:00:00.000Z'
|
|
12
|
+
*/
|
|
13
|
+
export declare function startOf(date: Date, unit: Unit): Date;
|
|
14
|
+
//# sourceMappingURL=start-of.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"start-of.d.ts","sourceRoot":"","sources":["../../src/lib/start-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;;GAUG;AAEH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CAwCpD"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the start of the specified time unit for the given date.
|
|
3
|
+
*
|
|
4
|
+
* @param date - The original date.
|
|
5
|
+
* @param unit - The time unit to get the start of ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second').
|
|
6
|
+
* @returns A new Date object set to the start of the specified unit.
|
|
7
|
+
* @example
|
|
8
|
+
* const date = new Date('2024-06-15T14:35:22.500Z');
|
|
9
|
+
* const startOfDay = startOf(date, 'day');
|
|
10
|
+
* console.log(startOfDay.toISOString()); // '2024-06-15T00:00:00.000Z'
|
|
11
|
+
*/
|
|
12
|
+
export function startOf(date, unit) {
|
|
13
|
+
const time = new Date(date.getTime());
|
|
14
|
+
switch (unit) {
|
|
15
|
+
case 'year': {
|
|
16
|
+
time.setUTCMonth(0, 1);
|
|
17
|
+
time.setUTCHours(0, 0, 0, 0);
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
case 'month': {
|
|
21
|
+
time.setUTCDate(1);
|
|
22
|
+
time.setUTCHours(0, 0, 0, 0);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
case 'week': {
|
|
26
|
+
const day = time.getUTCDay();
|
|
27
|
+
const diff = (day === 0 ? -6 : 1) - day; // Adjust when day is Sunday
|
|
28
|
+
time.setUTCDate(time.getUTCDate() + diff);
|
|
29
|
+
time.setUTCHours(0, 0, 0, 0);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case 'day': {
|
|
33
|
+
time.setUTCHours(0, 0, 0, 0);
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case 'hour': {
|
|
37
|
+
time.setUTCMinutes(0, 0, 0);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case 'minute': {
|
|
41
|
+
time.setUTCSeconds(0, 0);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
case 'second': {
|
|
45
|
+
time.setUTCMilliseconds(0);
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return time;
|
|
50
|
+
}
|
package/esm/lib/sub.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
type SubOptions = {
|
|
2
|
+
years?: number;
|
|
3
|
+
months?: number;
|
|
4
|
+
weeks?: number;
|
|
5
|
+
days?: number;
|
|
6
|
+
hours?: number;
|
|
7
|
+
minutes?: number;
|
|
8
|
+
seconds?: number;
|
|
9
|
+
milliseconds?: number;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Subtracts specified time units from the given date.
|
|
13
|
+
*
|
|
14
|
+
* @param date - The original date.
|
|
15
|
+
* @param opt - The time units to subtract.
|
|
16
|
+
* @returns A new Date object with the specified time units subtracted.
|
|
17
|
+
* @example
|
|
18
|
+
* const date = new Date('2024-03-31T12:00:00Z');
|
|
19
|
+
* const newDate = sub(date, { years: 1, months: 1 });
|
|
20
|
+
* console.log(newDate.toISOString()); // '2023-02-28T12:00:00.000Z'
|
|
21
|
+
*/
|
|
22
|
+
export declare function sub(date: Date, opt: SubOptions): Date;
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=sub.d.ts.map
|