Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBC22-3158: used timezone data coming from DIT #783

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/backend/apps/event/migrations/0020_event_timezone.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
3 changes: 3 additions & 0 deletions src/backend/apps/event/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/backend/apps/event/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/Components/map/panels/EventPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ export default function EventPanel(props) {
<div className="popup__content__block">
<div className="popup__content__description last-update">
<p>Last update</p>
<FriendlyTime date={eventData.last_updated} />
<FriendlyTime date={eventData.last_updated} timezone={eventData.timezone} />
</div>

{eventData.next_update &&
<div className="popup__content__description next-update">
<p>Next update</p>
<FriendlyTime date={eventData.next_update} isNextUpdate={true} />
<FriendlyTime date={eventData.next_update} isNextUpdate={true} timezone={eventData.timezone} />
</div>
}
</div>
Expand Down
30 changes: 16 additions & 14 deletions src/frontend/src/Components/shared/FriendlyTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,7 +82,7 @@ export default function FriendlyTime({ date, asDate=false, includeFullIfHumanize
>
<div className="friendly-time-text">
{(isNextUpdate && timeDiff > 0) ? "Update expected as soon as possible, please continue to check back." :
<ReactTimeAgo date={dt} locale="en-US"/>
<ReactTimeAgo date={dt} verboseDate={dateFormatted} locale="en-US"/>
}
</div>
{ !(isNextUpdate && timeDiff > 0) &&
Expand Down
Loading