mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 06:21:50 -05:00
Split out handling of BST queries for reuse
This commit is contained in:
17
app/utils.py
17
app/utils.py
@@ -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))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user