重复事件逻辑 [英] recurring event logic

查看:219
本文介绍了重复事件逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Groovy / Java日历类型的应用程序,允许用户输入具有开始日期和可选重复的事件。如果是周期性活动,则可能会在与开始日期相对应的月份的某一日期每月重复

I'm working on a Groovy/Java calendar-type application that allows the user to enter events with a start date and an optional recurrence. If it's a recurring event, it may recurr:


  • 每周的一天对应于开始日期

  • 每周的一天对应开始日期
  • >
  • 等。

  • monthly on a date of the month that corresponds to the start date
  • weekly on a day of the week of that corresponds to the start date
  • every 2 weeks on a day of the week of that corresponds to the start date
  • etc.

我原本打算使用Google日历API来执行所有的重复逻辑,它证明是一个巨大的PITA,原因我会进一步讨论,如果有人关心。

I originally planned on using the Google calendar API to do all the recurrence logic, but it proved to be an enormous PITA, for reasons I'll discuss further if anyone cares.

现在,我决定自己解决方案。给定一个日期,我想知道这个日期是否发生了周期性事件。我的逻辑(伪代码)如下:

So now, I've decided to roll my own solution. Given a date, I want to figure out whether a recurring event occurs on this date. My logic (in pseudocode) will be as follows:

public boolean occursOnDate(def date, def event) {

  def firstDate = event.startDate

  if (firstDate > date) {
    return false;

  } else if (event.isWeekly()) {
    return event.dayOfWeek() == date.dayOfWeek()

  } else if (event.isMonthly()) {
    return event.dayOfMonth() == date.dayOfMonth()

  } else {
    // At this point we know the event occurs every X weeks where X > 1
    // Increment firstDate by adding X weeks to it as many times as possible, without
    // going past date
    return firstDate == date
  }  
}

这个逻辑似乎是合理的,但实际上,奇怪的边缘情况(例如,如何处理每月的重复事件在2月,第一次发生是1月31日)。

This logic seems reasonable, but will actually be quite a lot of effort to implement when you consider all the weird edge cases (e.g. how to handle a monthly recurring event in February whose first occurrence is Jan 31).

有一个图书馆可以帮助我实现这一点吗?

Is there a library that can take help me to implement this? Some specifics would be much appreciated (e.g. no credit will be awarded for "Use Joda Time").

感谢,
Don

Thanks, Don

获得这个正确的细节可以很好地参与。我建议您使用 google-rfc-2445 媒体库进行此操作,或者执行此操作规范如 iCal4J

The sort of recurrence rules you want are reasonably well specified in RFC-2445 (basically, the iCal spec). Getting the minutiae of this correct can be pretty involved. I'd suggest using the the google-rfc-2445 library for this, or another implementation of that spec like iCal4J.

这篇关于重复事件逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