Skip to content

with pendulum.test(...) gets mangled by timezone issues #608

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
2 tasks done
rileyjohngibbs opened this issue Mar 9, 2022 · 1 comment
Open
2 tasks done

with pendulum.test(...) gets mangled by timezone issues #608

rileyjohngibbs opened this issue Mar 9, 2022 · 1 comment

Comments

@rileyjohngibbs
Copy link

rileyjohngibbs commented Mar 9, 2022

  • I am on the latest Pendulum version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • OS version and name: macOS 11.6.1
  • Pendulum version: 2.1.2, 3.0.0a0

Issue

When using the pendulum.test context in tests, the local timezone is getting set to Toronto when using pendulum.today(). To see this behavior, add this test to tests/date/test_diff.py:

def test_pendulum_test():
    with pendulum.test(pendulum.datetime(2016, 3, 1)):
        today = pendulum.today().date()
        assert today == pendulum.datetime(2016, 3, 1).date()

Alternatively, try:

def test_pendulum_test():
    with pendulum.test(pendulum.datetime(2016, 3, 1)):
        assert str(pendulum.now().tz) == "UTC"
        assert str(pendulum.today().tz) == "UTC"

You can see that .now() returns a UTC timezone but .today() returns Toronto. (Or at least, this is what I'm seeing, even though I'm not in the Toronto timezone, I'm in the Los Angeles timezone.) As a result, pendulum.today().date() evaluates to (timezone-naive) February 29, because it's still February in Toronto when it's midnight on March 1 in UTC.

You can see this in a REPL too:

>>> import pendulum
>>> with pendulum.test(pendulum.datetime(2016, 3, 1)):
...     print(pendulum.today().date())
... 
2016-02-29

This is occurring in some existing tests in test_diff, for example test_diff_for_humans_now_and_month, but seems it is being masked by another bug (see #606) that causes the time difference to be calculated incorrectly (or at the very least inconsistently) for months that do not have 31 days.

@alejandro-angulo
Copy link

I think the Toronto timezone you're noticing is related to the setup fixture https://github.com/sdispater/pendulum/blob/master/tests/conftest.py#L8

Though, I guess it is a bit strange that the timezone isn't consistent (now uses UTC but today uses what's set by set_local_timezone).

@rileyjohngibbs rileyjohngibbs mentioned this issue Mar 10, 2022
2 tasks
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

No branches or pull requests

2 participants