@@ -53,29 +53,37 @@ def parse_rfc3339(s):
5353 if not s .tzinfo :
5454 return s .replace (tzinfo = UTC )
5555 return s
56- groups = _re_rfc3339 .search (s ).groups ()
57- dt = [0 ] * 7
58- for x in range (6 ):
59- dt [x ] = int (groups [x ])
60- us = 0
61- if groups [6 ] is not None :
62- partial_sec = float (groups [6 ].replace ("," , "." ))
63- us = int (MICROSEC_PER_SEC * partial_sec )
64- tz = UTC
65- if groups [7 ] is not None and groups [7 ] != 'Z' and groups [7 ] != 'z' :
66- tz_groups = _re_timezone .search (groups [7 ]).groups ()
67- hour = int (tz_groups [1 ])
68- minute = 0
69- if tz_groups [0 ] == "-" :
70- hour *= - 1
71- if tz_groups [2 ]:
72- minute = int (tz_groups [2 ])
73- tz = TimezoneInfo (hour , minute )
74- return datetime .datetime (
75- year = dt [0 ], month = dt [1 ], day = dt [2 ],
76- hour = dt [3 ], minute = dt [4 ], second = dt [5 ],
77- microsecond = us , tzinfo = tz )
7856
57+ match = _re_rfc3339 .search (s )
58+
59+ if match is None :
60+ raise ValueError (f"Error in RFC339 Date Formatting { s } " )
61+ try :
62+ groups = match .groups ()
63+
64+ dt = [0 ] * 7
65+ for x in range (6 ):
66+ dt [x ] = int (groups [x ])
67+ us = 0
68+ if groups [6 ] is not None :
69+ partial_sec = float (groups [6 ].replace ("," , "." ))
70+ us = int (MICROSEC_PER_SEC * partial_sec )
71+ tz = UTC
72+ if groups [7 ] is not None and groups [7 ] != 'Z' and groups [7 ] != 'z' :
73+ tz_groups = _re_timezone .search (groups [7 ]).groups ()
74+ hour = int (tz_groups [1 ])
75+ minute = 0
76+ if tz_groups [0 ] == "-" :
77+ hour *= - 1
78+ if tz_groups [2 ]:
79+ minute = int (tz_groups [2 ])
80+ tz = TimezoneInfo (hour , minute )
81+ return datetime .datetime (
82+ year = dt [0 ], month = dt [1 ], day = dt [2 ],
83+ hour = dt [3 ], minute = dt [4 ], second = dt [5 ],
84+ microsecond = us , tzinfo = tz )
85+ except Exception :
86+ raise ValueError (f"Error in RFC339 Date Formatting { s } " )
7987
8088def format_rfc3339 (date_time ):
8189 if date_time .tzinfo is None :
0 commit comments