Split out handling of BST queries for reuse

This commit is contained in:
Chris Hill-Scott
2017-01-30 16:46:47 +00:00
parent 8381a5e11c
commit 45b505f379
3 changed files with 22 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ from datetime import datetime, timedelta
import pytz
from flask import url_for
from sqlalchemy import func
from notifications_utils.template import SMSMessageTemplate, PlainTextEmailTemplate
@@ -46,3 +47,19 @@ def get_london_midnight_in_utc(date):
def get_midnight_for_day_before(date):
day_before = date - timedelta(1)
return get_london_midnight_in_utc(day_before)
def get_london_month_from_utc_column(column):
"""
Where queries need to count notifications by month it needs to be
the month in BST (British Summer Time).
The database stores all timestamps as UTC without the timezone.
- First set the timezone on created_at to UTC
- then convert the timezone to BST (or Europe/London)
- lastly truncate the datetime to month with which we can group
queries
"""
return func.date_trunc(
"month",
func.timezone("Europe/London", func.timezone("UTC", column))
)