-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIEXT-941: Work around date-fns-tz issues
See the open issue here: marnusw/date-fns-tz#302 UIEXT-941 (Date&Time widget displays always browser timezone)
- Loading branch information
Showing
4 changed files
with
247 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
org.knime.js.pagebuilder/src/util/widgetUtil/dateTime/__tests__/dateTime.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { fromZonedTime, toZonedTime } from ".."; | ||
|
||
describe("fromZonedTime", () => { | ||
const createUTCDate = ({ | ||
year = 2021, | ||
monthIndex = 0, | ||
day = 1, | ||
hours = 0, | ||
minutes = 0, | ||
seconds = 0, | ||
milliseconds = 0, | ||
}: { | ||
year?: number; | ||
monthIndex?: number; | ||
day?: number; | ||
hours?: number; | ||
minutes?: number; | ||
seconds?: number; | ||
milliseconds?: number; | ||
} = {}) => { | ||
return new Date( | ||
Date.UTC(year, monthIndex, day, hours, minutes, seconds, milliseconds), | ||
); | ||
}; | ||
|
||
it.each([fromZonedTime, toZonedTime])( | ||
"is idempotent for a UTC date to itself", | ||
(fromOrToZonedTime) => { | ||
const utcTime = createUTCDate(); | ||
expect(fromOrToZonedTime(utcTime, "UTC")).toStrictEqual(utcTime); | ||
}, | ||
); | ||
|
||
const cetUtcPairs = [ | ||
[ | ||
"winter", | ||
{ | ||
cetTime: createUTCDate({ hours: 23 }), | ||
utcTime: createUTCDate({ hours: 22 }), | ||
}, | ||
] as const, | ||
[ | ||
"summer", | ||
{ | ||
cetTime: createUTCDate({ monthIndex: 6, day: 1, hours: 0 }), | ||
utcTime: createUTCDate({ monthIndex: 5, day: 30, hours: 22 }), | ||
}, | ||
] as const, | ||
]; | ||
|
||
it.each(cetUtcPairs)( | ||
"should convert CET to UTC in the %s", | ||
(_, { cetTime, utcTime }) => { | ||
expect(fromZonedTime(cetTime, "CET")).toStrictEqual(utcTime); | ||
}, | ||
); | ||
|
||
it.each(cetUtcPairs)( | ||
"should convert UTC to CET in the %s", | ||
(_, { cetTime, utcTime }) => { | ||
expect(toZonedTime(utcTime, "CET")).toStrictEqual(cetTime); | ||
}, | ||
); | ||
|
||
it("can convert a string to a zoned date", () => { | ||
const { utcTime, cetTime } = cetUtcPairs[0][1]; | ||
const cetTimeString = cetTime.toISOString(); | ||
expect(fromZonedTime(cetTimeString, "CET")).toStrictEqual(utcTime); | ||
expect(fromZonedTime(cetTimeString.replace("Z", ""), "CET")).toStrictEqual( | ||
utcTime, | ||
); | ||
}); | ||
|
||
it("takes offsets in strings into account", () => { | ||
expect(fromZonedTime("2021-01-01T00:00:00+01:00", "UTC")).toStrictEqual( | ||
createUTCDate({ hours: -1 }), | ||
); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
org.knime.js.pagebuilder/src/util/widgetUtil/dateTime/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { toDate, type OptionsWithTZ } from "date-fns-tz"; | ||
// @ts-expect-error | ||
import tzParseTimezone from "@@/node_modules/date-fns-tz/_lib/tzParseTimezone"; | ||
// @ts-expect-error | ||
import tzPattern from "@@/node_modules/date-fns-tz/_lib/tzPattern"; | ||
/** | ||
* This method is used to circumvent the following open issue in date-fns-tz | ||
* https://github.com/marnusw/date-fns-tz/issues/302 | ||
* | ||
* The problem is that the zonedTimeToUtc returns a Date object so that | ||
* when e.g. getHours is called, the respective UTC time hours are returned. | ||
* But since getHours depends on the systems timezone, | ||
* the actual underlying UTC time is shifted accordingly. | ||
* | ||
* | ||
* The code is an adapted version of date-fns-tz 3.2.0 | ||
* https://www.npmjs.com/package/date-fns-tz?activeTab=code | ||
* /date-fns-tz/dist/cjs/fromZonedTime/index.js | ||
*/ | ||
export const fromZonedTime = ( | ||
date: string | Date, | ||
timeZone: string, | ||
options?: OptionsWithTZ, | ||
) => { | ||
// Same code | ||
if (typeof date === "string" && !date.match(tzPattern)) { | ||
return toDate( | ||
date, | ||
Object.assign(Object.assign({}, options), { timeZone }), | ||
); | ||
} | ||
date = toDate(date, options); | ||
/** | ||
* Here we differ. Original code: | ||
const utc = newDateUTC(date.getFullYear(), date.getMonth(), date.getDate(), | ||
date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()) | ||
.getTime(); | ||
const offsetMilliseconds = tzParseTimezone(timeZone, new Date(utc)); | ||
return new Date(utc + offsetMilliseconds); | ||
*/ | ||
const offsetMilliseconds = tzParseTimezone(timeZone, date); | ||
return new Date(date.getTime() + offsetMilliseconds); | ||
}; | ||
|
||
/** | ||
* Similarly to fromTimeZone, we need this method to replace the utcToZonedTime method, | ||
* since this method is the inverse of the (incorrect) zonedTimeToUtc method. | ||
* | ||
* The code is an adapted version of date-fns-tz 3.2.0 | ||
* https://www.npmjs.com/package/date-fns-tz?activeTab=code | ||
* /date-fns-tz/dist/cjs/toZonedTime/index.js | ||
*/ | ||
export const toZonedTime = ( | ||
date: string | Date, | ||
timeZone: string, | ||
options?: OptionsWithTZ, | ||
) => { | ||
date = toDate(date, options); | ||
const offsetMilliseconds = tzParseTimezone(timeZone, date, true); | ||
return new Date(date.getTime() - offsetMilliseconds); | ||
/** | ||
* The original code does not return here but instead assigns what return to a variable d and transforms this: | ||
const resultDate = new Date(0); | ||
resultDate.setFullYear(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()); | ||
resultDate.setHours(d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds()); | ||
return resultDate; | ||
*/ | ||
}; |