Skip to content

Commit

Permalink
Apply timezone offset specific to date rather than standard offset
Browse files Browse the repository at this point in the history
We now check the timezone offset for each timestamp rather than using
a standard offset.

(as part of a series of workarounds to deal with cal-heatmap's lack
of UTC support)
  • Loading branch information
glutanimate committed Nov 5, 2018
1 parent c92382c commit 5bdec85
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions resources/web/review-heatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,16 @@ function onHmContrib(event, button) {
// Date mangling
// ##########################################################################

function stdTimezoneOffset(date) {
// Returns regular time (not DST) timezone offset in hours
// (offset is negative for timezones west of UTC,
// positive for timezones east of UTC)
// Based on: https://stackoverflow.com/a/11888430/1708932
var jan = new Date(date.getFullYear(), 0, 1);
var jul = new Date(date.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
};

// return "zero"-ed local datetime (workaround for lack of UTC time support
// in cal-heatmap)
function applyDateOffset(date) {
var offset = stdTimezoneOffset(date);
return new Date(date.getTime() + offset * 60 * 1000)
return new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000);
}

// return local timezone offset in seconds at given unix timestamp
function tzOffsetByTimestamp(timestamp) {
date = new Date(timestamp * 1000);
return date.getTimezoneOffset() * 60;
}

// Heatmap
Expand Down Expand Up @@ -219,14 +216,12 @@ function initHeatmap(options, data) {
// handler. You will have to take the updated datetime into
// account in that case.
//
// based on a GitHub comment by sergeysolovev
// cf. https://github.com/wa0x6e/cal-heatmap/issues/126
var offset = stdTimezoneOffset(new Date()) * 60;
// adapted from: https://github.com/wa0x6e/cal-heatmap/issues/126
var results = {};
for (var timestamp in timestamps) {
var value = timestamps[timestamp];
timestamp = parseInt(timestamp, 10);
results[timestamp + offset] = value;
results[timestamp + tzOffsetByTimestamp(timestamp)] = value;
};
return results;
},
Expand Down

0 comments on commit 5bdec85

Please sign in to comment.