Skip to content

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

Open

Description

@rileyjohngibbs
  • 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.

Activity

  1. alejandro-angulo commented on Mar 9, 2022

    @alejandro-angulo

    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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions