2017-05-02 10:00:47 +01:00
|
|
|
from datetime import datetime, timedelta
|
2017-04-26 14:16:47 +01:00
|
|
|
|
2018-11-26 12:53:39 +00:00
|
|
|
from notifications_utils.timezones import convert_bst_to_utc
|
2017-04-26 14:16:47 +01:00
|
|
|
import pytz
|
|
|
|
|
|
|
|
|
|
|
2017-08-18 12:45:05 +01:00
|
|
|
def get_months_for_financial_year(year):
|
|
|
|
|
return [
|
|
|
|
|
convert_bst_to_utc(month) for month in (
|
2019-04-29 15:49:12 +01:00
|
|
|
get_months_for_year(4, 13, year)
|
|
|
|
|
+ get_months_for_year(1, 4, year + 1)
|
2017-08-18 12:45:05 +01:00
|
|
|
)
|
2018-06-29 16:40:24 +01:00
|
|
|
if convert_bst_to_utc(month) < datetime.now()
|
2017-08-18 12:45:05 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_months_for_year(start, end, year):
|
|
|
|
|
return [datetime(year, month, 1) for month in range(start, end)]
|
|
|
|
|
|
|
|
|
|
|
2017-04-26 14:16:47 +01:00
|
|
|
def get_financial_year(year):
|
2017-05-02 10:00:47 +01:00
|
|
|
return get_april_fools(year), get_april_fools(year + 1) - timedelta(microseconds=1)
|
2017-04-26 14:16:47 +01:00
|
|
|
|
|
|
|
|
|
2019-04-29 15:49:12 +01:00
|
|
|
def get_current_financial_year():
|
|
|
|
|
now = datetime.utcnow()
|
|
|
|
|
current_month = int(now.strftime('%-m'))
|
|
|
|
|
current_year = int(now.strftime('%Y'))
|
|
|
|
|
year = current_year if current_month > 3 else current_year - 1
|
|
|
|
|
return get_financial_year(year)
|
|
|
|
|
|
|
|
|
|
|
2017-04-26 14:16:47 +01:00
|
|
|
def get_april_fools(year):
|
|
|
|
|
"""
|
|
|
|
|
This function converts the start of the financial year April 1, 00:00 as BST (British Standard Time) to UTC,
|
2017-10-18 15:09:05 +01:00
|
|
|
the tzinfo is lastly removed from the datetime because the database stores the timestamps without timezone.
|
2017-04-26 14:16:47 +01:00
|
|
|
:param year: the year to calculate the April 1, 00:00 BST for
|
|
|
|
|
:return: the datetime of April 1 for the given year, for example 2016 = 2016-03-31 23:00:00
|
|
|
|
|
"""
|
|
|
|
|
return pytz.timezone('Europe/London').localize(datetime(year, 4, 1, 0, 0, 0)).astimezone(pytz.UTC).replace(
|
|
|
|
|
tzinfo=None)
|
2017-07-18 18:21:35 +01:00
|
|
|
|
|
|
|
|
|
2017-08-10 16:29:13 +01:00
|
|
|
def get_month_start_and_end_date_in_utc(month_year):
|
2017-07-18 18:21:35 +01:00
|
|
|
"""
|
|
|
|
|
This function return the start and date of the month_year as UTC,
|
|
|
|
|
:param month_year: the datetime to calculate the start and end date for that month
|
|
|
|
|
:return: start_date, end_date, month
|
|
|
|
|
"""
|
|
|
|
|
import calendar
|
|
|
|
|
_, num_days = calendar.monthrange(month_year.year, month_year.month)
|
|
|
|
|
first_day = datetime(month_year.year, month_year.month, 1, 0, 0, 0)
|
|
|
|
|
last_day = datetime(month_year.year, month_year.month, num_days, 23, 59, 59, 99999)
|
2017-07-25 17:38:53 +01:00
|
|
|
return convert_bst_to_utc(first_day), convert_bst_to_utc(last_day)
|
2017-11-02 12:19:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_financial_year_start_year():
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
financial_year_start = now.year
|
|
|
|
|
start_date, end_date = get_financial_year(now.year)
|
|
|
|
|
if now < start_date:
|
|
|
|
|
financial_year_start = financial_year_start - 1
|
|
|
|
|
return financial_year_start
|
2019-08-06 13:29:59 +01:00
|
|
|
|
|
|
|
|
|
2019-08-28 14:27:08 +01:00
|
|
|
def get_financial_year_for_datetime(start_date):
|
|
|
|
|
year = int(start_date.strftime('%Y'))
|
|
|
|
|
if start_date < get_april_fools(year):
|
|
|
|
|
return year - 1
|
2019-08-06 13:29:59 +01:00
|
|
|
else:
|
2019-08-28 14:27:08 +01:00
|
|
|
return year
|