@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.mjs CHANGED
@@ -1400,13 +1400,41 @@ function adjustedInterval(cb, ms) {
1400
1400
  if (timeout) clearTimeout(timeout);
1401
1401
  };
1402
1402
  }
1403
- function adjustTz(date, offset) {
1404
- const currentOffset = date.getTimezoneOffset();
1405
- offset = currentOffset - offset * 60;
1406
- return new Date(date.getTime() + offset * 6e4);
1407
- }
1408
1403
  function formatDate(date, format = "YYYY-MM-DD H:mm", tz) {
1409
- if (typeof date == "number" || typeof date == "string") date = new Date(date);
1404
+ const timezones = [
1405
+ ["IDLW", -12],
1406
+ ["SST", -11],
1407
+ ["HST", -10],
1408
+ ["AKST", -9],
1409
+ ["PST", -8],
1410
+ ["MST", -7],
1411
+ ["CST", -6],
1412
+ ["EST", -5],
1413
+ ["AST", -4],
1414
+ ["BRT", -3],
1415
+ ["MAT", -2],
1416
+ ["AZOT", -1],
1417
+ ["UTC", 0],
1418
+ ["CET", 1],
1419
+ ["EET", 2],
1420
+ ["MSK", 3],
1421
+ ["AST", 4],
1422
+ ["PKT", 5],
1423
+ ["BST", 6],
1424
+ ["ICT", 7],
1425
+ ["CST", 8],
1426
+ ["JST", 9],
1427
+ ["AEST", 10],
1428
+ ["SBT", 11],
1429
+ ["FJT", 12],
1430
+ ["TOT", 13],
1431
+ ["LINT", 14]
1432
+ ];
1433
+ function adjustTz(date2, gmt) {
1434
+ const currentOffset = date2.getTimezoneOffset();
1435
+ const adjustedOffset = gmt * 60;
1436
+ return new Date(date2.getTime() + (adjustedOffset + currentOffset) * 6e4);
1437
+ }
1410
1438
  function day(num) {
1411
1439
  switch (num) {
1412
1440
  case 0:
@@ -1479,10 +1507,13 @@ function formatDate(date, format = "YYYY-MM-DD H:mm", tz) {
1479
1507
  const minutes = offset % 60;
1480
1508
  return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
1481
1509
  }
1482
- function timezone(date2) {
1483
- return new Intl.DateTimeFormat("en-US", { timeZoneName: "short" }).format(date2).split(" ").pop() || "";
1484
- }
1485
- 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));
1510
+ if (typeof date == "number" || typeof date == "string") date = new Date(date);
1511
+ let t;
1512
+ if (tz == null) tz = -(date.getTimezoneOffset() / 60);
1513
+ t = timezones.find((t2) => isNaN(tz) ? t2[0] == tz : t2[1] == tz);
1514
+ if (!t) throw new Error(`Unknown timezone: ${tz}`);
1515
+ date = adjustTz(date, t[1]);
1516
+ 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]);
1486
1517
  }
1487
1518
  function instantInterval(fn, interval) {
1488
1519
  fn();
@@ -1536,7 +1567,6 @@ export {
1536
1567
  TypedEmitter,
1537
1568
  UnauthorizedError,
1538
1569
  addUnique,
1539
- adjustTz,
1540
1570
  adjustedInterval,
1541
1571
  arrayDiff,
1542
1572
  caseInsensitiveSort,