feat: mini pretty dates (#31331)

Narrow format pretty dates for list views, works the same way as client
side function.
This commit is contained in:
Ankush Menat 2025-02-20 10:48:45 +05:30 committed by GitHub
parent 6113452b73
commit 17d6d81fb5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 3 deletions

View file

@ -685,7 +685,6 @@ class TestDateUtils(IntegrationTestCase):
def test_pretty_date(self):
from frappe import _
# differnt cases
now = get_datetime()
test_cases = {
@ -707,6 +706,8 @@ class TestDateUtils(IntegrationTestCase):
for dt, exp_message in test_cases.items():
self.assertEqual(pretty_date(dt), exp_message)
self.assertEqual(pretty_date(add_to_date(now, days=-5), mini=True), "5d")
def test_date_from_timegrain(self):
start_date = getdate("2021-01-01")

View file

@ -1692,7 +1692,7 @@ def escape_html(text: str) -> str:
return "".join(html_escape_table.get(c, c) for c in text)
def pretty_date(iso_datetime: datetime.datetime | str) -> str:
def pretty_date(iso_datetime: datetime.datetime | str, mini=False) -> str:
"""Return a localized string representation of the delta to the current system time.
For example, "1 hour ago", "2 days ago", "in 5 seconds", etc.
@ -1706,7 +1706,12 @@ def pretty_date(iso_datetime: datetime.datetime | str) -> str:
iso_datetime = get_datetime(iso_datetime)
now_dt = now_datetime()
locale = frappe.local.lang.replace("-", "_") if frappe.local.lang else None
return format_timedelta(iso_datetime - now_dt, add_direction=True, locale=locale)
return format_timedelta(
iso_datetime - now_dt,
add_direction=not mini,
locale=locale,
format="long" if not mini else "narrow",
)
def comma_or(some_list: list | tuple, add_quotes=True) -> str: