Skip to content

Commit dcb9730

Browse files
deparkesjacobtomlinson
authored andcommitted
Fix forecast.now() not working (#26)
* fix forecast.now() not working self.date and datetime were in different formats so never evaluating to TRUE * print stuff for debugging * different return * Revert "fix forecast.now() not working" This reverts commit 015fc18. * Fix forecast.now() not working * Unit test to expose forecast.now() bug * add blank timedelta test * Remove timedelta test * Add fix for python 2.6 * Fix self.timedelta_total_seconds
1 parent b85bfe3 commit dcb9730

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

datapoint/Forecast.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import sys
23

34
class Forecast(object):
45
def __init__(self, api_key=""):
@@ -14,14 +15,24 @@ def __init__(self, api_key=""):
1415
self.elevation = None
1516
self.days = []
1617

18+
def timedelta_total_seconds(self, timedelta):
19+
return (
20+
timedelta.microseconds + 0.0 +
21+
(timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6
22+
1723
def now(self):
1824
"""
1925
Function to return just the current timestep from this forecast
2026
"""
2127
now = None
2228
d = datetime.datetime.utcnow()
23-
msm = (d - d.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 60
24-
if self.days[0].date == d.strftime("%Y-%m-%dZ"):
29+
for_total_seconds = d - d.replace(hour=0, minute=0, second=0, microsecond=0)
30+
# python 2.6 does not have timedelta.total_seconds()
31+
if sys.version_info < (2,7):
32+
msm = self.timedelta_total_seconds(for_total_seconds) / 60
33+
else:
34+
msm = for_total_seconds.total_seconds() / 60
35+
if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"):
2536
for timestep in self.days[0].timesteps:
2637
if timestep.name > msm:
2738
break

tests/unit/forecast_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from types import *
2+
from nose.tools import *
3+
4+
import datetime
5+
6+
import datapoint
7+
8+
9+
10+
class TestForecast:
11+
12+
def __init__(self):
13+
self.forecast = datapoint.Forecast.Forecast()
14+
15+
def test_forecast_now_works(self):
16+
test_day = datapoint.Day.Day()
17+
test_day.date = datetime.datetime.utcnow()
18+
19+
test_timestep = datapoint.Timestep.Timestep()
20+
test_timestep.name = 1
21+
22+
test_day.timesteps.append(test_timestep)
23+
24+
self.forecast.days.append(test_day)
25+
assert self.forecast.now()

0 commit comments

Comments
 (0)