From 8d38f87beea885e493c1963da9cfbd04977e3c44 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 3 Aug 2023 11:07:51 +0200 Subject: [PATCH] Replace deprecated datetime.utcfromtimestamp() --- src/README.rst | 5 ++--- src/pytz/tzinfo.py | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/README.rst b/src/README.rst index 1af9169..c7ef642 100644 --- a/src/README.rst +++ b/src/README.rst @@ -148,7 +148,7 @@ section for more details) Converting between timezones is more easily done, using the standard astimezone method. ->>> utc_dt = utc.localize(datetime.utcfromtimestamp(1143408899)) +>>> utc_dt = datetime.fromtimestamp(1143408899, tz=utc) >>> utc_dt.strftime(fmt) '2006-03-26 21:34:59 UTC+0000' >>> au_tz = timezone('Australia/Sydney') @@ -166,7 +166,7 @@ conversions. ``normalize()`` and ``localize()`` are not really necessary when there are no daylight saving time transitions to deal with. ->>> utc_dt = datetime.utcfromtimestamp(1143408899).replace(tzinfo=utc) +>>> utc_dt = datetime.fromtimestamp(1143408899, tz=utc) >>> utc_dt.strftime(fmt) '2006-03-26 21:34:59 UTC+0000' >>> au_tz = timezone('Australia/Sydney') @@ -605,4 +605,3 @@ Contact ~~~~~~~ Stuart Bishop - diff --git a/src/pytz/tzinfo.py b/src/pytz/tzinfo.py index 89a50af..5651518 100644 --- a/src/pytz/tzinfo.py +++ b/src/pytz/tzinfo.py @@ -1,6 +1,6 @@ '''Base classes and helpers for building zone specific tzinfo classes''' -from datetime import datetime, timedelta, tzinfo +from datetime import datetime, timedelta, timezone, tzinfo from bisect import bisect_right try: set @@ -24,7 +24,7 @@ def memorized_timedelta(seconds): _timedelta_cache[seconds] = delta return delta -_epoch = datetime.utcfromtimestamp(0) +_epoch = datetime.fromtimestamp(0, tz=timezone.utc) _datetime_cache = {0: _epoch} @@ -33,7 +33,7 @@ def memorized_datetime(seconds): try: return _datetime_cache[seconds] except KeyError: - # NB. We can't just do datetime.utcfromtimestamp(seconds) as this + # NB. We can't just do datetime.fromtimestamp(seconds, tz=timezone.utc) as this # fails with negative values under Windows (Bug #90096) dt = _epoch + timedelta(seconds=seconds) _datetime_cache[seconds] = dt