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

refactor: datetime -> Aware for created/last_modified #1155

Merged
merged 14 commits into from
Dec 13, 2024

Conversation

dbirman
Copy link
Member

@dbirman dbirman commented Nov 12, 2024

This PR refactors the created/last_modified fields to use AwareDatetimeWithDefault to force users to include timezone information or have it added automatically.

Note that there are still a lot of places in the code where people are using datetime.utcnow.isoformat() which returns a naive datetime object without timezone information. Everybody needs to switch to using datetime.now(timezone.utc).isoformat()

@dbirman dbirman linked an issue Nov 12, 2024 that may be closed by this pull request
@dbirman dbirman requested a review from jtyoung84 November 12, 2024 18:30
Copy link
Collaborator

@jtyoung84 jtyoung84 left a comment

Choose a reason for hiding this comment

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

We should also add either a validator to coerce the timezone to utc, or a custom serializer so that the string representation is always in UTC. There is a discussion why it is useful to keep the timestamp string representations in DocDB sortable: https://stackoverflow.com/questions/38497442/what-is-the-best-way-to-store-datetime-value-in-documentdb

@dbirman
Copy link
Member Author

dbirman commented Nov 12, 2024

We should also add either a validator to coerce the timezone to utc, or a custom serializer so that the string representation is always in UTC. There is a discussion why it is useful to keep the timestamp string representations in DocDB sortable: https://stackoverflow.com/questions/38497442/what-is-the-best-way-to-store-datetime-value-in-documentdb

Do you know how times in docdb get the ms information? Just want to make sure the final output is in the right format.

@jtyoung84
Copy link
Collaborator

We should also add either a validator to coerce the timezone to utc, or a custom serializer so that the string representation is always in UTC. There is a discussion why it is useful to keep the timestamp string representations in DocDB sortable: https://stackoverflow.com/questions/38497442/what-is-the-best-way-to-store-datetime-value-in-documentdb

Do you know how times in docdb get the ms information? Just want to make sure the final output is in the right format.

@helen-m-lin Do you know? I'm pretty sure they just take the json we send them. When we write to DocDB, we first run m.model_dump_json() on the pydantic model. It might depend on the version of python unless we add a custom serializer.

@helen-m-lin
Copy link
Contributor

@helen-m-lin Do you know? I'm pretty sure they just take the json we send them. When we write to DocDB, we first run m.model_dump_json() on the pydantic model. It might depend on the version of python

Yeah, the datestamps are being stored as strings in docdb. I'm finding that the serialization is not consistent where some have UTC timezone and some do not. I'll send some examples offline.

@dbirman
Copy link
Member Author

dbirman commented Nov 12, 2024

Sounds like we should add a field_serializer to force it to a specific format?

@jtyoung84
Copy link
Collaborator

Sounds like we should add a field_serializer to force it to a specific format?

Sounds good. Saskia has an example here:

@field_serializer("output_specimen_ids", when_used="json")

@dbirman dbirman requested a review from jtyoung84 November 19, 2024 03:59
@dbirman
Copy link
Member Author

dbirman commented Nov 19, 2024

Ah just noticed that we should update the docs to reflect that timezones are in UTC by default, I'll fix that before we merge this nevermind I think we're good

@saskiad
Copy link
Collaborator

saskiad commented Nov 25, 2024

We don't want to use UTC. we want tz_aware.

@jtyoung84
Copy link
Collaborator

We don't want to use UTC. we want tz_aware.

These are for timestamps specific to what's getting stored in DocDB. We want those in a consistent serialized format to make them sortable.

@dbirman
Copy link
Member Author

dbirman commented Dec 13, 2024

@jtyoung84 @helen-m-lin is this still waiting on something? I think I cleared out all the requested changes

@dbirman dbirman added this pull request to the merge queue Dec 13, 2024
Merged via the queue into dev with commit 7527b27 Dec 13, 2024
5 checks passed
@dbirman dbirman deleted the 1087-last_modified-field-is-losing-its-timestamp-in-docdb branch December 13, 2024 21:46
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.

last_modified field is losing its timestamp in DocDB
4 participants