Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions datapoint/Forecast.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import sys

class Forecast(object):
def __init__(self, api_key=""):
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/forecast_test.py
Original file line number Diff line number Diff line change
@@ -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()