Skip to content

Commit

Permalink
feat(util): remove getTimeZoneDesignator and getTimeZoneOffset helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Dec 26, 2024
1 parent 7bb9e7a commit 742898d
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 73 deletions.
2 changes: 0 additions & 2 deletions packages/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export {
formatZonedToLocalDate,
formatLocalToZonedDate,
getDate,
getTimeZoneDesignator,
getTimeZoneOffset,
isDate,
} from "./lib/date.js";
export { sanitise } from "./lib/object.js";
Expand Down
46 changes: 1 addition & 45 deletions packages/util/lib/date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tz, tzOffset } from "@date-fns/tz";
import { tz } from "@date-fns/tz";
import { format, parseISO } from "date-fns";
import * as locales from "date-fns/locale";

Expand Down Expand Up @@ -124,50 +124,6 @@ export const getDate = (setting, dateString = "") => {
return formattedDateTime;
};

/**
* Get local time zone offset in hours and minutes
* @param {number} [minutes] - Time zone offset in minutes
* @returns {string} Local time zone designator, i.e. +05:30, -06:00 or Z
*/
export const getTimeZoneDesignator = (minutes) => {
minutes = minutes || minutes === 0 ? minutes : new Date().getTimezoneOffset();
const hours = Math.abs(minutes / 60).toString();

const offsetHours = Number.parseInt(hours, 10);
const offsetMinutes = Math.abs(minutes % 60);

const hh = String(offsetHours).padStart(2, "0");
const mm = String(offsetMinutes).padStart(2, "0");

// Prepend positive/negative symbol to designator
// If offset minutes is 0, time zone is UTC, so use Z
let designator;
if (minutes < 0) {
designator = `+${hh}:${mm}`;
} else if (minutes > 0) {
designator = `-${hh}:${mm}`;
} else if (minutes === 0) {
designator = "+00:00";
}

return designator;
};

/**
* Get offset minutes from time zone name
* @param {string} timeZone - IANA tz timezone
* @param {Date} date - Date time
* @returns {number} Minutes offset from UTC
*/
export const getTimeZoneOffset = (timeZone, date) => {
const minutes = tzOffset(timeZone, date);

// Ensure `tzOffset()` returns same value as
// `Date.prototype.getTimezoneOffset()`
const offset = minutes === 0 ? 0 : minutes * -1;
return offset;
};

/**
* Check if a string can be parsed as a date
* @param {string} string - String
Expand Down
26 changes: 0 additions & 26 deletions packages/util/test/unit/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
formatZonedToLocalDate,
formatLocalToZonedDate,
getDate,
getTimeZoneDesignator,
getTimeZoneOffset,
isDate,
} from "../../lib/date.js";

Expand Down Expand Up @@ -199,30 +197,6 @@ describe("util/lib/date", () => {
assert.equal(result, "2020-01-02T16:00:00.000Z");
});

it("Gets server timezone offset from minutes", () => {
assert.equal(getTimeZoneDesignator(0), "+00:00");
assert.equal(getTimeZoneDesignator(150), "-02:30");
assert.equal(getTimeZoneDesignator(-300), "+05:00");
});

it("Gets server timezone offset from local time", () => {
process.env.TZ = "Asia/Taipei"; // Does not observe DST
assert.equal(getTimeZoneDesignator(), "+08:00");

process.env.TZ = "America/Panama"; // Does not observe DST
assert.equal(getTimeZoneDesignator(), "-05:00");

process.env.TZ = "UTC";
assert.equal(getTimeZoneDesignator(), "+00:00");
});

it("Gets offset minutes from time zone name", () => {
const date = new Date();
assert.equal(getTimeZoneOffset("Asia/Taipei", date), -480);
assert.equal(getTimeZoneOffset("America/Panama", date), 300);
assert.equal(getTimeZoneOffset("UTC", date), 0);
});

it("Check if a string can be parsed as a date", () => {
assert.equal(isDate("2024-02-14T13:24:00+0100"), true);
assert.equal(isDate("Wed Feb 14 2024 13:24:00 GMT+0100"), true);
Expand Down

0 comments on commit 742898d

Please sign in to comment.