Skip to content

Commit

Permalink
take timezone offset into account to fix today view. stick to utc for…
Browse files Browse the repository at this point in the history
… those ahead of utc... see #134 (needs a better fix)
  • Loading branch information
dannyvankooten committed Sep 21, 2018
1 parent 5507fbc commit bbdf67f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
8 changes: 4 additions & 4 deletions assets/src/js/components/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ function timeFormatPicker(n) {

function prepareData(startUnix, endUnix, data) {
// add timezone offset back in to get local start date
//const timezoneOffset = (new Date()).getTimezoneOffset() * 60;
let startDate = new Date((startUnix) * 1000);
let endDate = new Date((endUnix) * 1000);
const timezoneOffset = (new Date()).getTimezoneOffset() * 60;
let startDate = new Date((startUnix + timezoneOffset) * 1000);
let endDate = new Date((endUnix + timezoneOffset) * 1000);
let datamap = [];
let newData = [];

Expand All @@ -65,7 +65,7 @@ function prepareData(startUnix, endUnix, data) {
d = data[i];
// replace date with actual date object & store in datamap
dateParts = d.Date.split('T')[0].split('-');
date = new Date(Date.UTC(dateParts[0], dateParts[1]-1, dateParts[2], 0, 0, 0))
date = new Date(dateParts[0], dateParts[1]-1, dateParts[2], 0, 0, 0)
key = date.getFullYear() + "-" + padZero(date.getMonth() + 1) + "-" + padZero(date.getDate());
d.Date = date;
datamap[key] = d;
Expand Down
41 changes: 28 additions & 13 deletions assets/src/js/components/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,72 @@ import { h, Component } from 'preact';
import { bind } from 'decko';
import Pikadayer from './Pikadayer.js';

const defaultPeriod = 'last-7-days';
const padZero = function(n){return n<10? '0'+n:''+n;}

function getNow() {
let now = new Date()
let tzOffset = now.getTimezoneOffset() * 60 * 1000;

// if we're ahead of UTC, stick to UTC's "now"
// this is ugly but a sad necessity for now because we store and aggregate statistics using UTC dates (without time data)
// For those ahead of UTC, "today" will always be empty if they're checking early on in their day
// see https://github.com/usefathom/fathom/issues/134
if (tzOffset < 0) {
now.setTime(now.getTime() + tzOffset )
}

return now
}

// today, yesterday, this week, last 7 days, last 30 days
const availablePeriods = {
'today': {
label: 'Today',
start: function() {
const now = new Date();
const now = getNow();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
},
end: function() {
const now = new Date();
const now = getNow();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
},
},
'last-7-days': {
label: 'Last 7 days',
start: function() {
const now = new Date();
const now = getNow();
return new Date(now.getFullYear(), now.getMonth(), now.getDate()-6);
},
end: function() {
const now = new Date();
const now = getNow();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
},
},
'last-30-days': {
label: 'Last 30 days',
start: function() {
const now = new Date();
const now = getNow();
return new Date(now.getFullYear(), now.getMonth(), now.getDate()-29);
},
end: function() {
const now = new Date();
const now = getNow();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
},
},
'this-year': {
label: 'This year',
start: function() {
const now = new Date();
const now = getNow();
return new Date(now.getFullYear(), 0, 1);
},
end: function() {
const now = new Date();
const now = getNow();
return new Date(this.start().getFullYear() + 1, 0, 0);
},
},
}

const defaultPeriod = 'last-7-days';
const padZero = function(n){return n<10? '0'+n:''+n;}

class DatePicker extends Component {
constructor(props) {
super(props)
Expand Down Expand Up @@ -95,8 +109,9 @@ class DatePicker extends Component {

// create unix timestamps from local date objects
let before, after;
before = Math.round((+endDate) / 1000);
after = Math.round((+startDate) / 1000);
const timezoneOffset = (new Date()).getTimezoneOffset() * 60;
before = Math.round((+endDate) / 1000) - timezoneOffset;
after = Math.round((+startDate) / 1000) - timezoneOffset;

this.setState({
period: period || '',
Expand Down

0 comments on commit bbdf67f

Please sign in to comment.