From 76e20a5928746ed439daea20dd22c1f5417479f2 Mon Sep 17 00:00:00 2001 From: bcgov-brwang <87880048+bcgov-brwang@users.noreply.github.com> Date: Mon, 16 Dec 2024 11:51:53 -0800 Subject: [PATCH 1/3] DBC22-3158: used timezone data coming from DIT --- .../event/migrations/0020_event_timezone.py | 18 +++++++++++ src/backend/apps/event/models.py | 3 ++ src/backend/apps/event/tasks.py | 3 +- .../src/Components/map/panels/EventPanel.js | 4 +-- .../src/Components/shared/FriendlyTime.js | 30 ++++++++++--------- 5 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 src/backend/apps/event/migrations/0020_event_timezone.py diff --git a/src/backend/apps/event/migrations/0020_event_timezone.py b/src/backend/apps/event/migrations/0020_event_timezone.py new file mode 100644 index 000000000..2aec9b022 --- /dev/null +++ b/src/backend/apps/event/migrations/0020_event_timezone.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.16 on 2024-12-16 17:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('event', '0019_event_polygon'), + ] + + operations = [ + migrations.AddField( + model_name='event', + name='timezone', + field=models.CharField(blank=True, max_length=32, null=True), + ), + ] diff --git a/src/backend/apps/event/models.py b/src/backend/apps/event/models.py index a24e182b2..4bee8a471 100644 --- a/src/backend/apps/event/models.py +++ b/src/backend/apps/event/models.py @@ -41,6 +41,9 @@ class Event(BaseModel): start = models.DateTimeField(null=True) end = models.DateTimeField(null=True) + # Timezone + timezone = models.CharField(max_length=32, null=True, blank=True) + def save(self, *args, **kwargs): uses_polygon = self.event_type in ['ROAD_CONDITION'] # chain up not used for now width = 1500 if self.event_type == 'CHAIN_UP' else 2000 diff --git a/src/backend/apps/event/tasks.py b/src/backend/apps/event/tasks.py index e46e02615..bed62a689 100644 --- a/src/backend/apps/event/tasks.py +++ b/src/backend/apps/event/tasks.py @@ -118,7 +118,8 @@ def populate_all_event_data(): event_data["route_at"] = cars_data['route_at'] # DBC22-3081 replace timezone with DIT API data, to be removed if source is corrected - if 'timezone' in cars_data: + if 'timezone' in cars_data and cars_data['timezone']: + event_data['timezone'] = cars_data['timezone'] new_tz = ZoneInfo(cars_data['timezone']) first_created_time = event_data["first_created"].replace(tzinfo=new_tz) diff --git a/src/frontend/src/Components/map/panels/EventPanel.js b/src/frontend/src/Components/map/panels/EventPanel.js index 51850f3a2..829497602 100644 --- a/src/frontend/src/Components/map/panels/EventPanel.js +++ b/src/frontend/src/Components/map/panels/EventPanel.js @@ -67,13 +67,13 @@ export default function EventPanel(props) {

Last update

- +
{eventData.next_update &&

Next update

- +
}
diff --git a/src/frontend/src/Components/shared/FriendlyTime.js b/src/frontend/src/Components/shared/FriendlyTime.js index 45923e46f..16301cf52 100644 --- a/src/frontend/src/Components/shared/FriendlyTime.js +++ b/src/frontend/src/Components/shared/FriendlyTime.js @@ -9,30 +9,32 @@ import OutsideClickHandler from 'react-outside-click-handler'; // Styling import './FriendlyTime.scss'; -const datetimeFormat = { - weekday: 'short', - month: 'short', - day: 'numeric', - hour: 'numeric', - minute: 'numeric', - year: 'numeric', - timeZoneName: 'short', -}; -const formatter = new Intl.DateTimeFormat('en-US', datetimeFormat); const ONE_DAY = 1000 * 60 * 60 * 24; // 24 hours in milliseconds -export const formatDate = (date, isNextUpdate=false) => { +export const formatDate = (date, tz='America/Vancouver', isNextUpdate=false) => { + const datetimeFormat = { + weekday: 'short', + month: 'short', + day: 'numeric', + hour: 'numeric', + minute: 'numeric', + year: 'numeric', + timeZoneName: 'short', + timeZone: tz, + }; + const formatter = new Intl.DateTimeFormat('en-US', datetimeFormat); + // get time difference in milliseconds const timeDiff = (new Date() - new Date(date)); return (isNextUpdate && timeDiff > 0) ? "Update expected as soon as possible, please continue to check back." : formatter.format(new Date(date)); } -export default function FriendlyTime({ date, asDate=false, includeFullIfHumanized=false, timeOnly=false, isNextUpdate=false }) { +export default function FriendlyTime({ date, timezone, asDate=false, includeFullIfHumanized=false, timeOnly=false, isNextUpdate=false }) { const [showTooltip, setShowTooltip] = useState(false); // get time difference in milliseconds const timeDiff = (new Date() - new Date(date)); - const dateFormatted = formatDate(date, isNextUpdate); + const dateFormatted = formatDate(date, timezone, isNextUpdate); // if difference is less than 24hrs const humanize = timeDiff < ONE_DAY; @@ -80,7 +82,7 @@ export default function FriendlyTime({ date, asDate=false, includeFullIfHumanize >
{(isNextUpdate && timeDiff > 0) ? "Update expected as soon as possible, please continue to check back." : - + }
{ !(isNextUpdate && timeDiff > 0) && From db6b36c31860732534b457e3cf80db8d973aa870 Mon Sep 17 00:00:00 2001 From: bcgov-brwang <87880048+bcgov-brwang@users.noreply.github.com> Date: Thu, 2 Jan 2025 11:48:08 -0800 Subject: [PATCH 2/3] DBC22-3158: updated timezone for chain ups DBC22-3158: updated timezone for chain ups --- src/backend/apps/event/tasks.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/backend/apps/event/tasks.py b/src/backend/apps/event/tasks.py index bed62a689..a90bc5c76 100644 --- a/src/backend/apps/event/tasks.py +++ b/src/backend/apps/event/tasks.py @@ -116,10 +116,9 @@ def populate_all_event_data(): event_data["start_point_linear_reference"] = cars_data.get('start_point_linear_reference', None) if 'route_at' in cars_data and cars_data['route_at'] != '': event_data["route_at"] = cars_data['route_at'] - + event_data['timezone'] = cars_data['timezone'] # DBC22-3081 replace timezone with DIT API data, to be removed if source is corrected if 'timezone' in cars_data and cars_data['timezone']: - event_data['timezone'] = cars_data['timezone'] new_tz = ZoneInfo(cars_data['timezone']) first_created_time = event_data["first_created"].replace(tzinfo=new_tz) @@ -148,6 +147,8 @@ def populate_all_event_data(): for chain_up in chain_ups: active_event_ids.append(chain_up.validated_data['id']) chain_up.save() + tz=chain_up.validated_data['first_created'].tzinfo.key + Event.objects.filter(id=chain_up.validated_data['id']).update(timezone=tz) # Purge events absent in the feed Event.objects.filter(status=EVENT_STATUS.ACTIVE)\ From 89e06930b55477f70a1c1471896c424aa57bb314 Mon Sep 17 00:00:00 2001 From: bcgov-brwang <87880048+bcgov-brwang@users.noreply.github.com> Date: Fri, 3 Jan 2025 14:12:05 -0800 Subject: [PATCH 3/3] DBC22-3158: rework done DBC22-3158: rework done --- src/backend/apps/event/serializers.py | 3 +++ src/backend/apps/event/tasks.py | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/backend/apps/event/serializers.py b/src/backend/apps/event/serializers.py index bfe518880..83ded23fe 100644 --- a/src/backend/apps/event/serializers.py +++ b/src/backend/apps/event/serializers.py @@ -207,6 +207,9 @@ def to_internal_value(self, data): # CARS API does not offer any time of creation, or start/end times data['first_created'] = data['last_updated'] + first_created = data['first_created'] + if first_created and first_created.tzinfo: + data['timezone'] = first_created.tzinfo.key data['start'] = data['last_updated'] if 'next-update-time' in data: diff --git a/src/backend/apps/event/tasks.py b/src/backend/apps/event/tasks.py index a90bc5c76..2271060f6 100644 --- a/src/backend/apps/event/tasks.py +++ b/src/backend/apps/event/tasks.py @@ -116,10 +116,10 @@ def populate_all_event_data(): event_data["start_point_linear_reference"] = cars_data.get('start_point_linear_reference', None) if 'route_at' in cars_data and cars_data['route_at'] != '': event_data["route_at"] = cars_data['route_at'] - event_data['timezone'] = cars_data['timezone'] # DBC22-3081 replace timezone with DIT API data, to be removed if source is corrected if 'timezone' in cars_data and cars_data['timezone']: new_tz = ZoneInfo(cars_data['timezone']) + event_data['timezone'] = cars_data['timezone'] first_created_time = event_data["first_created"].replace(tzinfo=new_tz) event_data["first_created"] = cap_time_to_now(first_created_time) @@ -134,6 +134,8 @@ def populate_all_event_data(): if "end" in event_data: pacific_end_time = event_data["end"].astimezone(ZoneInfo('America/Vancouver')) event_data["end"] = pacific_end_time.replace(tzinfo=new_tz) + else: + event_data['timezone'] = 'America/Vancouver' # Populate db obj populate_event_from_data(event_data) @@ -147,8 +149,6 @@ def populate_all_event_data(): for chain_up in chain_ups: active_event_ids.append(chain_up.validated_data['id']) chain_up.save() - tz=chain_up.validated_data['first_created'].tzinfo.key - Event.objects.filter(id=chain_up.validated_data['id']).update(timezone=tz) # Purge events absent in the feed Event.objects.filter(status=EVENT_STATUS.ACTIVE)\