Use humanize for fuzzy time differences

It seems to do a bit better than ago (e.g. 4 months vs 146 days), and
looks like it’s maintained more often.
This commit is contained in:
Chris Hill-Scott
2020-02-14 15:09:03 +00:00
parent f369f76ae4
commit 9590643527
4 changed files with 11 additions and 20 deletions

View File

@@ -5,7 +5,7 @@ from datetime import datetime, timedelta, timezone
from functools import partial
from time import monotonic
import ago
import humanize
import jinja2
from flask import (
Markup,
@@ -348,28 +348,17 @@ def format_delta(date):
return "just now"
if delta < timedelta(seconds=60):
return "in the last minute"
return ago.human(
delta,
future_tense='{} from now', # No-one should ever see this
past_tense='{} ago',
precision=1
)
return humanize.naturaltime(delta)
def format_delta_days(date):
now = datetime.now(timezone.utc)
date = utc_string_to_aware_gmt_datetime(date)
delta = now - date
if date.strftime('%Y-%M-%D') == now.strftime('%Y-%M-%D'):
return "today"
if date.strftime('%Y-%M-%D') == (now - timedelta(days=1)).strftime('%Y-%M-%D'):
return "yesterday"
return ago.human(
delta,
precision=1,
future_tense='{} from now',
past_tense='{} ago',
)
return humanize.naturaltime(now - date)
def valid_phone_number(phone_number):

View File

@@ -2,6 +2,7 @@
# with package version changes made in requirements-app.txt
ago==0.0.93
humanize==1.0.0
Flask==1.1.1
Flask-WTF==0.14.3
Flask-Login==0.4.1

View File

@@ -4,6 +4,7 @@
# with package version changes made in requirements-app.txt
ago==0.0.93
humanize==1.0.0
Flask==1.1.1
Flask-WTF==0.14.3
Flask-Login==0.4.1
@@ -29,10 +30,10 @@ git+https://github.com/alphagov/notifications-utils.git@36.6.0#egg=notifications
git+https://github.com/alphagov/govuk-frontend-jinja.git@v0.5.1-alpha#egg=govuk-frontend-jinja==0.5.1-alpha
## The following requirements were added by pip freeze:
awscli==1.17.15
awscli==1.18.2
bleach==3.1.0
boto3==1.10.38
botocore==1.14.15
botocore==1.15.2
certifi==2019.11.28
chardet==3.0.4
Click==7.0

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([
'07900 900000 message-1 1 hour ago',
'07900 900000 message-2 1 hour ago',
'07900 900000 message-3 1 hour ago',
'07900 900000 message-1 an hour ago',
'07900 900000 message-2 an hour ago',
'07900 900000 message-3 an hour ago',
'07900 900002 message-4 3 hours ago',
'07900 900004 message-5 5 hours ago',
'07900 900006 message-6 7 hours ago',
@@ -558,7 +558,7 @@ def test_returned_letters_shows_count_of_recently_returned_letters(
'0 returned letters latest report 8 days ago'
)),
('2019-09-09', (
'0 returned letters latest report 146 days ago'
'0 returned letters latest report 4 months ago'
)),
('2010-10-10', (
'0 returned letters latest report 9 years ago'