data-8 / datascience

A Python library for introductory data science
https://www.data8.org/datascience/
BSD 3-Clause "New" or "Revised" License
626 stars 295 forks source link

test_date_formatter_format_value() should call tzset() #586

Closed davidwagner closed 1 year ago

davidwagner commented 1 year ago

The test for test_date_formatter_format_value() failed on my system:

E       AssertionError: 
E         2022-10-20 04:14:49.900400
E         !=
E         2022-10-20 11:14:49.900400

But when I explicitly set the TZ environment variable (export TZ=UTC), then run the tests, the test passes.

I think after setting os.environ["TZ"] = "UTC", it is necessary to call time.tzset(). See https://docs.python.org/3/library/time.html#time.tzset.

See this test to demonstrate that just setting os.environ isn't enough:

$ cat test_tz.py 
from datetime import datetime, timezone
import os

print(os.environ["TZ"] if "TZ" in os.environ else "unset")
print(datetime.fromtimestamp(1666264489.9004).strftime("%Y-%m-%d %H:%M:%S.%f"))

os.environ["TZ"] = "UTC"

print(os.environ["TZ"] if "TZ" in os.environ else "unset")
print(datetime.fromtimestamp(1666264489.9004).strftime("%Y-%m-%d %H:%M:%S.%f"))
$ echo $TZ

$ python3 test_tz.py 
unset
2022-10-20 04:14:49.900400
UTC
2022-10-20 04:14:49.900400
$ export TZ=UTC
$ echo $TZ
UTC
$ python3 test_tz.py
UTC
2022-10-20 11:14:49.900400
UTC
2022-10-20 11:14:49.900400

If I add a call to time.tzset(), then everything works as expected.

adnanhemani commented 1 year ago

Merged #587. Thanks for this! It was working fine on my machine, so I didn't catch this myself :)