0

We have weekly recurring events in our application, and have decided to store the start and end times for each event as the number of seconds from the beginning of the week (Sunday 12am). This makes querying, sorting, etc. much easier.

However, there is one small problem: If we want to get the event that is happening at a time near the week reset, that event could bridge that gap, and it makes querying difficult.

A normal query, which works 99% of the time, to grab the records where the given time is between the record's start and end time.

time = 216000 # Tuesday, 12:00PM
SELECT * FROM events WHERE start_time <= 216000 AND end_time > 216000

The hard part is when there is an event that starts at, say, Saturday at 11pm, and ends on Sunday at 2am:

time = 3600 # Sunday, 1am
SELECT * FROM events WHERE start_time <= 3600 AND end_time > 3600

This won't return anything because the start_time will NOT be less than 3600, since it starts near the end of the weekly cycle.

I've been racking my brain for a few hours trying to come up with a good solution, but have fallen short so far, so I'd like to open it up to SO to see if there is solution that I'm not thinking of. I have a feeling that there is some simple math equation I can use to get this done, but math isn't my best subject :)

Please let me know if I need to explain better or give better examples.

Thanks!

5
  • Are the full dates also stored anywhere? Withonly that information you'd have to say if start date is less than X and end time is less than X or start time is also "near the end of the week" and end time is "near the start of the week". Horribly imprecise, but all that I can think of. Commented Oct 28, 2012 at 20:00
  • No. The data being stored is weekly recurring events, so there are no full dates. The closest thing to that would be to store the time of day in a TIME column, and the day of the week in an INT column, but the "seconds-since-beginning-of-week" is practically the same thing.
    – bricker
    Commented Oct 28, 2012 at 20:03
  • Sorry edited my question as you were replying. Commented Oct 28, 2012 at 20:05
  • @MartinLyne Actually, I had started considering a solution where I would use something like WHERE (end_time < start_time) AND ..., but couldn't figure out the ... bit.
    – bricker
    Commented Oct 28, 2012 at 20:11
  • You could say if the end time is in the first X% of the week and the start time is at the last X% of the week? But that's then hardcoded and an event that goes from Friday to Monday would still not get detected. I might have a think/play in sqlfiddle. Commented Oct 28, 2012 at 20:14

3 Answers 3

1

You can try something like:

select * from events 
   where (start_time <= 3600 or start_time>end_time) and end_time > 3600; 

This way you will be able to catch the events that were started before your rest time and are still happening at the specified time.

0

You could just make the start time very high.

SELECT * FROM events WHERE start_time <= 590000 AND end_time > 3600

0

You should store your time as a timestamp and just calculate the start_time of every week and do a little math. This prevents the problems you are describing, then you:

select * from events where (start_time < SUNDAYS_TIMESTAMP and end_time > SUNDAYS_TIMESTAMP or whatever you really want to try.

You should store your data in a universal format and figure out what you want from that.

2
  • To me, an integer is as universal a format as you can get. Storing a real timestamp doesn't work for weekly recurring events, unless you have a separate record for every event in every week, which is an unsustainable data model. Thanks for the suggestion, though.
    – bricker
    Commented Oct 28, 2012 at 22:13
  • a timestamp is an integer - but anyhow, you can still determine what week it is from with the timestamp. All the trouble of trying to record week starts is already done in the notion of the timestamp model.
    – Michael
    Commented Oct 28, 2012 at 22:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.