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

Fix Booking View with daylight savings #306

Merged
merged 10 commits into from
Mar 13, 2024
Merged

Conversation

MelissaAutumn
Copy link
Member

Fixes #289

We should no longer have varying schedule times based on when daylight savings hits
image

The real fix here is adjusting the start/end times from UTC to the subscriber's timezone based on the date the schedule object was changed. I believe we only ever update the schedule in a scenario when the schedule also updates so this should work. If that changes we'll have to add a schedule_changed_at datetime.

cc @malini

…ezone (based on day to account for daylight savings), and fix the frontend from assuming it's utc time.
…where the start of the month is PST and the end is PDT both with the proper times. (And fix the missing timezone string check.)
@MelissaAutumn MelissaAutumn self-assigned this Mar 12, 2024
@@ -11,13 +12,11 @@
from google.oauth2.credentials import Credentials
from icalendar import Calendar, Event, vCalAddress, vText
from datetime import datetime, timedelta, timezone, UTC
from zoneinfo import ZoneInfo
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had ruff set to run on save. Hence the unused import removals, and re-appearence of the zoneinfo.

def start_time_local(self) -> datetime.time:
"""Start Time in the Schedule's Calendar's Owner's timezone"""
time_of_save = self.time_updated.replace(hour=self.start_time.hour, minute=self.start_time.minute, second=0)
return time_of_save.astimezone(zoneinfo.ZoneInfo(self.calendar.owner.timezone)).time()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we save start_time we convert it and store it as UTC. To get back their original intended start time we simply grab the date the user saved their schedule, append the UTC start time and convert that back to their timezone.

owner = relationship("Subscriber", back_populates="calendars")
appointments = relationship("Appointment", cascade="all,delete", back_populates="calendar")
schedules = relationship("Schedule", cascade="all,delete", back_populates="calendar")
owner: Subscriber = relationship("Subscriber", back_populates="calendars")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got annoyed at the lack of type hinting.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, thanks for the types. And sorry for not doing it right from the start 😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries! 😄

# Based off the farthest_booking our latest slot is 4:30pm
assert slots[-1]['start'] == '2024-04-15T16:30:00'
# Note: This should be in PDT (Pacific Daylight Time)
assert slots[-1]['start'] == '2024-03-15T16:30:00-07:00'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if we need a more exhaustive test here. make_pro_subscriber creates a subscriber (using faker data) and is hardcoded to America/Vancouver for the timezone. Since daylight savings did happen this year this shouldn't break if we remove daylight savings from BC going forward.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me. Since we (you) just had a perfect example for DST, makes totally sense to just take that as testing example 👍🏻

@MelissaAutumn
Copy link
Member Author

...this test should actually not be passing on my local machine. Looking into it.

@MelissaAutumn
Copy link
Member Author

datetimes without any timezone information will automatically take the system's timezone rather than utc. Duh. So I've fixed it for the conversion code.

Copy link
Collaborator

@devmount devmount left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix, thanks Mel.

owner = relationship("Subscriber", back_populates="calendars")
appointments = relationship("Appointment", cascade="all,delete", back_populates="calendar")
schedules = relationship("Schedule", cascade="all,delete", back_populates="calendar")
owner: Subscriber = relationship("Subscriber", back_populates="calendars")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, thanks for the types. And sorry for not doing it right from the start 😬

backend/src/appointment/database/models.py Outdated Show resolved Hide resolved
# Based off the farthest_booking our latest slot is 4:30pm
assert slots[-1]['start'] == '2024-04-15T16:30:00'
# Note: This should be in PDT (Pacific Daylight Time)
assert slots[-1]['start'] == '2024-03-15T16:30:00-07:00'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me. Since we (you) just had a perfect example for DST, makes totally sense to just take that as testing example 👍🏻

@MelissaAutumn MelissaAutumn merged commit cfa8f6c into main Mar 13, 2024
2 checks passed
@MelissaAutumn MelissaAutumn deleted the features/289-timezones branch March 13, 2024 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert dates to subscriber's timezone
2 participants