From e999fc4711993e05f409a637229c4c71c611db41 Mon Sep 17 00:00:00 2001 From: Georgi Kostov Date: Fri, 28 Jul 2023 16:41:31 +0300 Subject: [PATCH 1/2] updated link: protocol to file: as of more recent npm versions --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index efe50fd..d8f595a 100644 --- a/package.json +++ b/package.json @@ -152,7 +152,7 @@ "rollup-plugin-cleanup": "3.2.1", "rollup-plugin-swc-minify": "1.0.5", "serve-static": "1.15.0", - "timezone-support": "link:", + "timezone-support": "file:", "typescript": "4.9.3" }, "keywords": [ From e2242c869a2a3581b8498c0a02298dfd9d1f71c1 Mon Sep 17 00:00:00 2001 From: Georgi Kostov Date: Fri, 28 Jul 2023 16:43:10 +0300 Subject: [PATCH 2/2] Fixed getUnixTime which was selecting the wrong offset when converting across a DST switch points --- src/convert/convert.js | 10 +++++++++- test/getUnixTime.test.js | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/convert/convert.js b/src/convert/convert.js index bacba69..ebb408e 100644 --- a/src/convert/convert.js +++ b/src/convert/convert.js @@ -48,17 +48,25 @@ function getUnixTime (time, timeZone) { return epoch } const unixTime = getUnixTimeFromUTC(time) + let convertedUnixTime; if (zone) { if (timeZone) { throw new Error('Both own and other time zones specified. Omit the other one.') } + convertedUnixTime = unixTime + zone.offset * 60000 } else { if (!timeZone) { throw new Error('Missing other time zone.') } zone = getTransition(unixTime, timeZone) + convertedUnixTime = unixTime + zone.offset * 60000 + const convertedZoneOffset = getTransition(convertedUnixTime, timeZone).offset + // check if the converted date may have moved to a different offset (probably because of a DST switch) + if (convertedZoneOffset !== zone.offset) { + convertedUnixTime = unixTime + convertedZoneOffset * 60000 + } } - return unixTime + zone.offset * 60000 + return convertedUnixTime } function setTimeZone (time, timeZone, options) { diff --git a/test/getUnixTime.test.js b/test/getUnixTime.test.js index 9b3b4cf..09b6f18 100644 --- a/test/getUnixTime.test.js +++ b/test/getUnixTime.test.js @@ -84,6 +84,24 @@ it('recognizes daylight-saving time', () => { expect(unixTime).toEqual(epoch) }) +it('recognizes daylight-saving time switch point of explicit timezone', () => { + // This test depends on what local TZ it is being executed in!!! + // The local TZ has to be at least 2 hours closer to UTC than Melbourne + // The TZ where this has been confirmed was UTC+3 (Europe/Sofia) + // This test shall fail without the fix because the UTC moment is after the DST switch and the target moment is before that + const melbourneTime = { + year: 2023, + month: 10, + day: 1, + hours: 0, + minutes: 0 + } + const unixTime = getUnixTime(melbourneTime, findTimeZone('Australia/Melbourne')) + expect(typeof unixTime === 'number').toBeTruthy() + const epoch = Date.UTC(2023, 8, 30, 14, 0) + expect(unixTime).toEqual(epoch) +}) + it('checks, that other time zone is specified, if required', () => { const berlinTime = { year: 2018,