From 84ace57cc39f8014d539f94ff1c5e64a36347952 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 19 Jul 2023 21:24:17 +0530 Subject: [PATCH] Replace use of timezone objects from dateutil package with those from the stdlib The dateutil local_tz malfunctions on windows for dates before 1970-1-1 See https://bugs.launchpad.net/calibre/+bug/2028019 for details. --- src/calibre/utils/iso8601.py | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/src/calibre/utils/iso8601.py b/src/calibre/utils/iso8601.py index 66fff14299..22890c1383 100644 --- a/src/calibre/utils/iso8601.py +++ b/src/calibre/utils/iso8601.py @@ -2,38 +2,13 @@ # License: GPLv3 Copyright: 2016, Kovid Goyal -from datetime import datetime +from datetime import datetime, timezone -from dateutil.tz import tzlocal, tzutc, tzoffset from calibre_extensions import speedup -class SafeLocalTimeZone(tzlocal): - - def _isdst(self, dt): - # This method in tzlocal raises ValueError if dt is out of range (in - # older versions of dateutil) - # In such cases, just assume that dt is not DST. - try: - return super()._isdst(dt) - except Exception: - pass - return False - - def _naive_is_dst(self, dt): - # This method in tzlocal raises ValueError if dt is out of range (in - # newer versions of dateutil) - # In such cases, just assume that dt is not DST. - try: - return super()._naive_is_dst(dt) - except Exception: - pass - return False - - -utc_tz = tzutc() -local_tz = SafeLocalTimeZone() -del tzutc, tzlocal +utc_tz = timezone.utc +local_tz = datetime.now().astimezone().tzinfo UNDEFINED_DATE = datetime(101,1,1, tzinfo=utc_tz) @@ -48,7 +23,7 @@ def parse_iso8601(date_string, assume_utc=False, as_utc=True, require_aware=Fals else: sign = '-' if tzseconds < 0 else '+' description = "%s%02d:%02d" % (sign, abs(tzseconds) // 3600, (abs(tzseconds) % 3600) // 60) - tz = tzoffset(description, tzseconds) + tz = timezone(tzseconds, description) elif require_aware: raise ValueError(f'{date_string} does not specify a time zone') dt = dt.replace(tzinfo=tz)