3

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.

Duncan Lock
  • 12,351
  • 5
  • 40
  • 47
  • 1
    Have a look here : https://stackoverflow.com/questions/2720319/python-figure-out-local-timezone. External libs such as the well-known `pytz` and `dateutil` are widely used and I've followed the same path after trying hard with the standard module to deal with timezones. Definitively too tricky imho. – 0x0fba Nov 17 '22 at 19:22
  • Looks like a Python bug? Your examples are working fine on my system with the same config. – exciteabletom Nov 17 '22 at 19:39
  • with Python 3.9+, handle time zones with the [zoneinfo](https://docs.python.org/3/library/zoneinfo.html) module from the standard library. – FObersteiner Nov 19 '22 at 09:29

1 Answers1

0

Turns out this was caused by Homebrew/linuxbrew.

It had installed its own versions of python as dependencies and poetry has picked up one of those, as you can see from the poetry debug info here.

I removed Homebrew completely (it's Linux implementation has always felt like a bad idea to me, and this was enough for me to drop it). This broke poetry, so I uninstalled & reinstalled poetry - and that fixed it.

Duncan Lock
  • 12,351
  • 5
  • 40
  • 47