From 701200303939229bcd7db614d4eb1b9cd560ed71 Mon Sep 17 00:00:00 2001 From: psyuktha <1ds22is191@dsce.edu.in> Date: Fri, 25 Oct 2024 13:20:21 +0530 Subject: [PATCH] enhanced humanize() --- arrow/arrow.py | 9 +++++++++ tests/test_arrow.py | 18 +++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/arrow/arrow.py b/arrow/arrow.py index 9d1f5e30..d3d59ab6 100644 --- a/arrow/arrow.py +++ b/arrow/arrow.py @@ -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) diff --git a/tests/test_arrow.py b/tests/test_arrow.py index daf6209f..62467e06 100644 --- a/tests/test_arrow.py +++ b/tests/test_arrow.py @@ -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( @@ -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) @@ -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)