Skip to content

Commit

Permalink
Catch another time error during DST change
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Oct 31, 2016
1 parent 7b86a51 commit a9bb0c6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
15 changes: 4 additions & 11 deletions src/pywws/Plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@
from pywws import DataStore
from pywws import Localisation
from pywws.Logger import ApplicationLogger
from pywws.TimeZone import Local, utc
from pywws.TimeZone import Local, local_utc_offset, utc

class GraphNode(object):
def __init__(self, node):
Expand Down Expand Up @@ -596,19 +596,12 @@ def __init__(self, params, status, raw_data, hourly_data,
raise RuntimeError(
'Directory "' + self.work_dir + '" does not exist.')

def _local_offset(self, time):
try:
result = Local.utcoffset(time)
except pytz.InvalidTimeError:
result = Local.utcoffset(time + HOUR)
return result

def _eval_time(self, time_str):
# get timestamp of last data item
result = self.hourly_data.before(datetime.max)
if not result:
result = datetime.utcnow() # only if no hourly data
result += self._local_offset(result)
result += local_utc_offset(result)
# set to start of the day
result = result.replace(hour=0, minute=0, second=0, microsecond=0)
# apply time string
Expand Down Expand Up @@ -650,7 +643,7 @@ def DoPlot(self, input_file, output_file):
self.x_hi = self.hourly_data.before(datetime.max)
if not self.x_hi:
self.x_hi = datetime.utcnow() # only if no hourly data
self.x_hi += self._local_offset(self.x_hi)
self.x_hi += local_utc_offset(self.x_hi)
if self.duration < timedelta(hours=6):
# set end of graph to start of the next minute after last item
self.x_hi += timedelta(seconds=55)
Expand All @@ -660,7 +653,7 @@ def DoPlot(self, input_file, output_file):
self.x_hi += timedelta(minutes=55)
self.x_hi = self.x_hi.replace(minute=0, second=0)
self.x_lo = self.x_hi - self.duration
self.utcoffset = self._local_offset(self.x_hi)
self.utcoffset = local_utc_offset(self.x_hi)
# open gnuplot command file
self.tmp_files = []
cmd_file = os.path.join(self.work_dir, 'plot.cmd')
Expand Down
6 changes: 3 additions & 3 deletions src/pywws/Tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from pywws.calib import Calib
from pywws import Plot
from pywws import Template
from pywws.TimeZone import STDOFFSET, Local
from pywws.TimeZone import STDOFFSET, local_utc_offset
from pywws.toservice import ToService
from pywws import Upload
from pywws import WindRose
Expand Down Expand Up @@ -99,7 +99,7 @@ def __init__(self, params, status,
self.cron[section].get_prev()
last_update = self.status.get_datetime('last update', section)
if last_update:
last_update = last_update + Local.utcoffset(last_update)
last_update = last_update + local_utc_offset(last_update)
while self.cron[section].get_current(datetime) <= last_update:
self.cron[section].get_next()
# create service uploader objects
Expand Down Expand Up @@ -279,7 +279,7 @@ def _do_cron(self, live_data=None):
if not now:
now = datetime.utcnow()
# convert to local time
local_now = now + Local.utcoffset(now)
local_now = now + local_utc_offset(now)
# get list of due sections
sections = []
for section in self.cron:
Expand Down
11 changes: 11 additions & 0 deletions src/pywws/TimeZone.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,24 @@
import pytz
import tzlocal

from pywws.constants import HOUR

utc = pytz.utc
Local = tzlocal.get_localzone()

_now = datetime.now(tz=Local)
STDOFFSET = _now.utcoffset() - _now.dst()
del _now

def local_utc_offset(time):
try:
result = Local.utcoffset(time)
except pytz.InvalidTimeError:
result = Local.utcoffset(time + HOUR)
except pytz.AmbiguousTimeError:
result = Local.utcoffset(time - HOUR)
return result

def main():
print datetime.now().strftime('%Y/%m/%d %H:%M %Z')
print datetime.now(utc).strftime('%Y/%m/%d %H:%M %Z')
Expand Down
4 changes: 2 additions & 2 deletions src/pywws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '16.10.0'
_release = '1363'
_commit = 'aa707bc'
_release = '1364'
_commit = '7b86a51'

0 comments on commit a9bb0c6

Please sign in to comment.