实施“现在开放”在网络应用程序中过滤场地列表
我需要为我的网站实施“现在开放”过滤器,列出场地,但我不知道从哪里开始。我的网站是使用Python、webpy、MySQL数据库实现的。
我必须存储每个场馆的开放时间并选择现在开放的场馆。
开放时间示例:“工作日上午 9:30 至晚上 11 点工作,周末上午 11 点至晚上 11 点”或“全天候工作,上午 6 点至上午 7 点除外”。
一周中每一天的开放时间可能有所不同,一天可能有多个时间间隔,并且我需要针对特定假期进行例外处理。
欢迎对界面、数据模式和查询提出任何提示/建议。
UPD:我已经想出了这个解决方案,可以吗?
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"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用过一个名为 django-schedule 的应用程序,它具有一些类似的功能。
如果您深入研究源代码,您会发现它在下面使用 python-dateutil (请参阅 rrules)。它在数据库中保存每个事件的调度规则列表。
您可以使用类似的策略。当查询场地开放状态时,查看当前日期时间是否符合场地时间表规则提供的事件的开始/结束时间。如果是这样,它是开放的。
I've used an app called django-schedule which has some similar features.
If you dig in to the source, you'll see it uses python-dateutil (see rrules) underneath. It keeps a list of schedule rules for each event in the database.
You could use a similar tactic. When venue-open-status is queried see if the current datetime fits inside the start/end of an occurrence provided by the venue's schedule rules. If so, it is open.