chore: move api-client datetime utils to shared
This commit is contained in:
parent
e232ad4c41
commit
3ce60d4881
|
@ -1,4 +1,3 @@
|
||||||
import datetime
|
|
||||||
import logging
|
import logging
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
@ -164,29 +163,3 @@ class RequestProvider:
|
||||||
return self.requests[attr]
|
return self.requests[attr]
|
||||||
else:
|
else:
|
||||||
return super().__getattribute__(attr)
|
return super().__getattribute__(attr)
|
||||||
|
|
||||||
|
|
||||||
def time_in_seconds(value):
|
|
||||||
return (
|
|
||||||
value.hour * 60 * 60
|
|
||||||
+ value.minute * 60
|
|
||||||
+ value.second
|
|
||||||
+ value.microsecond / 1000000.0
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def time_in_milliseconds(value):
|
|
||||||
return time_in_seconds(value) * 1000
|
|
||||||
|
|
||||||
|
|
||||||
def fromisoformat(time_string):
|
|
||||||
"""
|
|
||||||
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:
|
|
||||||
datetime_obj = datetime.datetime.strptime(time_string, "%H:%M:%S.%f")
|
|
||||||
except ValueError:
|
|
||||||
datetime_obj = datetime.datetime.strptime(time_string, "%H:%M:%S")
|
|
||||||
return datetime_obj.time()
|
|
||||||
|
|
|
@ -11,9 +11,14 @@ from datetime import datetime, timedelta
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
from dateutil.parser import isoparse
|
from dateutil.parser import isoparse
|
||||||
|
from libretime_shared.datetime import (
|
||||||
|
fromisoformat,
|
||||||
|
time_in_milliseconds,
|
||||||
|
time_in_seconds,
|
||||||
|
)
|
||||||
|
|
||||||
from ._config import Config
|
from ._config import Config
|
||||||
from .utils import RequestProvider, fromisoformat, time_in_milliseconds, time_in_seconds
|
from .utils import RequestProvider
|
||||||
|
|
||||||
LIBRETIME_API_VERSION = "2.0"
|
LIBRETIME_API_VERSION = "2.0"
|
||||||
EVENT_KEY_FORMAT = "%Y-%m-%d-%H-%M-%S"
|
EVENT_KEY_FORMAT = "%Y-%m-%d-%H-%M-%S"
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
import datetime
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from libretime_api_client import utils
|
|
||||||
|
|
||||||
|
|
||||||
def test_time_in_seconds():
|
|
||||||
time = datetime.time(hour=0, minute=3, second=34, microsecond=649600)
|
|
||||||
assert abs(utils.time_in_seconds(time) - 214.65) < 0.009
|
|
||||||
|
|
||||||
|
|
||||||
def test_time_in_milliseconds():
|
|
||||||
time = datetime.time(hour=0, minute=0, second=0, microsecond=500000)
|
|
||||||
assert utils.time_in_milliseconds(time) == 500
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"payload, expected",
|
|
||||||
[
|
|
||||||
("00:00:00.500000", datetime.time(microsecond=500000)),
|
|
||||||
("00:04:30.092540", datetime.time(minute=4, second=30, microsecond=92540)),
|
|
||||||
("00:04:30", datetime.time(minute=4, second=30)),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_fromisoformat(payload, expected):
|
|
||||||
assert utils.fromisoformat(payload) == expected
|
|
|
@ -1,4 +1,4 @@
|
||||||
from datetime import time
|
from datetime import datetime, time
|
||||||
|
|
||||||
|
|
||||||
def time_in_seconds(value: time) -> float:
|
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:
|
def time_in_milliseconds(value: time) -> float:
|
||||||
return time_in_seconds(value) * 1000
|
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()
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
from datetime import time
|
from datetime import time
|
||||||
|
|
||||||
from pytest import approx
|
from pytest import approx, mark
|
||||||
|
|
||||||
from libretime_shared.datetime import time_in_milliseconds, time_in_seconds
|
from libretime_shared.datetime import (
|
||||||
|
fromisoformat,
|
||||||
|
time_in_milliseconds,
|
||||||
|
time_in_seconds,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_time_in_seconds():
|
def test_time_in_seconds():
|
||||||
|
@ -13,3 +17,15 @@ def test_time_in_seconds():
|
||||||
def test_time_in_milliseconds():
|
def test_time_in_milliseconds():
|
||||||
value = time(hour=0, minute=0, second=0, microsecond=500000)
|
value = time(hour=0, minute=0, second=0, microsecond=500000)
|
||||||
assert time_in_milliseconds(value) == 500
|
assert time_in_milliseconds(value) == 500
|
||||||
|
|
||||||
|
|
||||||
|
@mark.parametrize(
|
||||||
|
"payload, expected",
|
||||||
|
[
|
||||||
|
("00:00:00.500000", time(microsecond=500000)),
|
||||||
|
("00:04:30.092540", time(minute=4, second=30, microsecond=92540)),
|
||||||
|
("00:04:30", time(minute=4, second=30)),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_fromisoformat(payload, expected):
|
||||||
|
assert fromisoformat(payload) == expected
|
||||||
|
|
Loading…
Reference in New Issue