0

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?

user28221
  • 47
  • 3
  • related: [How to get system timezone setting and pass it to pytz.timezone?](https://stackoverflow.com/q/13218506/4279) – jfs Apr 05 '20 at 07:13

2 Answers2

2

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0
import pytz


def valid_timezone(timezone):
    try:
        pytz.timezone(timezone)
    except pytz.exceptions.UnknownTimeZoneError:
        return False
    return True

if valid_timezone('CST'):
    # go ahead...
    pass
else:
    # not supported timezone..
    pass
  • 2
    Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes. – Mark Rotteveel Apr 05 '20 at 08:17