@formkit/tempo 0.0.2 → 0.0.4

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?: string;
129
+ /**
130
+ * Whether or not to escape literals.
131
+ */
132
+ genitive?: boolean;
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.
@@ -179,6 +205,18 @@ declare function applyOffset(dateInput: DateInput, offset?: string): Date;
179
205
  */
180
206
  declare function date(date?: DateInput): Date;
181
207
 
208
+ /**
209
+ * Creates a date object for the input date at the given timezone. For example
210
+ * `tzDate("2017-05-06T12:00", "Europe/Amsterdam")` will return a date object
211
+ * for 2017-05-06T10:00:00Z since 12:00 in Amsterdam is 10:00Z.
212
+ *
213
+ * If given a Date object it will use local time and convert it to the given
214
+ * timezone, thus "changing" the date.
215
+ * @param inputDate - An iso8601 date string with no timezone
216
+ * @param tz - A timezone string
217
+ */
218
+ declare function tzDate(inputDate: DateInput, tz: string): Date;
219
+
182
220
  /**
183
221
  * Gets the what day of the year a given date is. For example, August 1st is
184
222
  * the 213th day of the year on non-leapyears and 214th on leapyears.
@@ -200,33 +238,35 @@ declare function dayStart(inputDate: DateInput): Date;
200
238
 
201
239
  /**
202
240
  * 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
241
+ * token | description
242
+ * ------|------------
243
+ * YY | 2 digit year
244
+ * YYYY | 4 digit year
245
+ * M | The month 1-12
246
+ * MM | The month 01-12
247
+ * MMM | Short name Jan-Dec
248
+ * MMMM | Full name January | December
249
+ * D | The day of the month 1-31
250
+ * DD | The day of the month 01-31
251
+ * d | Single digit day "T"
252
+ * ddd | Short day name Thu
253
+ * dddd | Full day name Wednesday
254
+ * H | Minimum hour digits, 24 hour, 0-23
255
+ * HH | 2 hour digits, 24 hour, 00-23
256
+ * h | Minimum hour digits, 12 hour clock, 1-12
257
+ * hh | 2 hour digits, 12 hour clock, 01-12
258
+ * m | The minute 0-59
259
+ * mm | The minute 00-59
260
+ * s | The second 0-59
261
+ * ss | The second 00-59
262
+ * a | am/pm
263
+ * A | AM/PM
264
+ * Z | +0800, +0530, -1345
226
265
  *
227
266
  * @param inputDate - A date object or ISO 8601 string
228
267
  * @param format - A format
229
268
  */
269
+ declare function format(options: FormatOptions): string;
230
270
  declare function format(inputDate: DateInput, format?: Format, locale?: string, genitive?: boolean, partFilter?: (part: Part) => boolean): string;
231
271
 
232
272
  /**
@@ -287,8 +327,8 @@ declare function nearestDay(inputDate: DateInput, search: (date: Date) => boolea
287
327
  * ISO8601 compatible offsets like -0800 or +0530.
288
328
  *
289
329
  * @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.
330
+ * @param tzA - (default: UTC) The second timezone to compare determine the offset between.
331
+ * @param tzB - (default: device) The first timezone to compare determine the offset between.
292
332
  */
293
333
  declare function offset(utcTime: DateInput, tzA?: string, tzB?: string): string;
294
334
 
@@ -349,4 +389,4 @@ declare function weekStart(inputDate: DateInput, startOfWeekDay?: number): Date;
349
389
  */
350
390
  declare function yearDays(inputDate: DateInput): number;
351
391
 
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 };
392
+ 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, tzDate, 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?: string;
129
+ /**
130
+ * Whether or not to escape literals.
131
+ */
132
+ genitive?: boolean;
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.
@@ -179,6 +205,18 @@ declare function applyOffset(dateInput: DateInput, offset?: string): Date;
179
205
  */
180
206
  declare function date(date?: DateInput): Date;
181
207
 
