python 对预定事件进行排序
所以我有一些类似于警报的事件列表。它们由开始和结束时间(以小时和分钟为单位)、天数范围(即 1-3,即星期日到星期三)和月份范围(即 1-3、一月到三月)来定义。该数据的格式基本上是不可更改的。我需要,不一定对列表进行排序,但我需要根据当前时间找到下一个即将发生的事件。有很多不同的方法可以做到这一点,也有很多不同的极端情况。这是我的伪代码:
now = time()
diff = []
# Start difference between now and start times
for s in schedule #assuming appending to diff
diff.minutes = s.minutes - time.minutes #
diff.hours = s.hours - time.hours
diff.days = s.days - time.days
diff.months = s.months - time.months
for d in diff
if d < 0
d = period + d
# period is the maximum period of the attribute. ie minutes is 60, hours is 24
# repeat for event end times
现在我有一个小时、分钟、天和周差异的元组列表。该元组已经考虑到它是否已超过开始时间但在结束时间之前。假设现在是 8 月,事件的开始月份是 7 月,结束月份是 9 月,因此 diff.month == 0
。
现在,这个特定的极端情况给我带来了麻烦:
假设计划运行时间为 8 月星期四的 0 点到 23:59。今天是 27 号星期五。运行我的算法,几个月的差异将是 0,而实际上它要到明年 8 月才会再次运行,所以它应该是 12。我被困住了。我认为月份是唯一的问题,因为月份是唯一直接取决于特定月份的日期(而不是日期)的属性。我的算法可以吗?我可以处理这种特殊情况吗?或者有更好的东西吗?
这是我正在使用的数据
map['start_time']=''
map['end_time']=''
map['start_moy']=''
map['end_moy']=''
map['start_dow']=''
map['end_dow']=''
。 Schedule getAllSchedules 方法仅返回所有计划的列表。我可以改变课程安排,但我不确定我能在那里做出什么改变。我无法添加/更改给定的时间表的格式
So I have list of events that are sort of like alarms. They're defined by their start and end time (in hours and minutes), a range of days (ie 1-3 which is sunday through wed.), and a range of months (ie 1-3, january through march). The format of that data is largely unchangeable. I need to, not necessarily sort the list, but I need to find the next upcoming event based on the current time. There's just so many different ways to do this and so many different corner cases. This is my pseudo code:
now = time()
diff = []
# Start difference between now and start times
for s in schedule #assuming appending to diff
diff.minutes = s.minutes - time.minutes #
diff.hours = s.hours - time.hours
diff.days = s.days - time.days
diff.months = s.months - time.months
for d in diff
if d < 0
d = period + d
# period is the maximum period of the attribute. ie minutes is 60, hours is 24
# repeat for event end times
So now I have a list of tuples of differences in hours, minutes, days, and weeks. This tuple already takes into account if it's passed the start time, but before the end time. So let's say it's in August and the start month of the event is July and the end month is September, so diff.month == 0
.
Now this specific corner case is giving me trouble:
Let's say a schedule runs from 0 to 23:59 thursdays in august. And it's Friday the 27th. Running my algorithm, the difference in months would be 0 when in reality it won't run again until next august, so it should be 12. And I'm stuck. The month is the only problem I think because the month is the only attribute that directly depends on what the date of the specific month is (versus just the day). Is my algorithm OK and I can just deal with this special case? Or is there something better out there for this?
This is the data I'm working with
map['start_time']=''
map['end_time']=''
map['start_moy']=''
map['end_moy']=''
map['start_dow']=''
map['end_dow']=''
The schedule getAllSchedules method just returns a list to all of the schedules. I can change the schedule class but I'm not sure what difference I can make there. I can't add/change the format of the schedules I'm given
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将计划中的项目转换为 datetime 对象。然后你可以简单地对它们进行排序
Convert the items from the schedule into datetime objects. Then you can simply sort them
由于您的分辨率以分钟为单位,并且假设您没有很多事件,那么我只需每分钟扫描一次所有事件。
筛选事件,以便获得一个新列表,其中事件范围与当前月份和日期相匹配。
然后,对于每个事件,根据当前时间是否与事件范围匹配来声明它们是活动的还是非活动的。
Since your resolution is in minutes, and assuming that you don't have many events, then I'd simply scan all the events every minute.
Filter your events so that you have a new list where the event range match the current month and day.
Then for each of those events declare that they are active or inactive according to whether the current time matches the event's range.
主要问题似乎在于您使用的是星期几,而不是月份的明确日期。
虽然您引用的边缘情况是一个例子,但这个问题是否不会在当前月份之外的任何月份安排的所有活动中突然出现?
我认为这里最可靠的方法是将计划的事件转换为日期时间格式,然后使用 @gnibbler 对日期时间对象进行排序的建议。
一旦确定当前月份的最后一个事件已经过去,请计算到该事件发生的下个月的距离(无论是 + 1 年,还是 + 1 个月),然后使用该信息构造一个 datetime 对象:
通过使用该月的第一天,您可以使用:
给出该月的第一天是一周中的哪一天,然后您可以使用它来计算要添加多少天才能到达第一天、第二天、该月一周中给定日期的第三个实例等。一旦有了这一天,您就可以构造一个有效的
datetime
对象,并与 now() 进行任何您想要的比较。The primary issue seems to be with the fact that you're using the day of the week, instead of explicit days of the month.
While your cited edge case is one example, does this issue not crop up with all events scheduled in any month outside of the current one?
I think the most robust approach here would be to do the work to get your scheduled events into datetime format, then use @gnibbler's suggestion of sorting the datetime objects.
Once you have determined that the last event for the current month has already passed, calculate the distance to the next month the event occurs in (be it + 1 year, or just + 1 month), then construct a datetime object with that information:
By using the first day of the month, you can then use:
To give you what day of the week the first of that month falls on, which you can then use to calculate how many days to add to get to the first, second, third, etc. instance of a given day of the week, for that month. Once you have that day, you can construct a valid
datetime
object and do whatever comparisons you wish with now().我无法弄清楚如何仅使用日期时间来做到这一点。但我找到了一个模块并使用了它。非常完美
http://labix.org/python-dateutil
I couldn't figure out how to do it using only datetimes. But I found a module and used this. It's perfect
http://labix.org/python-dateutil