Timezone Information Missing in pytz?
Understand that the time zone data in pytz comes from the IANA time zone database, also known as the Olson database, or simply the tz database.
In this data, certain identifiers (such as US/Eastern
) are simply pointers (aka "links" or "aliases") to the real time zone. Links are there for several different reasons, usually for backwards compatibility purposes. In this case, the US/Eastern
time zone is a link to America/New_York
, which is the true time zone that you should be using. (I believe this particular switch happened in 1993).
You can see other time zones that are just there for backwards compatibility here. See also this chart on Wikipedia which lists the time zones, and clearly indicates which zones are links and where those links point to.
As to why pytz isn't accepting backward compatible zones on your system, I'm not exactly certain. It certainly should, and even shows these in their documentation. You might try re-installing it as Jacob suggested. But even then, you should prefer America/New_York
instead of US/Eastern
.
I had a similar exception UnknownTimeZoneError: Can not find any timezone configuration
when i tried to run my app inside a docker container with the latest ubuntu images. It turned out that tzdata
was missing. Installing tzdata
package fixed it:
apt-get install -y tzdata
# Maybe you will need to reconfigure the timezone as well:
ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime
dpkg-reconfigure -f noninteractive tzdata
(Use sudo
if you have to)
I cannot say why your installation of pytz is broken, but here's a possible fix:
- Download the .zip archive of pytz from the Python Package Index.
- In Terminal.app, run
pip show pytz
. - Using the path it returns, run
open /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
(replacing my path with yours if different). This will launch a Finder window with your Python modules. - Find the pytz/ folder. Open it.
- Replace the zoneinfo/ folder with the zoneinfo/ folder that's in the .zip archive you downloaded in step 1 from PyPI.
Tried reinstall, but bug not gone.
Then, I open pytz/__init__.py
, add a line zone = 'UTC'
, problem is gone:
zone = _unmunge_zone(zone)
zone = 'UTC'
if zone not in _tzinfo_cache:
if zone in all_timezones_set:
fp = open_resource(zone)
try:
_tzinfo_cache[zone] = build_tzinfo(zone, fp)
finally:
fp.close()
else:
raise UnknownTimeZoneError(zone)
This is quick and simple solution, but you'd better find the real problem( version? system? ) if you have time.