From f3e52d310b354b037196aa076344fa2d982b9d57 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 2 Feb 2017 09:35:18 +0000 Subject: [PATCH] Make calculation of business hours timezone aware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `replace` doesn’t convert a time from one timezone to another. It just changes the label that says what timezone a time is in 😬 `.localize` is how we handle these kind of issues in the API (see https://github.com/alphagov/notifications-api/blob/d0b467b2fb8fab65ee7f483d91b24ea5eb678f4e/app/utils.py#L42-L44 ) So this commit changes the calculation to use `.localize`, and makes the tests timezone aware to check we’re doing this right. --- app/main/views/feedback.py | 11 +++++++---- tests/app/main/views/test_feedback.py | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/main/views/feedback.py b/app/main/views/feedback.py index 6a243a6bd..aaa5cb056 100644 --- a/app/main/views/feedback.py +++ b/app/main/views/feedback.py @@ -143,15 +143,18 @@ def thanks(): def in_business_hours(): - now = datetime.now().replace(tzinfo=pytz.timezone('Europe/London')) + now = datetime.utcnow().replace(tzinfo=pytz.utc) if is_weekend(now) or is_bank_holiday(now): return False - opening_time = now.replace(hour=9, minute=30, second=0, tzinfo=pytz.timezone('Europe/London')) - closing_time = now.replace(hour=17, minute=30, second=0, tzinfo=pytz.timezone('Europe/London')) + return london_time_today_as_utc(9, 30) <= now < london_time_today_as_utc(17, 30) - return opening_time <= now < closing_time + +def london_time_today_as_utc(hour, minute): + return pytz.timezone('Europe/London').localize( + datetime.now().replace(hour=hour, minute=minute) + ).astimezone(pytz.utc) def is_weekend(time): diff --git a/tests/app/main/views/test_feedback.py b/tests/app/main/views/test_feedback.py index 92d1efd8a..3027bdef8 100644 --- a/tests/app/main/views/test_feedback.py +++ b/tests/app/main/views/test_feedback.py @@ -315,19 +315,19 @@ def test_has_live_services( @pytest.mark.parametrize('when, is_in_business_hours', [ - ('2016-06-06 09:29:59', False), # opening time, summer and winter - ('2016-12-12 09:29:59', False), - ('2016-06-06 09:30:00', True), - ('2016-12-12 09:30:00', True), + ('2016-06-06 09:29:59+0100', False), # opening time, summer and winter + ('2016-12-12 09:29:59+0000', False), + ('2016-06-06 09:30:00+0100', True), + ('2016-12-12 09:30:00+0000', True), - ('2016-12-12 12:00:00', True), # middle of the day + ('2016-12-12 12:00:00+0000', True), # middle of the day - ('2016-12-12 17:29:59', True), # closing time - ('2016-12-12 17:30:00', False), + ('2016-12-12 17:29:59+0000', True), # closing time + ('2016-12-12 17:30:00+0000', False), - ('2016-12-10 12:00:00', False), # Saturday - ('2016-12-11 12:00:00', False), # Sunday - ('2016-01-01 12:00:00', False), # Bank holiday + ('2016-12-10 12:00:00+0000', False), # Saturday + ('2016-12-11 12:00:00+0000', False), # Sunday + ('2016-01-01 12:00:00+0000', False), # Bank holiday ]) def test_in_business_hours(when, is_in_business_hours):