regebro / tzlocal

A Python module that tries to figure out what your local timezone is
MIT License
184 stars 58 forks source link

UnknownTimeZoneError: '/UTC' #101

Closed movermeyer closed 2 years ago

movermeyer commented 3 years ago

On the ubuntu Docker image, the contents of /etc/timezone is /UTC, which causes tzlocal to raise:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.9/dist-packages/tzlocal/unix.py", line 165, in get_localzone
    _cache_tz = _get_localzone()
  File "/usr/local/lib/python3.9/dist-packages/tzlocal/unix.py", line 86, in _get_localzone
    tz = pytz.timezone(etctz.replace(' ', '_'))
  File "/usr/local/lib/python3.9/dist-packages/pytz/__init__.py", line 181, in timezone
    raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: '/UTC'
Dockerfile that reproduces the issue ```dockerfile FROM ubuntu RUN apt-get update && \ apt install -y software-properties-common && \ add-apt-repository ppa:deadsnakes/ppa && \ apt-get update # Install Python 3 RUN apt install -y python python-dev && \ apt install -y python3.9 python3.9-dev python3.9-venv # Make Python3 the default `python` RUN ln -s /usr/lib/python3.9/_sysconfigdata__linux_x86_64-linux-gnu.py /usr/lib/python3.9/_sysconfigdata__x86_64-linux-gnu.py && \ update-alternatives --install /usr/bin/python python /usr/bin/python3.9 10 # Install pip RUN apt-get install -y git curl gcc build-essential && \ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \ python get-pip.py # Get tzlocal RUN pip install tzlocal # Trigger the bug RUN python -c "from tzlocal import get_localzone; tz = get_localzone()" ```
movermeyer commented 3 years ago

I reported this bug (?) upstream: https://bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1899343

regebro commented 2 years ago

Is this still an issue? Because if it is I might just release a version that explicitly strips that infornation, but it's pretty silly to hack around a bug in a docker image config. :-)

movermeyer commented 2 years ago

@regebro I can still reproduce the issue using the steps listed in the issue I made upstream :

FROM ubuntu

RUN apt-get update && \
    apt-get install -y tzdata
$ docker build --no-cache -t tztest .
$ docker run -it --rm=true tztest cat /etc/timezone
/UTC
regebro commented 2 years ago

An interesting question is what /etc/timezone is before the install of tzdata? I suspect it's non-existent, but perhaps not? The package install does read it, and there could be something going wrong there if it exists but with unexpected data...

regebro commented 2 years ago

I added a workaround in 4.0.1. Annoying since this is a trivial bug in the Ubuntu config, but I have no idea how to contribute, it's very confusing.