Skip to content

Commit

Permalink
DBC22-3081: fixed time capping using the wrong timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-oxd committed Dec 9, 2024
1 parent 5795ead commit ce13f8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
18 changes: 16 additions & 2 deletions src/backend/apps/event/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import logging
from zoneinfo import ZoneInfo

import pytz
from apps.event.enums import EVENT_DIFF_FIELDS, EVENT_STATUS, EVENT_UPDATE_FIELDS
from apps.event.models import Event
from apps.event.serializers import EventInternalSerializer
Expand Down Expand Up @@ -81,6 +83,14 @@ def populate_event_from_data(new_event_data):
event_serializer.save()


def cap_time_to_now(datetime_value):
# Use current time instead of future time
if datetime_value and datetime_value > datetime.datetime.now(pytz.utc):
return datetime.datetime.now(pytz.timezone('America/Vancouver'))

return datetime_value


def populate_all_event_data():
client = FeedClient()
closures, chain_ups = client.get_dit_event_dict()
Expand Down Expand Up @@ -112,8 +122,12 @@ def populate_all_event_data():
# DBC22-3081 replace timezone with DIT API data
if cars_data['timezone']:
new_tz = ZoneInfo(cars_data['timezone'])
event_data["first_created"] = event_data["first_created"].replace(tzinfo=new_tz)
event_data["last_updated"] = event_data["last_updated"].replace(tzinfo=new_tz)

first_created_time = event_data["first_created"].replace(tzinfo=new_tz)
event_data["first_created"] = cap_time_to_now(first_created_time)

last_updated_time = event_data["last_updated"].replace(tzinfo=new_tz)
event_data["last_updated"] = cap_time_to_now(last_updated_time)

# Populate db obj
populate_event_from_data(event_data)
Expand Down
7 changes: 0 additions & 7 deletions src/backend/apps/feed/fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import datetime

import pytz
from apps.shared.helpers import parse_and_localize_time_str
from django.contrib.gis.geos import Point
from rest_framework import serializers
Expand Down Expand Up @@ -39,10 +36,6 @@ def __init__(self, custom_field_name, *args, **kwargs):
def to_internal_value(self, value):
datetime_value = super().to_internal_value(value)

# Use current time instead of future time
if datetime_value and datetime_value > datetime.datetime.now(pytz.utc):
datetime_value = datetime.datetime.now(pytz.timezone('America/Vancouver'))

res = {
self.custom_field_name: datetime_value,
}
Expand Down

0 comments on commit ce13f8f

Please sign in to comment.