From 7013eb9696de6385de655cc89a510137f03c212b Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 5 Mar 2021 16:26:26 +0000 Subject: [PATCH] Make get_current_financial_year timezone aware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It would have given the wrong answer for the first hour of the 2021 financial year. This was OK before, because we didn’t need this kind of precision. But now it could mean someone signing up in the middle of the night getting the wrong free text message allowance, so we should fix it. --- app/utils.py | 4 +++- tests/app/test_utils.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/utils.py b/app/utils.py index b89c88262..31b89267e 100644 --- a/app/utils.py +++ b/app/utils.py @@ -334,7 +334,9 @@ def get_template( def get_current_financial_year(): - now = datetime.utcnow() + now = utc_string_to_aware_gmt_datetime( + datetime.utcnow() + ) current_month = int(now.strftime('%-m')) current_year = int(now.strftime('%Y')) return current_year if current_month > 3 else current_year - 1 diff --git a/tests/app/test_utils.py b/tests/app/test_utils.py index 47bdf1a2e..3ebc2bda6 100644 --- a/tests/app/test_utils.py +++ b/tests/app/test_utils.py @@ -16,6 +16,7 @@ from app.utils import ( generate_next_dict, generate_notifications_csv, generate_previous_dict, + get_current_financial_year, get_letter_printing_statement, get_letter_validation_error, get_logo_cdn_domain, @@ -657,3 +658,14 @@ def test_merge_jsonlike_merges_jsonlike_objects_correctly(source_object, destina )) def test_round_to_significant_figures(value, significant_figures, expected_result): assert round_to_significant_figures(value, significant_figures) == expected_result + + +@pytest.mark.parametrize('datetime_string, financial_year', ( + ('2021-01-01T00:00:00+00:00', 2020), # Start of 2021 + ('2021-03-31T22:59:59+00:00', 2020), # One minute before midnight (BST) + ('2021-03-31T23:00:00+00:00', 2021), # Midnight (BST) + ('2021-12-12T12:12:12+01:00', 2021), # Later in the year +)) +def test_get_financial_year(datetime_string, financial_year): + with freeze_time(datetime_string): + assert get_current_financial_year() == financial_year