Skip to content

Commit

Permalink
Avoid AmbiguousTimeError errors at end of DST
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 25, 2015
1 parent ef2bb67 commit 8b20e9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/pywws/Plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@
import sys
import xml.dom.minidom

import pytz

from .constants import HOUR
from .conversions import *
from . import DataStore
from . import Localisation
Expand Down Expand Up @@ -520,12 +523,19 @@ def __init__(self, params, status, raw_data, hourly_data,
if not os.path.isdir(self.work_dir):
os.makedirs(self.work_dir)

def _local_offset(self, time):
try:
result = Local.utcoffset(time)
except pytz.AmbiguousTimeError:
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 += Local.utcoffset(result)
result += self._local_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 @@ -567,7 +577,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 += Local.utcoffset(self.x_hi)
self.x_hi += self._local_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 @@ -577,7 +587,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 = Local.utcoffset(self.x_hi)
self.utcoffset = self._local_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/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '15.10.0.dev1314'
_release = '1314'
_commit = 'b44bfff'
__version__ = '15.10.0.dev1315'
_release = '1315'
_commit = 'ef2bb67'

0 comments on commit 8b20e9d

Please sign in to comment.