I need to implement "is open now" filter for my website that lists venues, but I don't know where to start. My website is implemented using Python, webpy, MySQL database.
I have to store the opening hours for every venue and select venues that are open now.
An example of opening hours: "Works from 9:30am to 11pm on weekdays, 11am to 11pm on weekend", or "Works round the clock excluding 6am to 7am".
Opening hours may be different for every day of week, day can have more than one interval of time, and I need to hack in exceptions for specific holidays.
Any tips/suggestions on the interface, data schema and query are welcome.
UPD: I have managed to come up with this solution, is it ok?
from dateutil.relativedelta import *
from dateutil.rrule import *
from datetime import *
from date import *
# venue can have many rules
hours_start = 23
minutes_start = 30
hours_end = 13 # if time_end is less than time_start then it ends next day
minutes_end = 30
days_of_week = (TH)
hours_diff = hours_end - hours_start
minutes_diff = minutes_end - minutes_start
if hours_diff < 0 or hours_diff == 0 and minutes_diff < 0:
hours_diff += 24
datetime_start = date.today() + relativedelta(hours=+hours_start, minutes=+minutes_start, days=-1) # Yesterday time_start
now = datetime.now()
occurrence = rrule(WEEKLY, wkst=MO, byweekday=days_of_week, dtstart=datetime_start).before(now, True)
if now <= occurrence + relativedelta(hours=hours_diff, minutes=minutes_diff):
print "IS OPEN NOW"
from ... import *
. Better explicitely specify what you want to import. – ThiefMaster Oct 28 '11 at 9:07import *
in my final code. – Andrey Kuzmin Oct 28 '11 at 9:43