Oh, yeah. No problem. I was mostly concerned with why the start/end assignments were being repeated for the logic when I could only see a difference in last_period for those two conditions. Something like

if period_length == 'day':
    last_period = time - datetime.timedelta(days=1)
elif period_length == 'hour':
    last_period = time - datetime.timedelta(hours=1)

start = datetime.datetime(year=last_period.year,
                                     month=last_period.month,
                                     day=last_period.day,
                                     hour=last_period.hour)
end = datetime.datetime(year=time.year,
                                    month=time.month,
                                    day=time.day,
                                    hour=time.hour)

But, I guess in the case of day you leave off the 'hour' attribute in start/end to automatically zero it out.