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 issue #2037 - modifying a dayjs instance created with plugin timezone in UTC corrupts it #2118

Open
wants to merge 23 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4542a1f
Merge branch 'master' of github.com:iamkun/dayjs into dev
BePo65 Oct 21, 2022
cec0cc0
Merge branch 'dev' of github.com:iamkun/dayjs into dev
BePo65 Nov 8, 2022
4898982
Merge branch 'dev' of github.com:iamkun/dayjs into dev
BePo65 Dec 15, 2022
b64337c
Merge branch 'dev' of github.com:iamkun/dayjs into dev
BePo65 Jan 7, 2023
2e886c2
Merge branch 'dev' of github.com:iamkun/dayjs into dev
BePo65 Jul 4, 2023
ef1e96e
Merge branch 'dev' of github.com:iamkun/dayjs into dev
BePo65 Sep 20, 2023
bbb28a6
Merge branch 'dev' of github.com:iamkun/dayjs into dev
BePo65 Sep 23, 2023
39308c9
chore(release): 1.11.9 [skip ci]
semantic-release-bot Jul 1, 2023
7e2c794
chore(release): 1.11.10 [skip ci]
semantic-release-bot Sep 19, 2023
d3cfe79
refactor: remove duplicate empty lines
BePo65 Oct 22, 2022
1b6dafc
build: use npx to run eslint
BePo65 Oct 20, 2022
093cf9c
test: fix duplicate tests / test names
BePo65 Oct 17, 2022
c194751
test: suppress console.warn when testing moment with illegal value
BePo65 Oct 20, 2022
9561c98
test: split test 'parse timestamp' for 'timezone' for easier debugging
BePo65 Oct 21, 2022
e0af8d9
test: add tests for issue 2037 and issue #2110 in plugin/timezone.tes…
BePo65 Oct 21, 2022
ea7bf75
test: increase test coverage of timezone to 100%
BePo65 Nov 3, 2022
f541369
fix: handling of UTC timezones in Dayjs.tz
BePo65 Nov 3, 2022
96b49fd
fix: handling of input strings with offset in dayjs.tz
BePo65 Nov 4, 2022
f4e9819
test: add tests for timezone gmt (utcOffset 0)
BePo65 Mar 25, 2023
1af5a59
test: add tests for issue 2037 and issue #2110 in plugin/timezone.tes…
BePo65 Oct 21, 2022
086c7f8
test: add tests for timezone name and tz.offsetName
BePo65 Sep 23, 2023
4cb0ef7
test: fix MockDate linter error
BePo65 Sep 23, 2023
ed63026
Merge branch 'dev' into fix/issue2037
BePo65 Jul 8, 2024
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 @@ -7,7 +7,7 @@
"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-tz": "date && jest test/timezone.test --coverage=false",
"lint": "./node_modules/.bin/eslint src/* test/* build/*",
"lint": "npx 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
22 changes: 15 additions & 7 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const typeToPos = {
// Cache time-zone lookups from Intl.DateTimeFormat,
// as it is a *very* slow method.
const dtfCache = {}
const getDateTimeFormat = (timezone, options = {}) => {
const getDateTimeFormat = (timezone, options) => {
const timeZoneName = options.timeZoneName || 'short'
const key = `${timezone}|${timeZoneName}`
let dtf = dtfCache[key]
Expand Down Expand Up @@ -97,8 +97,13 @@ export default (o, c, d) => {
const date = this.toDate()
const target = date.toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((date - new Date(target)) / 1000 / 60)
let ins = d(target, { locale: this.$L }).$set(MS, this.$ms)
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
const offset = -date.getTimezoneOffset() - diff
if (offset === 0) {
return this.utc(keepLocalTime)
BePo65 marked this conversation as resolved.
Show resolved Hide resolved
}
let ins = d(target)
.$set(MS, this.$ms)
.utcOffset(offset, true)
if (keepLocalTime) {
const newOffset = ins.utcOffset()
ins = ins.add(oldOffset - newOffset, MIN)
Expand Down Expand Up @@ -128,10 +133,13 @@ export default (o, c, d) => {
d.tz = function (input, arg1, arg2) {
const parseFormat = arg2 && arg1
const timezone = arg2 || arg1 || defaultTimezone
const previousOffset = tzOffset(+d(), timezone)
if (typeof input !== 'string') {
// timestamp number || js Date || Day.js
return d(input).tz(timezone)
const previousOffset = tzOffset(+d(input, parseFormat), timezone)
// To differentiate date only string (e.g. yy-mm-dd) from date string with negative
// 2-digit offset (hour offset zz, e.g. yy-mm-zz) we require at least 8 characters
// before offset (i.e. yy-mm-dd-zz)
if ((typeof input !== 'string') || /.{8,}[+-]\d\d:?(\d\d)?$|Z$/i.test(input)) {
BePo65 marked this conversation as resolved.
Show resolved Hide resolved
// timestamp number || js Date || Day.js || input string with offset (e.g. -03:00)
return d(input, parseFormat).tz(timezone)
}
const localTs = d.utc(input, parseFormat).valueOf()
const [targetTs, targetOffset] = fixOffset(localTs, previousOffset, timezone)
Expand Down
1 change: 0 additions & 1 deletion src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function offsetFromString(value = '') {
return indicator === '+' ? totalOffsetInMinutes : -totalOffsetInMinutes
}


export default (option, Dayjs, dayjs) => {
const proto = Dayjs.prototype
dayjs.utc = function (date) {
Expand Down
78 changes: 0 additions & 78 deletions test/issues/issue2027.correct-order.test.js

This file was deleted.

79 changes: 0 additions & 79 deletions test/issues/issue2027.swapped-order.test.js

This file was deleted.

Loading
Loading