【问题标题】:Ice Cube How to set a duration for every occurrenceIce Cube 如何为每次发生设置持续时间
【发布时间】:2019-12-06 18:47:55
【问题描述】:

我正在使用 Ice Cube gem 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 小时的持续时间似乎只适用于第一次发生(从 start_date 开始的 6 小时),我想要的是从每周一开始的 6 小时。

【问题讨论】:

    标签: ruby-on-rails ice-cube


    【解决方案1】:

    我正在寻找类似的东西,所以我偶然发现了你的问题。 如果您还没有弄清楚这一点,这就是有效的方法。

    所以,首先我们设置事件的结束时间:

    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_timeend_time 之间的时间段内,所有的事件都会正常工作

    【讨论】:

    • 感谢@alexts,如果我记得很清楚,我找到了解决该问题的方法...自从我没有更新该项目以来已经有一段时间了,我会尽快回复并确保尝试您的回答。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    相关资源
    最近更新 更多