pendulum icon indicating copy to clipboard operation
pendulum copied to clipboard

InvalidTimezone: Invalid timezone "+00:00"

Open ecerulm opened this issue 3 years ago • 1 comments

  • [x] I am on the latest Pendulum version.
  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • OS version and name: macOS BigSur
  • Pendulum version: 2.1.2

Issue

pendulum.timezone('+00:00') raises a InvalidTimezone exception

>>> import pendulum
>>> pendulum.timezone("+00:00")
Traceback (most recent call last):
  File "/Users/rubelagu/tmp/venv-pendulum/lib/python3.8/site-packages/pendulum/tz/zoneinfo/reader.py", line 50, in read_for
    file_path = pytzdata.tz_path(timezone)
  File "/Users/rubelagu/tmp/venv-pendulum/lib/python3.8/site-packages/pytzdata/__init__.py", line 74, in tz_path
    raise TimezoneNotFound('Timezone {} not found at {}'.format(name, filepath))
pytzdata.exceptions.TimezoneNotFound: Timezone +00:00 not found at /Users/rubelagu/tmp/venv-pendulum/lib/python3.8/site-packages/pytzdata/zoneinfo/+00:00

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rubelagu/tmp/venv-pendulum/lib/python3.8/site-packages/pendulum/tz/__init__.py", line 37, in timezone
    tz = _Timezone(name, extended=extended)
  File "/Users/rubelagu/tmp/venv-pendulum/lib/python3.8/site-packages/pendulum/tz/timezone.py", line 40, in __init__
    tz = read(name, extend=extended)
  File "/Users/rubelagu/tmp/venv-pendulum/lib/python3.8/site-packages/pendulum/tz/zoneinfo/__init__.py", line 9, in read
    return Reader(extend=extend).read_for(name)
  File "/Users/rubelagu/tmp/venv-pendulum/lib/python3.8/site-packages/pendulum/tz/zoneinfo/reader.py", line 52, in read_for
    raise InvalidTimezone(timezone)
pendulum.tz.zoneinfo.exceptions.InvalidTimezone: Invalid timezone "+00:00"

It's surprising that if you convert a regular datetime.datetime to pendulum it will give you the +00:00 timezone but you can't look up that timezone with pendulum.timezone

from datetime import datetime, timezone
import pendulum
dt = datetime(2021,6,1,tzinfo=timezone.utc) # datetime.datetime(2021, 6, 1, 0, 0, tzinfo=datetime.timezone.utc)
pdt = pendulum.instance(dt) # DateTime(2021, 6, 1, 0, 0, 0, tzinfo=Timezone('+00:00'))
pdt.tzinfo.name # '+00:00'
pendulum.timezone(pdt.tzinfo.name) # raises InvalidTimezone

one would except that you can serialize a timezone and deserialize safely using the above steps but it will give you a InvalidTimezone for non-named (fixedoffset) timezone. This is something that for example hits Apache Airflow they do keep a timezone attribute and they serialize /deserialize that way and it only works when the timezone you passed was a named timezone.

ecerulm avatar Jun 23 '21 09:06 ecerulm

The only way that I found to parse +00:00, +01:00, etc back to pendulum timezones is to

pendulum.parse(datetime.datetime.utcnow().isoformat()+tz_string_plus_number_color_number).tzinfo

In other words, get a regular naive datetime.datetime, converted to iso string , append the +xx:yy and parse with pendulum.parse(). That is a lot of work to get from +00:00 to the corresponding pendulum.tz.timezone.FixedTimezone.

ecerulm avatar Jun 24 '21 08:06 ecerulm