Skip to content

Commit

Permalink
chore(util): update date-fns dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Sep 21, 2024
1 parent 3b3c1a5 commit 0d972ee
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
30 changes: 10 additions & 20 deletions packages/util/lib/date.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { parseISO } from "date-fns";
import {
format,
getTimezoneOffset,
fromZonedTime,
toZonedTime,
} from "date-fns-tz";
import { format, parseISO } from "date-fns";
import { tz, tzOffset } from "@date-fns/tz";
import * as locales from "date-fns/locale";

export const dateTokens = [
Expand Down Expand Up @@ -64,9 +59,8 @@ export const formatDate = (string, tokens, options = {}) => {
* @returns {string} Formatted local date, i.e. 2023-08-28T12:30
*/
export const formatDateToLocal = (string, timeZone) => {
const zonedDateTime = fromZonedTime(string, timeZone);
const dateTime = format(zonedDateTime, "yyyy-MM-dd'T'HH:mm", {
timeZone,
const dateTime = format(string, "yyyy-MM-dd'T'HH:mm", {
in: tz(timeZone),
});

return dateTime;
Expand All @@ -90,7 +84,7 @@ export const getDate = (setting, dateString = "") => {
}

// Date time is given date or new date, set us UTC
let dateTime = dateString ? new Date(dateString) : new Date();
const dateTime = dateString ? new Date(dateString) : new Date();

// Desired time zone
const serverTimeZone = getTimeZoneDesignator();
Expand All @@ -102,17 +96,14 @@ export const getDate = (setting, dateString = "") => {
const dateIsShort = dateString && !dateHasTime;
if (dateIsShort) {
const offset = format(dateTime, "XXX", {
timeZone: outputTimeZone,
in: tz(outputTimeZone),
});
return `${dateString}T00:00:00.000${offset}`;
}

// JS converts dateString to UTC, so need to convert it to output zoned time
dateTime = toZonedTime(dateTime, outputTimeZone);

// Return date time with desired timezone offset
const formattedDateTime = format(dateTime, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", {
timeZone: outputTimeZone,
in: tz(outputTimeZone),
});

return formattedDateTime;
Expand Down Expand Up @@ -150,15 +141,14 @@ export const getTimeZoneDesignator = (minutes) => {
/**
* Get offset minutes from time zone name
* @param {string} timeZone - IANA tz timezone
* @param {Date|number} date - Date time
* @param {Date} date - Date time
* @returns {number} Minutes offset from UTC
*/
export const getTimeZoneOffset = (timeZone, date) => {
const milliseconds = getTimezoneOffset(timeZone, date);
const minutes = tzOffset(timeZone, date);

// Ensure `dateFnsTz.getTimezoneOffset()` returns same value as
// Ensure `tzOffset()` returns same value as
// `Date.prototype.getTimezoneOffset()`
const minutes = milliseconds / 1000 / 60;
const offset = minutes === 0 ? 0 : minutes * -1;
return offset;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"directory": "packages/util"
},
"dependencies": {
"@date-fns/tz": "^1.0.2",
"@sindresorhus/slugify": "^2.1.0",
"date-fns": "^3.6.0",
"date-fns-tz": "^3.0.0",
"date-fns": "^4.0.0",
"mongodb": "^6.8.0"
},
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions packages/util/test/unit/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe("util/lib/date", () => {
);
const utc = formatDateToLocal("2019-11-30T12:30:00+01:00", "UTC");

assert.equal(tz1, "2019-11-30T03:30");
assert.equal(tz2, "2019-11-30T16:30");
assert.equal(tz1, "2019-11-30T19:30");
assert.equal(tz2, "2019-11-30T06:30");
assert.equal(utc, "2019-11-30T11:30");
});

Expand Down

0 comments on commit 0d972ee

Please sign in to comment.