Skip to content

Commit

Permalink
Remove dependency on pytz
Browse files Browse the repository at this point in the history
Starting with Python 3.9 a zoneinfo package is available in the standard
library for this feature. There is therefore no need for this dependency
and a compatibility layer is available at
https://pypi.org/project/backports.zoneinfo/ for older Python versions.
  • Loading branch information
Jenselme committed Mar 1, 2023
1 parent a5dc7bb commit 773daf8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion agate/data_types/date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DateTime(DataType):
A formatting string for :meth:`datetime.datetime.strptime` to use
instead of using regex-based parsing.
:param timezone:
A `pytz <http://pytz.sourceforge.net/>`_ timezone to apply to each
A ``ZoneInfo`` timezone to apply to each
parsed date.
:param locale:
A locale specification such as :code:`en_US` or :code:`de_DE` to use
Expand Down
18 changes: 13 additions & 5 deletions docs/cookbook/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ The second way is to specify a timezone as an argument to the type constructor:

.. code-block:: python
import pytz
try:
from zoneinfo import ZoneInfo
except ImportError:
# Fallback for Python < 3.9
from backports.zoneinfo import ZoneInfo
eastern = pytz.timezone('US/Eastern')
eastern = ZoneInfo('US/Eastern')
datetime_type = agate.DateTime(timezone=eastern)
In this case all timezones that are processed will be set to have the Eastern timezone. Note, the timezone will be **set**, not converted. You cannot use this method to convert your timezones from UTC to another timezone. To do that see :ref:`convert_timezones`.
Expand All @@ -60,17 +64,21 @@ If you load data from a spreadsheet in one timezone and you need to convert it t

.. code-block:: python
import pytz
try:
from zoneinfo import ZoneInfo
except ImportError:
# Fallback for Python < 3.9
from backports.zoneinfo import ZoneInfo
us_eastern = pytz.timezone('US/Eastern')
us_eastern = ZoneInfo('US/Eastern')
datetime_type = agate.DateTime(timezone=us_eastern)
column_names = ['what', 'when']
column_types = [text_type, datetime_type]
table = agate.Table.from_csv('events.csv', columns)
rome = timezone('Europe/Rome')
rome = ZoneInfo('Europe/Rome')
timezone_shifter = agate.Formula(lambda r: r['when'].astimezone(rome))
table = agate.Table.compute([
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
'PyICU>=2.4.2;sys_platform=="linux"',
'pytest',
'pytest-cov',
'pytz>=2015.4',
'backports.zoneinfo;python_version<"3.9"',
],
'docs': [
'Sphinx>=1.2.2',
Expand Down
15 changes: 10 additions & 5 deletions tests/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from decimal import Decimal

import parsedatetime
import pytz

try:
from zoneinfo import ZoneInfo
except ImportError:
# Fallback for Python < 3.9
from backports.zoneinfo import ZoneInfo

from agate.data_types import Boolean, Date, DateTime, Number, Text, TimeDelta
from agate.exceptions import CastError
Expand Down Expand Up @@ -350,16 +355,16 @@ def test_cast_parser(self):
))

def test_cast_parser_timezone(self):
tzinfo = pytz.timezone('US/Pacific')
tzinfo = ZoneInfo('US/Pacific')
datetime_type = DateTime(timezone=tzinfo)

values = ('3/1/1994 12:30 PM', '2/17/2011 06:30', None, 'January 5th, 1984 22:37', 'n/a')
casted = tuple(datetime_type.cast(v) for v in values)
self.assertSequenceEqual(casted, (
tzinfo.localize(datetime.datetime(1994, 3, 1, 12, 30, 0, 0)),
tzinfo.localize(datetime.datetime(2011, 2, 17, 6, 30, 0, 0)),
datetime.datetime(1994, 3, 1, 12, 30, 0, 0, tzinfo=tzinfo),
datetime.datetime(2011, 2, 17, 6, 30, 0, 0, tzinfo=tzinfo),
None,
tzinfo.localize(datetime.datetime(1984, 1, 5, 22, 37, 0, 0)),
datetime.datetime(1984, 1, 5, 22, 37, 0, 0, tzinfo=tzinfo),
None
))

Expand Down

0 comments on commit 773daf8

Please sign in to comment.