chore: move api-client datetime utils to shared

This commit is contained in:
jo 2022-06-24 16:17:12 +02:00 committed by Kyle Robbertze
parent e232ad4c41
commit 3ce60d4881
5 changed files with 38 additions and 58 deletions

View file

@ -1,4 +1,4 @@
from datetime import time
from datetime import datetime, time
def time_in_seconds(value: time) -> float:
@ -12,3 +12,16 @@ def time_in_seconds(value: time) -> float:
def time_in_milliseconds(value: time) -> float:
return time_in_seconds(value) * 1000
def fromisoformat(value: str) -> time:
"""
This is required for Python 3.6 support. datetime.time.fromisoformat was
only added in Python 3.7. Until LibreTime drops Python 3.6 support, this
wrapper uses the old way of doing it.
"""
try:
obj = datetime.strptime(value, "%H:%M:%S.%f")
except ValueError:
obj = datetime.strptime(value, "%H:%M:%S")
return obj.time()