Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #182: wrong offset within 1hr of DST start #233

Merged
merged 2 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const timezone = s => {
if (result.hasDst === false) {
result.current.offset = summer
result.current.isDST = false
} else if (inSummerTime(s.epoch, result.change.start, result.change.back, summer) === true) {
} else if (inSummerTime(s.epoch, result.change.start, result.change.back, summer, winter) === true) {
result.current.offset = summer
result.current.isDST = result.hemisphere === 'North' //dst 'on' in winter in north
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/timezone/quick.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const quickOffset = s => {
dec = jul - 1
}
let split = obj.dst.split('->')
let inSummer = isSummer(s.epoch, split[0], split[1], jul)
let inSummer = isSummer(s.epoch, split[0], split[1], jul, dec)
if (inSummer === true) {
return jul
}
Expand Down
36 changes: 12 additions & 24 deletions src/timezone/summerTime.js
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