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

Frozen time doesn't adher to timezone information #325

Open
svaningelgem opened this issue Feb 12, 2023 · 3 comments
Open

Frozen time doesn't adher to timezone information #325

svaningelgem opened this issue Feb 12, 2023 · 3 comments

Comments

@svaningelgem
Copy link

Python Version

3.9.13

pytest Version

7.1.2

Package Version

2.9.0

Description

I'll dive right in with a code sample:

import pytz
from datetime import datetime
import time_machine

def test_freezegun():
    tz = "Australia/Sydney"
    print('')
    print(f'{tz=}')
    tzinfo = pytz.timezone(tz)
    print('unfrozen time:', datetime.now().astimezone())
    with time_machine.travel(datetime(2023, 2, 2, 10, 0, 0, tzinfo=tzinfo), tick=False):
        print('frozen time:', datetime.now())
        print('frozen time:', datetime.now().astimezone())

Result:

tz='Australia/Sydney'
unfrozen time: 2023-02-12 12:33:26.072036+01:00
frozen time: 2023-02-02 00:55:00
frozen time: 2023-02-02 00:55:00+01:00

Expected result:

tz='Australia/Sydney'
unfrozen time: 2023-02-12 12:33:26.072036+01:00
frozen time: 2023-02-02 10:00:00
frozen time: 2023-02-02 10:00:00+11:00

I don't know why there is such a strange number subtracted because afaik timezones are 1h or half an hour difference.
Additionally the "frozen time" should reflect the provided datetime?
As I understand it: I want to travel back to Feb 2 10 am in Brisbane...?

@svaningelgem
Copy link
Author

Reading through the sources and adjusting the test for using zoneinfo instead of pytz:

...
    tzinfo = zoneinfo.ZoneInfo(tz)
...

Results into:

tz='Australia/Sydney'
unfrozen time: 2023-02-12 12:53:15.123167+01:00
frozen time: 2023-02-02 00:00:00
frozen time: 2023-02-02 10:00:00+11:00

This looks already better, however the first call (without the tzinfo) should imho also result in 10am.

@adamchainz
Copy link
Owner

pytz is effectively deprecated in favour of zoneinfo, see the problems with it in this post: https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html

I don’t really wanna dive in and add support myself but if you can come up with a short well tested PR, I would consider it.

@svaningelgem
Copy link
Author

Thanks @adamchainz for the enlighting post. After reading it, I don't think I even want to try to implement it.
The most I would want to do is to implement a HAVE_PYTZ_INFO, and if the tzinfo is of this type >> raise an exception with a reference to the link you've given.
WDYT?

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