Skip to content

Commit fcec751

Browse files
committed
Fixes normalization of microseconds in durations
1 parent 7f4f5ea commit fcec751

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pendulum/interval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __new__(cls, days=0, seconds=0, microseconds=0,
6060
if total < 0:
6161
m = -1
6262

63-
self._microseconds = abs(round(total % 1 * 1e6)) * m
63+
self._microseconds = round(total % m * 1e6)
6464
self._seconds = abs(int(total)) % SECONDS_PER_DAY * m
6565
self._days = abs(int(total)) // SECONDS_PER_DAY * m
6666

tests/pendulum_tests/test_diff.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
import pendulum
4+
35
from datetime import datetime
46
from contextlib import contextmanager
57
from pendulum import Pendulum
@@ -593,3 +595,13 @@ def test_subtraction(self):
593595

594596
self.assertEqual(3600, (future - d).total_seconds())
595597
self.assertEqual(3600, (future_dt - d).total_seconds())
598+
599+
def test_normalization(self):
600+
d1 = pendulum.create(2012, 1, 1, 1, 2, 3, 123456)
601+
d2 = pendulum.create(2011, 12, 31, 22, 2, 3)
602+
delta = d2 - d1
603+
604+
assert delta.days == 0
605+
assert delta.seconds == -10800
606+
assert delta.microseconds == -123456
607+
assert d1 + delta == d2

0 commit comments

Comments
 (0)