Skip to content

Commit b575c81

Browse files
authored
fix: timezone plugin currect parse UTC tz (#2693)
1 parent f3ef705 commit b575c81

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const dayjs = function (date, c) {
5050
const wrapper = (date, instance) =>
5151
dayjs(date, {
5252
locale: instance.$L,
53-
utc: instance.$offset !== 0 && instance.$u,
53+
utc: instance.$u,
5454
x: instance.$x,
5555
$offset: instance.$offset // todo: refactor; do not use this.$offset in you code
5656
})

src/plugin/timezone/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,18 @@ export default (o, c, d) => {
9797
const date = this.toDate()
9898
const target = date.toLocaleString('en-US', { timeZone: timezone })
9999
const diff = Math.round((date - new Date(target)) / 1000 / 60)
100-
let ins = d(target, { locale: this.$L }).$set(MS, this.$ms)
101-
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
102-
if (keepLocalTime) {
103-
const newOffset = ins.utcOffset()
104-
ins = ins.add(oldOffset - newOffset, MIN)
100+
const offset = (-Math.round(date.getTimezoneOffset() / 15) * 15) - diff
101+
const isUTC = !Number(offset)
102+
let ins
103+
if (isUTC) { // if utcOffset is 0, turn it to UTC mode
104+
ins = this.utcOffset(0, keepLocalTime)
105+
} else {
106+
ins = d(target, { locale: this.$L }).$set(MS, this.$ms)
107+
.utcOffset(offset, true)
108+
if (keepLocalTime) {
109+
const newOffset = ins.utcOffset()
110+
ins = ins.add(oldOffset - newOffset, MIN)
111+
}
105112
}
106113
ins.$x.$timezone = timezone
107114
return ins

test/plugin/timezone.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,25 @@ describe('startOf and endOf', () => {
330330
expect(tzWithLocality.startOf('week').format('YYYY-MM-DD')).toEqual('2023-02-15')
331331
})
332332
})
333+
334+
335+
describe('UTC timezone', () => {
336+
it('TZ with UTC with Locale', () => {
337+
const test1 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Seoul').locale('en')
338+
expect(test1.hour()).toBe(9)
339+
const test2 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Hong_Kong').locale('en')
340+
expect(test2.hour()).toBe(8)
341+
const test3 = dayjs('2000-01-01T09:00:00+09:00').tz('Etc/UTC').locale('en')
342+
expect(test3.hour()).toBe(0)
343+
})
344+
345+
it('TZ with UTC', () => {
346+
const dayjs1 = dayjs('2000-01-01T09:01:00+09:00').tz('Etc/UTC', false)
347+
expect(dayjs1.format()).toBe('2000-01-01T00:01:00Z')
348+
const moment1 = moment('2000-01-01T09:01:00+09:00').tz('Etc/UTC', false)
349+
expect(moment1.format()).toBe('2000-01-01T00:01:00Z')
350+
const dayjs2 = dayjs('2000-01-01T09:01:00+09:00').tz('Etc/UTC', true)
351+
const moment2 = moment('2000-01-01T09:01:00+09:00').tz('Etc/UTC', true)
352+
expect(dayjs2.format()).toBe(moment2.format())
353+
})
354+
})

test/timezone.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,3 @@ it('UTC diff in DST', () => {
8181
expect(day1.diff(day2, 'd'))
8282
.toBe(-3)
8383
})
84-
85-
it('TZ with Locale', () => {
86-
const test1 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Seoul').locale('en')
87-
expect(test1.hour()).toBe(9)
88-
const test2 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Hong_Kong').locale('en')
89-
expect(test2.hour()).toBe(8)
90-
const test3 = dayjs('2000-01-01T09:00:00+09:00').tz('Etc/UTC').locale('en')
91-
expect(test3.hour()).toBe(0)
92-
})

0 commit comments

Comments
 (0)