208
+ /**
209
+ * Creates a date object for the input date at the given timezone. For example
210
+ * `tzDate("2017-05-06T12:00", "Europe/Amsterdam")` will return a date object
211
+ * for 2017-05-06T10:00:00Z since 12:00 in Amsterdam is 10:00Z.
212
+ *
213
+ * If given a Date object it will use local time and convert it to the given
214
+ * timezone, thus "changing" the date.
215
+ * @param inputDate - An iso8601 date string with no timezone
216
+ * @param tz - A timezone string
217
+ */
218
+ declare function tzDate(inputDate: DateInput, tz: string): Date;
219
+
182
220
  /**
183
221
  * Gets the what day of the year a given date is. For example, August 1st is
184
222
  * the 213th day of the year on non-leapyears and 214th on leapyears.
@@ -200,33 +238,35 @@ declare function dayStart(inputDate: DateInput): Date;
200
238
 
201
239
  /**
202
240
  * 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
241
+ * token | description
242
+ * ------|------------
243
+ * YY | 2 digit year
244
+ * YYYY | 4 digit year
245
+ * M | The month 1-12
246
+ * MM | The month 01-12
247
+ * MMM | Short name Jan-Dec
248
+ * MMMM | Full name January | December
249
+ * D | The day of the month 1-31
250
+ * DD | The day of the month 01-31
251
+ * d | Single digit day "T"
252
+ * ddd | Short day name Thu
253
+ * dddd | Full day name Wednesday
254
+ * H | Minimum hour digits, 24 hour, 0-23
255
+ * HH | 2 hour digits, 24 hour, 00-23
256
+ * h | Minimum hour digits, 12 hour clock, 1-12
257
+ * hh | 2 hour digits, 12 hour clock, 01-12
258
+ * m | The minute 0-59
259
+ * mm | The minute 00-59
260
+ * s | The second 0-59
261
+ * ss | The second 00-59
262
+ * a | am/pm
263
+ * A | AM/PM
264
+ * Z | +0800, +0530, -1345
226
265
  *
227
266
  * @param inputDate - A date object or ISO 8601 string
228
267
  * @param format - A format
229
268
  */
269
+ declare function format(options: FormatOptions): string;
230
270
  declare function format(inputDate: DateInput, format?: Format, locale?: string, genitive?: boolean, partFilter?: (part: Part) => boolean): string;
231
271
 
232
272
  /**
@@ -287,8 +327,8 @@ declare function nearestDay(inputDate: DateInput, search: (date: Date) => boolea
287
327
  * ISO8601 compatible offsets like -0800 or +0530.
288
328
  *
289
329
  * @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.
330
+ * @param tzA - (default: UTC) The second timezone to compare determine the offset between.
331
+ * @param tzB - (default: device) The first timezone to compare determine the offset between.
292
332
  */
293
333
  declare function offset(utcTime: DateInput, tzA?: string, tzB?: string): string;
294
334
 
@@ -349,4 +389,4 @@ declare function weekStart(inputDate: DateInput, startOfWeekDay?: number): Date;
349
389
  */
350
390
  declare function yearDays(inputDate: DateInput): number;
351
391
 
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 };
392
+ 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, tzDate, weekEnd, weekStart, yearDays };
package/dist/index.mjs CHANGED
@@ -341,6 +341,46 @@ function applyOffset(dateInput, offset2 = "+0000") {
341
341
  return new Date(d.getTime() + timeDiffInMins * 1e3 * 60);
342
342
  }
343
343
 
