tags:

views:

295

answers:

3

I am looking for a library doing set operations on time periods with support of weekdays and time of day. I have started to roll my own, but it seems like a error-prone job so I would like a tested solution for this. I don't mind spending money.

If I have a set of two time periods (this can be several)

Setup:

// [period 1]:
TimeSet.Add(1 January 2008-31 January 2008 every Tuesday 14:00-15:00) 
// [period 2]
TimeSet.AddWithOr(1 January 2008-15 January 2008 every Tuesday 13:00-14:00)

Results:

TimeSet.ContainsTime(2 January 2008 13:45) = False
TimeSet.ContainsTime(2 January 2008 14:15) = True // [period 1]
TimeSet.ContainsTime(2 January 2008 13:15) = True // [period 2]

I also need range checking functions:

// Only Tuesday 13-15 is in range:
TimeSet.ContainsTime(24 December 2007 13:45,1 January 2008 13:00) = False
TimeSet.ContainsTime(24 December 2007 13:45,2 January 2008 12:59) = False 
TimeSet.ContainsTime(24 December 2007 13:45,2 January 2008 13:00) = True

Other more advanced set functions is a plus.

I tried googling for it, but was unable to find anything.

A: 

You can use the versatile DateTime structure. It has operators for comparing two DateTime's, e.g. LessThan. Thus the set operations and the range checking functions are fairly trivial to implement in a simple class for this purpose.

The central data structure in such a class could be a list of DateTime ranges sorted by start time. If a new DateTime range is added then it is merged with an existing DateTime range if there is any overlap. If it is merged with one of them a run through the new list is required to see if any DateTime range now overlap (and then a merge is required). This is repeated until there are no more overlaps.

Peter Mortensen
I have created my own (using DateTime). If I split each period definition into several "DateRanges" I end up with a lot of ranges - 365-366 ranges for 1 Jan-31 Dec 14:00-15:00.
Erwin
OK, I see. This is not an efficient representation then, but it should still work. Could it be a floating point rounding problem? E.g. 14.00 may not be the same as 13.00 + 1 hour. –
Peter Mortensen
A: 

Have a look at the TimeSpan Structure

Martin
How would TimeSpan structure help me?
Erwin
+1  A: 

I don't know any that exist as a C#/.NET framework however you may be able to adapt the Quartz project's (a C# scheduler) calendar namespace. This includes a CronCalendar object. I haven't used cron for a while but I can remember it supports the kind of occurrence system you're after.

There's quite a few helper extensions you could use on codeproject, like a AddBusinessDay method which may also help out.

In terms of parsing what you have for the format - you may want to change this to a fluent interface to make development/testing quicker and save having to write a complicated token parsing class. You could just keep it in Cron format to make the Quartz code even easier to integrate, unless it's being used by none-techies.

Chris S
The Quartz classes looks promising, as for parsing the dates are stored in a database.
Erwin