1
- import sys
2
1
from calendar import timegm
3
- from datetime import datetime , tzinfo
4
-
5
- from dateutil import tz as dateutil_tz
6
- from pytz import utc
7
-
8
- if sys .version_info >= (3 , 9 ): # pragma: no cover
9
- from zoneinfo import ZoneInfo
10
- else : # pragma: no cover
11
- from backports .zoneinfo import ZoneInfo
12
-
13
-
14
- def _can_detect_ambiguous (tz : tzinfo ) -> bool :
15
- """Helper function to determine if a timezone can detect ambiguous times using dateutil."""
16
-
17
- return isinstance (tz , ZoneInfo ) or hasattr (tz , "is_ambiguous" )
18
-
19
-
20
- def _is_ambigious (dt : datetime , tz : tzinfo ) -> bool :
21
- """Helper function to determine if a timezone is ambiguous using python's dateutil module.
22
-
23
- Returns False if the timezone cannot detect ambiguity, or if there is no ambiguity, otherwise True.
24
-
25
- In order to detect ambiguous datetimes, the timezone must be built using ZoneInfo, or have an is_ambiguous
26
- method. Previously, pytz timezones would throw an AmbiguousTimeError if the localized dt was ambiguous,
27
- but now we need to specifically check for ambiguity with dateutil, as pytz is deprecated.
28
- """
29
-
30
- return _can_detect_ambiguous (tz ) and dateutil_tz .datetime_ambiguous (dt )
2
+ from datetime import datetime , timezone , tzinfo
31
3
32
4
33
5
def is_naive (dt : datetime ) -> bool :
@@ -39,14 +11,12 @@ def make_aware(dt: datetime, tz: tzinfo) -> datetime:
39
11
"""Set timezone for a :class:`~datetime.datetime` object."""
40
12
41
13
dt = dt .replace (tzinfo = tz )
42
- if _is_ambigious (dt , tz ): # pragma: no cover
43
- dt = min (dt .replace (fold = 0 ), dt .replace (fold = 1 ))
44
14
return dt
45
15
46
16
47
17
def make_utc (dt : datetime ) -> datetime :
48
18
if is_naive (dt ):
49
- dt = make_aware (dt , tz = utc )
19
+ dt = make_aware (dt , tz = timezone . utc )
50
20
return dt
51
21
52
22
0 commit comments