django-tz-detect icon indicating copy to clipboard operation
django-tz-detect copied to clipboard

exception UnknownTimeZoneError: 'Etc/GMT 3'

Open CleitonDeLima opened this issue 1 year ago • 2 comments

In version 0.5 this error occurs. The timezone entered is incorrect.

CleitonDeLima avatar Apr 04 '24 13:04 CleitonDeLima

I faced the same problem and I wrote a wrapper around the middleware to avoid it:

class TimezoneMiddleware(BaseTimezoneMiddleware):
    def process_request(self, request):
        try:
            super().process_request(request)
        except Exception:
            logger.exception("Timezone Middleware Error")

Apparently pytz would be fine if the timezone would have a plus or minus sign instead of a white-space:

>>> import pytz
>>> import zoneinfo
>>> zonename = 'Etc/GMT+3'
>>> pytz.timezone(zonename)
<StaticTzInfo 'Etc/GMT+3'>
>>> zoneinfo.ZoneInfo(zonename)
zoneinfo.ZoneInfo(key='Etc/GMT+3')
>>> zonename = 'Etc/GMT-3'
>>> pytz.timezone(zonename)
<StaticTzInfo 'Etc/GMT-3'>
>>> zoneinfo.ZoneInfo(zonename)
zoneinfo.ZoneInfo(key='Etc/GMT-3')
>>> zonename = 'Etc/GMT 3'
>>> pytz.timezone(zonename)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/michel/.virtualenvs/teachermade/lib/python3.9/site-packages/pytz/__init__.py", line 188, in timezone
    raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'Etc/GMT 3'
>>> zoneinfo.ZoneInfo(zonename)
Traceback (most recent call last):
  File "/usr/lib/python3.9/zoneinfo/_common.py", line 12, in load_tzdata
    return importlib.resources.open_binary(package_name, resource_name)
  File "/usr/lib/python3.9/importlib/resources.py", line 91, in open_binary
    return reader.open_resource(resource)
  File "<frozen importlib._bootstrap_external>", line 1055, in open_resource
FileNotFoundError: [Errno 2] No such file or directory: '/home/michel/.virtualenvs/teachermade/lib/python3.9/site-packages/tzdata/zoneinfo/Etc/GMT 3'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/zoneinfo/_common.py", line 24, in load_tzdata
    raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key Etc/GMT 3'

I didn't dig further to see what is causing timezone with this pattern (with a white space).

michelts avatar Aug 22 '24 16:08 michelts