@ztimson/utils 0.22.11 → 0.23.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.
package/dist/index.cjs CHANGED
@@ -1404,13 +1404,42 @@ 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
- const adjustedOffset = offset * 60;
1410
- return new Date(date.getTime() + (currentOffset - adjustedOffset) * 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
+ ["IST", 5.5],
1428
+ ["BST", 6],
1429
+ ["ICT", 7],
1430
+ ["CST", 8],
1431
+ ["JST", 9],
1432
+ ["AEST", 10],
1433
+ ["SBT", 11],
1434
+ ["FJT", 12],
1435
+ ["TOT", 13],
1436
+ ["LINT", 14]
1437
+ ];
1438
+ function adjustTz(date2, gmt) {
1439
+ const currentOffset = date2.getTimezoneOffset();
1440
+ const adjustedOffset = gmt * 60;
1441
+ return new Date(date2.getTime() + (adjustedOffset + currentOffset) * 6e4);
1442
+ }
1414
1443
  function day(num) {
1415
1444
  switch (num) {
1416
1445
  case 0:
@@ -1483,10 +1512,13 @@ var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "s
1483
1512
  const minutes = offset % 60;
1484
1513
  return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
1485
1514
  }
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));
1515
+ if (typeof date == "number" || typeof date == "string") date = new Date(date);
1516
+ let t;
1517
+ if (tz == null) tz = -(date.getTimezoneOffset() / 60);
1518
+ t = timezones.find((t2) => isNaN(tz) ? t2[0] == tz : t2[1] == tz);
1519
+ if (!t) throw new Error(`Unknown timezone: ${tz}`);
1520
+ date = adjustTz(date, t[1]);
1521
+ 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
1522
  }
1491
1523
  function instantInterval(fn, interval) {
1492
1524
  fn();
@@ -1539,7 +1571,6 @@ var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "s
1539
1571
  exports.TypedEmitter = TypedEmitter;
1540
1572
  exports.UnauthorizedError = UnauthorizedError;
1541
1573
  exports.addUnique = addUnique;
1542
- exports.adjustTz = adjustTz;
1543
1574
  exports.adjustedInterval = adjustedInterval;
1544
1575
  exports.arrayDiff = arrayDiff;
1545
1576
  exports.caseInsensitiveSort = caseInsensitiveSort;