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

InvalidTimezone: Invalid timezone "+00:00" #554

Open
2 tasks done
ecerulm opened this issue Jun 23, 2021 · 1 comment
Open
2 tasks done

InvalidTimezone: Invalid timezone "+00:00" #554

ecerulm opened this issue Jun 23, 2021 · 1 comment

Comments

@ecerulm
Copy link

ecerulm commented Jun 23, 2021

  • I am on the latest Pendulum version.
  • 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
Copy link
Author

ecerulm commented Jun 24, 2021

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.

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

No branches or pull requests

1 participant