On my Ubuntu Linux system, the system timezone is correctly set to America/Vancouver
:
$ file /etc/localtime
/etc/localtime: symbolic link to /usr/share/zoneinfo/America/Vancouver
$ date
Thu 17 Nov 10:31:38 PST 2022
$ date '+%Y-%m-%d %H:%M:%S%z'
2022-11-17 10:32:57-0800
$ date '+%Y-%m-%d %H:%M:%S%Z'
2022-11-17 10:33:04PST
This is all correct and working as expected.
However, python appears to think its timezone is UTC:
$ python -c "import time; print(time.tzname); print(time.localtime());"
('UTC', 'UTC')
time.struct_time(tm_year=2022, tm_mon=11, tm_mday=17, tm_hour=18, tm_min=34, tm_sec=0, tm_wday=3, tm_yday=321, tm_isdst=0)
$ python -c 'from datetime import datetime; print(datetime.utcnow()); print(datetime.now());'
2022-11-17 18:34:38.878930
2022-11-17 18:34:38.878956
This system is running Python 3.10.8 & Ubuntu 22.04.1 LTS.
Am I misunderstanding something about how Pythons timezone stuff works? What's going on? How do I get python's time.tzname
to match the system timezone of PST
? I don't really want to manually hardcode the timezone in my python script - I just want it to use the current systems local timezone.