mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-17 04:59:37 -04:00
Make calculation of business hours timezone aware
`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
d0b467b2fb/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.
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user