Skip to content

Commit 3020cef

Browse files
committed
fix: duration float division raises ValueError
1 parent 8012a7e commit 3020cef

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

pendulum/duration.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def _divide_and_round(a, b):
2323
# Based on the reference implementation for divmod_near
2424
# in Objects/longobject.c.
2525
q, r = divmod(a, b)
26+
27+
if isinstance(q, float) and q == int(q):
28+
q = int(q)
29+
2630
# round up if either r / b > 0.5, or r / b == 0.5 and q is odd.
2731
# The expression r / b > 0.5 is equivalent to 2 * r > b if b is
2832
# positive, 2 * r < b if b negative.

tests/duration/test_arithmetic.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,24 @@ def test_divide():
3838
assert isinstance(mul, pendulum.Duration)
3939
assert_duration(mul, 0, 0, 0, 1, 0, 0, 17, 761111)
4040

41+
it = pendulum.duration(days=2, seconds=35, microseconds=522222)
42+
mul = it / 1.1
43+
44+
assert isinstance(mul, pendulum.Duration)
45+
assert_duration(mul, 0, 0, 0, 1, 19, 38, 43, 202020)
46+
4147
it = pendulum.duration(years=2, months=4, days=2, seconds=35, microseconds=522222)
4248
mul = it / 2
4349

4450
assert isinstance(mul, pendulum.Duration)
4551
assert_duration(mul, 1, 2, 0, 1, 0, 0, 17, 761111)
4652

53+
it = pendulum.duration(years=2, months=4, days=2, seconds=35, microseconds=522222)
54+
mul = it / 2.0
55+
56+
assert isinstance(mul, pendulum.Duration)
57+
assert_duration(mul, 1, 2, 0, 1, 0, 0, 17, 761111)
58+
4759

4860
def test_floor_divide():
4961
it = pendulum.duration(days=2, seconds=34, microseconds=522222)

0 commit comments

Comments
 (0)