Search code examples
pythontimezonepytztimestamp-with-timezone

How to handle POSIX timezone information (like CST) for pytz?


I have timezone string 'CST' and try to use it within pytz.

Unfortunately this fails:

cst = pytz.timezone('CST')

File "/home/user/venv/numba/lib/python3.6/site-packages/pytz/__init__.py", line 181, in timezone
  raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'CST'

What do I have to do to avoid this error?


Solution

  • CST unto itself is not a valid time zone identifier.

    There is no way to know whether CST is to be interpreted as (US) "Central Standard Time" (UTC-6), "Cuba Standard Time" (UTC-5), or "China Standard Time" (UTC+8).

    Instead, pass a fully qualified locality-based IANA time zone identifier, such as America/Chicago, America/Havana, or Asia/Shanghai.