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

Tests: Use UTC-based utcnow().date() instead of localtime-based date.today() #441

Merged
merged 1 commit into from
Jul 25, 2022

Conversation

amotl
Copy link
Member

@amotl amotl commented Jul 22, 2022

Motivation

About

datetime.date.today() returns the current local date. So, its outcome depends on the system time zone. Because we want to use UTC-based date and time objects instead, datetime.utcnow().date() is a better choice.

@amotl amotl force-pushed the amo/fix-date-today branch 5 times, most recently from ee86c14 to 07b8d79 Compare July 22, 2022 23:18
@amotl amotl marked this pull request as ready for review July 22, 2022 23:28
Copy link
Member

@mfussenegger mfussenegger left a comment

Choose a reason for hiding this comment

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

Other than the test addition this looks good to me.

The commit message could be clarified a bit. Using UTC is not automatically better - it depends on the use case.

What is a problem is that we were comparing utc variants with local variants in

>>> from datetime import datetime
>>> now = datetime.utcnow()
>>> dt = location.date
>>> dt.year == now.year
True
>>> dt.month == now.month
True
>>> dt.day == now.day
True
>>> (now - location.datetime_tz).seconds < 4
True

That is the problem and caused test flakyness.

import time_machine


class DateTodayTest(TestCase):
Copy link
Member

Choose a reason for hiding this comment

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

Please remove, there's no need to create tests that verify documented behavior of the standard library.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. I've removed the test cases and amended the commit message.

`datetime.date.today()` returns the current local date. In order to
reduce test flakyness when comparing UTC-based datetimes with localtime-
based variants, `datetime.utcnow().date()` is a better choice.
@amotl amotl merged commit 17bda3e into master Jul 25, 2022
@amotl amotl deleted the amo/fix-date-today branch July 25, 2022 08:23
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