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

enhanced humanize() #1199

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,15 @@ def humanize(

try:
if granularity == "auto":
# Calculate day difference to handle custom cases
days_diff = (self._datetime.date() - dt.date()).days

if days_diff == 1:
return "Yesterday"

elif days_diff == -1:
return "Tomorrow"

if diff < 10:
return locale.describe("now", only_distance=only_distance)

Expand Down
18 changes: 13 additions & 5 deletions tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1982,8 +1982,8 @@ def test_hours(self):
def test_day(self):
later = self.now.shift(days=1)

assert self.now.humanize(later) == "a day ago"
assert later.humanize(self.now) == "in a day"
assert self.now.humanize(later) == "Tomorrow"
assert later.humanize(self.now) == "Yesterday"

# regression test for issue #697
less_than_48_hours = self.now.shift(
Expand All @@ -1997,9 +1997,6 @@ def test_day(self):
# humanize other argument does not take raw datetime.date objects
self.now.humanize(less_than_48_hours_date)

assert self.now.humanize(later, only_distance=True) == "a day"
assert later.humanize(self.now, only_distance=True) == "a day"

def test_days(self):
later = self.now.shift(days=2)

Expand Down Expand Up @@ -2055,6 +2052,17 @@ def test_month_plus_4_days(self):
assert self.now.humanize(later) == "a month ago"
assert later.humanize(self.now) == "in a month"

def test_humanize_now(self):
assert self.now.humanize() == "just now"

def test_humanize_yesterday(self):
past = self.now.shift(days=-1)
assert self.now.humanize(past) == "Yesterday"

def test_humanize_tomorrow(self):
future = self.now.shift(days=1)
assert self.now.humanize(future) == "Tomorrow"

@pytest.mark.xfail(reason="known issue with humanize month limits")
def test_months(self):
later = self.now.shift(months=2)
Expand Down
Loading