Commit 575011b1 authored by Vincent Pelletier's avatar Vincent Pelletier

Add support for "week" and "quarter" periods.

parent ff90c6f5
...@@ -505,11 +505,31 @@ def _asMonthString(timestamp): ...@@ -505,11 +505,31 @@ def _asMonthString(timestamp):
_, month, year = dt.split(':', 1)[0].split('/') _, month, year = dt.split(':', 1)[0].split('/')
return '%s/%02i' % (year, MONTH_VALUE_DICT[month]) return '%s/%02i' % (year, MONTH_VALUE_DICT[month])
def _asWeekString(timestamp):
dt, _ = timestamp.split(' ')
day, month, year = dt.split(':', 1)[0].split('/')
return '%s/%02i/%02i' % (year, MONTH_VALUE_DICT[month], int(day) / 7 * 7 + 1)
def _weekStringAsQuarterString(timestamp):
year, month, _ = timestamp.split('/')
return '%s/%02i' % (year, int(month) / 3 * 3 + 1)
def _asDayString(timestamp): def _asDayString(timestamp):
dt, _ = timestamp.split(' ') dt, _ = timestamp.split(' ')
day, month, year = dt.split(':', 1)[0].split('/') day, month, year = dt.split(':', 1)[0].split('/')
return '%s/%02i/%s' % (year, MONTH_VALUE_DICT[month], day) return '%s/%02i/%s' % (year, MONTH_VALUE_DICT[month], day)
def _as6HourString(timestamp):
dt, _ = timestamp.split(' ')
date, hour, _ = dt.split(':', 2)
day, month, year = date.split('/')
return '%s/%02i/%s %02i' % (year, MONTH_VALUE_DICT[month], day,
int(hour) / 6 * 6)
def _hourAsWeekString(timestamp):
dt = datetime.strptime(timestamp, '%Y/%m/%d %H')
return (dt - timedelta(dt.weekday())).date().strftime('%Y/%m/%d')
def _asHourString(timestamp): def _asHourString(timestamp):
dt, _ = timestamp.split(' ') dt, _ = timestamp.split(' ')
date, hour, _ = dt.split(':', 2) date, hour, _ = dt.split(':', 2)
...@@ -535,6 +555,15 @@ period_parser = { ...@@ -535,6 +555,15 @@ period_parser = {
# Longest month: 31 days # Longest month: 31 days
timedelta(31), timedelta(31),
), ),
'quarter': (
_asWeekString,
_weekStringAsQuarterString,
# Note: Not calendar weeks, but chunks of 7 days starting on first month's
# day. Cheaper to compute, and *should* not be a problem.
'7 days',
'%Y/%m/%d',
timedelta(7),
),
'month': ( 'month': (
_asDayString, _asDayString,
lambda x: '/'.join(x.split('/', 2)[:2]), lambda x: '/'.join(x.split('/', 2)[:2]),
...@@ -543,6 +572,13 @@ period_parser = { ...@@ -543,6 +572,13 @@ period_parser = {
# Longest day: 24 hours + 1h DST (never more ?) # Longest day: 24 hours + 1h DST (never more ?)
timedelta(seconds=3600 * 25), timedelta(seconds=3600 * 25),
), ),
'week': (
_as6HourString,
_hourAsWeekString,
'6 hours',
'%Y/%m/%d %H',
timedelta(seconds=3600 * 6),
),
'day': ( 'day': (
_asHourString, _asHourString,
lambda x: x.split(' ')[0], lambda x: x.split(' ')[0],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment