@ztimson/utils 0.22.10 → 0.23.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/dist/index.cjs CHANGED
@@ -1404,13 +1404,41 @@ var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "s
1404
1404
  if (timeout) clearTimeout(timeout);
1405
1405
  };
1406
1406
  }
1407
- function adjustTz(date, offset) {
1408
- const currentOffset = date.getTimezoneOffset();
1409
- offset = currentOffset - offset * 60;
1410
- return new Date(date.getTime() + offset * 6e4);
1411
- }
1412
1407
  function formatDate(date, format = "YYYY-MM-DD H:mm", tz) {
1413
- if (typeof date == "number" || typeof date == "string") date = new Date(date);
1408
+ const timezones = [
1409
+ ["IDLW", -12],
1410
+ ["SST", -11],
1411
+ ["HST", -10],
1412
+ ["AKST", -9],
1413
+ ["PST", -8],
1414
+ ["MST", -7],
1415
+ ["CST", -6],
1416
+ ["EST", -5],
1417
+ ["AST", -4],
1418
+ ["BRT", -3],
1419
+ ["MAT", -2],
1420
+ ["AZOT", -1],
1421
+ ["UTC", 0],
1422
+ ["CET", 1],
1423
+ ["EET", 2],
1424
+ ["MSK", 3],
1425
+ ["AST", 4],
1426
+ ["PKT", 5],
1427
+ ["BST", 6],
1428
+ ["ICT", 7],
1429
+ ["CST", 8],
1430
+ ["JST", 9],
1431
+ ["AEST", 10],
1432
+ ["SBT", 11],
1433
+ ["FJT", 12],
1434
+ ["TOT", 13],
1435
+ ["LINT", 14]
1436
+ ];
1437
+ function adjustTz(date2, gmt) {
1438
+ const currentOffset = date2.getTimezoneOffset();
1439
+ const adjustedOffset = gmt * 60;
1440
+ return new Date(date2.getTime() + (adjustedOffset + currentOffset) * 6e4);
1441
+ }
1414
1442
  function day(num) {
1415
1443
  switch (num) {
1416
1444
  case 0:
@@ -1483,10 +1511,13 @@ var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "s
1483
1511
  const minutes = offset % 60;
1484
1512
  return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
1485
1513
  }
1486
- function timezone(date2) {
1487
- return new Intl.DateTimeFormat("en-US", { timeZoneName: "short" }).format(date2).split(" ").pop() || "";
1488
- }
1489
- return format.replaceAll("YYYY", date.getFullYear().toString()).replaceAll("YY", date.getFullYear().toString().slice(2)).replaceAll("MMMM", month(date.getMonth())).replaceAll("MMM", month(date.getMonth()).slice(0, 3)).replaceAll("MM", (date.getMonth() + 1).toString().padStart(2, "0")).replaceAll("M", (date.getMonth() + 1).toString()).replaceAll("DDD", doy(date).toString()).replaceAll("DD", date.getDate().toString().padStart(2, "0")).replaceAll("Do", suffix(date.getDate())).replaceAll("D", date.getDate().toString()).replaceAll("dddd", day(date.getDay())).replaceAll("ddd", day(date.getDay()).slice(0, 2)).replaceAll("dd", date.getDate().toString().padStart(2, "0")).replaceAll("d", date.getDay().toString()).replaceAll("HH", date.getHours().toString().padStart(2, "0")).replaceAll("H", date.getHours().toString()).replaceAll("hh", (date.getHours() > 12 ? date.getHours() - 12 : date.getHours()).toString().padStart(2, "0")).replaceAll("h", (date.getHours() > 12 ? date.getHours() - 12 : date.getHours()).toString()).replaceAll("mm", date.getMinutes().toString().padStart(2, "0")).replaceAll("m", date.getMinutes().toString()).replaceAll("ss", date.getSeconds().toString().padStart(2, "0")).replaceAll("s", date.getSeconds().toString()).replaceAll("SSS", date.getMilliseconds().toString().padEnd(3, "0")).replaceAll("SS", date.getMilliseconds().toString().slice(0, 1).padEnd(2, "0")).replaceAll("S", date.getMilliseconds().toString()[0]).replaceAll("A", date.getHours() >= 12 ? "PM" : "AM").replaceAll("a", date.getHours() >= 12 ? "pm" : "am").replaceAll("ZZ", tzOffset(isNaN(tz) ? date.getTimezoneOffset() : tz).replace(":", "")).replaceAll("Z", tzOffset(isNaN(tz) ? date.getTimezoneOffset() : tz)).replaceAll("z", typeof tz == "string" ? tz : timezone(date));
1514
+ if (typeof date == "number" || typeof date == "string") date = new Date(date);
1515
+ let t;
1516
+ if (tz == null) tz = -(date.getTimezoneOffset() / 60);
1517
+ t = timezones.find((t2) => isNaN(tz) ? t2[0] == tz : t2[1] == tz);
1518
+ if (!t) throw new Error(`Unknown timezone: ${tz}`);
1519
+ date = adjustTz(date, t[1]);
1520
+ return format.replaceAll("YYYY", date.getFullYear().toString()).replaceAll("YY", date.getFullYear().toString().slice(2)).replaceAll("MMMM", month(date.getMonth())).replaceAll("MMM", month(date.getMonth()).slice(0, 3)).replaceAll("MM", (date.getMonth() + 1).toString().padStart(2, "0")).replaceAll("M", (date.getMonth() + 1).toString()).replaceAll("DDD", doy(date).toString()).replaceAll("DD", date.getDate().toString().padStart(2, "0")).replaceAll("Do", suffix(date.getDate())).replaceAll("D", date.getDate().toString()).replaceAll("dddd", day(date.getDay())).replaceAll("ddd", day(date.getDay()).slice(0, 2)).replaceAll("dd", date.getDate().toString().padStart(2, "0")).replaceAll("d", date.getDay().toString()).replaceAll("HH", date.getHours().toString().padStart(2, "0")).replaceAll("H", date.getHours().toString()).replaceAll("hh", (date.getHours() > 12 ? date.getHours() - 12 : date.getHours()).toString().padStart(2, "0")).replaceAll("h", (date.getHours() > 12 ? date.getHours() - 12 : date.getHours()).toString()).replaceAll("mm", date.getMinutes().toString().padStart(2, "0")).replaceAll("m", date.getMinutes().toString()).replaceAll("ss", date.getSeconds().toString().padStart(2, "0")).replaceAll("s", date.getSeconds().toString()).replaceAll("SSS", date.getMilliseconds().toString().padEnd(3, "0")).replaceAll("SS", date.getMilliseconds().toString().slice(0, 1).padEnd(2, "0")).replaceAll("S", date.getMilliseconds().toString()[0]).replaceAll("A", date.getHours() >= 12 ? "PM" : "AM").replaceAll("a", date.getHours() >= 12 ? "pm" : "am").replaceAll("ZZ", tzOffset(t[1] * 60).replace(":", "")).replaceAll("Z", tzOffset(t[1] * 60)).replaceAll("z", typeof tz == "string" ? tz : t[0]);
1490
1521
  }
1491
1522
  function instantInterval(fn, interval) {
1492
1523
  fn();
@@ -1539,7 +1570,6 @@ var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "s
1539
1570
  exports.TypedEmitter = TypedEmitter;
1540
1571
  exports.UnauthorizedError = UnauthorizedError;
1541
1572
  exports.addUnique = addUnique;
1542
- exports.adjustTz = adjustTz;
1543
1573
  exports.adjustedInterval = adjustedInterval;
1544
1574
  exports.arrayDiff = arrayDiff;
1545
1575
  exports.caseInsensitiveSort = caseInsensitiveSort;