File tree Expand file tree Collapse file tree 2 files changed +33
-6
lines changed Expand file tree Collapse file tree 2 files changed +33
-6
lines changed Original file line number Diff line number Diff line change 7
7
from twilio import deserialize
8
8
9
9
10
+ class Iso8601DateTestCase (unittest .TestCase ):
11
+
12
+ def test_parsable (self ):
13
+ actual = deserialize .iso8601_date ('2015-01-02' )
14
+ expected = datetime .date (2015 , 1 , 2 )
15
+ self .assertEqual (expected , actual )
16
+
17
+ def test_not_parsable (self ):
18
+ actual = deserialize .iso8601_date ('not-a-date' )
19
+ self .assertEqual ('not-a-date' , actual )
20
+
21
+
10
22
class Iso8601DateTimeTestCase (unittest .TestCase ):
11
23
12
24
def test_parsable (self ):
@@ -15,8 +27,8 @@ def test_parsable(self):
15
27
self .assertEqual (expected , actual )
16
28
17
29
def test_not_parsable (self ):
18
- actual = deserialize .iso8601_datetime ('not-a-date ' )
19
- self .assertEqual ('not-a-date ' , actual )
30
+ actual = deserialize .iso8601_datetime ('not-a-datetime ' )
31
+ self .assertEqual ('not-a-datetime ' , actual )
20
32
21
33
22
34
class DecimalTestCase (unittest .TestCase ):
Original file line number Diff line number Diff line change 3
3
from email .utils import parsedate
4
4
import pytz
5
5
6
+ ISO8601_DATE_FORMAT = '%Y-%m-%d'
7
+ ISO8601_DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
8
+
9
+
10
+ def iso8601_date (s ):
11
+ """
12
+ Parses an ISO 8601 date string and returns a UTC date object or the string
13
+ if the parsing failed.
14
+ :param s: ISO 8601-formatted date string (2015-01-25)
15
+ :return:
16
+ """
17
+ try :
18
+ return datetime .datetime .strptime (s , ISO8601_DATE_FORMAT ).replace (tzinfo = pytz .utc ).date ()
19
+ except ValueError :
20
+ return s
21
+
6
22
7
23
def iso8601_datetime (s ):
8
24
"""
9
- Parses an ISO 8601 date string and returns a UTC datetime object,
25
+ Parses an ISO 8601 datetime string and returns a UTC datetime object,
10
26
or the string if parsing failed.
11
- :param s: ISO 8601-formatted string date
27
+ :param s: ISO 8601-formatted datetime string (2015-01-25T12:34:56Z)
12
28
:return: datetime or str
13
29
"""
14
- format = "%Y-%m-%dT%H:%M:%SZ"
15
30
try :
16
- return datetime .datetime .strptime (s , format ).replace (tzinfo = pytz .utc )
31
+ return datetime .datetime .strptime (s , ISO8601_DATETIME_FORMAT ).replace (tzinfo = pytz .utc )
17
32
except ValueError :
18
33
return s
19
34
You can’t perform that action at this time.
0 commit comments