Skip to content

Commit 31f2b52

Browse files
committed
fixes
1 parent 2b998d1 commit 31f2b52

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

onvif/types.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def pythonvalue(self, value: str) -> time:
8484
# For example, 2024-08-17T00:61:16Z so we need
8585
# to fix the overflow
8686
fixed_time, offset = _try_fix_time_overflow(value)
87-
try:
88-
return isodate.parse_time(fixed_time) + timedelta(**offset)
89-
except ValueError:
90-
return isodate.parse_time(value)
87+
if fixed_dt := _try_parse_datetime(f"2024-01-15T{fixed_time}Z"):
88+
return (fixed_dt + timedelta(**offset)).time()
89+
return isodate.parse_time(value)

tests/test_types.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from onvif.client import ONVIFCamera
99
from onvif.settings import DEFAULT_SETTINGS
1010
from onvif.transport import ASYNC_TRANSPORT
11+
from onvif.types import FastDateTime, ForgivingTime
1112

1213
INVALID_TERM_TIME = b'<?xml version="1.0" encoding="UTF-8"?>\r\n<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:tev="http://www.onvif.org/ver10/events/wsdl" xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" xmlns:wsa5="http://www.w3.org/2005/08/addressing" xmlns:chan="http://schemas.microsoft.com/ws/2005/02/duplex" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:tt="http://www.onvif.org/ver10/schema" xmlns:tns1="http://www.onvif.org/ver10/topics">\r\n<SOAP-ENV:Header>\r\n<wsa5:Action>http://www.onvif.org/ver10/events/wsdl/PullPointSubscription/PullMessagesResponse</wsa5:Action>\r\n</SOAP-ENV:Header>\r\n<SOAP-ENV:Body>\r\n<tev:PullMessagesResponse>\r\n<tev:CurrentTime>2024-08-17T00:56:16Z</tev:CurrentTime>\r\n<tev:TerminationTime>2024-08-17T00:61:16Z</tev:TerminationTime>\r\n</tev:PullMessagesResponse>\r\n</SOAP-ENV:Body>\r\n</SOAP-ENV:Envelope>\r\n'
1314
_WSDL_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "onvif", "wsdl")
@@ -37,3 +38,30 @@ async def test_parse_invalid_time(caplog: pytest.LogCaptureFixture) -> None:
3738
2024, 8, 17, 1, 1, 16, tzinfo=datetime.timezone.utc
3839
)
3940
assert "ValueError" not in caplog.text
41+
42+
43+
def test_fix_datetime_overflow() -> None:
44+
assert FastDateTime().pythonvalue("2024-08-17T00:61:16Z") == datetime.datetime(
45+
2024, 8, 17, 1, 1, 16, tzinfo=datetime.timezone.utc
46+
)
47+
assert FastDateTime().pythonvalue("2024-08-17T00:60:16Z") == datetime.datetime(
48+
2024, 8, 17, 1, 0, 16, tzinfo=datetime.timezone.utc
49+
)
50+
assert FastDateTime().pythonvalue("2024-08-17T00:59:16Z") == datetime.datetime(
51+
2024, 8, 17, 0, 59, 16, tzinfo=datetime.timezone.utc
52+
)
53+
assert FastDateTime().pythonvalue("2024-08-17T23:59:59Z") == datetime.datetime(
54+
2024, 8, 17, 23, 59, 59, tzinfo=datetime.timezone.utc
55+
)
56+
assert FastDateTime().pythonvalue("2024-08-17T24:00:00Z") == datetime.datetime(
57+
2024, 8, 18, 0, 0, 0, tzinfo=datetime.timezone.utc
58+
)
59+
60+
61+
def test_fix_time_overflow() -> None:
62+
assert ForgivingTime().pythonvalue("24:00:00") == datetime.time(0, 0, 0)
63+
assert ForgivingTime().pythonvalue("23:59:59") == datetime.time(23, 59, 59)
64+
assert ForgivingTime().pythonvalue("23:59:60") == datetime.time(0, 0, 0)
65+
assert ForgivingTime().pythonvalue("23:59:61") == datetime.time(0, 0, 1)
66+
assert ForgivingTime().pythonvalue("23:60:00") == datetime.time(0, 0, 0)
67+
assert ForgivingTime().pythonvalue("23:61:00") == datetime.time(0, 1, 0)

0 commit comments

Comments
 (0)