我正在使用Ice Cube宝石https://github.com/seejohnrun/ice_cube安排活动,我希望每个活动在每周的星期一出现在应用程序中,并且6小时后,活动标题应标记为红色。
在doc ice cube中,有一个用于设置持续时间的示例
# or give the schedule a duration and ask if occurring_at?
schedule = IceCube::Schedule.new(now, :duration => 3600)
schedule.add_recurrence_rule IceCube::Rule.daily
schedule.occurring_at?(now + 1800) # true
我将其翻译为以下内容:
start_date = Time.now.utc - 10.days # any datetime basically
schedule = IceCube::Schedule.new(start_date, :duration => (6.hours).seconds)
schedule.add_recurrence_rule IceCube::Rule.weekly(1).day(:monday)
但是从我看来,6个小时的持续时间仅适用于首次发生(从起始日期开始6个小时),我想要的是每周的每个星期一6个小时。
答案 0 :(得分:0)
我在寻找类似的东西,所以偶然发现了你的问题。 如果您还没有弄清楚,这就是可行的方法。
因此,首先我们设置事件的结束时间:
start_date = Time.now.utc - 10.days
schedule = IceCube::Schedule.new(start_date, end_time: start_date + 6.hours.seconds)
schedule.add_recurrence_rule IceCube::Rule.weekly(1).day(:monday)
我们还设置了每周规则(每个星期一)。 现在我们看到这对于出现的情况很好用,例如
schedule.next_occurrences(6)
# [2019-12-09 10:01:13 UTC, 2019-12-16 10:01:13 UTC, 2019-12-23 10:01:13 UTC, 2019-12-30 10:01:13 UTC, 2020-01-06 10:01:13 UTC, 2020-01-13 10:01:13 UTC]
让我们看看上面列出的第一次出现的“持续时间”:
# The event has started
schedule.occurring_at? Time.utc(2019, 12, 9, 10, 1, 14)
# true
# Almost 6 hours later
schedule.occurring_at? Time.utc(2019, 12, 9, 16, 1, 12)
# true
schedule.occurring_at? Time.utc(2019, 12, 9, 16, 1, 13)
# false
# The event just ended
因此,如果您检查事件occurring_at?
,则可以更改所需的颜色。
如果我们为事件设置了持续时间,则在该持续时间之后,事件将停止。
但是,如果我们将end_time
设置为start_time
至end_time