Skip to content

Commit

Permalink
fix: update timezone plugin parse Date instance / timestamp logic & r…
Browse files Browse the repository at this point in the history
…emove useless test (#1183)
  • Loading branch information
iamkun authored Nov 5, 2020
1 parent 73513ec commit a7f858b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,11 @@ export default (o, c, d) => {
const parseFormat = arg2 && arg1
const timezone = arg2 || arg1 || defaultTimezone
const previousOffset = tzOffset(+d(), timezone)
let localTs
if (typeof input !== 'string') {
// timestamp number || js Date || Day.js
localTs = d(input) + (previousOffset * 60 * 1000)
return d(input).tz(timezone)
}
localTs = localTs || d.utc(input, parseFormat).valueOf()
const localTs = d.utc(input, parseFormat).valueOf()
const [targetTs, targetOffset] = fixOffset(localTs, previousOffset, timezone)
const ins = d(targetTs).utcOffset(targetOffset)
ins.$x.$timezone = timezone
Expand Down
22 changes: 9 additions & 13 deletions test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,20 @@ describe('DST, a time that never existed Fall Back', () => {
expect(d.valueOf()).toBe(1352005199000)
})
})
it('2012-11-04 01:00:00', () => {
const s = '2012-11-04 01:00:00';
[dayjs, moment].forEach((_) => {
const d = _.tz(s, NY)
expect(d.format()).toBe('2012-11-04T01:00:00-04:00')
expect(d.utcOffset()).toBe(-240)
expect(d.valueOf()).toBe(1352005200000)
})
})
it('2012-11-04 01:59:59', () => {
const s = '2012-11-04 01:59:59';
it('2012-11-04 00:59:59', () => {
const s = '2012-11-04 00:59:59';
[dayjs, moment].forEach((_) => {
const d = _.tz(s, NY)
expect(d.format()).toBe('2012-11-04T01:59:59-04:00')
expect(d.format()).toBe('2012-11-04T00:59:59-04:00')
expect(d.utcOffset()).toBe(-240)
expect(d.valueOf()).toBe(1352008799000)
expect(d.valueOf()).toBe(1352005199000)
})
})

// there's no sense to test "2012-11-04 01:59:59 America/New_York"
// cause it's an invalid date and never exist
// and dayjs result it as "2012-11-04T01:59:00-05:00"

it('2012-11-04 02:00:00', () => {
const s = '2012-11-04 02:00:00';
[dayjs, moment].forEach((_) => {
Expand Down

0 comments on commit a7f858b

Please sign in to comment.