@formkit/tempo 0.0.2 → 0.0.3

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/dist/index.d.cts CHANGED
@@ -113,6 +113,32 @@ interface ParseOptions {
113
113
  */
114
114
  dateOverflow?: "forward" | "backward" | "throw";
115
115
  }
116
+ interface FormatOptions {
117
+ /**
118
+ * A date object or ISO 8601 string.
119
+ */
120
+ date: DateInput;
121
+ /**
122
+ * A format string or object.
123
+ */
124
+ format: Format;
125
+ /**
126
+ * A locale or en by default.
127
+ */
128
+ locale?: "en";
129
+ /**
130
+ * Whether or not to escape literals.
131
+ */
132
+ genitive?: false;
133
+ /**
134
+ * A function to filter parts.
135
+ */
136
+ tz?: string;
137
+ /**
138
+ * A function to filter parts.
139
+ */
140
+ partFilter?: (part: Part) => boolean;
141
+ }
116
142
 
117
143
  /**
118
144
  * Returns a new date object 1/n days after the original one.
@@ -200,33 +226,35 @@ declare function dayStart(inputDate: DateInput): Date;
200
226
 
201
227
  /**
202
228
  * Produce a formatted string. Available strings:
203
- *
204
- * YY - 2 digit year
205
- * YYYY - 4 digit year
206
- * M - The month 1-12
207
- * MM - The month 01-12
208
- * MMM - Short name Jan-Dec
209
- * MMMM - Full name January - December
210
- * D - The day of the month 1-31
211
- * DD - The day of the month 01-31
212
- * d - Single digit day "T"
213
- * ddd - Short day name Thu
214
- * dddd - Full day name Wednesday
215
- * H - Minimum hour digits, 24 hour, 0-23
216
- * HH - 2 hour digits, 24 hour, 00-23
217
- * h - Minimum hour digits, 12 hour clock, 1-12
218
- * hh - 2 hour digits, 12 hour clock, 01-12
219
- * m - The minute 0-59
220
- * mm - The minute 00-59
221
- * s - The second 0-59
222
- * ss - The second 00-59
223
- * a - am/pm
224
- * A - AM/PM
225
- * Z - +0800, +0530, -1345
229
+ * token | description
230
+ * ------|------------
231
+ * YY | 2 digit year
232
+ * YYYY | 4 digit year
233
+ * M | The month 1-12
234
+ * MM | The month 01-12
235
+ * MMM | Short name Jan-Dec
236
+ * MMMM | Full name January | December
237
+ * D | The day of the month 1-31
238
+ * DD | The day of the month 01-31
239
+ * d | Single digit day "T"
240
+ * ddd | Short day name Thu
241
+ * dddd | Full day name Wednesday
242
+ * H | Minimum hour digits, 24 hour, 0-23
243
+ * HH | 2 hour digits, 24 hour, 00-23
244
+ * h | Minimum hour digits, 12 hour clock, 1-12
245
+ * hh | 2 hour digits, 12 hour clock, 01-12
246
+ * m | The minute 0-59
247
+ * mm | The minute 00-59
248
+ * s | The second 0-59
249
+ * ss | The second 00-59
250
+ * a | am/pm
251
+ * A | AM/PM
252
+ * Z | +0800, +0530, -1345
226
253
  *
227
254
  * @param inputDate - A date object or ISO 8601 string
228
255
  * @param format - A format
229
256
  */
257
+ declare function format(options: FormatOptions): string;
230
258
  declare function format(inputDate: DateInput, format?: Format, locale?: string, genitive?: boolean, partFilter?: (part: Part) => boolean): string;
231
259
 
232
260
  /**
@@ -287,8 +315,8 @@ declare function nearestDay(inputDate: DateInput, search: (date: Date) => boolea
287
315
  * ISO8601 compatible offsets like -0800 or +0530.
288
316
  *
289
317
  * @param dateInput - The date on which to determine the offset.
290
- * @param tzA - The second timezone to compare determine the offset between.
291
- * @param tzB - The first timezone to compare determine the offset between.
318
+ * @param tzA - (default: UTC) The second timezone to compare determine the offset between.
319
+ * @param tzB - (default: device) The first timezone to compare determine the offset between.
292
320
  */
293
321
  declare function offset(utcTime: DateInput, tzA?: string, tzB?: string): string;
