3
3
from email .utils import parsedate
4
4
from typing import Optional , Union
5
5
6
- import pytz
7
-
8
6
ISO8601_DATE_FORMAT = "%Y-%m-%d"
9
7
ISO8601_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
10
8
11
9
12
- def iso8601_date (s : str ) -> Optional [ Union [datetime .date , str ] ]:
10
+ def iso8601_date (s : str ) -> Union [datetime .date , str ]:
13
11
"""
14
12
Parses an ISO 8601 date string and returns a UTC date object or the string
15
13
if the parsing failed.
@@ -19,7 +17,7 @@ def iso8601_date(s: str) -> Optional[Union[datetime.date, str]]:
19
17
try :
20
18
return (
21
19
datetime .datetime .strptime (s , ISO8601_DATE_FORMAT )
22
- .replace (tzinfo = pytz .utc )
20
+ .replace (tzinfo = datetime . timezone .utc )
23
21
.date ()
24
22
)
25
23
except (TypeError , ValueError ):
@@ -28,15 +26,15 @@ def iso8601_date(s: str) -> Optional[Union[datetime.date, str]]:
28
26
29
27
def iso8601_datetime (
30
28
s : str ,
31
- ) -> Optional [ Union [datetime .datetime , str ] ]:
29
+ ) -> Union [datetime .datetime , str ]:
32
30
"""
33
31
Parses an ISO 8601 datetime string and returns a UTC datetime object,
34
32
or the string if parsing failed.
35
33
:param s: ISO 8601-formatted datetime string (2015-01-25T12:34:56Z)
36
34
"""
37
35
try :
38
36
return datetime .datetime .strptime (s , ISO8601_DATETIME_FORMAT ).replace (
39
- tzinfo = pytz .utc
37
+ tzinfo = datetime . timezone .utc
40
38
)
41
39
except (TypeError , ValueError ):
42
40
return s
@@ -52,10 +50,10 @@ def rfc2822_datetime(s: str) -> Optional[datetime.datetime]:
52
50
date_tuple = parsedate (s )
53
51
if date_tuple is None :
54
52
return None
55
- return datetime .datetime (* date_tuple [:6 ]).replace (tzinfo = pytz .utc )
53
+ return datetime .datetime (* date_tuple [:6 ]).replace (tzinfo = datetime . timezone .utc )
56
54
57
55
58
- def decimal (d : Optional [str ]) -> Optional [ Union [Decimal , str ] ]:
56
+ def decimal (d : Optional [str ]) -> Union [Decimal , str ]:
59
57
"""
60
58
Parses a decimal string into a Decimal
61
59
:param d: decimal string
@@ -65,7 +63,7 @@ def decimal(d: Optional[str]) -> Optional[Union[Decimal, str]]:
65
63
return Decimal (d , BasicContext )
66
64
67
65
68
- def integer (i : str ) -> Optional [ Union [int , str ] ]:
66
+ def integer (i : str ) -> Union [int , str ]:
69
67
"""
70
68
Parses an integer string into an int
71
69
:param i: integer string
0 commit comments