@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.mjs CHANGED
@@ -1400,13 +1400,42 @@ 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
- const adjustedOffset = offset * 60;
1406
- return new Date(date.getTime() + (currentOffset - adjustedOffset) * 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
+ ["IST", 5.5],
1424
+ ["BST", 6],
1425
+ ["ICT", 7],
1426
+ ["CST", 8],
1427
+ ["JST", 9],
1428
+ ["AEST", 10],
1429
+ ["SBT", 11],
1430
+ ["FJT", 12],
1431
+ ["TOT", 13],
1432
+ ["LINT", 14]
1433
+ ];
1434
+ function adjustTz(date2, gmt) {
1435
+ const currentOffset = date2.getTimezoneOffset();
1436
+ const adjustedOffset = gmt * 60;
1437
+ return new Date(date2.getTime() + (adjustedOffset + currentOffset) * 6e4);
1438
+ }
1410
1439
  function day(num) {
1411
1440
  switch (num) {
1412
1441
  case 0:
@@ -1479,10 +1508,13 @@ function formatDate(date, format = "YYYY-MM-DD H:mm", tz) {
1479
1508
  const minutes = offset % 60;
1480
1509
  return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
1481
1510
  }
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));
1511
+ if (typeof date == "number" || typeof date == "string") date = new Date(date);
1512
+ let t;
1513
+ if (tz == null) tz = -(date.getTimezoneOffset() / 60);
1514
+ t = timezones.find((t2) => isNaN(tz) ? t2[0] == tz : t2[1] == tz);
1515
+ if (!t) throw new Error(`Unknown timezone: ${tz}`);
1516
+ date = adjustTz(date, t[1]);
1517
+ 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
1518
  }
1487
1519
  function instantInterval(fn, interval) {
1488
1520
  fn();
@@ -1536,7 +1568,6 @@ export {
1536
1568
  TypedEmitter,
1537
1569
  UnauthorizedError,
1538
1570
  addUnique,
1539
- adjustTz,
1540
1571
  adjustedInterval,
1541
1572
  arrayDiff,
1542
1573
  caseInsensitiveSort,