babel icon indicating copy to clipboard operation
babel copied to clipboard

Timezone for `America/Montreal` display `-0400` instead of `EDT` or `EST`

Open ikus060 opened this issue 2 years ago • 5 comments

Overview Description

Using babel to format date with local timezone show different result for America/Montreal and America/Toronto.

Steps to Reproduce

>>> dates.format_datetime(format='long', tzinfo=pytz.timezone('America/Toronto'))
'August 2, 2023 at 1:27:22 p.m. EDT'
>>> dates.format_datetime(format='long', tzinfo=pytz.timezone('America/Montreal'))
'August 2, 2023 at 1:27:29 p.m. -0400'

Expected Results

America/Toronto and America/Montreal are always on the same timezone. So I'm expecting timezone to be displayed as EDT for America/Montreal.

Maybe an alias is missing ?

ikus060 avatar Aug 02 '23 17:08 ikus060

I also have similar issue with Europe/Zurich where it get displayed as +0200 instead of CEST

ikus060 avatar Aug 03 '23 14:08 ikus060

I have noticed the same for all IST values "Asia/Kolkata"...

celbaz avatar Sep 11 '23 00:09 celbaz

They're linked in pytz for backward which adjusts a datetime to the previous valid time, typically when transitioning from daylight saving time to standard time.

https://github.com/stub42/pytz/blob/3944f75a65268002562b9fd4c912d8bf566e0a66/tz/backward#L276

but then Montreal is whack for backzone which refers to the timezone label or offset immediately before the backward transition occurs, often used internally in timezone databases.

https://github.com/stub42/pytz/blob/3944f75a65268002562b9fd4c912d8bf566e0a66/tz/backzone#L687

They're aliases now, but they weren't always, which is likely leading to this behaviour

Obscuretone avatar Dec 29 '24 01:12 Obscuretone

I don't think it is a bug of Babel.

The name of the timezone in the formatted datetime is depended the given locale, so that it is needed to pass locale parameter.

>>> import babel, pytz
>>> from datetime import datetime
>>> from babel.dates import format_datetime
>>> babel.__version__
'2.16.0'
>>> pytz.__version__
'2025.1'
>>> dt = datetime.fromisoformat('2023-08-02T17:27:22+00:00')
>>> dt
datetime.datetime(2023, 8, 2, 17, 27, 22, tzinfo=datetime.timezone.utc)
>>> for locale in ('en_US', 'en_GB'):
...   for tz in ('America/Toronto', 'America/Montreal', 'Europe/Zurich'):
...     result = format_datetime(dt, format='long', locale=locale, tzinfo=pytz.timezone(tz))
...     print(f'{locale}  {tz:16s}  {result}')
...
en_US  America/Toronto   August 2, 2023, 1:27:22 PM EDT
en_US  America/Montreal  August 2, 2023, 1:27:22 PM EDT
en_US  Europe/Zurich     August 2, 2023, 7:27:22 PM +0200
en_GB  America/Toronto   2 August 2023, 13:27:22 Eastern Daylight Time
en_GB  America/Montreal  2 August 2023, 13:27:22 Eastern Daylight Time
en_GB  Europe/Zurich     2 August 2023, 19:27:22 CEST

jun66j5 avatar Feb 01 '25 01:02 jun66j5

@akx what exactly is the question label for?

StanFromIreland avatar Mar 04 '25 21:03 StanFromIreland