您将如何存储可能重复出现的时间?

发布于 2024-07-11 17:58:40 字数 112 浏览 11 评论 0原文

我需要存储某件事是否发生一次、每天、工作日、每周、一周中的某些天、每月的某些天,这可能是数字或符号,例如每个月的第一个星期一等等。

有什么建议吗? 有什么代码、数据结构或模式可供查看吗?

I need to store whether something happens once, daily, weekdays, weekly, some days of the week, some days of the month, which may be numerical or symbolic, like first Monday of each month, and so on.

Any recommendations? Any code, data structure or schema to look at?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

百变从容 2024-07-18 17:58:40

有复杂的解决方案和简单的解决方案。 两个最简单的解决方案是:

  1. 将重复事件扇出到一定数量的实例,或者将来的某个固定日期范围。 为每个实例存储一个 FK recurrence_id,该 ID 指向重复的描述,并允许批量编辑和取消。

    预先计算的扇出方法的优点是它可以非常轻松地实现重复异常,这几乎肯定会是您收到的第一个功能请求。

  2. 在显示时间计算。 计算机速度很快,根据您希望能够回答的有关数据的问题,计算某个日期范围内的所有事件通常非常容易。 您可以聪明地尝试在进行重复计算之前快速将日期范围括起来,也可以从开始日期开始暴力计算。

除此之外,您只需要一个存储重复规则的解决方案,该规则适用于您用来计算重复的任何内容。 (例如,如果您使用 iCalendar 启用库,则您的架构为 varchar(255),其中包含 RRULE 值)

如果您必须使用自己的重复计算器,并且希望保持简单,将重复次数限制为每日、每周、每月或每年涵盖了您的前 80% 用例,并且非常容易计算。

此时您潜在的重复模式看起来像这样:

id
recurrence_start
recurrence_end
type (daily|weekly|monthly|yearly)
day_of_week (for weekly)
month
day_of_month

坦率地说,复杂的解决方案可能不值得:)

There are complex solutions and easy solutions. The two easiest solutions are:

  1. Fan out recurring events up to some constant number of instances, or up to some fixed date range in the future. Store a FK recurrence_id with each instance that points to a description of the recurrence, and allows for mass editing and canceling.

    The advantage of the pre-calced fan out approach is it makes it very very easy to implement recurrence exceptions, which will almost certainly be the first feature request you get.

  2. Calculate at display time. Computers are fast, depending on the questions you want to be able to answer about your data, it will often be trivially easy to calculate all the occurrences in a date range. You can be smart and try to quickly bracket your date range before doing the recurrence calculation, or you can brute force it from the onset date.

Beyond that you just need a solution for storing the recurrence rule that works with whatever you're using to calculate recurrences. (e.g. if you're using an iCalendar enable library, your schema is varchar(255) with RRULE values in it)

If you're having to roll your own recurrence calculator, and you want to keep it simple, limiting your recurrences to daily, weekly, monthly, or yearly covers your first 80% use case and is trivially easy to calc.

At which point your potential recurrence schema looks something like:

id
recurrence_start
recurrence_end
type (daily|weekly|monthly|yearly)
day_of_week (for weekly)
month
day_of_month

And frankly the complex solutions probably aren't worth it :)

我的鱼塘能养鲲 2024-07-18 17:58:40

Martin Fowler 就此写了一篇非常精彩的论文。 您可以在 runt(一个用于处理时间表达式的 Ruby 库)中找到他讨论的许多相同想法。

Martin Fowler wrote a really great paper about this. You can find a lot of the same ideas he discusses in runt, a Ruby library for dealing with temporal expressions.

乖乖兔^ω^ 2024-07-18 17:58:40

这听起来像是“重复发生的事件”,例如在 Outlook 中。 我将使用一个名为 RecurrenceType 的表来存储每个时间段(每日、每周等)。另一个名为 Event 的表将通过键引用 RecurrenceType。 然后可以使用标准日期函数计算大多数重复类型的未来日期。

This sounds like a "recurring event" such as in Outlook. I would use one table called RecurrenceType to store each time period (daily, weekly, etc.) Another table called Event woud refer by key to RecurrenceType. Future dates for most recurrence types can then be calculated using standard Date functions.

不美如何 2024-07-18 17:58:40

尝试一个具有调度功能的开源项目吗?

一些调度接口/定义

https://www.rfc-editor.org/rfc/rfc4791

http://en.wikipedia.org/wiki/ICalendar

Try an open source project that has scheduling functionality?

Some scheduling interface/definitions

https://www.rfc-editor.org/rfc/rfc4791

and

http://en.wikipedia.org/wiki/ICalendar

情何以堪。 2024-07-18 17:58:40

问题是指定这样的间隔有无限的可能性。 例如 - “如果日期是偶数但不是 4,并且该月份不是长年的二月,则为每隔一个月的第一个星期一”。 你愿意走多远? 最终,您只需让用户输入一个布尔表达式,该表达式在事件重复发生的日期为 TRUE。 从用户界面的角度来看不太好。

您应该决定您的系统的一些限制。 一旦你知道了这些,剩下的就很容易了——或者至少是可以回答的。 :)

The problem is that there are infinite possibilities for specifying such an interval. For example - "The first monday of every other month if the date is even, but not 4, and the month isn't February of a long year". How far are you willing to go? Eventually you'll just have to make the users type in a boolean expression which evaluates to TRUE on days that the event is to recur. Not very nice from a UI perspective.

You should decide on some limitations for your system. Once you know those, the rest should be easy - or at least answerable on SO. :)

猥︴琐丶欲为 2024-07-18 17:58:40

此处描述的模式捕获重复日期

http://github.com/bakineggs/recurring_events_for

The schema described here captures recurring dates

http://github.com/bakineggs/recurring_events_for

灰色世界里的红玫瑰 2024-07-18 17:58:40

It's mentioned on a related, useful SO thread, but a nice, actively-maintained alternative to Runt if you happen to be using Ruby is ice_cube. You haven't mentioned what your storage backend requirements are, but for database schema, @kellan's and some on the aforementioned thread are good starts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文