294
322
 
@@ -349,4 +377,4 @@ declare function weekStart(inputDate: DateInput, startOfWeekDay?: number): Date;
349
377
  */
350
378
  declare function yearDays(inputDate: DateInput): number;
351
379
 
352
- export { type DateInput, type FilledPart, type Format, type FormatPattern, type FormatStyle, type FormatStyleObj, type FormatToken, type NamedFormatOption, type NamedFormats, type ParseOptions, type Part, addDay, addHour, addMinute, addMonth, addSecond, addYear, ap, applyOffset, date, dayEnd, dayOfYear, dayStart, format, formatStr, fourDigitYear, iso8601, monthDays, monthEnd, monthStart, nearestDay, offset, parse, parts, range, removeOffset, sameDay, weekEnd, weekStart, yearDays };
380
+ export { type DateInput, type FilledPart, type Format, type FormatOptions, type FormatPattern, type FormatStyle, type FormatStyleObj, type FormatToken, type NamedFormatOption, type NamedFormats, type ParseOptions, type Part, addDay, addHour, addMinute, addMonth, addSecond, addYear, ap, applyOffset, date, dayEnd, dayOfYear, dayStart, format, formatStr, fourDigitYear, iso8601, monthDays, monthEnd, monthStart, nearestDay, offset, parse, parts, range, removeOffset, sameDay, weekEnd, weekStart, yearDays };
package/dist/index.d.ts CHANGED
@@ -113,6 +113,32 @@ interface ParseOptions {
113
113
  */
114
114
  dateOverflow?: "forward" | "backward" | "throw";
115
115
  }
116
+ interface FormatOptions {
117
+ /**
118
+ * A date object or ISO 8601 string.
119
+ */
120
+ date: DateInput;
121
+ /**
122
+ * A format string or object.
123
+ */
124
+ format: Format;
125
+ /**
126
+ * A locale or en by default.
127
+ */
128
+ locale?: "en";
129
+ /**
130
+ * Whether or not to escape literals.
131
+ */
132
+ genitive?: false;
133
+ /**
134
+ * A function to filter parts.
135
+ */
136
+ tz?: string;
137
+ /**
138
+ * A function to filter parts.
139
+ */
140
+ partFilter?: (part: Part) => boolean;
141
+ }
116
142
 
117
143
  /**
118
144
  * Returns a new date object 1/n days after the original one.
@@ -200,33 +226,35 @@ declare function dayStart(inputDate: DateInput): Date;
200
226
 
201
227
  /**
202
228
  * Produce a formatted string. Available strings:
203
- *
204
- * YY - 2 digit year
205
- * YYYY - 4 digit year
206
- * M - The month 1-12
207
- * MM - The month 01-12
208
- * MMM - Short name Jan-Dec
209
- * MMMM - Full name January - December
210
- * D - The day of the month 1-31
211
- * DD - The day of the month 01-31
212
- * d - Single digit day "T"
213
- * ddd - Short day name Thu
214
- * dddd - Full day name Wednesday
215
- * H - Minimum hour digits, 24 hour, 0-23
216
- * HH - 2 hour digits, 24 hour, 00-23
217
- * h - Minimum hour digits, 12 hour clock, 1-12
218
- * hh - 2 hour digits, 12 hour clock, 01-12
219
- * m - The minute 0-59
220
- * mm - The minute 00-59
221
- * s - The second 0-59
222
- * ss - The second 00-59
223
- * a - am/pm
224
- * A - AM/PM
225
- * Z - +0800, +0530, -1345
229
+ * token | description
230
+ * ------|------------
231
+ * YY | 2 digit year
232
+ * YYYY | 4 digit year
233
+ * M | The month 1-12
234
+ * MM | The month 01-12
235
+ * MMM | Short name Jan-Dec
236
+ * MMMM | Full name January | December
237
+ * D | The day of the month 1-31
238
+ * DD | The day of the month 01-31
239
+ * d | Single digit day "T"
240
+ * ddd | Short day name Thu
241
+ * dddd | Full day name Wednesday
242
+ * H | Minimum hour digits, 24 hour, 0-23
243
+ * HH | 2 hour digits, 24 hour, 00-23
244
+ * h | Minimum hour digits, 12 hour clock, 1-12
245
+ * hh | 2 hour digits, 12 hour clock, 01-12
246
+ * m | The minute 0-59
247
+ * mm | The minute 00-59
248
+ * s | The second 0-59
249
+ * ss | The second 00-59
250
+ * a | am/pm
251
+ * A | AM/PM
252
+ * Z | +0800, +0530, -1345
226
253
  *
227
254
  * @param inputDate - A date object or ISO 8601 string
228
255
  * @param format - A format
229
256
  */
