From 64074eed03e597c7e2342d15f8dd832fe6ff3bab Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Sat, 15 Feb 2020 18:29:58 +0000 Subject: [PATCH] =?UTF-8?q?Say=20=E2=80=981=20hour/month=20ago=E2=80=99=20?= =?UTF-8?q?not=20=E2=80=98an=20hour/a=20month=20ago=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I think it read better without the indefinite article when it’s, for example, placed alongside messages that read ‘2 hours ago’. --- app/__init__.py | 13 +++++++++++-- tests/app/main/views/test_dashboard.py | 9 ++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 6b7197f3c..494c05720 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,5 +1,6 @@ import itertools import os +import re import urllib from datetime import datetime, timedelta, timezone from functools import partial @@ -338,6 +339,14 @@ def _format_datetime_short(datetime): return datetime.strftime('%d %B').lstrip('0') +def naturaltime_without_indefinite_article(date): + return re.sub( + 'an? (.*) ago', + lambda match: '1 {} ago'.format(match.group(1)), + humanize.naturaltime(date), + ) + + def format_delta(date): delta = ( datetime.now(timezone.utc) @@ -348,7 +357,7 @@ def format_delta(date): return "just now" if delta < timedelta(seconds=60): return "in the last minute" - return humanize.naturaltime(delta) + return naturaltime_without_indefinite_article(delta) def format_delta_days(date): @@ -358,7 +367,7 @@ def format_delta_days(date): return "today" if date.strftime('%Y-%M-%D') == (now - timedelta(days=1)).strftime('%Y-%M-%D'): return "yesterday" - return humanize.naturaltime(now - date) + return naturaltime_without_indefinite_article(now - date) def valid_phone_number(phone_number): diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index 6920b840c..3f09b4e18 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -262,9 +262,9 @@ def test_inbound_messages_shows_count_of_messages_when_there_are_no_messages( @pytest.mark.parametrize('index, expected_row', enumerate([ - '07900 900000 message-1 an hour ago', - '07900 900000 message-2 an hour ago', - '07900 900000 message-3 an hour ago', + '07900 900000 message-1 1 hour ago', + '07900 900000 message-2 1 hour ago', + '07900 900000 message-3 1 hour ago', '07900 900002 message-4 3 hours ago', '07900 900004 message-5 5 hours ago', '07900 900006 message-6 7 hours ago', @@ -557,6 +557,9 @@ def test_returned_letters_shows_count_of_recently_returned_letters( ('2020-01-25', ( '0 returned letters latest report 8 days ago' )), + ('2020-01-01', ( + '0 returned letters latest report 1 month ago' + )), ('2019-09-09', ( '0 returned letters latest report 4 months ago' )),