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: The time zone offset is incorrectly calculated when the DST exits at 00:00. #2835

Open
wants to merge 3 commits into
base: dev
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
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
"main": "dayjs.min.js",
"types": "index.d.ts",
"scripts": {
"test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && TZ=America/Whitehorse npm run test-tz && npm run test-tz && jest",
"test": "npm run test-tz && npm run test-tz1 && npm run test-tz2 && npm run test-tz3 && npm run test-tz4 && jest",
"test-tz": "date && jest test/timezone.test --coverage=false",
"lint": "./node_modules/.bin/eslint src/* test/* build/*",
"test-tz1": "cross-env TZ=Pacific/Auckland npm run test-tz",
"test-tz2": "cross-env TZ=Europe/London npm run test-tz",
"test-tz3": "cross-env TZ=America/Whitehorse npm run test-tz",
"test-tz4": "cross-env TZ=America/Asuncion npm run test-tz",
"lint": "eslint src/* test/* build/*",
"prettier": "prettier --write \"docs/**/*.md\"",
"babel": "cross-env BABEL_ENV=build babel src --out-dir esm --copy-files && node build/esm",
"build": "cross-env BABEL_ENV=build node build && npm run size",
Expand Down
3 changes: 1 addition & 2 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ export default (o, c, d) => {
const oldOffset = this.utcOffset()
const date = this.toDate()
const target = date.toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((date - new Date(target)) / 1000 / 60)
const offset = (-Math.round(date.getTimezoneOffset() / 15) * 15) - diff
const offset = tzOffset(date.getTime(), timezone)
const isUTC = !Number(offset)
let ins
if (isUTC) { // if utcOffset is 0, turn it to UTC mode
Expand Down
12 changes: 12 additions & 0 deletions test/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ it('UTC diff in DST', () => {
expect(day1.diff(day2, 'd'))
.toBe(-3)
})

describe('Test America/Asuncion', () => {
it('Entering daylight saving time', () => {
const testDate = dayjs('2024-03-24T02:20:57.000+00:00').tz('America/Asuncion').format('YYYY-MM-DD HH:mm:ssZ')
expect(testDate).toBe('2024-03-23 23:20:57-03:00')
})

it('Exit daylight saving time', () => {
const testDate = dayjs('2024-03-24T03:20:57.000+00:00').tz('America/Asuncion').format('YYYY-MM-DD HH:mm:ssZ')
expect(testDate).toBe('2024-03-23 23:20:57-04:00')
})
})