@@ -72,10 +72,25 @@ def __init__(self, api_object: shellhub.models.base.ShellHub, device_json): # t
7272
7373 @staticmethod
7474 def _safe_isoformat_to_datetime (date_string : str ) -> datetime :
75+ # Replace "Z" with "+00:00" to indicate UTC in a format compatible with Python 3.7-3.10.
76+ if date_string .endswith ("Z" ):
77+ date_string = date_string [:- 1 ] + "+00:00"
7578 try :
79+ # Direct conversion using fromisoformat
7680 return datetime .fromisoformat (date_string )
77- except ValueError as e :
78- raise ShellHubApiError (f"Invalid date string: { date_string } (Couldn't convert to datetime)" ) from e
81+ except ValueError :
82+ try :
83+ # For Python versions that do not handle offset-aware datetimes well in fromisoformat
84+ # This part is more of a catch-all to ensure even non-standard or unexpected formats
85+ # might be parsed, but primarily, the first attempt should work for ISO 8601 formats.
86+ # Note: strptime might not be necessary if fromisoformat works after the 'Z' to '+00:00' replacement,
87+ # but it's here as an example if further customization is needed.
88+ return datetime .strptime (date_string , "%Y-%m-%dT%H:%M:%S%z" )
89+ except ValueError as e :
90+ # If the first attempt fails due to the format not being exactly ISO 8601 after 'Z' replacement,
91+ # this additional attempt can catch other variations. This might not be strictly necessary,
92+ # depending on your input formats.
93+ raise ShellHubApiError (f"Invalid date string: { date_string } (Couldn't convert to datetime)" ) from e
7994
8095 def delete (self ) -> bool :
8196 """
0 commit comments