@formkit/tempo 1.0.0 → 1.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/README.md +22 -1
- package/dist/add.d.ts +11 -0
- package/dist/add.mjs +50 -0
- package/dist/add.mjs.map +1 -0
- package/dist/addMonth.mjs +2 -11
- package/dist/addMonth.mjs.map +1 -1
- package/dist/addYear.mjs +6 -11
- package/dist/addYear.mjs.map +1 -1
- package/dist/bundle.d.ts +110 -1
- package/dist/bundle.mjs +214 -46
- package/dist/bundle.mjs.map +1 -1
- package/dist/diff.d.ts +38 -0
- package/dist/diff.mjs +86 -0
- package/dist/diff.mjs.map +1 -0
- package/dist/handleDateOverflow.d.ts +12 -0
- package/dist/handleDateOverflow.mjs +18 -0
- package/dist/handleDateOverflow.mjs.map +1 -0
- package/dist/index.cjs +223 -46
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +110 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.mjs +18 -0
- package/dist/index.mjs.map +1 -1
- package/dist/offset.mjs +14 -12
- package/dist/offset.mjs.map +1 -1
- package/dist/setDayOfMonth.d.ts +11 -0
- package/dist/setDayOfMonth.mjs +16 -0
- package/dist/setDayOfMonth.mjs.map +1 -0
- package/dist/setHour.d.ts +10 -0
- package/dist/setHour.mjs +11 -0
- package/dist/setHour.mjs.map +1 -0
- package/dist/setMilliseconds.d.ts +10 -0
- package/dist/setMilliseconds.mjs +11 -0
- package/dist/setMilliseconds.mjs.map +1 -0
- package/dist/setMinutes.d.ts +10 -0
- package/dist/setMinutes.mjs +11 -0
- package/dist/setMinutes.mjs.map +1 -0
- package/dist/setMonth.d.ts +11 -0
- package/dist/setMonth.mjs +9 -0
- package/dist/setMonth.mjs.map +1 -0
- package/dist/setSeconds.d.ts +10 -0
- package/dist/setSeconds.mjs +11 -0
- package/dist/setSeconds.mjs.map +1 -0
- package/dist/setYear.d.ts +11 -0
- package/dist/setYear.mjs +9 -0
- package/dist/setYear.mjs.map +1 -0
- package/dist/types.d.ts +15 -1
- package/package.json +3 -3
package/dist/diff.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Duration, DateInput, MaybeDateInput } from './types.js';
|
|
2
|
+
|
|
3
|
+
type DurationKey = keyof Duration;
|
|
4
|
+
/**
|
|
5
|
+
* Options for `diff` function
|
|
6
|
+
*/
|
|
7
|
+
interface DiffOptions {
|
|
8
|
+
/**
|
|
9
|
+
* whether the difference should be absolute (not negative)
|
|
10
|
+
*/
|
|
11
|
+
abs?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* units you want to skip, for example weeks
|
|
14
|
+
*/
|
|
15
|
+
skip?: DurationKey[] | Set<DurationKey>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Returns the difference between 2 dates in an object
|
|
19
|
+
* @param dateA - A date to compare with the right date
|
|
20
|
+
* @param [dateB] - A date to compare with the left date or nothing to compare with the current time
|
|
21
|
+
* @param [options] additional options
|
|
22
|
+
* @param [options.skip] units you want skip
|
|
23
|
+
* @param [options.abs] whether the difference should be absolute
|
|
24
|
+
* @returns an object which could be used with `Intl.DurationFormat.format'`
|
|
25
|
+
*/
|
|
26
|
+
declare function diff(dateA: DateInput, dateB?: MaybeDateInput, options?: DiffOptions): Duration;
|
|
27
|
+
/**
|
|
28
|
+
* Returns the difference between 2 dates in an object
|
|
29
|
+
* @param [dateA] - A date to compare with the right date or null to compare with the current time
|
|
30
|
+
* @param dateB - A date to compare with the left date
|
|
31
|
+
* @param [options] additional options
|
|
32
|
+
* @param [options.skip] units you want skip
|
|
33
|
+
* @param [options.abs] whether the difference should be absolute
|
|
34
|
+
* @returns an object which could be used with `Intl.DurationFormat.format'`
|
|
35
|
+
*/
|
|
36
|
+
declare function diff(dateA: MaybeDateInput, dateB: DateInput, options?: DiffOptions): Duration;
|
|
37
|
+
|
|
38
|
+
export { type DiffOptions, diff };
|
package/dist/diff.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// src/diff.ts
|
|
2
|
+
import { addDay } from "./addDay.mjs";
|
|
3
|
+
import { addHour } from "./addHour.mjs";
|
|
4
|
+
import { addMinute } from "./addMinute.mjs";
|
|
5
|
+
import { addMonth } from "./addMonth.mjs";
|
|
6
|
+
import { addSecond } from "./addSecond.mjs";
|
|
7
|
+
import { addYear } from "./addYear.mjs";
|
|
8
|
+
import { date } from "./date.mjs";
|
|
9
|
+
import { diffDays } from "./diffDays.mjs";
|
|
10
|
+
import { diffHours } from "./diffHours.mjs";
|
|
11
|
+
import { diffMilliseconds } from "./diffMilliseconds.mjs";
|
|
12
|
+
import { diffMinutes } from "./diffMinutes.mjs";
|
|
13
|
+
import { diffMonths } from "./diffMonths.mjs";
|
|
14
|
+
import { diffSeconds } from "./diffSeconds.mjs";
|
|
15
|
+
import { diffWeeks } from "./diffWeeks.mjs";
|
|
16
|
+
import { diffYears } from "./diffYears.mjs";
|
|
17
|
+
function negateDuration(duration) {
|
|
18
|
+
const negated = {};
|
|
19
|
+
for (const unit of Object.keys(duration)) {
|
|
20
|
+
negated[unit] = -duration[unit];
|
|
21
|
+
}
|
|
22
|
+
return negated;
|
|
23
|
+
}
|
|
24
|
+
function calendarDiff(current, target, diffUnit, addUnit) {
|
|
25
|
+
let amount = diffUnit(current, target);
|
|
26
|
+
let next = addUnit(current, -amount);
|
|
27
|
+
while (amount > 0 && next < target) {
|
|
28
|
+
amount--;
|
|
29
|
+
next = addUnit(current, -amount);
|
|
30
|
+
}
|
|
31
|
+
return [amount, next];
|
|
32
|
+
}
|
|
33
|
+
function diff(dateA, dateB, options) {
|
|
34
|
+
let a = date(dateA);
|
|
35
|
+
let b = date(dateB);
|
|
36
|
+
if (a < b) {
|
|
37
|
+
const duration2 = diff(b, a, options);
|
|
38
|
+
return (options == null ? void 0 : options.abs) ? duration2 : negateDuration(duration2);
|
|
39
|
+
}
|
|
40
|
+
const skip = new Set(options == null ? void 0 : options.skip);
|
|
41
|
+
const duration = {};
|
|
42
|
+
if (!skip.has("years")) {
|
|
43
|
+
const [years, next] = calendarDiff(a, b, diffYears, addYear);
|
|
44
|
+
a = next;
|
|
45
|
+
if (years) duration.years = years;
|
|
46
|
+
}
|
|
47
|
+
if (!skip.has("months")) {
|
|
48
|
+
const [months, next] = calendarDiff(a, b, diffMonths, addMonth);
|
|
49
|
+
a = next;
|
|
50
|
+
if (months) duration.months = months;
|
|
51
|
+
}
|
|
52
|
+
if (!skip.has("weeks")) {
|
|
53
|
+
const weeks = diffWeeks(a, b);
|
|
54
|
+
a = addDay(a, -(weeks * 7));
|
|
55
|
+
if (weeks) duration.weeks = weeks;
|
|
56
|
+
}
|
|
57
|
+
if (!skip.has("days")) {
|
|
58
|
+
const days = diffDays(a, b);
|
|
59
|
+
a = addDay(a, -days);
|
|
60
|
+
if (days) duration.days = days;
|
|
61
|
+
}
|
|
62
|
+
if (!skip.has("hours")) {
|
|
63
|
+
const hours = diffHours(a, b);
|
|
64
|
+
a = addHour(a, -hours);
|
|
65
|
+
if (hours) duration.hours = hours;
|
|
66
|
+
}
|
|
67
|
+
if (!skip.has("minutes")) {
|
|
68
|
+
const minutes = diffMinutes(a, b);
|
|
69
|
+
a = addMinute(a, -minutes);
|
|
70
|
+
if (minutes) duration.minutes = minutes;
|
|
71
|
+
}
|
|
72
|
+
if (!skip.has("seconds")) {
|
|
73
|
+
const seconds = diffSeconds(a, b);
|
|
74
|
+
a = addSecond(a, -seconds);
|
|
75
|
+
if (seconds) duration.seconds = seconds;
|
|
76
|
+
}
|
|
77
|
+
if (!skip.has("milliseconds")) {
|
|
78
|
+
const ms = diffMilliseconds(a, b);
|
|
79
|
+
if (ms) duration.milliseconds = ms;
|
|
80
|
+
}
|
|
81
|
+
return duration;
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
diff
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=diff.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/diff.ts"],"sourcesContent":["import { addDay } from \"./addDay\"\nimport { addHour } from \"./addHour\"\nimport { addMinute } from \"./addMinute\"\nimport { addMonth } from \"./addMonth\"\nimport { addSecond } from \"./addSecond\"\nimport { addYear } from \"./addYear\"\nimport { date } from \"./date\"\nimport { diffDays } from \"./diffDays\"\nimport { diffHours } from \"./diffHours\"\nimport { diffMilliseconds } from \"./diffMilliseconds\"\nimport { diffMinutes } from \"./diffMinutes\"\nimport { diffMonths } from \"./diffMonths\"\nimport { diffSeconds } from \"./diffSeconds\"\nimport { diffWeeks } from \"./diffWeeks\"\nimport { diffYears } from \"./diffYears\"\nimport type { DateInput, Duration, MaybeDateInput } from \"./types\"\n\ntype DurationKey = keyof Duration\n\nfunction negateDuration(duration: Duration): Duration {\n const negated: Duration = {}\n for (const unit of Object.keys(duration) as DurationKey[]) {\n negated[unit] = -duration[unit]!\n }\n return negated\n}\n\nfunction calendarDiff(\n current: Date,\n target: Date,\n diffUnit: (dateA: Date, dateB: Date) => number,\n addUnit: (date: Date, count: number) => Date\n): [number, Date] {\n let amount = diffUnit(current, target)\n let next = addUnit(current, -amount)\n while (amount > 0 && next < target) {\n amount--\n next = addUnit(current, -amount)\n }\n return [amount, next]\n}\n\n/**\n * Options for `diff` function\n */\nexport interface DiffOptions {\n /**\n * whether the difference should be absolute (not negative)\n */\n abs?: boolean\n /**\n * units you want to skip, for example weeks\n */\n skip?: DurationKey[] | Set<DurationKey>\n}\n\n/**\n * Returns the difference between 2 dates in an object\n * @param dateA - A date to compare with the right date\n * @param [dateB] - A date to compare with the left date or nothing to compare with the current time\n * @param [options] additional options\n * @param [options.skip] units you want skip\n * @param [options.abs] whether the difference should be absolute\n * @returns an object which could be used with `Intl.DurationFormat.format'`\n */\nexport function diff(\n dateA: DateInput,\n dateB?: MaybeDateInput,\n options?: DiffOptions\n): Duration\n\n/**\n * Returns the difference between 2 dates in an object\n * @param [dateA] - A date to compare with the right date or null to compare with the current time\n * @param dateB - A date to compare with the left date\n * @param [options] additional options\n * @param [options.skip] units you want skip\n * @param [options.abs] whether the difference should be absolute\n * @returns an object which could be used with `Intl.DurationFormat.format'`\n */\nexport function diff(\n dateA: MaybeDateInput,\n dateB: DateInput,\n options?: DiffOptions\n): Duration\n\nexport function diff(\n dateA: MaybeDateInput,\n dateB?: MaybeDateInput,\n options?: DiffOptions\n): Duration {\n let a = date(dateA)\n let b = date(dateB)\n\n if (a < b) {\n const duration = diff(b, a, options)\n return options?.abs ? duration : negateDuration(duration)\n }\n\n const skip = new Set(options?.skip)\n const duration: Duration = {}\n\n if (!skip.has(\"years\")) {\n const [years, next] = calendarDiff(a, b, diffYears, addYear)\n a = next\n if (years) duration.years = years\n }\n\n if (!skip.has(\"months\")) {\n const [months, next] = calendarDiff(a, b, diffMonths, addMonth)\n a = next\n if (months) duration.months = months\n }\n\n if (!skip.has(\"weeks\")) {\n const weeks = diffWeeks(a, b)\n a = addDay(a, -(weeks * 7))\n if (weeks) duration.weeks = weeks\n }\n\n if (!skip.has(\"days\")) {\n const days = diffDays(a, b)\n a = addDay(a, -days)\n if (days) duration.days = days\n }\n\n if (!skip.has(\"hours\")) {\n const hours = diffHours(a, b)\n a = addHour(a, -hours)\n if (hours) duration.hours = hours\n }\n\n if (!skip.has(\"minutes\")) {\n const minutes = diffMinutes(a, b)\n a = addMinute(a, -minutes)\n if (minutes) duration.minutes = minutes\n }\n\n if (!skip.has(\"seconds\")) {\n const seconds = diffSeconds(a, b)\n a = addSecond(a, -seconds)\n if (seconds) duration.seconds = seconds\n }\n\n if (!skip.has(\"milliseconds\")) {\n const ms = diffMilliseconds(a, b)\n // removing ms isn't needed as it's the last\n if (ms) duration.milliseconds = ms\n }\n return duration\n}\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,eAAe;AACxB,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAC1B,SAAS,eAAe;AACxB,SAAS,YAAY;AACrB,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAC1B,SAAS,wBAAwB;AACjC,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAC3B,SAAS,mBAAmB;AAC5B,SAAS,iBAAiB;AAC1B,SAAS,iBAAiB;AAK1B,SAAS,eAAe,UAA8B;AACpD,QAAM,UAAoB,CAAC;AAC3B,aAAW,QAAQ,OAAO,KAAK,QAAQ,GAAoB;AACzD,YAAQ,IAAI,IAAI,CAAC,SAAS,IAAI;AAAA,EAChC;AACA,SAAO;AACT;AAEA,SAAS,aACP,SACA,QACA,UACA,SACgB;AAChB,MAAI,SAAS,SAAS,SAAS,MAAM;AACrC,MAAI,OAAO,QAAQ,SAAS,CAAC,MAAM;AACnC,SAAO,SAAS,KAAK,OAAO,QAAQ;AAClC;AACA,WAAO,QAAQ,SAAS,CAAC,MAAM;AAAA,EACjC;AACA,SAAO,CAAC,QAAQ,IAAI;AACtB;AA8CO,SAAS,KACd,OACA,OACA,SACU;AACV,MAAI,IAAI,KAAK,KAAK;AAClB,MAAI,IAAI,KAAK,KAAK;AAElB,MAAI,IAAI,GAAG;AACT,UAAMA,YAAW,KAAK,GAAG,GAAG,OAAO;AACnC,YAAO,mCAAS,OAAMA,YAAW,eAAeA,SAAQ;AAAA,EAC1D;AAEA,QAAM,OAAO,IAAI,IAAI,mCAAS,IAAI;AAClC,QAAM,WAAqB,CAAC;AAE5B,MAAI,CAAC,KAAK,IAAI,OAAO,GAAG;AACtB,UAAM,CAAC,OAAO,IAAI,IAAI,aAAa,GAAG,GAAG,WAAW,OAAO;AAC3D,QAAI;AACJ,QAAI,MAAO,UAAS,QAAQ;AAAA,EAC9B;AAEA,MAAI,CAAC,KAAK,IAAI,QAAQ,GAAG;AACvB,UAAM,CAAC,QAAQ,IAAI,IAAI,aAAa,GAAG,GAAG,YAAY,QAAQ;AAC9D,QAAI;AACJ,QAAI,OAAQ,UAAS,SAAS;AAAA,EAChC;AAEA,MAAI,CAAC,KAAK,IAAI,OAAO,GAAG;AACtB,UAAM,QAAQ,UAAU,GAAG,CAAC;AAC5B,QAAI,OAAO,GAAG,EAAE,QAAQ,EAAE;AAC1B,QAAI,MAAO,UAAS,QAAQ;AAAA,EAC9B;AAEA,MAAI,CAAC,KAAK,IAAI,MAAM,GAAG;AACrB,UAAM,OAAO,SAAS,GAAG,CAAC;AAC1B,QAAI,OAAO,GAAG,CAAC,IAAI;AACnB,QAAI,KAAM,UAAS,OAAO;AAAA,EAC5B;AAEA,MAAI,CAAC,KAAK,IAAI,OAAO,GAAG;AACtB,UAAM,QAAQ,UAAU,GAAG,CAAC;AAC5B,QAAI,QAAQ,GAAG,CAAC,KAAK;AACrB,QAAI,MAAO,UAAS,QAAQ;AAAA,EAC9B;AAEA,MAAI,CAAC,KAAK,IAAI,SAAS,GAAG;AACxB,UAAM,UAAU,YAAY,GAAG,CAAC;AAChC,QAAI,UAAU,GAAG,CAAC,OAAO;AACzB,QAAI,QAAS,UAAS,UAAU;AAAA,EAClC;AAEA,MAAI,CAAC,KAAK,IAAI,SAAS,GAAG;AACxB,UAAM,UAAU,YAAY,GAAG,CAAC;AAChC,QAAI,UAAU,GAAG,CAAC,OAAO;AACzB,QAAI,QAAS,UAAS,UAAU;AAAA,EAClC;AAEA,MAAI,CAAC,KAAK,IAAI,cAAc,GAAG;AAC7B,UAAM,KAAK,iBAAiB,GAAG,CAAC;AAEhC,QAAI,GAAI,UAAS,eAAe;AAAA,EAClC;AACA,SAAO;AACT;","names":["duration"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { MaybeDateInput } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Handles date overflow when changing month or year.
|
|
5
|
+
* @param [inputDate] - A string, Date object or null for the current time
|
|
6
|
+
* @param action - The action that changes the month/year
|
|
7
|
+
* @param [dateOverflow] - Whether to allow the date to overflow into another month.
|
|
8
|
+
* @returns A new Date object with the month/year change applied.
|
|
9
|
+
*/
|
|
10
|
+
declare function handleOverflow(inputDate: MaybeDateInput | undefined, action: (d: Date) => void, dateOverflow?: boolean): Date;
|
|
11
|
+
|
|
12
|
+
export { handleOverflow };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/handleDateOverflow.ts
|
|
2
|
+
import { date } from "./date.mjs";
|
|
3
|
+
import { monthDays } from "./monthDays.mjs";
|
|
4
|
+
function handleOverflow(inputDate, action, dateOverflow = false) {
|
|
5
|
+
const d = date(inputDate);
|
|
6
|
+
const dayOfMonth = d.getDate();
|
|
7
|
+
if (!dateOverflow) d.setDate(1);
|
|
8
|
+
action(d);
|
|
9
|
+
if (!dateOverflow) {
|
|
10
|
+
const daysInMonth = monthDays(d);
|
|
11
|
+
d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth);
|
|
12
|
+
}
|
|
13
|
+
return d;
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
handleOverflow
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=handleDateOverflow.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/handleDateOverflow.ts"],"sourcesContent":["import { date } from \"./date\"\nimport { monthDays } from \"./monthDays\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Handles date overflow when changing month or year.\n * @param [inputDate] - A string, Date object or null for the current time\n * @param action - The action that changes the month/year\n * @param [dateOverflow] - Whether to allow the date to overflow into another month.\n * @returns A new Date object with the month/year change applied.\n */\nexport function handleOverflow(\n inputDate: MaybeDateInput | undefined,\n action: (d: Date) => void,\n dateOverflow = false\n): Date {\n const d = date(inputDate)\n const dayOfMonth = d.getDate()\n // If overflowing is disallowed, set the date back to the first of the month\n if (!dateOverflow) d.setDate(1)\n\n action(d)\n // If overflowing is disallowed, we need to set the date back to the proper\n // day or the last day of the month.\n if (!dateOverflow) {\n const daysInMonth = monthDays(d)\n d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth)\n }\n return d\n}\n"],"mappings":";AAAA,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAUnB,SAAS,eACd,WACA,QACA,eAAe,OACT;AACN,QAAM,IAAI,KAAK,SAAS;AACxB,QAAM,aAAa,EAAE,QAAQ;AAE7B,MAAI,CAAC,aAAc,GAAE,QAAQ,CAAC;AAE9B,SAAO,CAAC;AAGR,MAAI,CAAC,cAAc;AACjB,UAAM,cAAc,UAAU,CAAC;AAC/B,MAAE,QAAQ,cAAc,aAAa,cAAc,UAAU;AAAA,EAC/D;AACA,SAAO;AACT;","names":[]}
|
package/dist/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
add: () => add,
|
|
23
24
|
addDay: () => addDay,
|
|
24
25
|
addHour: () => addHour,
|
|
25
26
|
addMillisecond: () => addMillisecond,
|
|
@@ -33,6 +34,7 @@ __export(index_exports, {
|
|
|
33
34
|
dayEnd: () => dayEnd,
|
|
34
35
|
dayOfYear: () => dayOfYear,
|
|
35
36
|
dayStart: () => dayStart,
|
|
37
|
+
diff: () => diff,
|
|
36
38
|
diffDays: () => diffDays,
|
|
37
39
|
diffHours: () => diffHours,
|
|
38
40
|
diffMilliseconds: () => diffMilliseconds,
|
|
@@ -70,6 +72,13 @@ __export(index_exports, {
|
|
|
70
72
|
sameMinute: () => sameMinute,
|
|
71
73
|
sameSecond: () => sameSecond,
|
|
72
74
|
sameYear: () => sameYear,
|
|
75
|
+
setDayOfMonth: () => setDayOfMonth,
|
|
76
|
+
setHour: () => setHour,
|
|
77
|
+
setMilliseconds: () => setMilliseconds,
|
|
78
|
+
setMinutes: () => setMinutes,
|
|
79
|
+
setMonth: () => setMonth,
|
|
80
|
+
setSeconds: () => setSeconds,
|
|
81
|
+
setYear: () => setYear,
|
|
73
82
|
tzDate: () => tzDate,
|
|
74
83
|
weekEnd: () => weekEnd,
|
|
75
84
|
weekStart: () => weekStart,
|
|
@@ -128,6 +137,27 @@ function addDay(inputDate, count = 1) {
|
|
|
128
137
|
return d;
|
|
129
138
|
}
|
|
130
139
|
|
|
140
|
+
// src/addHour.ts
|
|
141
|
+
function addHour(inputDate, count = 1) {
|
|
142
|
+
const d = date(inputDate);
|
|
143
|
+
d.setHours(d.getHours() + count);
|
|
144
|
+
return d;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// src/addMillisecond.ts
|
|
148
|
+
function addMillisecond(inputDate, count = 1) {
|
|
149
|
+
const d = date(inputDate);
|
|
150
|
+
d.setMilliseconds(d.getMilliseconds() + count);
|
|
151
|
+
return d;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/addMinute.ts
|
|
155
|
+
function addMinute(inputDate, count = 1) {
|
|
156
|
+
const d = date(inputDate);
|
|
157
|
+
d.setMinutes(d.getMinutes() + count);
|
|
158
|
+
return d;
|
|
159
|
+
}
|
|
160
|
+
|
|
131
161
|
// src/monthEnd.ts
|
|
132
162
|
function monthEnd(inputDate) {
|
|
133
163
|
const d = date(inputDate);
|
|
@@ -143,25 +173,12 @@ function monthDays(inputDate) {
|
|
|
143
173
|
return d.getDate();
|
|
144
174
|
}
|
|
145
175
|
|
|
146
|
-
// src/
|
|
147
|
-
function
|
|
148
|
-
const d = date(inputDate);
|
|
149
|
-
const dayOfMonth = d.getDate();
|
|
150
|
-
if (!dateOverflow) d.setDate(1);
|
|
151
|
-
d.setMonth(d.getMonth() + count);
|
|
152
|
-
if (!dateOverflow) {
|
|
153
|
-
const daysInMonth = monthDays(d);
|
|
154
|
-
d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth);
|
|
155
|
-
}
|
|
156
|
-
return d;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// src/addYear.ts
|
|
160
|
-
function addYear(inputDate, count = 1, dateOverflow = false) {
|
|
176
|
+
// src/handleDateOverflow.ts
|
|
177
|
+
function handleOverflow(inputDate, action, dateOverflow = false) {
|
|
161
178
|
const d = date(inputDate);
|
|
162
179
|
const dayOfMonth = d.getDate();
|
|
163
180
|
if (!dateOverflow) d.setDate(1);
|
|
164
|
-
|
|
181
|
+
action(d);
|
|
165
182
|
if (!dateOverflow) {
|
|
166
183
|
const daysInMonth = monthDays(d);
|
|
167
184
|
d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth);
|
|
@@ -169,18 +186,9 @@ function addYear(inputDate, count = 1, dateOverflow = false) {
|
|
|
169
186
|
return d;
|
|
170
187
|
}
|
|
171
188
|
|
|
172
|
-
// src/
|
|
173
|
-
function
|
|
174
|
-
|
|
175
|
-
d.setHours(d.getHours() + count);
|
|
176
|
-
return d;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// src/addMinute.ts
|
|
180
|
-
function addMinute(inputDate, count = 1) {
|
|
181
|
-
const d = date(inputDate);
|
|
182
|
-
d.setMinutes(d.getMinutes() + count);
|
|
183
|
-
return d;
|
|
189
|
+
// src/addMonth.ts
|
|
190
|
+
function addMonth(inputDate, count = 1, dateOverflow = false) {
|
|
191
|
+
return handleOverflow(inputDate, (d) => d.setMonth(d.getMonth() + count), dateOverflow);
|
|
184
192
|
}
|
|
185
193
|
|
|
186
194
|
// src/addSecond.ts
|
|
@@ -190,10 +198,51 @@ function addSecond(inputDate, count = 1) {
|
|
|
190
198
|
return d;
|
|
191
199
|
}
|
|
192
200
|
|
|
193
|
-
// src/
|
|
194
|
-
function
|
|
195
|
-
|
|
196
|
-
|
|
201
|
+
// src/addYear.ts
|
|
202
|
+
function addYear(inputDate, count = 1, dateOverflow = false) {
|
|
203
|
+
return handleOverflow(
|
|
204
|
+
inputDate,
|
|
205
|
+
(d) => d.setFullYear(d.getFullYear() + count),
|
|
206
|
+
dateOverflow
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// src/add.ts
|
|
211
|
+
function add(inputDate, duration, dateOverflow = false) {
|
|
212
|
+
var _a, _b;
|
|
213
|
+
let d = date(inputDate);
|
|
214
|
+
const applyFixedUnits = () => {
|
|
215
|
+
if (duration.weeks) {
|
|
216
|
+
d = addDay(d, duration.weeks * 7);
|
|
217
|
+
}
|
|
218
|
+
if (duration.days) {
|
|
219
|
+
d = addDay(d, duration.days);
|
|
220
|
+
}
|
|
221
|
+
if (duration.hours) {
|
|
222
|
+
d = addHour(d, duration.hours);
|
|
223
|
+
}
|
|
224
|
+
if (duration.minutes) {
|
|
225
|
+
d = addMinute(d, duration.minutes);
|
|
226
|
+
}
|
|
227
|
+
if (duration.seconds) {
|
|
228
|
+
d = addSecond(d, duration.seconds);
|
|
229
|
+
}
|
|
230
|
+
if (duration.milliseconds) {
|
|
231
|
+
d = addMillisecond(d, duration.milliseconds);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
const applyCalendarUnits = () => {
|
|
235
|
+
if (duration.months) {
|
|
236
|
+
d = addMonth(d, duration.months, dateOverflow);
|
|
237
|
+
}
|
|
238
|
+
if (duration.years) {
|
|
239
|
+
d = addYear(d, duration.years, dateOverflow);
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
const calendarFirst = ((_a = duration.months) != null ? _a : 0) < 0 || ((_b = duration.years) != null ? _b : 0) < 0;
|
|
243
|
+
if (calendarFirst) applyCalendarUnits();
|
|
244
|
+
applyFixedUnits();
|
|
245
|
+
if (!calendarFirst) applyCalendarUnits();
|
|
197
246
|
return d;
|
|
198
247
|
}
|
|
199
248
|
|
|
@@ -462,22 +511,24 @@ function deviceTZ() {
|
|
|
462
511
|
// src/offset.ts
|
|
463
512
|
function relativeTime(d, timeZone) {
|
|
464
513
|
const utcParts = new Intl.DateTimeFormat("en-US", {
|
|
514
|
+
era: "short",
|
|
465
515
|
year: "numeric",
|
|
466
|
-
month: "
|
|
467
|
-
day: "
|
|
468
|
-
hour: "
|
|
469
|
-
minute: "
|
|
470
|
-
second: "
|
|
516
|
+
month: "numeric",
|
|
517
|
+
day: "numeric",
|
|
518
|
+
hour: "numeric",
|
|
519
|
+
minute: "numeric",
|
|
520
|
+
second: "numeric",
|
|
471
521
|
timeZone,
|
|
472
522
|
hourCycle: "h23"
|
|
473
|
-
}).formatToParts(d)
|
|
474
|
-
const
|
|
523
|
+
}).formatToParts(d);
|
|
524
|
+
const p = {};
|
|
475
525
|
utcParts.forEach((part) => {
|
|
476
|
-
|
|
526
|
+
if (part.type !== "literal") p[part.type] = part.value;
|
|
477
527
|
});
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
);
|
|
528
|
+
const year = p.era === "BC" ? 1 - Number(p.year) : Number(p.year);
|
|
529
|
+
const result = new Date(Date.UTC(0, 0, 1, Number(p.hour), Number(p.minute), Number(p.second)));
|
|
530
|
+
result.setUTCFullYear(year, Number(p.month) - 1, Number(p.day));
|
|
531
|
+
return result;
|
|
481
532
|
}
|
|
482
533
|
function offset(utcTime, tzA = "UTC", tzB = "device", timeZoneToken = "Z") {
|
|
483
534
|
var _a;
|
|
@@ -1100,12 +1151,61 @@ function sameYear(inputDateA, inputDateB) {
|
|
|
1100
1151
|
return a.getFullYear() === b.getFullYear();
|
|
1101
1152
|
}
|
|
1102
1153
|
|
|
1154
|
+
// src/setMilliseconds.ts
|
|
1155
|
+
function setMilliseconds(inputDate, ms) {
|
|
1156
|
+
const d = date(inputDate);
|
|
1157
|
+
d.setMilliseconds(ms);
|
|
1158
|
+
return d;
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
// src/setSeconds.ts
|
|
1162
|
+
function setSeconds(inputDate, second) {
|
|
1163
|
+
const d = date(inputDate);
|
|
1164
|
+
d.setSeconds(second);
|
|
1165
|
+
return d;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
// src/setMinutes.ts
|
|
1169
|
+
function setMinutes(inputDate, minute) {
|
|
1170
|
+
const d = date(inputDate);
|
|
1171
|
+
d.setMinutes(minute);
|
|
1172
|
+
return d;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
// src/setHour.ts
|
|
1176
|
+
function setHour(inputDate, hour) {
|
|
1177
|
+
const d = date(inputDate);
|
|
1178
|
+
d.setHours(hour);
|
|
1179
|
+
return d;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
// src/setDayOfMonth.ts
|
|
1183
|
+
function setDayOfMonth(inputDate, day, dateOverflow = false) {
|
|
1184
|
+
const d = date(inputDate);
|
|
1185
|
+
const daysInMonth = monthDays(d);
|
|
1186
|
+
if (!dateOverflow) {
|
|
1187
|
+
day = day > daysInMonth ? daysInMonth : day;
|
|
1188
|
+
}
|
|
1189
|
+
d.setDate(day);
|
|
1190
|
+
return d;
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
// src/setMonth.ts
|
|
1194
|
+
function setMonth(inputDate, month, dateOverflow = false) {
|
|
1195
|
+
return handleOverflow(inputDate, (d) => d.setMonth(month), dateOverflow);
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
// src/setYear.ts
|
|
1199
|
+
function setYear(inputDate, year, dateOverflow = false) {
|
|
1200
|
+
return handleOverflow(inputDate, (d) => d.setFullYear(year), dateOverflow);
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1103
1203
|
// src/weekStart.ts
|
|
1104
1204
|
function weekStart(inputDate, startOfWeekDay = 0) {
|
|
1105
1205
|
const d = date(inputDate);
|
|
1106
|
-
let
|
|
1107
|
-
if (
|
|
1108
|
-
d.setDate(d.getDate() +
|
|
1206
|
+
let diff2 = startOfWeekDay - d.getDay();
|
|
1207
|
+
if (diff2 > 0) diff2 = diff2 - 7;
|
|
1208
|
+
d.setDate(d.getDate() + diff2);
|
|
1109
1209
|
d.setHours(0, 0, 0, 0);
|
|
1110
1210
|
return d;
|
|
1111
1211
|
}
|
|
@@ -1253,8 +1353,77 @@ function diffYears(dateA, dateB) {
|
|
|
1253
1353
|
);
|
|
1254
1354
|
return r == 0 ? 0 : r;
|
|
1255
1355
|
}
|
|
1356
|
+
|
|
1357
|
+
// src/diff.ts
|
|
1358
|
+
function negateDuration(duration) {
|
|
1359
|
+
const negated = {};
|
|
1360
|
+
for (const unit of Object.keys(duration)) {
|
|
1361
|
+
negated[unit] = -duration[unit];
|
|
1362
|
+
}
|
|
1363
|
+
return negated;
|
|
1364
|
+
}
|
|
1365
|
+
function calendarDiff(current, target, diffUnit, addUnit) {
|
|
1366
|
+
let amount = diffUnit(current, target);
|
|
1367
|
+
let next = addUnit(current, -amount);
|
|
1368
|
+
while (amount > 0 && next < target) {
|
|
1369
|
+
amount--;
|
|
1370
|
+
next = addUnit(current, -amount);
|
|
1371
|
+
}
|
|
1372
|
+
return [amount, next];
|
|
1373
|
+
}
|
|
1374
|
+
function diff(dateA, dateB, options) {
|
|
1375
|
+
let a = date(dateA);
|
|
1376
|
+
let b = date(dateB);
|
|
1377
|
+
if (a < b) {
|
|
1378
|
+
const duration2 = diff(b, a, options);
|
|
1379
|
+
return (options == null ? void 0 : options.abs) ? duration2 : negateDuration(duration2);
|
|
1380
|
+
}
|
|
1381
|
+
const skip = new Set(options == null ? void 0 : options.skip);
|
|
1382
|
+
const duration = {};
|
|
1383
|
+
if (!skip.has("years")) {
|
|
1384
|
+
const [years, next] = calendarDiff(a, b, diffYears, addYear);
|
|
1385
|
+
a = next;
|
|
1386
|
+
if (years) duration.years = years;
|
|
1387
|
+
}
|
|
1388
|
+
if (!skip.has("months")) {
|
|
1389
|
+
const [months, next] = calendarDiff(a, b, diffMonths, addMonth);
|
|
1390
|
+
a = next;
|
|
1391
|
+
if (months) duration.months = months;
|
|
1392
|
+
}
|
|
1393
|
+
if (!skip.has("weeks")) {
|
|
1394
|
+
const weeks = diffWeeks(a, b);
|
|
1395
|
+
a = addDay(a, -(weeks * 7));
|
|
1396
|
+
if (weeks) duration.weeks = weeks;
|
|
1397
|
+
}
|
|
1398
|
+
if (!skip.has("days")) {
|
|
1399
|
+
const days = diffDays(a, b);
|
|
1400
|
+
a = addDay(a, -days);
|
|
1401
|
+
if (days) duration.days = days;
|
|
1402
|
+
}
|
|
1403
|
+
if (!skip.has("hours")) {
|
|
1404
|
+
const hours = diffHours(a, b);
|
|
1405
|
+
a = addHour(a, -hours);
|
|
1406
|
+
if (hours) duration.hours = hours;
|
|
1407
|
+
}
|
|
1408
|
+
if (!skip.has("minutes")) {
|
|
1409
|
+
const minutes = diffMinutes(a, b);
|
|
1410
|
+
a = addMinute(a, -minutes);
|
|
1411
|
+
if (minutes) duration.minutes = minutes;
|
|
1412
|
+
}
|
|
1413
|
+
if (!skip.has("seconds")) {
|
|
1414
|
+
const seconds = diffSeconds(a, b);
|
|
1415
|
+
a = addSecond(a, -seconds);
|
|
1416
|
+
if (seconds) duration.seconds = seconds;
|
|
1417
|
+
}
|
|
1418
|
+
if (!skip.has("milliseconds")) {
|
|
1419
|
+
const ms = diffMilliseconds(a, b);
|
|
1420
|
+
if (ms) duration.milliseconds = ms;
|
|
1421
|
+
}
|
|
1422
|
+
return duration;
|
|
1423
|
+
}
|
|
1256
1424
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1257
1425
|
0 && (module.exports = {
|
|
1426
|
+
add,
|
|
1258
1427
|
addDay,
|
|
1259
1428
|
addHour,
|
|
1260
1429
|
addMillisecond,
|
|
@@ -1268,6 +1437,7 @@ function diffYears(dateA, dateB) {
|
|
|
1268
1437
|
dayEnd,
|
|
1269
1438
|
dayOfYear,
|
|
1270
1439
|
dayStart,
|
|
1440
|
+
diff,
|
|
1271
1441
|
diffDays,
|
|
1272
1442
|
diffHours,
|
|
1273
1443
|
diffMilliseconds,
|
|
@@ -1305,6 +1475,13 @@ function diffYears(dateA, dateB) {
|
|
|
1305
1475
|
sameMinute,
|
|
1306
1476
|
sameSecond,
|
|
1307
1477
|
sameYear,
|
|
1478
|
+
setDayOfMonth,
|
|
1479
|
+
setHour,
|
|
1480
|
+
setMilliseconds,
|
|
1481
|
+
setMinutes,
|
|
1482
|
+
setMonth,
|
|
1483
|
+
setSeconds,
|
|
1484
|
+
setYear,
|
|
1308
1485
|
tzDate,
|
|
1309
1486
|
weekEnd,
|
|
1310
1487
|
weekStart,
|