Timezone Information Missing in pytz?

后端 未结 8 1548
轻奢々
轻奢々 2020-12-31 16:17

I\'m having a very weird problem with Python\'s pytz: it seems to have an incomplete catalog of timezones on my system (MacOS X 10.8.5, system Python 2.7.5).

相关标签:
8条回答
  • 2020-12-31 16:44

    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.

    0 讨论(0)
  • 2020-12-31 16:44

    All solutions didn't work for me in a Spark cluster. But I found the "pendulum" library and it works just fine.

    import pendulum
    timezone = pendulum.timezone('Europe/Saratov')
    
    0 讨论(0)
  • 2020-12-31 16:51

    I ran into a similar issue and the problem ended up being that the pip installation had previously failed. This fixed it for me:

    pip uninstall -y pytz
    pip install pytz
    
    0 讨论(0)
  • 2020-12-31 16:54

    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.

    0 讨论(0)
  • 2020-12-31 16:56

    I had a similar problem (Mac and python 3.6). it found many timezones but none under America. For some reason (may due to pycharm settings), there was a case conflict in directory names under directory /lib/python3.6/site-packages/pytz/zoneinfo For example, there was an empty america directory and a directory called america (Case Conflict) (and other similar). Renaming this directory to America solved the problem.

    0 讨论(0)
  • 2020-12-31 16:57

    For me it as was easy as updating the pytz package via pip3 -install pytz --update On AWS linux machines I had to execute it like this python3 -m pip install pytz --update

    My app was complaining about unknown timezone 'America/Punta_Arenas'

    0 讨论(0)
提交回复
热议问题