你将如何存储可能重复的时间? [英] How would you store possibly recurring times?

查看:149
本文介绍了你将如何存储可能重复的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要存储某事是否发生一次,每日,工作日,每周,一周中的某些日子,一些月中的某些日子,这可能是数字的或象征性的,如每个月的第一个星期一等等。 p>

有任何建议吗?要解决的任何代码,数据结构或模式?

解决方案

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


  1. 排除重复事件达到一定的实例数,或达到某个固定的日期范围在将来。存储FK recurrence_id,每个实例指向重复的描述,并允许大量编辑和取消。



    预处理扇出方法的优点是它非常容易实现循环异常,这几乎肯定是您获得的第一个功能请求。 / p>


  2. 在显示时间计算。计算机运行速度快,根据您希望能够回答的数据的问题,计算日期范围内的所有事件通常非常简单。



$您可以聪明并尝试在执行循环计算之前快速将日期范围括起来,或者您可以从开始日期开始强制使用。 b $ b

除此之外,你只需要一个解决方案来存储循环规则,它可以与你用来计算循环的任何内容一起使用。 (例如,如果您使用的是iCalendar启用库,则您的模式为varchar(255),其中包含RRULE值)



如果您需要滚动自己的重复计算器,你想保持简单,限制你的每日,每周,每月或每年的覆盖你的第一个80%的用例,并轻松计算。



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

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

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


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?

解决方案

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 :)

这篇关于你将如何存储可能重复的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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