diff --git a/app/formatters.py b/app/formatters.py index f62d23f88..c00f6d85c 100644 --- a/app/formatters.py +++ b/app/formatters.py @@ -9,7 +9,6 @@ from numbers import Number import ago import dateutil import humanize -import pytz from flask import Markup, current_app, url_for from notifications_utils.field import Field from notifications_utils.formatters import make_quotes_smart @@ -166,11 +165,12 @@ def naturaltime_without_indefinite_article(date): def format_delta(date): + # This method assumes that date is in UTC date = parse_naive_dt(date) delta = ( - datetime.now(timezone.utc) + datetime.utcnow() ) - ( - convert_utc_to_local_timezone(date).replace(tzinfo=pytz.utc) + date ) if delta < timedelta(seconds=30): return "just now" @@ -180,9 +180,9 @@ def format_delta(date): def format_delta_days(date): + # This method assumes that date is in UTC date = parse_naive_dt(date) - now = datetime.now(timezone.utc) - date = convert_utc_to_local_timezone(date).replace(tzinfo=pytz.utc) + now = datetime.utcnow() 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'): diff --git a/tests/app/main/test_formatters.py b/tests/app/main/test_formatters.py index 48202584f..01116073e 100644 --- a/tests/app/main/test_formatters.py +++ b/tests/app/main/test_formatters.py @@ -1,3 +1,4 @@ +from datetime import datetime from functools import partial import pytest @@ -7,6 +8,7 @@ from freezegun import freeze_time from app.formatters import ( email_safe, format_datetime_relative, + format_delta, format_notification_status_as_url, format_number_in_pounds_as_currency, round_to_significant_figures, @@ -129,3 +131,8 @@ def test_round_to_significant_figures(value, significant_figures, expected_resul ]) def test_email_safe_return_dot_separated_email_domain(service_name, safe_email): assert email_safe(service_name) == safe_email + + +def test_format_delta(): + naive_now_utc = datetime.utcnow().isoformat() + assert format_delta(naive_now_utc) == "just now" diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index 4751ed68c..f63ab2db6 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -230,7 +230,7 @@ def test_inbound_messages_shows_count_of_messages_when_there_are_messages( banner = page.select('a.banner-dashboard')[1] assert normalize_spaces( banner.text - ) == '9,999 text messages received latest message 5 hours ago' + ) == '9,999 text messages received latest message just now' assert banner['href'] == url_for( 'main.inbox', service_id=SERVICE_ONE_ID ) @@ -264,14 +264,14 @@ def test_inbound_messages_shows_count_of_messages_when_there_are_no_messages( @pytest.mark.parametrize('index, expected_row', enumerate([ - '07900 900000 message-1 6 hours ago', - '07900 900000 message-2 6 hours ago', - '07900 900000 message-3 6 hours ago', - '07900 900002 message-4 8 hours ago', - '+33 1 12 34 56 78 message-5 10 hours ago', - '+1 202-555-0104 message-6 12 hours ago', - '+1 202-555-0104 message-7 14 hours ago', - '+682 12345 message-8 14 hours 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', + '+33 1 12 34 56 78 message-5 5 hours ago', + '+1 202-555-0104 message-6 7 hours ago', + '+1 202-555-0104 message-7 9 hours ago', + '+682 12345 message-8 9 hours ago', ])) def test_inbox_showing_inbound_messages( client_request, diff --git a/tests/app/main/views/test_find_users.py b/tests/app/main/views/test_find_users.py index fd209de97..3d117545a 100644 --- a/tests/app/main/views/test_find_users.py +++ b/tests/app/main/views/test_find_users.py @@ -121,7 +121,7 @@ def test_user_information_page_shows_information_about_user( 'test@gsa.gov', '+447700900986', 'Text message code', - 'Last logged in 5 hours ago', + 'Last logged in just now', ] assert '0 failed login attempts' not in page.text diff --git a/tests/app/main/views/test_templates.py b/tests/app/main/views/test_templates.py index aba7a9b0b..c169531a7 100644 --- a/tests/app/main/views/test_templates.py +++ b/tests/app/main/views/test_templates.py @@ -1867,7 +1867,7 @@ def test_should_show_delete_template_page_with_time_block( mocker.patch('app.template_statistics_client.get_last_used_date_for_template', return_value='2012-01-01 12:00:00') - with freeze_time('2012-01-01 7:10:00'): + with freeze_time('2012-01-01 12:10:00'): page = client_request.get( '.delete_service_template', service_id=SERVICE_ONE_ID,