forked from spencermountain/spacetime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue spencermountain#182: wrong offset within 1hr of DST start
Solved to Chicken and Egg Problem described in summerTime.js: "we can't get the date, without knowing the timezone, and vice-versa". In order to eliminate the time zone offset effect the given UTC time is compared to the DST change boundaries converted to UTC.
- Loading branch information
Showing
3 changed files
with
14 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,17 @@ | ||
const zeroPad = require('../fns').zeroPad | ||
const MSEC_IN_HOUR = 60 * 60 * 1000; | ||
|
||
const serialize = d => | ||
zeroPad(d.getMonth() + 1) + '/' + zeroPad(d.getDate()) + ':' + zeroPad(d.getHours()) | ||
const toUtc = (dstChange, offset, year) => { | ||
const [month, rest] = dstChange.split('/'); | ||
const [day, hour] = rest.split(':'); | ||
return Date.UTC(year, month - 1, day, hour) - offset * MSEC_IN_HOUR; | ||
}; | ||
|
||
// a timezone will begin with a specific offset in january | ||
// then some will switch to something else between november-march | ||
const shouldChange = (epoch, start, end, defaultOffset) => { | ||
//note: this has a cray order-of-operations issue | ||
//we can't get the date, without knowing the timezone, and vice-versa | ||
//it's possible that we can miss a dst-change by a few hours. | ||
let d = new Date(epoch) | ||
//(try to mediate this a little?) | ||
let bias = d.getTimezoneOffset() || 0 | ||
let shift = bias + defaultOffset * 60 //in minutes | ||
shift = shift * 60 * 1000 //in ms | ||
d = new Date(epoch + shift) | ||
const shouldChange = (epoch, start, end, summerOffset, winterOffset) => { | ||
const year = new Date(epoch).getUTCFullYear(); | ||
const startUtc = toUtc(start, winterOffset, year); | ||
const endUtc = toUtc(end, summerOffset, year); | ||
|
||
let current = serialize(d) | ||
//eg. is it after ~november? | ||
if (current >= start) { | ||
//eg. is it before ~march~ too? | ||
if (current < end) { | ||
return true | ||
} | ||
} | ||
return false | ||
return epoch >= startUtc && epoch < endUtc; | ||
} | ||
|
||
module.exports = shouldChange |