344
+ // src/deviceTZ.ts
345
+ function deviceTZ() {
346
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
347
+ }
348
+
349
+ // src/offset.ts
350
+ function relativeTime(d, timeZone) {
351
+ const utcParts = new Intl.DateTimeFormat("en-US", {
352
+ year: "numeric",
353
+ month: "2-digit",
354
+ day: "2-digit",
355
+ hour: "2-digit",
356
+ minute: "2-digit",
357
+ second: "2-digit",
358
+ timeZone,
359
+ hourCycle: "h23"
360
+ }).formatToParts(d).map(normStr);
361
+ const parts2 = {};
362
+ utcParts.forEach((part) => {
363
+ parts2[part.type] = part.value;
364
+ });
365
+ return /* @__PURE__ */ new Date(
366
+ `${parts2.year}-${parts2.month}-${parts2.day}T${parts2.hour}:${parts2.minute}:${parts2.second}Z`
367
+ );
368
+ }
369
+ function offset(utcTime, tzA = "UTC", tzB = "device") {
370
+ tzB = tzB === "device" ? deviceTZ() : tzB;
371
+ const d = date(utcTime);
372
+ const timeA = relativeTime(d, tzA);
373
+ const timeB = relativeTime(d, tzB);
374
+ const timeDiffInMins = (timeB.getTime() - timeA.getTime()) / 1e3 / 60;
375
+ return minsToOffset(timeDiffInMins);
376
+ }
377
+
378
+ // src/tzDate.ts
379
+ function tzDate(inputDate, tz) {
380
+ const d = date(inputDate);
381
+ return applyOffset(d, offset(d, tz));
382
+ }
383
+
344
384
  // src/dayOfYear.ts
345
385
  function dayOfYear(inputDate) {
346
386
  const d = date(inputDate);
@@ -563,13 +603,48 @@ function partStyle(locale, part, value) {
563
603
  return formats ? formats[part][value] : void 0;
564
604
  }
565
605
 
606
+ // src/removeOffset.ts
607
+ function removeOffset(dateInput, offset2 = "+0000") {
608
+ const positive = offset2.slice(0, 1) === "+";
609
+ return applyOffset(
610
+ dateInput,
611
+ offset2.replace(positive ? "+" : "-", positive ? "-" : "+")
612
+ );
613
+ }
614
+
615
+ // src/deviceLocale.ts
616
+ function deviceLocale() {
617
+ return Intl.DateTimeFormat().resolvedOptions().locale;
618
+ }
619
+
566
620
  // src/format.ts
567
- function format(inputDate, format2 = "long", locale = "en", genitive = false, partFilter = () => true) {
621
+ function format(inputDateOrOptions, format2 = "long", locale = "device", genitive = false, partFilter) {
622
+ let tz;
623
+ if (typeof inputDateOrOptions === "object" && !(inputDateOrOptions instanceof Date)) {
624
+ ;
625
+ ({
626
+ date: inputDateOrOptions,
627
+ format: format2,
628
+ locale,
629
+ genitive,
630
+ partFilter,
631
+ tz
632
+ } = inputDateOrOptions);
633
+ }
568
634
  if (format2 === "ISO8601")
569
- return date(inputDate).toISOString();
635
+ return date(inputDateOrOptions).toISOString();
636
+ if (tz) {
637
+ inputDateOrOptions = removeOffset(
638
+ inputDateOrOptions,
639
+ offset(inputDateOrOptions, tz)
640
+ );
641
+ }
642
+ if (!locale || locale === "device") {
643
+ locale = deviceLocale();
644
+ }
570
645
  return fill(
571
- inputDate,
572
- parts(format2, locale).filter(partFilter),
646
+ inputDateOrOptions,
647
+ parts(format2, locale).filter(partFilter != null ? partFilter : () => true),
573
648
  locale,
574
649
  genitive
575
650
  ).map((p) => p.value).join("");
@@ -644,35 +719,6 @@ function nearestDay(inputDate, search, constraint = 7) {
644
719
  return null;
645
720
  }
646
721
 
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
722
  // src/range.ts
677
723
  function range(token, locale = "en", genitive = false) {
678
724
  const r = (n, c) => Array(n).fill("").map((_, i) => `${c(i)}`);
@@ -867,15 +913,6 @@ function parseParts(dateStr, formatParts) {
867
913
  return parsed;
868
914
  }
869
915
 
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
916
  // src/sameDay.ts
880
917
  function sameDay(inputDateA, inputDateB) {
881
918
  const a = date(inputDateA);
@@ -928,6 +965,7 @@ export {
928
965
  range,
929
966
  removeOffset,
930
967
  sameDay,
968
+ tzDate,
931
969
  weekEnd,
932
970
  weekStart,
933
971
  yearDays