Return period that is in the future for sure
def next_all(dt):
Returns 2-tuple of next year: start/end date
def next_year(dt):
Returns 2-tuple of next quarter: start/end date
def next_quarter(dt):
Returns 2-tuple of next month: start/end date
def next_month(dt):
Returns 2-tuple of next week: start/end date. Week starts on monday.
def next_week(dt):
Returns 2-tuple of next week: start/end date
def next_day(dt):
Returns list of 2-tuples with startdate/enddates.
def calc_aggregation_periods(start_date, end_date, aggregation_period):
periods = []
next_period_functions = {
ALL: next_all,
YEAR: next_year,
QUARTER: next_quarter,
MONTH: next_month,
WEEK: next_week,
DAY: next_day}
next_period = next_period_functions[aggregation_period]
next_period_start, next_period_end = next_period(start_date)
periods.append((start_date, min(next_period_start, end_date)))
while next_period_start < end_date:
periods.append((next_period_start,
min(next_period_end, end_date)))
(next_period_start,
next_period_end) = next_period(next_period_start)
return periods
Returns fancy string of (start_date, end_date), format is
def fancy_period(start_date, end_date, aggregation_period):
determined by aggregation_period.
period_formats = {
ALL: lambda a, b: _("Whole period"),
YEAR: lambda a, b: a.strftime("%Y"),
QUARTER: lambda a, b: "%s %s" % (
QUARTERS[(a.month - 1) / 3], a.strftime("%Y")),
MONTH: lambda a, b: "%s %s" % (MONTHS[a.month], a.strftime("%Y")),
WEEK: lambda a, b: a.strftime("%Y %m %d"),
DAY: lambda a, b: a.strftime("%Y %m %d"),
}
return period_formats[aggregation_period](start_date, end_date)