@formkit/tempo 1.0.0 → 1.1.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.
Files changed (52) hide show
  1. package/README.md +22 -1
  2. package/dist/add.d.ts +11 -0
  3. package/dist/add.mjs +50 -0
  4. package/dist/add.mjs.map +1 -0
  5. package/dist/addMonth.mjs +2 -11
  6. package/dist/addMonth.mjs.map +1 -1
  7. package/dist/addYear.mjs +6 -11
  8. package/dist/addYear.mjs.map +1 -1
  9. package/dist/bundle.d.ts +110 -1
  10. package/dist/bundle.mjs +219 -49
  11. package/dist/bundle.mjs.map +1 -1
  12. package/dist/common.mjs +4 -2
  13. package/dist/common.mjs.map +1 -1
  14. package/dist/diff.d.ts +38 -0
  15. package/dist/diff.mjs +86 -0
  16. package/dist/diff.mjs.map +1 -0
  17. package/dist/handleDateOverflow.d.ts +12 -0
  18. package/dist/handleDateOverflow.mjs +18 -0
  19. package/dist/handleDateOverflow.mjs.map +1 -0
  20. package/dist/index.cjs +228 -49
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +110 -1
  23. package/dist/index.d.ts +10 -1
  24. package/dist/index.mjs +18 -0
  25. package/dist/index.mjs.map +1 -1
  26. package/dist/offset.mjs +14 -12
  27. package/dist/offset.mjs.map +1 -1
  28. package/dist/setDayOfMonth.d.ts +11 -0
  29. package/dist/setDayOfMonth.mjs +16 -0
  30. package/dist/setDayOfMonth.mjs.map +1 -0
  31. package/dist/setHour.d.ts +10 -0
  32. package/dist/setHour.mjs +11 -0
  33. package/dist/setHour.mjs.map +1 -0
  34. package/dist/setMilliseconds.d.ts +10 -0
  35. package/dist/setMilliseconds.mjs +11 -0
  36. package/dist/setMilliseconds.mjs.map +1 -0
  37. package/dist/setMinutes.d.ts +10 -0
  38. package/dist/setMinutes.mjs +11 -0
  39. package/dist/setMinutes.mjs.map +1 -0
  40. package/dist/setMonth.d.ts +11 -0
  41. package/dist/setMonth.mjs +9 -0
  42. package/dist/setMonth.mjs.map +1 -0
  43. package/dist/setSeconds.d.ts +10 -0
  44. package/dist/setSeconds.mjs +11 -0
  45. package/dist/setSeconds.mjs.map +1 -0
  46. package/dist/setYear.d.ts +11 -0
  47. package/dist/setYear.mjs +9 -0
  48. package/dist/setYear.mjs.map +1 -0
  49. package/dist/types.d.ts +15 -1
  50. package/dist/yearDays.mjs +1 -1
  51. package/dist/yearDays.mjs.map +1 -1
  52. package/package.json +3 -3
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/addMonth.ts
147
- function addMonth(inputDate, count = 1, dateOverflow = false) {
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
- d.setFullYear(d.getFullYear() + count);
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/addHour.ts
173
- function addHour(inputDate, count = 1) {
174
- const d = date(inputDate);
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/addMillisecond.ts
194
- function addMillisecond(inputDate, count = 1) {
195
- const d = date(inputDate);
196
- d.setMilliseconds(d.getMilliseconds() + count);
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
 
@@ -266,7 +315,7 @@ var tokens = /* @__PURE__ */ new Map(
266
315
  var dayPeriodMap = /* @__PURE__ */ new Map();
267
316
  var styles = ["full", "long", "medium", "short"];
268
317
  var two = (n) => String(n).padStart(2, "0");
269
- var four = (n) => String(n).padStart(2, "0");
318
+ var four = (n) => String(n).padStart(4, "0");
270
319
  function normStr(part) {
271
320
  if (part.type === "literal") {
272
321
  part.value = part.value.normalize("NFKC");
@@ -311,7 +360,9 @@ function createPartMap(inputDate, parts2, locale, genitive = false) {
311
360
  const valueParts = [];
312
361
  const genitiveParts = [];
313
362
  function addValues(requestedParts, hour122 = false) {
314
- const preciseLocale = `${locale}-u-hc-${hour122 ? "h12" : "h23"}`;
363
+ const preciseLocale = new Intl.Locale(locale, {
364
+ hourCycle: hour122 ? "h12" : "h23"
365
+ }).toString();
315
366
  valueParts.push(
316
367
  ...new Intl.DateTimeFormat(
317
368
  preciseLocale,
@@ -462,22 +513,24 @@ function deviceTZ() {
462
513
  // src/offset.ts
463
514
  function relativeTime(d, timeZone) {
464
515
  const utcParts = new Intl.DateTimeFormat("en-US", {
516
+ era: "short",
465
517
  year: "numeric",
466
- month: "2-digit",
467
- day: "2-digit",
468
- hour: "2-digit",
469
- minute: "2-digit",
470
- second: "2-digit",
518
+ month: "numeric",
519
+ day: "numeric",
520
+ hour: "numeric",
521
+ minute: "numeric",
522
+ second: "numeric",
471
523
  timeZone,
472
524
  hourCycle: "h23"
473
- }).formatToParts(d).map(normStr);
474
- const parts2 = {};
525
+ }).formatToParts(d);
526
+ const p = {};
475
527
  utcParts.forEach((part) => {
476
- parts2[part.type] = part.value;
528
+ if (part.type !== "literal") p[part.type] = part.value;
477
529
  });
478
- return /* @__PURE__ */ new Date(
479
- `${parts2.year}-${parts2.month}-${parts2.day}T${parts2.hour}:${parts2.minute}:${parts2.second}Z`
480
- );
530
+ const year = p.era === "BC" ? 1 - Number(p.year) : Number(p.year);
531
+ const result = new Date(Date.UTC(0, 0, 1, Number(p.hour), Number(p.minute), Number(p.second)));
532
+ result.setUTCFullYear(year, Number(p.month) - 1, Number(p.day));
533
+ return result;
481
534
  }
482
535
  function offset(utcTime, tzA = "UTC", tzB = "device", timeZoneToken = "Z") {
483
536
  var _a;
@@ -814,7 +867,7 @@ function monthStart(inputDate) {
814
867
  // src/yearDays.ts
815
868
  function yearDays(inputDate) {
816
869
  const d = date(inputDate);
817
- return (new Date(d.getFullYear() + 1, 0, 0).getTime() - new Date(d.getFullYear(), 0, 0).getTime()) / 864e5;
870
+ return (Date.UTC(d.getFullYear() + 1, 0, 0) - Date.UTC(d.getFullYear(), 0, 0)) / 864e5;
818
871
  }
819
872
 
820
873
  // src/nearestDay.ts
@@ -1100,12 +1153,61 @@ function sameYear(inputDateA, inputDateB) {
1100
1153
  return a.getFullYear() === b.getFullYear();
1101
1154
  }
1102
1155
 
1156
+ // src/setMilliseconds.ts
1157
+ function setMilliseconds(inputDate, ms) {
1158
+ const d = date(inputDate);
1159
+ d.setMilliseconds(ms);
1160
+ return d;
1161
+ }
1162
+
1163
+ // src/setSeconds.ts
1164
+ function setSeconds(inputDate, second) {
1165
+ const d = date(inputDate);
1166
+ d.setSeconds(second);
1167
+ return d;
1168
+ }
1169
+
1170
+ // src/setMinutes.ts
1171
+ function setMinutes(inputDate, minute) {
1172
+ const d = date(inputDate);
1173
+ d.setMinutes(minute);
1174
+ return d;
1175
+ }
1176
+
1177
+ // src/setHour.ts
1178
+ function setHour(inputDate, hour) {
1179
+ const d = date(inputDate);
1180
+ d.setHours(hour);
1181
+ return d;
1182
+ }
1183
+
1184
+ // src/setDayOfMonth.ts
1185
+ function setDayOfMonth(inputDate, day, dateOverflow = false) {
1186
+ const d = date(inputDate);
1187
+ const daysInMonth = monthDays(d);
1188
+ if (!dateOverflow) {
1189
+ day = day > daysInMonth ? daysInMonth : day;
1190
+ }
1191
+ d.setDate(day);
1192
+ return d;
1193
+ }
1194
+
1195
+ // src/setMonth.ts
1196
+ function setMonth(inputDate, month, dateOverflow = false) {
1197
+ return handleOverflow(inputDate, (d) => d.setMonth(month), dateOverflow);
1198
+ }
1199
+
1200
+ // src/setYear.ts
1201
+ function setYear(inputDate, year, dateOverflow = false) {
1202
+ return handleOverflow(inputDate, (d) => d.setFullYear(year), dateOverflow);
1203
+ }
1204
+
1103
1205
  // src/weekStart.ts
1104
1206
  function weekStart(inputDate, startOfWeekDay = 0) {
1105
1207
  const d = date(inputDate);
1106
- let diff = startOfWeekDay - d.getDay();
1107
- if (diff > 0) diff = diff - 7;
1108
- d.setDate(d.getDate() + diff);
1208
+ let diff2 = startOfWeekDay - d.getDay();
1209
+ if (diff2 > 0) diff2 = diff2 - 7;
1210
+ d.setDate(d.getDate() + diff2);
1109
1211
  d.setHours(0, 0, 0, 0);
1110
1212
  return d;
1111
1213
  }
@@ -1253,8 +1355,77 @@ function diffYears(dateA, dateB) {
1253
1355
  );
1254
1356
  return r == 0 ? 0 : r;
1255
1357
  }
1358
+
1359
+ // src/diff.ts
1360
+ function negateDuration(duration) {
1361
+ const negated = {};
1362
+ for (const unit of Object.keys(duration)) {
1363
+ negated[unit] = -duration[unit];
1364
+ }
1365
+ return negated;
1366
+ }
1367
+ function calendarDiff(current, target, diffUnit, addUnit) {
1368
+ let amount = diffUnit(current, target);
1369
+ let next = addUnit(current, -amount);
1370
+ while (amount > 0 && next < target) {
1371
+ amount--;
1372
+ next = addUnit(current, -amount);
1373
+ }
1374
+ return [amount, next];
1375
+ }
1376
+ function diff(dateA, dateB, options) {
1377
+ let a = date(dateA);
1378
+ let b = date(dateB);
1379
+ if (a < b) {
1380
+ const duration2 = diff(b, a, options);
1381
+ return (options == null ? void 0 : options.abs) ? duration2 : negateDuration(duration2);
1382
+ }
1383
+ const skip = new Set(options == null ? void 0 : options.skip);
1384
+ const duration = {};
1385
+ if (!skip.has("years")) {
1386
+ const [years, next] = calendarDiff(a, b, diffYears, addYear);
1387
+ a = next;
1388
+ if (years) duration.years = years;
1389
+ }
1390
+ if (!skip.has("months")) {
1391
+ const [months, next] = calendarDiff(a, b, diffMonths, addMonth);
1392
+ a = next;
1393
+ if (months) duration.months = months;
1394
+ }
1395
+ if (!skip.has("weeks")) {
1396
+ const weeks = diffWeeks(a, b);
1397
+ a = addDay(a, -(weeks * 7));
1398
+ if (weeks) duration.weeks = weeks;
1399
+ }
1400
+ if (!skip.has("days")) {
1401
+ const days = diffDays(a, b);
1402
+ a = addDay(a, -days);
1403
+ if (days) duration.days = days;
1404
+ }
1405
+ if (!skip.has("hours")) {
1406
+ const hours = diffHours(a, b);
1407
+ a = addHour(a, -hours);
1408
+ if (hours) duration.hours = hours;
1409
+ }
1410
+ if (!skip.has("minutes")) {
1411
+ const minutes = diffMinutes(a, b);
1412
+ a = addMinute(a, -minutes);
1413
+ if (minutes) duration.minutes = minutes;
1414
+ }
1415
+ if (!skip.has("seconds")) {
1416
+ const seconds = diffSeconds(a, b);
1417
+ a = addSecond(a, -seconds);
1418
+ if (seconds) duration.seconds = seconds;
1419
+ }
1420
+ if (!skip.has("milliseconds")) {
1421
+ const ms = diffMilliseconds(a, b);
1422
+ if (ms) duration.milliseconds = ms;
1423
+ }
1424
+ return duration;
1425
+ }
1256
1426
  // Annotate the CommonJS export names for ESM import in node:
1257
1427
  0 && (module.exports = {
1428
+ add,
1258
1429
  addDay,
1259
1430
  addHour,
1260
1431
  addMillisecond,
@@ -1268,6 +1439,7 @@ function diffYears(dateA, dateB) {
1268
1439
  dayEnd,
1269
1440
  dayOfYear,
1270
1441
  dayStart,
1442
+ diff,
1271
1443
  diffDays,
1272
1444
  diffHours,
1273
1445
  diffMilliseconds,
@@ -1305,6 +1477,13 @@ function diffYears(dateA, dateB) {
1305
1477
  sameMinute,
1306
1478
  sameSecond,
1307
1479
  sameYear,
1480
+ setDayOfMonth,
1481
+ setHour,
1482
+ setMilliseconds,
1483
+ setMinutes,
1484
+ setMonth,
1485
+ setSeconds,
1486
+ setYear,
1308
1487
  tzDate,
1309
1488
  weekEnd,
1310
1489
  weekStart,