More robust time string handling

This commit is contained in:
awsr
2025-12-27 16:58:37 -08:00
parent 214d84feea
commit b25bfb6cba
+10 -3
View File
@@ -13,9 +13,16 @@ if not os.path.exists(extensions_dir):
def parse_isotime(time_string: str) -> datetime:
# If Python minimum version is 3.11+, this function can be replaced with datetime.fromisoformat()
time_string = time_string.rstrip("Z")
time_string = time_string[:-4] if "." in time_string[-4:] else time_string
return datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc)
trimmed = time_string.rstrip("Z")
if "." in trimmed:
trimmed = trimmed.split(".")[0]
match len(trimmed):
case 16:
return datetime.strptime(trimmed, "%Y-%m-%dT%H:%M").replace(tzinfo=timezone.utc)
case 19:
return datetime.strptime(trimmed, "%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc)
case _:
raise ValueError(f"Unexpected time string format: '{time_string}'")
def format_eztime(d: datetime, local = False) -> str: