Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

language agnostic - Data structure for storing recurring events?

I'm looking for a data structure pattern for storing recurring events, but everything I came up with would result in a high number of special case handling or user input and data retrieval are overly complex. (I get the distinct feeling, that I haven't understand the problem domain well enough to do this.)

How can I store Outlook-style recurring events?

  • Every day at 8am
  • Every first tuesday in a month
  • Every December 1st for three years
  • Every two hours for a week
  • ...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are various papers describing data structures and algorithms for this use case. In addition you can see the code or descriptions of open source implementation of crontab and of Quartz (Java) or Quartz.NET (.NET).

This is one such paper

http://portal.acm.org/citation.cfm?id=359763.359801&coll=ACM&dl=ACM&CFID=63647367&CFTOKEN=55814330

For example, cron stores the information like this (* means every, so a * under month means every month)


.---------------- minute (0 - 59) 
|  .------------- hour (0 - 23)
|  |  .---------- day of month (1 - 31)
|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ... 
|  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat 
|  |  |  |  |
*  *  *  *  * 

There are several special entries, most of which are just shortcuts, 
that can be used instead of specifying the full cron entry:

Entry      Description                 Equivalent To
@reboot    Run once, at startup.       None
@yearly    Run once a year             0 0 1 1 *
@annually  (same as @yearly)           0 0 1 1 *
@monthly   Run once a month            0 0 1 * *
@weekly    Run once a week             0 0 * * 0
@daily     Run once a day              0 0 * * *
@midnight  (same as @daily)            0 0 * * *
@hourly    Run once an hour            0 * * * *


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...