Skip to content

Commit dc9923f

Browse files
committed
Fixes absolute intervals
1 parent 552c34d commit dc9923f

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Fix] Fixes setters and modifiers (start_of/end_of) to properly apply transitions.
1010
- [Fix] Fixes issue when compiling on 32 bit systems. (Thanks to [guyzmo](https://github.com/guyzmo))
1111
- [Fix] Fixes NameError Exception on Python 3.2. (Thanks to [guyzmo](https://github.com/guyzmo))
12+
- [Fix] Fixes absolute intervals.
1213

1314
### 0.5.5
1415

pendulum/interval.py

Lines changed: 6 additions & 6 deletions
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 = round(total % 1 * 1e6)
63+
self._microseconds = abs(round(total % 1 * 1e6)) * m
6464
self._seconds = abs(int(total)) % SECONDS_PER_DAY * m
6565
self._days = abs(int(total)) // SECONDS_PER_DAY * m
6666

@@ -292,16 +292,16 @@ class AbsoluteInterval(Interval):
292292

293293
def __new__(cls, days=0, seconds=0, microseconds=0,
294294
milliseconds=0, minutes=0, hours=0, weeks=0):
295-
self = super(AbsoluteInterval, cls).__new__(
295+
self = timedelta.__new__(
296296
cls, days, seconds, microseconds,
297297
milliseconds, minutes, hours, weeks
298298
)
299299

300300
# Intuitive normalization
301-
total = self.total_seconds()
301+
total = abs(self.total_seconds())
302302

303-
self._microseconds = abs(round(total % 1 * 1e6))
304-
self._seconds = abs(int(total)) % SECONDS_PER_DAY
305-
self._days = abs(int(total)) // SECONDS_PER_DAY
303+
self._microseconds = round(total % 1 * 1e6)
304+
self._seconds = int(total) % SECONDS_PER_DAY
305+
self._days = int(total) // SECONDS_PER_DAY
306306

307307
return self

tests/interval_tests/test_construct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_instance(self):
5050
def test_absolute_interval(self):
5151
pi = AbsoluteInterval(days=-1177, seconds=-7284, microseconds=-1000001)
5252
self.assertInterval(pi, 168, 1, 2, 1, 25)
53-
self.assertEqual(999999, pi.microseconds)
53+
self.assertEqual(1, pi.microseconds)
5454

5555
def test_invert(self):
5656
pi = Interval(days=1177, seconds=7284, microseconds=1000000)

0 commit comments

Comments
 (0)