8000 Fixed #34699 -- Added warning about using Trunc functions in filters by coolbootscoder · Pull Request #18660 · django/django · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixed #34699 -- Added warning about using Trunc functions in filters #18660

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

Open
wants to merge 22 commits into 8000
base: main
Choose a base branch
from

Conversation

coolbootscoder
Copy link
@coolbootscoder coolbootscoder commented Oct 9, 2024

Trac ticket number

ticket-34699

Branch description

A warning about using Trunc functions in filters when the timezone is not UTC was added. Tests that replicate the unexpected behavior and demonstrate one of the possible workarounds was added.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

…r when the timezone is not UTC to database-functions. Added tests to confirm that the documentation is correct.
Copy link
@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Hello! Thank you for your contribution 💪

As it's your first contribution be sure to check out the patch review checklist.

If you're fixing a ticket from Trac make sure to set the "Has patch" flag and include a link to this PR in the ticket!

If you have any design or process questions then you can ask in the Django forum.

Welcome aboard ⛵️!

Copy link
Contributor
@ontowhee ontowhee left a comment

Choose a reason for hiding this comment

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

Hello! I reviewed the changes to the date and the warning section. I confirmed that the offset for Melbourne is +11 on 2025-04-01, and the week starts on 2015-06-15 (Monday). I added some questions that came to mind as I was reading through the warning section.

timezone naive value. This can lead to unexp 10000 ected results if you are using
timezones other than UTC. Django will store date/time values in the
database in the UTC timezone. The following example demonstrates what
happens when using the timezone "Europe/Berlin" and how to adjust for this:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is "Europe/Berlin" coming from settings.TIME_ZONE?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I am referring to the time zone in the settings file. I selected "Europe/Berlin" because the ticket's reporter had used that time zone.

... trunc_start=TruncSecond("start_datetime")
... ).filter(trunc_start__lte=start)
>>> find_this_exp.count()
0 # We expect to find one result but 0 are found
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it help to include a comment that shows the value of start as well as the value of trunc_start on the database level?

Copy link
Author

Choose a reason for hiding this comment

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

That is a good idea. It might be better to not include it as a comment in the code section though based on what I've seen in the Django documentation. Normally, a statement is run as if we're executing code and then a result is given where the comment comes after. In this case it wouldn't work to do it that way because when we print the value of trunc_start, it would be converted to a time zone aware value by the ORM. I could, instead, make a note after that explains what the values are at the database level. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

A note sounds like a good place for this!

Copy link
Contributor
ontowhee commented May 9, 2025

🙌 @coolbootscoder Thank you for the changes! The note clarifies how the values differ at the database and ORM level.

I am not sure if the tests were meant for sanity check only or if they will be committed to the code base, but I'll leave it to other reviewers to weigh in.

Eventually, you'll want to squash your commits into a single commit. You'll also want to rename the PR and format the commit message to follow the format Fixed #xxxxx -- ... as suggested in the committing guidelines.

@coolbootscoder coolbootscoder changed the title Ticket 34699 Added a warning about using Trunc functions in filters when the timezone is not UTC. Fixed #34699 -- Added warning about using Trunc functions in filters May 14, 2025
Added a warning in the Trunc documentation when using Trunc in filters
when the timezone is not UTC. Added tests that demonstrate that the
documentation is correct.
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.

2 participants
0