Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct humanize behaviour with datetime.date objects #726

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,18 @@ def humanize(
return locale.describe("hours", hours, only_distance=only_distance)

elif diff < 129600:
if (
self._datetime.month == dt.month
and self._datetime.day == dt.day - 2
):
days = sign * int(max(delta / 86400, 2))
return locale.describe(
"days", days, only_distance=only_distance
)
return locale.describe("day", sign, only_distance=only_distance)
elif diff < 172801:
days = sign * int(max(delta / 86400, 2))
return locale.describe("days", days, only_distance=only_distance)
elif diff < 554400:
days = sign * int(max(delta / 86400, 2))
return locale.describe("days", days, only_distance=only_distance)
Expand Down
20 changes: 19 additions & 1 deletion tests/arrow_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from dateutil.relativedelta import FR, MO, SA, SU, TH, TU, WE
from mock import patch

from arrow import arrow, util
from arrow import arrow, factory, util


def assertDtEqual(dt1, dt2, within=10):
Expand Down Expand Up @@ -1682,6 +1682,24 @@ def test_untranslated_granularity(self):

with self.assertRaises(ValueError):
arw.humanize(later, granularity="week")

def test_oneday_date(self):

yesterday = datetime.utcnow() - timedelta(1)
result = factory.ArrowFactory().get(yesterday).humanize()

self.assertEqual(result, "a day ago")

yesterday_date = yesterday.date()
result = factory.ArrowFactory().get(yesterday_date).humanize()

self.assertEqual(result, "a day ago")

two_days_ago = datetime.utcnow() - timedelta(2)
new_two_days_ago = two_days_ago.replace(hour=23, minute=59)
result = factory.ArrowFactory().get(new_two_days_ago).humanize()

self.assertEqual(result, "2 days ago")


class ArrowHumanizeTestsWithLocale(Chai):
Expand Down