257
+ declare function format(options: FormatOptions): string;
230
258
  declare function format(inputDate: DateInput, format?: Format, locale?: string, genitive?: boolean, partFilter?: (part: Part) => boolean): string;
231
259
 
232
260
  /**
@@ -287,8 +315,8 @@ declare function nearestDay(inputDate: DateInput, search: (date: Date) => boolea
287
315
  * ISO8601 compatible offsets like -0800 or +0530.
288
316
  *
289
317
  * @param dateInput - The date on which to determine the offset.
290
- * @param tzA - The second timezone to compare determine the offset between.
291
- * @param tzB - The first timezone to compare determine the offset between.
318
+ * @param tzA - (default: UTC) The second timezone to compare determine the offset between.
319
+ * @param tzB - (default: device) The first timezone to compare determine the offset between.
292
320
  */
293
321
  declare function offset(utcTime: DateInput, tzA?: string, tzB?: string): string;
294
322
 
@@ -349,4 +377,4 @@ declare function weekStart(inputDate: DateInput, startOfWeekDay?: number): Date;
349
377
  */
350
378
  declare function yearDays(inputDate: DateInput): number;
351
379
 
352
- export { type DateInput, type FilledPart, type Format, type FormatPattern, type FormatStyle, type FormatStyleObj, type FormatToken, type NamedFormatOption, type NamedFormats, type ParseOptions, type Part, addDay, addHour, addMinute, addMonth, addSecond, addYear, ap, applyOffset, date, dayEnd, dayOfYear, dayStart, format, formatStr, fourDigitYear, iso8601, monthDays, monthEnd, monthStart, nearestDay, offset, parse, parts, range, removeOffset, sameDay, weekEnd, weekStart, yearDays };
380
+ export { type DateInput, type FilledPart, type Format, type FormatOptions, type FormatPattern, type FormatStyle, type FormatStyleObj, type FormatToken, type NamedFormatOption, type NamedFormats, type ParseOptions, type Part, addDay, addHour, addMinute, addMonth, addSecond, addYear, ap, applyOffset, date, dayEnd, dayOfYear, dayStart, format, formatStr, fourDigitYear, iso8601, monthDays, monthEnd, monthStart, nearestDay, offset, parse, parts, range, removeOffset, sameDay, weekEnd, weekStart, yearDays };
package/dist/index.mjs CHANGED
@@ -563,13 +563,82 @@ function partStyle(locale, part, value) {
563
563
  return formats ? formats[part][value] : void 0;
564
564
  }
565
565
 
566
+ // src/deviceTZ.ts
567
+ function deviceTZ() {
568
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
569
+ }
570
+
571
+ // src/offset.ts
572
+ function relativeTime(d, timeZone) {
573
+ const utcParts = new Intl.DateTimeFormat("en-US", {
574
+ year: "numeric",
575
+ month: "2-digit",
576
+ day: "2-digit",
577
+ hour: "2-digit",
578
+ minute: "2-digit",
579
+ second: "2-digit",
580
+ timeZone,
581
+ hourCycle: "h23"
582
+ }).formatToParts(d).map(normStr);
583
+ const parts2 = {};
584
+ utcParts.forEach((part) => {
585
+ parts2[part.type] = part.value;
586
+ });
587
+ return /* @__PURE__ */ new Date(
588
+ `${parts2.year}-${parts2.month}-${parts2.day}T${parts2.hour}:${parts2.minute}:${parts2.second}Z`
589
+ );
590
+ }
591
+ function offset(utcTime, tzA = "UTC", tzB = "device") {
592
+ tzB = tzB === "device" ? deviceTZ() : tzB;
593
+ const d = date(utcTime);
594
+ const timeA = relativeTime(d, tzA);
595
+ const timeB = relativeTime(d, tzB);
596
+ const timeDiffInMins = (timeB.getTime() - timeA.getTime()) / 1e3 / 60;
597
+ return minsToOffset(timeDiffInMins);
598
+ }
599
+
600
+ // src/removeOffset.ts
601
+ function removeOffset(dateInput, offset2 = "+0000") {
602
+ const positive = offset2.slice(0, 1) === "+";
603
+ return applyOffset(
604
+ dateInput,
605
+ offset2.replace(positive ? "+" : "-", positive ? "-" : "+")
606
+ );
607
+ }
608
+
609
+ // src/deviceLocale.ts
610
+ function deviceLocale() {
611
+ return Intl.DateTimeFormat().resolvedOptions().locale;
612
+ }
613
+
566
614
  // src/format.ts
