For a given Business with custom BusinessHours and Events, I need to figure out how get the nearest, previous business day for each event (say a weeks worth of events). For example, let's say a Business has BusinessHours on Sunday, Wednesday, Friday, and Saturday. Given an Event that starts on Wed, June 22nd, 2011 at 3pm, how can I efficiently determine that Sunday, June 19th, 2011 is the nearest, previous business day to this event? Here are the models:
class Business(models.Model):
name = models.CharField(max_length=50)
class BusinessHours(models.Model):
"""
I realize there are better ways to store business hours,
but this approach is simple and serves my purposes for now.
However, if another schema solves the problem above more efficiently,
feel free to make a suggestion.
"""
business = models.ForeignKey(Business)
sunday_open = models.TimeField(blank=True, null=True)
sunday_close = models.TimeField(blank=True, null=True)
monday_open = models.TimeField(blank=True, null=True)
monday_close = models.TimeField(blank=True, null=True)
... continue for each day ...
class Event(models.Model):
business = models.ForeignKey(Business)
start = models.DateTimeField()
end = models.DateTimeField()
I'm assuming most of the work needs to happen in python apart from Django, so ignore the Django models if it complicates the solution. I'd be happy to provide additional info if needed. Thanks in advance!