Consider the following list of day-of-week-hour pairs in 24H
format:
{'Mon': [9,23],'Thu': [12, 13, 14],'Tue': [11, 12, 14],'Wed': [11, 12, 13, 14]'Fri': [13],'Sat': [],'Sun': [],
}
and two time points, e.g.:
Start:
datetime.datetime(2015, 7, 22, 17, 58, 54, 746784)
End:
datetime.datetime(2015, 8, 30, 10, 22, 36, 363912)
Say we need to know how many hours there are between these two datetimes (either rounding up or down) for each of the day-of-week-hour pairs specified above.
How can I approach this problem in Python? I explored timedelta
and relativedelta
in a general level of detail but I didn't find anything that provides something close to this.
For simplicity, we can assume that everything refers to the same timezone.
Perhaps a simpler problem is to focus on a single day-hour pair, e.g. How many Wednesdays: 14
are there between two arbitrary datetimes?
Perhaps something like this:
from calendar import day_abbr
from datetime import datetime, timedeltadef solve(start, end, data):days = list(day_abbr)output = dict.fromkeys(days, 0)while start <= end:day = days[start.weekday()]if start.hour in data[day]:output[day] += 1start = start + timedelta(minutes=60)return outputdata = {'Mon': [9, 23],'Thu': [12, 13, 14],'Tue': [11, 12, 14],'Wed': [11, 12, 13, 14],'Fri': [13],'Sat': [],'Sun': [],
}start = datetime(2015, 7, 22, 17, 58, 54, 746784)
end = datetime(2015, 8, 30, 10, 22, 36, 363912)print solve(start, end, data)
# {'Wed': 20, 'Sun': 0, 'Fri': 6, 'Tue': 15, 'Mon': 10, 'Thu': 18, 'Sat': 0}
Getting count for each day by hour:
from calendar import day_abbr
from collections import defaultdict
from datetime import datetime, timedelta
from pprint import pprintdef solve(start, end, data):days = list(day_abbr)output = defaultdict(lambda: defaultdict(int))while start <= end:day = days[start.weekday()]if start.hour in data[day]:output[day][start.hour] += 1start = start + timedelta(minutes=60)return {k: dict(v) for k, v in output.items()}data = {'Mon': [9, 23],'Thu': [12, 13, 14],'Tue': [11, 12, 14],'Wed': [11, 12, 13, 14],'Fri': [13],'Sat': [],'Sun': [],
}start = datetime(2015, 7, 22, 17, 58, 54, 746784)
end = datetime(2015, 8, 30, 10, 22, 36, 363912)pprint(solve(start, end, data))
# output
{'Fri': {13: 6},'Mon': {9: 5, 23: 5},'Thu': {12: 6, 13: 6, 14: 6},'Tue': {11: 5, 12: 5, 14: 5},'Wed': {11: 5, 12: 5, 13: 5, 14: 5}}