7
7
"""
8
8
9
9
import logging
10
- from datetime import datetime
10
+ from datetime import datetime , timezone
11
11
from typing import List , Optional
12
12
13
13
import numpy as np
@@ -106,12 +106,21 @@ def create_partition(
106
106
107
107
108
108
def default_horizon_minutes (context ):
109
+ """Make a default horizon minutes for the ForecastValueSQLMixin"""
109
110
columns = context .get_current_parameters ()
110
111
target_time = columns ["target_time" ]
111
112
created_utc = columns ["created_utc" ]
112
113
113
114
if created_utc is None :
114
- created_utc = datetime .now (tz = target_time .tzinfo )
115
+ created_utc = datetime .now (tz = timezone .utc )
116
+
117
+ if isinstance (target_time , str ):
118
+ target_time = datetime .fromisoformat (target_time )
119
+
120
+ # make sure they both have tz, or not
121
+ if target_time .tzinfo is None :
122
+ created_utc = created_utc .replace (tzinfo = None )
123
+
115
124
delta = (target_time - created_utc ).total_seconds () / 60.0 # convert to minutes
116
125
return delta
117
126
@@ -130,9 +139,11 @@ class ForecastValueSQLMixin(CreatedMixin):
130
139
# Want to keep it as json so that we can store different properties for different forecasts
131
140
properties = Column (MutableDict .as_mutable (JSON ), nullable = True )
132
141
133
- # we want to store the forecast horizon. This is the difference between target_time and created_utc.
134
- # By storing the value, and index, queries should be faster when looking for an N hour forecast.
135
- # I tried to use server_default = "CAST(EXTRACT(EPOCH FROM (target_time - created_utc))/60 as INT)"
142
+ # we want to store the forecast horizon.
143
+ # This is the difference between target_time and created_utc.
144
+ # By storing the value queries should be faster when looking for an N hour forecast.
145
+ # I tried to use
146
+ # server_default = "CAST(EXTRACT(EPOCH FROM (target_time - created_utc))/60 as INT)"
136
147
# but this does not work.
137
148
horizon_minutes = Column (
138
149
Integer ,
0 commit comments