From d352c0eed9a70d8133c4ef4f3988ab18edc6effe Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 4 Oct 2016 13:00:37 +0100 Subject: [PATCH] Really fix the timezones MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two main changes: - uses `astimezone` instead of `replace` because `replace` doesn’t handle daylight savings time correctly [1] - create the notifications one second before midnight in BST, because midnight is actually counted as being start of the _next_ day, month, etc 1. http://www.saltycrane.com/blog/2009/05/converting-time-zones-datetime-objects-python/#add-timezone-localize --- app/dao/notifications_dao.py | 4 ++-- tests/app/dao/test_notification_dao.py | 19 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 037a0b7b4..870239837 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -359,6 +359,6 @@ def get_april_fools(year): def get_bst_month(datetime): - return pytz.utc.localize(datetime).replace( - tzinfo=pytz.timezone("Europe/London") + return pytz.utc.localize(datetime).astimezone( + pytz.timezone("Europe/London") ).strftime('%B') diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index 7e7ac1d18..ea7779ecb 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -691,21 +691,20 @@ def test_get_all_notifications_for_job_by_status(notify_db, notify_db_session, s def test_get_notification_billable_unit_count_per_month(notify_db, notify_db_session, sample_service): for year, month, day in ( - (2017, 1, 1), # ↓ 2016 financial year + (2017, 1, 15), # ↓ 2016 financial year (2016, 8, 1), - (2016, 7, 31), - (2016, 4, 6), - (2016, 4, 6), + (2016, 7, 15), + (2016, 4, 15), + (2016, 4, 15), (2016, 4, 1), # ↓ 2015 financial year (2016, 3, 31), - (2016, 1, 1) + (2016, 1, 15) ): sample_notification( notify_db, notify_db_session, service=sample_service, created_at=datetime( - year, month, day, 0, 0, 0, 0, - tzinfo=pytz.utc - ) + year, month, day, 0, 0, 0, 0 + ) - timedelta(hours=1, seconds=1) # one second before midnight ) for financial_year, months in ( @@ -715,11 +714,11 @@ def test_get_notification_billable_unit_count_per_month(notify_db, notify_db_ses ), ( 2016, - [('April', 2), ('July', 1), ('August', 1), ('January', 1)] + [('April', 2), ('July', 2), ('January', 1)] ), ( 2015, - [('January', 1), ('March', 1), ('April', 1)] + [('January', 1), ('March', 2)] ), ( 2014,