Say ‘1 hour/month ago’ not ‘an hour/a month ago’

I think it read better without the indefinite article when it’s, for
example, placed alongside messages that read ‘2 hours ago’.
This commit is contained in:
Chris Hill-Scott
2020-02-15 18:29:58 +00:00
parent 9590643527
commit 64074eed03
2 changed files with 17 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
import itertools import itertools
import os import os
import re
import urllib import urllib
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from functools import partial from functools import partial
@@ -338,6 +339,14 @@ def _format_datetime_short(datetime):
return datetime.strftime('%d %B').lstrip('0') 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): def format_delta(date):
delta = ( delta = (
datetime.now(timezone.utc) datetime.now(timezone.utc)
@@ -348,7 +357,7 @@ def format_delta(date):
return "just now" return "just now"
if delta < timedelta(seconds=60): if delta < timedelta(seconds=60):
return "in the last minute" return "in the last minute"
return humanize.naturaltime(delta) return naturaltime_without_indefinite_article(delta)
def format_delta_days(date): def format_delta_days(date):
@@ -358,7 +367,7 @@ def format_delta_days(date):
return "today" return "today"
if date.strftime('%Y-%M-%D') == (now - timedelta(days=1)).strftime('%Y-%M-%D'): if date.strftime('%Y-%M-%D') == (now - timedelta(days=1)).strftime('%Y-%M-%D'):
return "yesterday" return "yesterday"
return humanize.naturaltime(now - date) return naturaltime_without_indefinite_article(now - date)
def valid_phone_number(phone_number): def valid_phone_number(phone_number):

View File

@@ -262,9 +262,9 @@ def test_inbound_messages_shows_count_of_messages_when_there_are_no_messages(
@pytest.mark.parametrize('index, expected_row', enumerate([ @pytest.mark.parametrize('index, expected_row', enumerate([
'07900 900000 message-1 an hour ago', '07900 900000 message-1 1 hour ago',
'07900 900000 message-2 an hour ago', '07900 900000 message-2 1 hour ago',
'07900 900000 message-3 an hour ago', '07900 900000 message-3 1 hour ago',
'07900 900002 message-4 3 hours ago', '07900 900002 message-4 3 hours ago',
'07900 900004 message-5 5 hours ago', '07900 900004 message-5 5 hours ago',
'07900 900006 message-6 7 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', ( ('2020-01-25', (
'0 returned letters latest report 8 days ago' '0 returned letters latest report 8 days ago'
)), )),
('2020-01-01', (
'0 returned letters latest report 1 month ago'
)),
('2019-09-09', ( ('2019-09-09', (
'0 returned letters latest report 4 months ago' '0 returned letters latest report 4 months ago'
)), )),