Skip to content

Commit

Permalink
Fixed pytz deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Feb 9, 2022
1 parent 885ed76 commit 222ba49
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
7 changes: 4 additions & 3 deletions apscheduler/triggers/cron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from apscheduler.triggers.base import BaseTrigger
from apscheduler.triggers.cron.fields import (
BaseField, MonthField, WeekField, DayOfMonthField, DayOfWeekField, DEFAULT_VALUES)
from apscheduler.util import datetime_ceil, convert_to_datetime, datetime_repr, astimezone
from apscheduler.util import (
datetime_ceil, convert_to_datetime, datetime_repr, astimezone, localize, normalize)


class CronTrigger(BaseTrigger):
Expand Down Expand Up @@ -143,7 +144,7 @@ def _increment_field_value(self, dateval, fieldnum):
i += 1

difference = datetime(**values) - dateval.replace(tzinfo=None)
return self.timezone.normalize(dateval + difference), fieldnum
return normalize(dateval + difference), fieldnum

def _set_field_value(self, dateval, fieldnum, new_value):
values = {}
Expand All @@ -156,7 +157,7 @@ def _set_field_value(self, dateval, fieldnum, new_value):
else:
values[field.name] = new_value

return self.timezone.localize(datetime(**values))
return localize(datetime(**values), self.timezone)

def get_next_fire_time(self, previous_fire_time, now):
if previous_fire_time:
Expand Down
6 changes: 4 additions & 2 deletions apscheduler/triggers/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from tzlocal import get_localzone

from apscheduler.triggers.base import BaseTrigger
from apscheduler.util import convert_to_datetime, timedelta_seconds, datetime_repr, astimezone
from apscheduler.util import (
convert_to_datetime, normalize, timedelta_seconds, datetime_repr,
astimezone)


class IntervalTrigger(BaseTrigger):
Expand Down Expand Up @@ -63,7 +65,7 @@ def get_next_fire_time(self, previous_fire_time, now):
next_fire_time = self._apply_jitter(next_fire_time, self.jitter, now)

if not self.end_date or next_fire_time <= self.end_date:
return self.timezone.normalize(next_fire_time)
return normalize(next_fire_time)

def __getstate__(self):
return {
Expand Down
19 changes: 13 additions & 6 deletions apscheduler/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def iscoroutinefunction(func):
__all__ = ('asint', 'asbool', 'astimezone', 'convert_to_datetime', 'datetime_to_utc_timestamp',
'utc_timestamp_to_datetime', 'timedelta_seconds', 'datetime_ceil', 'get_callable_name',
'obj_to_ref', 'ref_to_obj', 'maybe_ref', 'repr_escape', 'check_callable_args',
'TIMEOUT_MAX')
'normalize', 'localize', 'TIMEOUT_MAX')


class _Undefined(object):
Expand Down Expand Up @@ -162,11 +162,7 @@ def convert_to_datetime(input, tz, arg_name):
if isinstance(tz, six.string_types):
tz = timezone(tz)

try:
return tz.localize(datetime_, is_dst=None)
except AttributeError:
raise TypeError(
'Only pytz timezones are supported (need the localize() and normalize() methods)')
return localize(datetime_, tz)


def datetime_to_utc_timestamp(timeval):
Expand Down Expand Up @@ -431,3 +427,14 @@ def iscoroutinefunction_partial(f):
# The asyncio version of iscoroutinefunction includes testing for @coroutine
# decorations vs. the inspect version which does not.
return iscoroutinefunction(f)


def normalize(dt):
return datetime.fromtimestamp(dt.timestamp(), dt.tzinfo)


def localize(dt, tzinfo):
if hasattr(tzinfo, 'localize'):
return tzinfo.localize(dt)

return normalize(dt.replace(tzinfo=tzinfo))
3 changes: 2 additions & 1 deletion docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ APScheduler, see the :doc:`migration section <migration>`.
UNRELEASED
----------

* No longer enforce pytz time zones (support for others is experimental in the 3.x series)
* Fixed compatibility with PyMongo 4

* Fixed pytz deprecation warnings

3.8.1
-----
Expand Down
5 changes: 0 additions & 5 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ def test_text_timezone(self):
returned = convert_to_datetime('2009-8-1', 'UTC', None)
assert returned == datetime(2009, 8, 1, tzinfo=pytz.utc)

def test_bad_timezone(self):
exc = pytest.raises(TypeError, convert_to_datetime, '2009-8-1', tzinfo(), None)
assert str(exc.value) == ('Only pytz timezones are supported (need the localize() and '
'normalize() methods)')


def test_datetime_to_utc_timestamp(timezone):
dt = timezone.localize(datetime(2014, 3, 12, 5, 40, 13, 254012))
Expand Down

0 comments on commit 222ba49

Please sign in to comment.