Skip to content

Commit e4d68e4

Browse files
committed
Saved at 2025-01-04 10:51:01 (Sat)
1 parent fd056fb commit e4d68e4

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

src/tests/test_datetime.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
text_clean,
121121
zoned_datetimes,
122122
)
123-
from utilities.math import is_integral
123+
from utilities.math import MAX_INT32, MIN_INT32, is_integral, round_to_float
124124
from utilities.zoneinfo import UTC, HongKong, Tokyo
125125

126126
if TYPE_CHECKING:
@@ -129,6 +129,7 @@
129129
from utilities.types import Number
130130

131131

132+
@mark.skip
132133
class TestAddDuration:
133134
@given(date=dates(), days=integers())
134135
def test_date(self, *, date: dt.date, days: int) -> None:
@@ -215,7 +216,6 @@ def test_datetime(self, *, datetime: dt.datetime) -> None:
215216
check_zoned_datetime(datetime)
216217

217218

218-
@mark.only
219219
class TestDateDurationToInt:
220220
@given(n=integers())
221221
def test_int(self, *, n: int) -> None:
@@ -253,7 +253,6 @@ def test_error_timedelta(self, *, n: int, frac: dt.timedelta) -> None:
253253
_ = date_duration_to_int(timedelta)
254254

255255

256-
@mark.only
257256
class TestDateDurationToTimeDelta:
258257
@given(n=integers())
259258
def test_int(self, *, n: int) -> None:
@@ -310,28 +309,34 @@ def test_main(self, *, date: dt.date) -> None:
310309

311310

312311
class TestDateTimeDurationToFloat:
313-
@given(duration=integers(0, 10) | floats(0.0, 10.0))
314-
def test_number(self, *, duration: Number) -> None:
315-
result = datetime_duration_to_float(duration)
316-
assert result == duration
312+
@given(n=int32s())
313+
def test_int(self, *, n: int) -> None:
314+
result = datetime_duration_to_float(n)
315+
assert result == n
317316

318-
@given(duration=timedeltas())
319-
def test_timedelta(self, *, duration: dt.timedelta) -> None:
320-
result = datetime_duration_to_float(duration)
321-
assert result == duration.total_seconds()
317+
@given(n=floats(allow_nan=False, allow_infinity=False))
318+
def test_float(self, *, n: Number) -> None:
319+
result = datetime_duration_to_float(n)
320+
assert result == n
321+
322+
@given(timedelta=timedeltas())
323+
def test_timedelta(self, *, timedelta: dt.timedelta) -> None:
324+
result = datetime_duration_to_float(timedelta)
325+
assert result == timedelta.total_seconds()
322326

323327

324328
class TestDateTimeDurationToTimedelta:
325-
@given(duration=integers(0, 10))
326-
def test_int(self, *, duration: int) -> None:
327-
result = datetime_duration_to_timedelta(duration)
328-
assert result.total_seconds() == duration
329+
@given(n=int32s())
330+
def test_int(self, *, n: int) -> None:
331+
result = datetime_duration_to_timedelta(n)
332+
assert result.total_seconds() == n
329333

330-
@given(duration=floats(0.0, 10.0))
331-
def test_float(self, *, duration: float) -> None:
332-
duration = round(10 * duration) / 10
333-
result = datetime_duration_to_timedelta(duration)
334-
assert isclose(result.total_seconds(), duration)
334+
@given(n=floats(min_value=MIN_INT32, max_value=MAX_INT32))
335+
def test_float(self, *, n: float) -> None:
336+
n = round_to_float(n, 1e-6)
337+
with assume_does_not_raise(OverflowError):
338+
result = datetime_duration_to_timedelta(n)
339+
assert isclose(result.total_seconds(), n)
335340

336341
@given(duration=timedeltas())
337342
def test_timedelta(self, *, duration: dt.timedelta) -> None:

src/utilities/datetime.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,26 @@ def __str__(self) -> str:
242242

243243
def datetime_duration_to_float(duration: Duration, /) -> float:
244244
"""Ensure a datetime duration is a float."""
245-
if isinstance(duration, int):
246-
return float(duration)
247-
if isinstance(duration, float):
248-
return duration
249-
return duration.total_seconds()
245+
match duration:
246+
case int():
247+
return float(duration)
248+
case float():
249+
return duration
250+
case dt.timedelta():
251+
return duration.total_seconds()
252+
case _ as never: # pyright: ignore[reportUnnecessaryComparison]
253+
assert_never(never)
250254

251255

252256
def datetime_duration_to_timedelta(duration: Duration, /) -> dt.timedelta:
253257
"""Ensure a datetime duration is a timedelta."""
254-
if isinstance(duration, int | float):
255-
return dt.timedelta(seconds=duration)
256-
return duration
258+
match duration:
259+
case int() | float():
260+
return dt.timedelta(seconds=duration)
261+
case dt.timedelta():
262+
return duration
263+
case _ as never: # pyright: ignore[reportUnnecessaryComparison]
264+
assert_never(never)
257265

258266

259267
##

0 commit comments

Comments
 (0)