Skip to content

Commit

Permalink
Update validation functions to use tz aware times
Browse files Browse the repository at this point in the history
  • Loading branch information
dalonsoa committed Jan 9, 2024
1 parent b0dbb68 commit 00547f0
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions validated/functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import calendar
from datetime import date, datetime, time
from datetime import date, datetime, time, tzinfo
from threading import Thread
from typing import Tuple, Union, overload, Sequence, List, Dict, Any
from decimal import Decimal
Expand All @@ -22,18 +22,18 @@


@overload
def set_time_limits(start_time: str, end_time: str) -> Tuple[str, str]:
def set_time_limits(start_time: str, end_time: str, tz: tzinfo) -> Tuple[str, str]:
...


@overload
def set_time_limits(
start_time: datetime, end_time: datetime
start_time: datetime, end_time: datetime, tz: tzinfo
) -> Tuple[datetime, datetime]:
...


def set_time_limits(start_time, end_time):
def set_time_limits(start_time, end_time, tz):
"""Complete the datetime objects to cover a whole day.
Completes the hour in start_date and end_date in order to have a whole day from the
Expand All @@ -46,15 +46,13 @@ def set_time_limits(start_time, end_time):
Returns:
A tuple with the same objects updated
"""
if isinstance(start_time, date):
start_time = datetime.combine(start_time, time(0, 0, 0, 0))
elif isinstance(start_time, str):
start_time = start_time + " 00:00:00"
if isinstance(start_time, str):
start_time = date(*[int(val) for val in start_time.split("-")])
if isinstance(end_time, str):
end_time = date(*[int(val) for val in end_time.split("-")])

if isinstance(end_time, date):
end_time = datetime.combine(end_time, time(23, 59, 59, 999999))
elif isinstance(end_time, str):
end_time = end_time + " 23:59:59"
start_time = datetime.combine(start_time, time(0, 0, 0, 0)).replace(tzinfo=tz)
end_time = datetime.combine(end_time, time(23, 59, 59, 999999)).replace(tzinfo=tz)

return start_time, end_time

Expand Down Expand Up @@ -636,10 +634,8 @@ def daily_report(
maximum: Maximum value expected for the variable.
minimum: Minimum value expected for the variable.
"""
start_time, end_time = set_time_limits(start_time, end_time)
tz = zoneinfo.ZoneInfo(station.timezone)
start_time = start_time.replace(tzinfo=tz)
end_time = end_time.replace(tzinfo=tz)
start_time, end_time = set_time_limits(start_time, end_time, tz)
(
measurement,
validated,
Expand Down Expand Up @@ -740,8 +736,8 @@ def detail_list(
A dictionary response for the detailed day table containing the report
of values and statistics, indicators, time series for plot, etc.
"""
start_time, end_time = set_time_limits(date_of_interest, date_of_interest)

tz = zoneinfo.ZoneInfo(station.timezone)
start_time, end_time = set_time_limits(date_of_interest, date_of_interest, tz)
(
measurement,
validated,
Expand Down Expand Up @@ -879,11 +875,9 @@ def save_to_validated(
True if the data saving to validation succeeds.
"""
validated = apps.get_model(app_label="validated", model_name=variable.variable_code)

start_date, end_date = set_time_limits(start_date, end_date)
tz = zoneinfo.ZoneInfo(station.timezone)
start_date = start_date.replace(tzinfo=tz)
end_date = end_date.replace(tzinfo=tz)
start_date, end_date = set_time_limits(start_date, end_date, tz)

_, _, _, selected, _, value_fields = preprocessing(
station, variable, start_date, end_date, minimum, maximum
)
Expand Down Expand Up @@ -983,8 +977,8 @@ def data_report(
raise ValueError(
f"Invalid temporality: {temporality}. Valid values are: {', '.join(valid)}"
)

start_time, end_time = set_time_limits(start_time, end_time)
tz = zoneinfo.ZoneInfo(station.timezone)
start_time, end_time = set_time_limits(start_time, end_time, tz)
data = (
apps.get_model(app_label=temporality, model_name=variable.variable_code)
.objects.filter(
Expand Down Expand Up @@ -1132,13 +1126,15 @@ def calculate_hourly(variable: Variable) -> None:

while register:
delta_t = DeltaT.objects.get(station__station_id=register.station_id)

tz = zoneinfo.ZoneInfo(
Station.objects.get(station_id=register.station_id).timezone
)
start_of_hour = datetime.combine(
register.time, time(register.time.hour, 0, 0, 0)
)
).replace(tzinfo=tz)
end_of_hour = datetime.combine(
register.time, time(start_of_hour.hour, 59, 59, 999999)
)
).replace(tzinfo=tz)
validated_block = validated.objects.filter(
station_id=register.station_id,
time__gte=start_of_hour,
Expand Down Expand Up @@ -1193,9 +1189,15 @@ def calculate_daily(variable):
if not register:
register_exists = False
return

start_of_day = datetime.combine(register.time, time(0, 0, 0, 0))
end_of_day = datetime.combine(register.time, time(23, 59, 59, 999999))
tz = zoneinfo.ZoneInfo(
Station.objects.get(station_id=register.station_id).timezone
)
start_of_day = datetime.combine(register.time, time(0, 0, 0, 0)).replace(
tzinfo=tz
)
end_of_day = datetime.combine(register.time, time(23, 59, 59, 999999)).replace(
tzinfo=tz
)
hourly_block = Hourly.objects.filter(
station_id=register.station_id, time__gte=start_of_day, time__lte=end_of_day
)
Expand Down Expand Up @@ -1254,12 +1256,16 @@ def calculate_monthly(variable):
if not register:
register_exists = False
return

start_of_month = datetime(register.time.year, register.time.month, 1, 0, 0)
tz = zoneinfo.ZoneInfo(
Station.objects.get(station_id=register.station_id).timezone
)
start_of_month = datetime(
register.time.year, register.time.month, 1, 0, 0
).replace(tzinfo=tz)
last_day = calendar.monthrange(register.time.year, register.time.month)[1]
end_of_month = datetime(
register.time.year, register.time.month, last_day, 23, 59
)
).replace(tzinfo=tz)
daily_block = Daily.objects.filter(
station_id=register.station_id,
time__gte=start_of_month,
Expand Down

0 comments on commit 00547f0

Please sign in to comment.