File tree Expand file tree Collapse file tree 1 file changed +12
-12
lines changed
Expand file tree Collapse file tree 1 file changed +12
-12
lines changed Original file line number Diff line number Diff line change @@ -10,18 +10,18 @@ def datetime_from_str(date_str: str) -> datetime:
1010 if not isinstance (date_str , str ):
1111 raise TypeError (f"Could not convert str to datetime, invalid type: { type (date_str ).__name__ } " )
1212
13- if "." not in date_str :
14- return datetime . strptime ( date_str , "%Y-%m-%dT%H:%M:%SZ" ) # raises ValueError if format does not match
15-
16- # Based on the https://www.w3.org/TR/xmlschema11-2/#dateTimeStamp
17- # The secondFrag allows fractional second notation as well.
18- # truncate the microsecond part
19- date_str = re . sub ( r"\.\d*Z$" , "Z" , date_str )
20- warnings . warn (
21- "Invalid date format. Expected YYYY-MM-DDThh:mm:ssZ. Sub-second fractions have been discarded." ,
22- category = UserWarning ,
23- stacklevel = 2 ,
24- )
13+ truncated_date_str = re . sub ( r"\.\d*Z$" , "Z" , date_str )
14+ if truncated_date_str != date_str :
15+ # Based on the https://www.w3.org/TR/xmlschema11-2/#dateTimeStamp
16+ # The secondFrag allows fractional second notation as well.
17+ # truncate the microsecond part
18+ warnings . warn (
19+ "Invalid date format. Expected YYYY-MM-DDThh:mm:ssZ. Sub-second fractions have been discarded." ,
20+ category = UserWarning ,
21+ stacklevel = 2 ,
22+ )
23+ date_str = truncated_date_str
24+
2525 return datetime .strptime (date_str , "%Y-%m-%dT%H:%M:%SZ" ) # raises ValueError if format does not match
2626
2727
You can’t perform that action at this time.
0 commit comments