Fix timezone problems

This commit is contained in:
Neale Pickett
2015-10-20 13:12:44 -06:00
parent 6d2102ab8b
commit 4f02093bc9
+20 -5
View File
@@ -6,6 +6,9 @@ import time
import yaml import yaml
import argparse import argparse
import logging import logging
import datetime
import dateutil.tz
import dateutil.parser
import fitbit import fitbit
from apiclient.discovery import build from apiclient.discovery import build
@@ -35,13 +38,21 @@ def GetGoogleClient(filename):
return client return client
dawnOfTime = datetime.datetime(1970, 1, 1, tzinfo=dateutil.tz.tzutc())
def epochOfFitbitLog(logEntry, tzinfo):
logTimestamp = "{} {}".format(logEntry["date"], logEntry["time"])
logTime = dateutil.parser.parse(logTimestamp).replace(tzinfo=tzinfo)
return (logTime - dawnOfTime).total_seconds()
def nano(val): def nano(val):
"""Converts a number to nano (str).""" """Converts a number to nano (str)."""
return '%d' % (val * 1e9) return '%d' % (val * 1e9)
def FitbitWeightToGoogleWeight(fitbitWeightLog): def FitbitWeightToGoogleWeight(fitbitWeightLog, tzinfo):
logSecs = fitbitWeightLog['logId'] / 1000 logSecs = epochOfFitbitLog(fitbitWeightLog, tzinfo)
logWeightLbs = fitbitWeightLog['weight'] logWeightLbs = fitbitWeightLog['weight']
logWeightKg = logWeightLbs / POUNDS_PER_KILOGRAM logWeightKg = logWeightLbs / POUNDS_PER_KILOGRAM
return dict( return dict(
@@ -77,17 +88,20 @@ def main():
fitbitClient = GetFitbitClient(args.fitbit_creds) fitbitClient = GetFitbitClient(args.fitbit_creds)
userProfile = fitbitClient.user_profile_get()
tzinfo = dateutil.tz.gettz(userProfile['user']['timezone'])
devices = fitbitClient.get_devices() devices = fitbitClient.get_devices()
(scale,) = (device for device in devices if device['type'] == 'SCALE') (scale,) = (device for device in devices if device['type'] == 'SCALE')
fitbitBodyweight = fitbitClient.get_bodyweight(period='1m') fitbitBodyweight = fitbitClient.get_bodyweight(period='1m')
fitbitWeightLogs = fitbitBodyweight['weight'] fitbitWeightLogs = fitbitBodyweight['weight']
fitbitWeightLogTimes = [log['logId'] / 1000 for log in fitbitWeightLogs] fitbitWeightLogTimes = [epochOfFitbitLog(log, tzinfo) for log in fitbitWeightLogs]
minLogNs = nano(min(fitbitWeightLogTimes)) minLogNs = nano(min(fitbitWeightLogTimes))
maxLogNs = nano(max(fitbitWeightLogTimes)) maxLogNs = nano(max(fitbitWeightLogTimes))
googleWeightLogs = [FitbitWeightToGoogleWeight(log) googleWeightLogs = [FitbitWeightToGoogleWeight(log, tzinfo)
for log in fitbitWeightLogs] for log in fitbitWeightLogs]
googleClient = GetGoogleClient(args.google_creds) googleClient = GetGoogleClient(args.google_creds)
@@ -131,7 +145,8 @@ def main():
dataSourceId=dataSourceId, dataSourceId=dataSourceId,
datasetId=datasetId).execute() datasetId=datasetId).execute()
#insert empty 'point' when there is nothing #insert empty 'point' when there is nothing
if 'point' not in ret: ret['point']=[] if 'point' not in ret:
ret['point']=[]
return ret return ret
def PointsDifference(left, right): def PointsDifference(left, right):