DST timezones not recognized

Asked by Ben

This is a duplicate of #703494 ( https://answers.launchpad.net/pytz/+question/703494 ) which expired.

From that issue:

>>> dt = datetime.datetime.now()
>>> dt # During DST
datetime.datetime(2022, 10, 15, 16, 35, 58, 20944)
>>> pytz.timezone('Europe/Paris').tzname(dt)
'CEST'
>>> pytz.timezone('CET').tzname(dt)
'CEST'
>>> pytz.timezone('CEST').tzname(dt)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/site-packages/pytz/__init__.py", line 188, in timezone
    raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'CEST'

And just to confirm:
>>> "CEST" in pytz.all_timezones
False
>>> "CET" in pytz.all_timezones
True

Question information

Language:
English Edit question
Status:
Answered
For:
pytz Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Manfred Hampl (m-hampl) said :
#1

As far as I know the pytz.timezone function expects a timezone name (e.g. 'Europe/Paris') as parameter and not an abbreviation.
Only few abbreviations are also working (e.g. 'UTC' and 'CET', but not 'CEST').

See the contents of the zoneinfo subdirectory

Revision history for this message
Ben (benjaminburke) said :
#2

I don't think this answers the question. If pytz emits a tzname that it can't parse that seems like a bug.

Revision history for this message
Manfred Hampl (m-hampl) said :
#3

If you are using the timezone function in a wrong way, you must not be astonished when you receive an error message.

'CEST' is not a valid parameter for pytz.timezone(...)

see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Only the values in the column 'TZ identifier' are allowed, not those in the 'Time zone abbreviation' columns.
Everything that's not in the identifier column gives an exception (which you can handle with "try ... except").

You can also execute

print(pytz.all_timezones)

to see the list of timezones in pytz.
CEST is not in that list.

Revision history for this message
Ben (benjaminburke) said :
#4

Sorry for being astonished. Can you point me to this in the docs?

Revision history for this message
Stuart Bishop (stub) said :
#5

The tzname is the timezone abbreviation to render. It is not the timezone identifier. Identifiers need to be unique. Timezone abbreviations are not unique. The few timezone abbreviations that do work are historical artifacts from the original Olson timezone database and are deprecated for use in the IANA timezone database.

Can you help with this problem?

Provide an answer of your own, or ask Ben for more information if necessary.

To post a message you must log in.