@@ -32,6 +32,32 @@ def __float__(self):
32
32
numbers .Real .register (MyFloat )
33
33
34
34
35
+ def test_parse_date_invalid ():
36
+ assert jsonutil .parse_date (None ) is None
37
+ assert jsonutil .parse_date ("" ) == ""
38
+ assert jsonutil .parse_date ("invalid-date" ) == "invalid-date"
39
+
40
+
41
+ def test_parse_date_valid ():
42
+ ref = REFERENCE_DATETIME
43
+ timestamp = "2013-07-03T16:34:52.249482Z"
44
+ parsed = jsonutil .parse_date (timestamp )
45
+ assert isinstance (parsed , datetime .datetime )
46
+
47
+
48
+ def test_parse_date_from_naive ():
49
+ ref = REFERENCE_DATETIME
50
+ timestamp = "2013-07-03T16:34:52.249482"
51
+
52
+ with pytest .deprecated_call (match = "Interpreting naive datetime as local" ):
53
+ parsed = jsonutil .parse_date (timestamp )
54
+
55
+ assert isinstance (parsed , datetime .datetime )
56
+ assert parsed .tzinfo is not None
57
+ assert parsed .tzinfo .utcoffset (ref ) == tzlocal ().utcoffset (ref )
58
+ assert parsed == ref
59
+
60
+
35
61
def test_extract_date_from_naive ():
36
62
ref = REFERENCE_DATETIME
37
63
timestamp = "2013-07-03T16:34:52.249482"
@@ -45,7 +71,17 @@ def test_extract_date_from_naive():
45
71
assert extracted == ref
46
72
47
73
48
- def test_extract_dates ():
74
+ def test_extract_dates_from_str ():
75
+ ref = REFERENCE_DATETIME
76
+ timestamp = "2013-07-03T16:34:52.249482Z"
77
+ extracted = jsonutil .extract_dates (timestamp )
78
+
79
+ assert isinstance (extracted , datetime .datetime )
80
+ assert extracted .tzinfo is not None
81
+ assert extracted .tzinfo .utcoffset (ref ) == timedelta (0 )
82
+
83
+
84
+ def test_extract_dates_from_list ():
49
85
ref = REFERENCE_DATETIME
50
86
timestamps = [
51
87
"2013-07-03T16:34:52.249482Z" ,
@@ -66,6 +102,28 @@ def test_extract_dates():
66
102
assert extracted [4 ].tzinfo .utcoffset (ref ) == timedelta (hours = 8 )
67
103
68
104
105
+ def test_extract_dates_from_dict ():
106
+ ref = REFERENCE_DATETIME
107
+ timestamps = {
108
+ 0 : "2013-07-03T16:34:52.249482Z" ,
109
+ 1 : "2013-07-03T16:34:52.249482-0800" ,
110
+ 2 : "2013-07-03T16:34:52.249482+0800" ,
111
+ 3 : "2013-07-03T16:34:52.249482-08:00" ,
112
+ 4 : "2013-07-03T16:34:52.249482+08:00" ,
113
+ }
114
+ extracted = jsonutil .extract_dates (timestamps )
115
+ for k in extracted :
116
+ dt = extracted [k ]
117
+ assert isinstance (dt , datetime .datetime )
118
+ assert dt .tzinfo is not None
119
+
120
+ assert extracted [0 ].tzinfo .utcoffset (ref ) == timedelta (0 )
121
+ assert extracted [1 ].tzinfo .utcoffset (ref ) == timedelta (hours = - 8 )
122
+ assert extracted [2 ].tzinfo .utcoffset (ref ) == timedelta (hours = 8 )
123
+ assert extracted [3 ].tzinfo .utcoffset (ref ) == timedelta (hours = - 8 )
124
+ assert extracted [4 ].tzinfo .utcoffset (ref ) == timedelta (hours = 8 )
125
+
126
+
69
127
def test_parse_ms_precision ():
70
128
base = "2013-07-03T16:34:52"
71
129
digits = "1234567890"
0 commit comments