Skip to content

Commit 055e147

Browse files
committed
Fixes diff_for_humans() when you cross DST transition.
1 parent c2a0a59 commit 055e147

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

pendulum/formatting/difference_formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ def diff_for_humans(self, date, other=None, absolute=False, locale=None):
6363

6464
if diff.remaining_days > 3:
6565
count += 1
66-
elif diff.days > 0:
66+
elif diff.remaining_days > 0:
6767
unit = 'day'
68-
count = diff.days
68+
count = diff.remaining_days
6969
elif diff.hours > 0:
7070
unit = 'hour'
7171
count = diff.hours

pendulum/period.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def days(self):
9999
def remaining_days(self):
100100
return abs(self._delta['days']) % 7 * self._sign(self._days)
101101

102+
@property
103+
def hours(self):
104+
return self._delta['hours']
105+
106+
@property
107+
def minutes(self):
108+
return self._delta['minutes']
109+
102110
@property
103111
def start(self):
104112
return self._start

tests/date_tests/test_diff.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,17 @@ def test_diff_for_humans_absolute_years(self):
284284
self.assertEqual('1 year', Date.today().diff_for_humans(Date.today().subtract(years=1), True))
285285
self.assertEqual('1 year', Date.today().diff_for_humans(Date.today().add(years=1), True))
286286

287+
def test_diff_accuracy(self):
288+
# DST
289+
today = Pendulum.create(2017, 3, 7, tz='America/Toronto')
290+
291+
with self.wrap_with_test_now(today):
292+
diff = today.add(days=6).diff(today, abs=True)
293+
self.assertEqual(5, diff.days)
294+
self.assertEqual(6, diff.remaining_days)
295+
self.assertEqual(0, diff.hours)
296+
self.assertEqual('6 days', diff.in_words())
297+
287298
def test_diff_for_humans_accuracy(self):
288299
today = Pendulum.today()
289300

@@ -297,6 +308,11 @@ def test_diff_for_humans_accuracy(self):
297308
self.assertEqual('2 weeks', today.add(days=14).diff_for_humans(absolute=True))
298309
self.assertEqual('2 weeks', today.add(days=13).diff_for_humans(absolute=True))
299310

311+
# DST
312+
today = Pendulum.create(2017, 3, 7, tz='America/Toronto')
313+
with self.wrap_with_test_now(today):
314+
self.assertEqual('6 days', today.add(days=6).diff_for_humans(absolute=True))
315+
300316
def test_subtraction(self):
301317
d = Date(2016, 7, 5)
302318
future_dt = date(2016, 7, 6)

tests/pendulum_tests/test_diff.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,11 @@ def test_diff_for_humans_accuracy(self):
573573
self.assertEqual('1 year', now.add(years=1, months=3).diff_for_humans(absolute=True))
574574
self.assertEqual('2 years', now.add(years=1, months=8).diff_for_humans(absolute=True))
575575

576+
# DST
577+
now = Pendulum.create(2017, 3, 7, tz='America/Toronto')
578+
with self.wrap_with_test_now(now):
579+
self.assertEqual('6 days', now.add(days=6).diff_for_humans(absolute=True))
580+
576581
def test_seconds_since_midnight(self):
577582
d = Pendulum.create(2016, 7, 5, 12, 32, 25, 0)
578583
self.assertEqual(25 + 32 * 60 + 12 * 3600, d.seconds_since_midnight())

0 commit comments

Comments
 (0)