diff options
author | Alex Grönholm <alex.gronholm@nextday.fi> | 2022-02-10 00:34:14 +0200 |
---|---|---|
committer | Alex Grönholm <alex.gronholm@nextday.fi> | 2022-02-10 00:34:14 +0200 |
commit | 222ba495202c02977a1af97709dc83d1f8d7cc44 (patch) | |
tree | 57ff31cecc1cc5d5beb38a58b0b73d90d277012d | |
parent | 885ed761917c49a2b5433f05112fb5e2e6d99302 (diff) | |
download | apscheduler-222ba495202c02977a1af97709dc83d1f8d7cc44.tar.gz |
Fixed pytz deprecation warnings
-rw-r--r-- | apscheduler/triggers/cron/__init__.py | 7 | ||||
-rw-r--r-- | apscheduler/triggers/interval.py | 6 | ||||
-rw-r--r-- | apscheduler/util.py | 19 | ||||
-rw-r--r-- | docs/versionhistory.rst | 3 | ||||
-rw-r--r-- | tests/test_util.py | 5 |
5 files changed, 23 insertions, 17 deletions
diff --git a/apscheduler/triggers/cron/__init__.py b/apscheduler/triggers/cron/__init__.py index fec6e3b..b5389dd 100644 --- a/apscheduler/triggers/cron/__init__.py +++ b/apscheduler/triggers/cron/__init__.py @@ -6,7 +6,8 @@ import six 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): @@ -143,7 +144,7 @@ class CronTrigger(BaseTrigger): 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 = {} @@ -156,7 +157,7 @@ class CronTrigger(BaseTrigger): 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: diff --git a/apscheduler/triggers/interval.py b/apscheduler/triggers/interval.py index 61094aa..b0e2dbd 100644 --- a/apscheduler/triggers/interval.py +++ b/apscheduler/triggers/interval.py @@ -4,7 +4,9 @@ from math import ceil 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): @@ -63,7 +65,7 @@ class IntervalTrigger(BaseTrigger): 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 { diff --git a/apscheduler/util.py b/apscheduler/util.py index 196b81d..6f70f92 100644 --- a/apscheduler/util.py +++ b/apscheduler/util.py @@ -34,7 +34,7 @@ except ImportError: __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): @@ -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): @@ -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)) diff --git a/docs/versionhistory.rst b/docs/versionhistory.rst index 24116e1..7c2933b 100644 --- a/docs/versionhistory.rst +++ b/docs/versionhistory.rst @@ -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 ----- diff --git a/tests/test_util.py b/tests/test_util.py index ab10f50..f7f900b 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -148,11 +148,6 @@ class TestConvertToDatetime(object): 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)) |