diff --git a/datapoint/Forecast.py b/datapoint/Forecast.py index 197c028..6ca6e4e 100644 --- a/datapoint/Forecast.py +++ b/datapoint/Forecast.py @@ -1,4 +1,5 @@ import datetime +import sys class Forecast(object): def __init__(self, api_key=""): @@ -14,14 +15,24 @@ def __init__(self, api_key=""): self.elevation = None self.days = [] + def timedelta_total_seconds(self, timedelta): + return ( + timedelta.microseconds + 0.0 + + (timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6 + def now(self): """ Function to return just the current timestep from this forecast """ now = None d = datetime.datetime.utcnow() - msm = (d - d.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 60 - if self.days[0].date == d.strftime("%Y-%m-%dZ"): + for_total_seconds = d - d.replace(hour=0, minute=0, second=0, microsecond=0) + # python 2.6 does not have timedelta.total_seconds() + if sys.version_info < (2,7): + msm = self.timedelta_total_seconds(for_total_seconds) / 60 + else: + msm = for_total_seconds.total_seconds() / 60 + if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"): for timestep in self.days[0].timesteps: if timestep.name > msm: break diff --git a/tests/unit/forecast_test.py b/tests/unit/forecast_test.py new file mode 100644 index 0000000..31c8a43 --- /dev/null +++ b/tests/unit/forecast_test.py @@ -0,0 +1,25 @@ +from types import * +from nose.tools import * + +import datetime + +import datapoint + + + +class TestForecast: + + def __init__(self): + self.forecast = datapoint.Forecast.Forecast() + + def test_forecast_now_works(self): + test_day = datapoint.Day.Day() + test_day.date = datetime.datetime.utcnow() + + test_timestep = datapoint.Timestep.Timestep() + test_timestep.name = 1 + + test_day.timesteps.append(test_timestep) + + self.forecast.days.append(test_day) + assert self.forecast.now()