567
- function format(inputDate, format2 = "long", locale = "en", genitive = false, partFilter = () => true) {
615
+ function format(inputDateOrOptions, format2 = "long", locale = "device", genitive = false, partFilter) {
616
+ let tz;
617
+ if (typeof inputDateOrOptions === "object" && !(inputDateOrOptions instanceof Date)) {
618
+ ;
619
+ ({
620
+ date: inputDateOrOptions,
621
+ format: format2,
622
+ locale,
623
+ genitive,
624
+ partFilter,
625
+ tz
626
+ } = inputDateOrOptions);
627
+ }
568
628
  if (format2 === "ISO8601")
569
- return date(inputDate).toISOString();
629
+ return date(inputDateOrOptions).toISOString();
630
+ if (tz) {
631
+ inputDateOrOptions = removeOffset(
632
+ inputDateOrOptions,
633
+ offset(inputDateOrOptions, tz)
634
+ );
635
+ }
636
+ if (!locale || locale === "device") {
637
+ locale = deviceLocale();
638
+ }
570
639
  return fill(
571
- inputDate,
572
- parts(format2, locale).filter(partFilter),
640
+ inputDateOrOptions,
641
+ parts(format2, locale).filter(partFilter != null ? partFilter : () => true),
573
642
  locale,
574
643
  genitive
575
644
  ).map((p) => p.value).join("");
@@ -644,35 +713,6 @@ function nearestDay(inputDate, search, constraint = 7) {
644
713
  return null;
645
714
  }
646
715
 
647
- // src/offset.ts
648
- function offset(utcTime, tzA = "UTC", tzB = "browser") {
649
- tzB = tzB === "browser" ? Intl.DateTimeFormat().resolvedOptions().timeZone : tzB;
650
- const d = date(utcTime);
651
- const relativeTime = (timeZone) => {
652
- const utcParts = new Intl.DateTimeFormat("en-US", {
653
- year: "numeric",
654
- month: "2-digit",
655
- day: "2-digit",
656
- hour: "2-digit",
657
- minute: "2-digit",
658
- second: "2-digit",
659
- timeZone,
660
- hourCycle: "h23"
661
- }).formatToParts(d).map(normStr);
662
- const parts2 = {};
663
- utcParts.forEach((part) => {
664
- parts2[part.type] = part.value;
665
- });
666
- return /* @__PURE__ */ new Date(
667
- `${parts2.year}-${parts2.month}-${parts2.day}T${parts2.hour}:${parts2.minute}:${parts2.second}Z`
668
- );
669
- };
670
- const timeA = relativeTime(tzA);
671
- const timeB = relativeTime(tzB);
672
- const timeDiffInMins = (timeB.getTime() - timeA.getTime()) / 1e3 / 60;
673
- return minsToOffset(timeDiffInMins);
674
- }
675
-
676
716
  // src/range.ts
677
717
  function range(token, locale = "en", genitive = false) {
678
718
  const r = (n, c) => Array(n).fill("").map((_, i) => `${c(i)}`);
@@ -867,15 +907,6 @@ function parseParts(dateStr, formatParts) {
867
907
  return parsed;
868
908
  }
869
909
 
870
- // src/removeOffset.ts
871
- function removeOffset(dateInput, offset2 = "+0000") {
872
- const positive = offset2.slice(0, 1) === "+";
873
- return applyOffset(
874
- dateInput,
875
- offset2.replace(positive ? "+" : "-", positive ? "-" : "+")
876
- );
877
- }
878
-
879
910
  // src/sameDay.ts
880
911
  function sameDay(inputDateA, inputDateB) {
881
912
  const a = date(inputDateA);