Coverage for value_object_pattern/usables/dates/time/string_time_value_object.py: 62.50%
16 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
1"""
2Provide a normalized ISO 8601 time string value object.
3"""
5from datetime import time 1abcdefghij
6from typing import NoReturn 1abcdefghij
8from value_object_pattern.decorators import process, validation 1abcdefghij
9from value_object_pattern.usables import NotEmptyStringValueObject, TrimmedStringValueObject 1abcdefghij
12class StringTimeValueObject(NotEmptyStringValueObject, TrimmedStringValueObject): 1abcdefghij
13 """
14 Validate and normalize a string accepted by `datetime.time.fromisoformat`.
16 Valid inputs are stored in the ISO 8601 representation produced by `datetime.time.isoformat`. For example,
17 `14:30` becomes `14:30:00` and a `Z` UTC suffix becomes `+00:00`.
19 Example:
20 ```python
21 from value_object_pattern.usables.dates import StringTimeValueObject
23 meeting_time = StringTimeValueObject(value='14:30')
24 print(repr(meeting_time))
25 # >>> StringTimeValueObject(value='14:30:00')
26 ```
27 """
29 _internal_time_object: time
31 @process(order=0) 1abcdefghij
32 def _normalize_time(self, value: str) -> str: 1abcdefghij
33 """
34 Normalize the parsed time to its ISO 8601 representation.
36 Args:
37 value (str): The validated time string.
39 Returns:
40 str: The normalized time string.
41 """
42 return self._internal_time_object.isoformat()
44 @validation(order=1) 1abcdefghij
45 def _ensure_value_is_time(self, value: str) -> None: 1abcdefghij
46 """
47 Parse and retain the time represented by the string.
49 Args:
50 value (str): The time string to validate.
52 Raises:
53 ValueError: If the value is not accepted by `datetime.time.fromisoformat`.
54 """
55 try:
56 self._internal_time_object = time.fromisoformat(value)
58 except ValueError:
59 self._raise_value_is_not_valid_time(value=value)
61 def _raise_value_is_not_valid_time(self, value: str) -> NoReturn: 1abcdefghij
62 """
63 Raise an error for an invalid time string.
65 Args:
66 value (str): The invalid time string.
68 Raises:
69 ValueError: Always raised with the invalid value.
70 """
71 raise ValueError(f'StringTimeValueObject value <<<{value}>>> is not a valid time.')