Time Zone
New in Python 3.9.
Update time zone database
Debian family
apt install tzdata
Red Hat family
dnf install -y tzdata
Database Location
import zoneinfo
>>> zoneinfo.TZPATH
('/usr/share/zoneinfo', '/usr/lib/zoneinfo', '/usr/share/lib/zoneinfo', '/etc/zoneinfo')
If no system time zone data is available,
or Python version less than 3.9,
the library will fall back to tzdata
package available on PyPI.
All Time Zones
import zoneinfo
zoneinfo.available_timezones()
Access Time Zone
import os
import time
>>> os.environ['TZ'] = 'Asia/Shanghai'
>>> time.tzset()
>>> time.tzname
('CST', 'CST')
UTC Now
from datetime import datetime, UTC
datetime.now(UTC)