Skip to content

Commit

Permalink
fix: timezone plugin DST error (#1352)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansdaniels committed Jan 28, 2021
1 parent 1138a3f commit 71bed15
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/plugin/timezone/index.js
Expand Up @@ -36,8 +36,6 @@ const getDateTimeFormat = (timezone, options = {}) => {
export default (o, c, d) => {
let defaultTimezone

const localUtcOffset = d().utcOffset()

const makeFormatParts = (timestamp, timezone, options = {}) => {
const date = new Date(timestamp)
const dtf = getDateTimeFormat(timezone, options)
Expand Down Expand Up @@ -96,9 +94,11 @@ export default (o, c, d) => {

proto.tz = function (timezone = defaultTimezone, keepLocalTime) {
const oldOffset = this.utcOffset()
const target = this.toDate().toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((this.toDate() - new Date(target)) / 1000 / 60)
let ins = d(target).$set(MS, this.$ms).utcOffset(localUtcOffset - diff, true)
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).$set(MS, this.$ms)
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
if (keepLocalTime) {
const newOffset = ins.utcOffset()
ins = ins.add(oldOffset - newOffset, MIN)
Expand Down
2 changes: 2 additions & 0 deletions test/plugin/timezone.test.js
Expand Up @@ -101,7 +101,9 @@ describe('Convert', () => {
const jun = _('2014-06-01T12:00:00Z')
const dec = _('2014-12-01T12:00:00Z')
expect(jun.tz('America/Los_Angeles').format('ha')).toBe('5am')
expect(jun.tz('America/Los_Angeles').utcOffset()).toBe(-7 * 60)
expect(dec.tz('America/Los_Angeles').format('ha')).toBe('4am')
expect(dec.tz('America/Los_Angeles').utcOffset()).toBe(-8 * 60)
expect(jun.tz(NY).format('ha')).toBe('8am')
expect(dec.tz(NY).format('ha')).toBe('7am')
expect(jun.tz(TOKYO).format('ha')).toBe('9pm')
Expand Down

0 comments on commit 71bed15

Please sign in to comment.