Skip to content

Commit

Permalink
Fix DST issue (closes #972, closes #992) (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp committed Dec 10, 2018
1 parent bc7f0c9 commit efdbf3b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
12 changes: 12 additions & 0 deletions scripts/test/dst.sh
@@ -0,0 +1,12 @@
#!/bin/bash

# The script runs the DST tests.
#
# It's a part of the test process.

set -ex

export PATH="$(yarn bin):$PATH"

env TZ=America/Sao_Paulo babel-node ./test/dst/toDate/basic.js
env TZ=Pacific/Apia babel-node ./test/dst/toDate/samoa.js
2 changes: 2 additions & 0 deletions scripts/test/travis.sh
Expand Up @@ -21,6 +21,8 @@ then

yarn test -- --single-run

./scripts/test/dst.sh

prebuild
./scripts/test/tz.sh

Expand Down
19 changes: 14 additions & 5 deletions src/toDate/index.js
Expand Up @@ -186,11 +186,20 @@ export default function toDate(argument, dirtyOptions) {
return new Date(NaN)
}
} else {
// get offset accurate to hour in timezones that change offset
offset = getTimezoneOffsetInMilliseconds(new Date(timestamp + time))
offset = getTimezoneOffsetInMilliseconds(
new Date(timestamp + time + offset)
)
var fullTime = timestamp + time
var fullTimeDate = new Date(fullTime)

offset = getTimezoneOffsetInMilliseconds(fullTimeDate)

// Adjust time when it's coming from DST
var fullTimeDateNextDay = new Date(fullTime)
fullTimeDateNextDay.setDate(fullTimeDate.getDate() + 1)
var offsetDiff =
getTimezoneOffsetInMilliseconds(fullTimeDateNextDay) -
getTimezoneOffsetInMilliseconds(fullTimeDate)
if (offsetDiff > 0) {
offset += offsetDiff
}
}

return new Date(timestamp + time + offset)
Expand Down
30 changes: 30 additions & 0 deletions test/dst/toDate/basic.js
@@ -0,0 +1,30 @@
// This is basic DST test for toDate

import toDate from '../../../src/toDate'
import assert from 'assert'

if (process.env.TZ !== 'America/Sao_Paulo')
throw new Error('The test must be run with TZ=America/Sao_Paulo')

if (parseInt(process.version.match(/^v(\d+)\./)[1]) < 10)
throw new Error('The test must be run on Node.js version >= 10')

// Test DST start edge
assert.equal(toDate('2018-11-03').getDate(), 3)
assert.equal(toDate('2018-11-04').getDate(), 4) // DST start
assert.equal(toDate('2018-11-05').getDate(), 5)

// Test DST end edge
assert.equal(toDate('2019-02-15').getDate(), 15)
assert.equal(toDate('2019-02-16').getDate(), 16) // DST end
assert.equal(toDate('2019-02-17').getDate(), 17)

// Test creation of nonexistent time
assert.equal(
toDate('2018-11-04T00:00').toString(),
'Sun Nov 04 2018 01:00:00 GMT-0200 (Brasilia Summer Time)'
)
assert.equal(
toDate('2018-11-04T00:30').toString(),
'Sun Nov 04 2018 01:30:00 GMT-0200 (Brasilia Summer Time)'
)
16 changes: 16 additions & 0 deletions test/dst/toDate/samoa.js
@@ -0,0 +1,16 @@
// This is basic DST test for toDate

import toDate from '../../../src/toDate'
import assert from 'assert'

if (process.env.TZ !== 'Pacific/Apia')
throw new Error('The test must be run with TZ=Pacific/Apia')

if (parseInt(process.version.match(/^v(\d+)\./)[1]) < 10)
throw new Error('The test must be run on Node.js version >= 10')

assert.equal(toDate('2011-12-30').getDate(), 31)
assert.equal(
toDate('2011-12-30T03:30').toString(),
'Sat Dec 31 2011 03:30:00 GMT+1400 (Apia Daylight Time)'
)

0 comments on commit efdbf3b

Please sign in to comment.