python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.29k stars 431 forks source link

RFE: use `zoneinfo` instead of `pytz` #1088

Open kloczek opened 1 month ago

kloczek commented 1 month ago

Switch to standard zoneinfo module. Below may help https://github.com/pydantic/pydantic-core/commit/fd262933

CHANGES.rst:* Dates: `pytz` is now optional; Babel will prefer it but will use `zoneinfo` when available. (:gh:`940`) - @ds-cbo
CHANGES.rst:* Note that installation requires pytz - Steve (Gadget) Barnes
CHANGES.rst:* General: Bumped version requirement on pytz - @chrisbrake (:gh:`592`)
CHANGES.rst:- added dummy version requirements for pytz so that it installs
CHANGES.rst:- depend on pytz now and wrap it nicer.  This gives us improved support
babel/dates.py:    import pytz
babel/dates.py:    pytz = None
babel/dates.py:    # Support localizing with both pytz and zoneinfo tzinfos
babel/dates.py:    if hasattr(tz, 'localize'):  # pytz
babel/dates.py:    if hasattr(tzinfo, 'zone'):  # pytz object
babel/dates.py:        if hasattr(tzinfo, 'normalize'):  # pytz
babel/dates.py:            if hasattr(tzinfo, 'normalize'):  # pytz
babel/dates.py:    returned comes from ``pytz`` or ``zoneinfo``, whichever is available.
babel/dates.py:    if pytz:
babel/dates.py:            return pytz.timezone(zone)
babel/dates.py:        except pytz.UnknownTimeZoneError as e:
babel/localtime/_helpers.py:    import pytz
babel/localtime/_helpers.py:    pytz = None
babel/localtime/_helpers.py:    """Get the tzinfo from `zoneinfo` or `pytz`
babel/localtime/_helpers.py:    if pytz:
babel/localtime/_helpers.py:            return pytz.timezone(tzenv)
babel/localtime/_helpers.py:        except pytz.UnknownTimeZoneError:
babel/localtime/_helpers.py:        if pytz:
babel/localtime/_helpers.py:            return pytz.tzfile.build_tzinfo('local', tzfile)
babel/localtime/_unix.py:    zoneinfo or pytz, over passing in the localtime file, as in the later
docs/dates.rst:Babel uses either `zoneinfo`_ or `pytz`_ for timezone support.
docs/dates.rst:If pytz is installed, it is preferred over the standard library's zoneinfo.
docs/dates.rst:You can use Babel together with ``zoneinfo`` or ``pytz`` to apply a time-zone
docs/dates.rst:.. _pytz: https://pythonhosted.org/pytz/
docs/dev.rst:Babel's timezone support relies on either ``pytz`` or ``zoneinfo``; if ``pytz``
docs/installation.rst:`zoneinfo`_ module is not available, `pytz`_  needs to be installed for
docs/installation.rst:timezone support. If `pytz`_  is installed, it is preferred over the
docs/installation.rst:.. _pytz: https://pythonhosted.org/pytz/
setup.py:        # pytz otherwise does not install on pip 1.4 or
setup.py:        # Python 3.9 and later include zoneinfo which replaces pytz
setup.py:        'pytz>=2015.7; python_version<"3.9"',
tests/conftest.py:    import pytz
tests/conftest.py:    pytz = None
tests/conftest.py:@pytest.fixture(params=["pytz.timezone", "zoneinfo.ZoneInfo"], scope="package")
tests/conftest.py:    if request.param == "pytz.timezone":
tests/conftest.py:        if pytz:
tests/conftest.py:            return pytz.timezone
tests/conftest.py:            pytest.skip("pytz not available")
tests/test_dates.py:@pytest.mark.parametrize("timezone_getter", ["pytz.timezone"], indirect=True)
tests/test_dates.py:def test_get_timezone_name_time_pytz(timezone_getter, tzname, params, expected):
tests/test_dates.py:    """pytz (by design) can't determine if the time is in DST or not,
tox.ini:    py{38}-pytz
tox.ini:    pytz: pytz
jun66j5 commented 1 month ago

Babel still supports Python 3.8 which doesn't have zoneinfo. Also pytz is optional for Python 3.9+ but Babel allows to use pytz if a user want to use pytz rather than zoneinfo. Therefore, I don't consider uses of pytz should be removed.

See setup.py:

    python_requires='>=3.8',
    packages=['babel', 'babel.messages', 'babel.localtime'],
    package_data={"babel": ["py.typed"]},
    include_package_data=True,
    install_requires=[
        # This version identifier is currently necessary as
        # pytz otherwise does not install on pip 1.4 or
        # higher.
        # Python 3.9 and later include zoneinfo which replaces pytz
        'pytz>=2015.7; python_version<"3.9"',
    ],
kloczek commented 1 month ago

Oct this year 3.8 will be EOSed. Also commit which I've mention shows example how to handle 3.8.