Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getUnixTime across dst #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
10 changes: 9 additions & 1 deletion src/convert/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions test/getUnixTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down