Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exception UnknownTimeZoneError: 'Etc/GMT 3' #70

Open
CleitonDeLima opened this issue Apr 4, 2024 · 2 comments · May be fixed by #75
Open

exception UnknownTimeZoneError: 'Etc/GMT 3' #70

CleitonDeLima opened this issue Apr 4, 2024 · 2 comments · May be fixed by #75

Comments

@CleitonDeLima
Copy link

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

@michelts
Copy link

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).

@ktosiek
Copy link

ktosiek commented Oct 15, 2024

The JS builds a wrong query string:

xmlHttp.send("offset=" + (new Date()).getTimezoneOffset() + '&timezone=' + Intl.DateTimeFormat().resolvedOptions().timeZone);

'&timezone=' + Intl.DateTimeFormat().resolvedOptions().timeZone
// -> '%timezone=Etc/GMT+6

The plus is then parsed as " " in Python:

from urllib import parse
parse.parse_qs("timezone=Etc/GMT+6")
# -> {'timezone': ['Etc/GMT 6']}

So there are a few steps required to fix this:

  1. fix the JS, by wrapping values in encodeURIComponent()
  2. ignore unknown timezones in the middleware, maybe with a warning
  3. (optional) try doing a .replace(" ", "+"), as a workaround to known problem

I'll prepare a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants