Coverage for value_object_pattern/usables/dates/duration_value_object.py: 55.26%

36 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-11-21 14:31 +0000

1""" 

2Provide the unrestricted duration value object. 

3""" 

4 

5from datetime import timedelta 1abcdefghij

6from typing import Any, NoReturn, Self 1abcdefghij

7 

8from value_object_pattern.decorators import validation 1abcdefghij

9from value_object_pattern.models import ValueObject 1abcdefghij

10from value_object_pattern.usables import IntegerValueObject 1abcdefghij

11 

12 

13class DurationValueObject(ValueObject[timedelta]): 1abcdefghij

14 """ 

15 Validate and store an exact `datetime.timedelta` value. 

16 

17 Positive, zero, and negative durations are accepted. Named constructors accept whole units, and conversion methods 

18 return the complete duration in the requested unit, including days instead of only the remainder exposed by 

19 `timedelta.seconds`. Use a sign-constrained subclass when the domain requires a positive or negative duration. 

20 

21 Example: 

22 ```python 

23 from datetime import timedelta 

24 

25 from value_object_pattern.usables.dates import DurationValueObject 

26 

27 timeout = DurationValueObject(value=timedelta(seconds=30)) 

28 print(repr(timeout)) 

29 # >>> DurationValueObject(value=datetime.timedelta(seconds=30)) 

30 ``` 

31 """ 

32 

33 @classmethod 1abcdefghij

34 def from_seconds(cls, *, seconds: int, title: str | None = None, parameter: str | None = None) -> Self: 1abcdefghij

35 """ 

36 Create a duration from a whole number of seconds. 

37 

38 Args: 

39 seconds (int): The number of seconds. 

40 title (str | None, optional): Title used in validation errors. Defaults to None. 

41 parameter (str | None, optional): Parameter used in validation errors. Defaults to None. 

42 

43 Raises: 

44 TypeError: If `seconds` is not an integer. 

45 

46 Returns: 

47 Self: A duration containing the requested number of seconds. 

48 

49 Example: 

50 ```python 

51 from value_object_pattern.usables.dates import DurationValueObject 

52 

53 timeout = DurationValueObject.from_seconds(seconds=30) 

54 print(repr(timeout.value)) 

55 # >>> datetime.timedelta(seconds=30) 

56 ``` 

57 """ 

58 IntegerValueObject(value=seconds, parameter='seconds') 

59 

60 return cls(value=timedelta(seconds=seconds), title=title, parameter=parameter) 

61 

62 @classmethod 1abcdefghij

63 def from_minutes(cls, *, minutes: int, title: str | None = None, parameter: str | None = None) -> Self: 1abcdefghij

64 """ 

65 Create a duration from a whole number of minutes. 

66 

67 Args: 

68 minutes (int): The number of minutes. 

69 title (str | None, optional): Title used in validation errors. Defaults to None. 

70 parameter (str | None, optional): Parameter used in validation errors. Defaults to None. 

71 

72 Raises: 

73 TypeError: If `minutes` is not an integer. 

74 

75 Returns: 

76 Self: A duration containing the requested number of minutes. 

77 

78 Example: 

79 ```python 

80 from value_object_pattern.usables.dates import DurationValueObject 

81 

82 timeout = DurationValueObject.from_minutes(minutes=5) 

83 print(repr(timeout.value)) 

84 # >>> datetime.timedelta(seconds=300) 

85 ``` 

86 """ 

87 IntegerValueObject(value=minutes, parameter='minutes') 

88 

89 return cls(value=timedelta(minutes=minutes), title=title, parameter=parameter) 

90 

91 @classmethod 1abcdefghij

92 def from_hours(cls, *, hours: int, title: str | None = None, parameter: str | None = None) -> Self: 1abcdefghij

93 """ 

94 Create a duration from a whole number of hours. 

95 

96 Args: 

97 hours (int): The number of hours. 

98 title (str | None, optional): Title used in validation errors. Defaults to None. 

99 parameter (str | None, optional): Parameter used in validation errors. Defaults to None. 

100 

101 Raises: 

102 TypeError: If `hours` is not an integer. 

103 

104 Returns: 

105 Self: A duration containing the requested number of hours. 

106 

107 Example: 

108 ```python 

109 from value_object_pattern.usables.dates import DurationValueObject 

110 

111 timeout = DurationValueObject.from_hours(hours=2) 

112 print(repr(timeout.value)) 

113 # >>> datetime.timedelta(seconds=7200) 

114 ``` 

115 """ 

116 IntegerValueObject(value=hours, parameter='hours') 

117 

118 return cls(value=timedelta(hours=hours), title=title, parameter=parameter) 

119 

120 @classmethod 1abcdefghij

121 def from_days(cls, *, days: int, title: str | None = None, parameter: str | None = None) -> Self: 1abcdefghij

122 """ 

123 Create a duration from a whole number of days. 

124 

125 Args: 

126 days (int): The number of days. 

127 title (str | None, optional): Title used in validation errors. Defaults to None. 

128 parameter (str | None, optional): Parameter used in validation errors. Defaults to None. 

129 

130 Raises: 

131 TypeError: If `days` is not an integer. 

132 

133 Returns: 

134 Self: A duration containing the requested number of days. 

135 

136 Example: 

137 ```python 

138 from value_object_pattern.usables.dates import DurationValueObject 

139 

140 timeout = DurationValueObject.from_days(days=1) 

141 print(repr(timeout.value)) 

142 # >>> datetime.timedelta(days=1) 

143 ``` 

144 """ 

145 IntegerValueObject(value=days, parameter='days') 

146 

147 return cls(value=timedelta(days=days), title=title, parameter=parameter) 

148 

149 def to_seconds(self) -> float: 1abcdefghij

150 """ 

151 Return the complete duration in seconds. 

152 

153 Returns: 

154 float: Total elapsed seconds, including fractional microseconds and full days. 

155 

156 Example: 

157 ```python 

158 from value_object_pattern.usables.dates import DurationValueObject 

159 

160 duration = DurationValueObject.from_minutes(minutes=5) 

161 print(duration.to_seconds()) 

162 # >>> 300.0 

163 ``` 

164 """ 

165 return self.value.total_seconds() 

166 

167 def to_minutes(self) -> float: 1abcdefghij

168 """ 

169 Return the complete duration in minutes. 

170 

171 Returns: 

172 float: Total elapsed minutes, including fractional seconds and full days. 

173 

174 Example: 

175 ```python 

176 from value_object_pattern.usables.dates import DurationValueObject 

177 

178 duration = DurationValueObject.from_seconds(seconds=90) 

179 print(duration.to_minutes()) 

180 # >>> 1.5 

181 ``` 

182 """ 

183 return self.value / timedelta(minutes=1) 

184 

185 def to_hours(self) -> float: 1abcdefghij

186 """ 

187 Return the complete duration in hours. 

188 

189 Returns: 

190 float: Total elapsed hours, including fractional minutes and full days. 

191 

192 Example: 

193 ```python 

194 from value_object_pattern.usables.dates import DurationValueObject 

195 

196 duration = DurationValueObject.from_minutes(minutes=90) 

197 print(duration.to_hours()) 

198 # >>> 1.5 

199 ``` 

200 """ 

201 return self.value / timedelta(hours=1) 

202 

203 def to_days(self) -> float: 1abcdefghij

204 """ 

205 Return the complete duration in days. 

206 

207 Returns: 

208 float: Total elapsed days, including fractional hours. 

209 

210 Example: 

211 ```python 

212 from value_object_pattern.usables.dates import DurationValueObject 

213 

214 duration = DurationValueObject.from_hours(hours=36) 

215 print(duration.to_days()) 

216 # >>> 1.5 

217 ``` 

218 """ 

219 return self.value / timedelta(days=1) 

220 

221 @validation(order=0) 1abcdefghij

222 def _ensure_value_is_timedelta(self, value: timedelta) -> None: 1abcdefghij

223 """ 

224 Ensure the value is exactly a datetime.timedelta. 

225 

226 Args: 

227 value (timedelta): The value to validate. 

228 

229 Raises: 

230 TypeError: If the value is not exactly a datetime.timedelta. 

231 """ 

232 if type(value) is not timedelta: 

233 self._raise_value_is_not_timedelta(value=value) 

234 

235 def _raise_value_is_not_timedelta(self, value: Any) -> NoReturn: 1abcdefghij

236 """ 

237 Raise an error for a non-timedelta value. 

238 

239 Args: 

240 value (Any): The invalid value. 

241 

242 Raises: 

243 TypeError: Always raised with the invalid type. 

244 """ 

245 raise TypeError(f'DurationValueObject value <<<{value}>>> must be a timedelta. Got <<<{type(value).__name__}>>> type.') # noqa: E501 # fmt: skip