Skip to content

NO TICKET: update all datetime fields to be timezone aware in the database and schemas#65

Merged
jirhiker merged 6 commits into
pre-productionfrom
jab-utc-datetimes
Aug 8, 2025
Merged

NO TICKET: update all datetime fields to be timezone aware in the database and schemas#65
jirhiker merged 6 commits into
pre-productionfrom
jab-utc-datetimes

Conversation

@jacob-a-brown

@jacob-a-brown jacob-a-brown commented Aug 7, 2025

Copy link
Copy Markdown
Contributor

Why

This PR addresses the following problem / context:

  • In the 2025-08-07 WDI Dev Team Rendezvous we decided that all datetime records will be stored in UTC. The frontend will convert datetimes to the appropriate time zone(s) for display (probably MT)

How

Implementation summary - the following was changed / added / removed:

  • For the database models...
    • set all DateTime columns to DateTime(timezone=True)
    • set all TIMESTAMP columns to TIMESTAMP(timezone=True)
    • for all columns where a server_default may be called I changed func.now() to func.timezone("UTC", func.now() to ensure that the time zone is UTC. It defaults to the time zone of the database for func.now() so this change ensures the stored time zone is UTC if that is not how it is set in the db.
  • For the Pydantic schemas...
    • used pydantic.AwareDatetime instead of datetime.datetime to require time zone aware fields to be passed and returned
    • ensured that any user-defined datetime fields must not be in the future by using Pydantic's Annotated field validations
    • converted user-defined datetime fields to UTC if not already in UTC

Notes

Any special considerations, workarounds, or follow-up work to note?

  • since all datetime field responses are UTC this uses the Z notation
  • did I miss any models or fields?
    • the sensor model has yet to be refactored, so that was not updated
  • the time zone must be set by the application layer. Because something(s) may slip through the cracks, should we convert AwareDatetime responses to UTC? Just to be extra safe.

@chasetmartin chasetmartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I tried to replicate the error I was getting on sample edit, and this fixed the issue. The response is returning the timezone, so even when the user does no editing before save, the front end now send back the expected format via the roundtrip. 👍

One question: is PastDatetime a built in Pydantic validator to check that the datetime is not in the future?

@jacob-a-brown

Copy link
Copy Markdown
Contributor Author

PastDatetime is a built-in Pydantic validator (and type) to ensure that the supplied datetime is not in the future. This is what an error looks like from it (status code is still 422)

image

@jirhiker jirhiker merged commit 17aad54 into pre-production Aug 8, 2025
3 checks passed

@ksmuczynski ksmuczynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me!

@jirhiker jirhiker deleted the jab-utc-datetimes branch December 3, 2025 04:57
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.

4 participants