views:

36

answers:

2

I'm currently dealing with a lot of possibly indefinite date spans, ie.

StartDate     EndDate
---------     ---------
01JAN1921     31DEC2009
10OCT1955     null
...

where the other end of the interval might be unknown or not defined. I've been working on little functions for detecting overlap, whether an interval is a subinterval of an other, calculating the gap between two intervals etc.

For example to detect overlaps, the problem is

        S       E            S and E are the start and end of the interval
        |       |            we're comparing to. Here both are known, but
s1------+---e1  |            either could be null. The small s:s and e:s 
        |       |  s2....e2  define the intervals we're comparing to and
        |s3--e3 |            again we'd like to allow for open intervals.
        |   s4--+----e4
  s5..e5|       |
    s6--+-------+--s7
        |       |

Based on questions related to detecting overlap with well defined intervals you need to check

    Coalesce(S,Coalesce(e-1,0))<Coalesce(e,Coalesce(S+1,1))
AND Coalesce(E,Coalesce(s+1,0))>Coalesce(s,Coalesce(E-1,1))

I suppose this is a such common thing (concerning not only date or time intervals) that lots of people have been dealing with it. I'm looking for existing implementations, preferrably just functions based on basic comparison operations.

+2  A: 

Interval arithmetic is a wide and complex topic. C++/Boost has a library for dealing with it. Python too, and I guess many other languages. Is this what you're asking about, or is this too general?

As for time intervals, there's this SO question, and probably others you can find in the 'Related' sidebar.

Eli Bendersky
Thanks, +1 for the links. I'll have to look if the Boost library supports intervals like [1,infinity).
Ville Koskinen
+1  A: 

There are three common ways people commonly represent dates in programs.

  • In the Gregorian calendar, with years, months, days, hours, minutes, seconds. It's very robust, but it's difficult to do math with (a leap year every fourth year, but not on the centuries, unless the year is a multiple of 400... can you remember that?)
  • As the number of seconds since a specific time, although this creates problems as you have to choose whether to count leap seconds (search for "leap seconds" online and you'll see why)
  • In the Julian calendar, as a number of days since a specific date and the number of seconds since the start of that day

As for implementations to use as a reference,

  • Java is an example of what not to do (folks, don't create mutable datetime types)
  • Python's is also a mess (no easy way to parse dates)
  • PostgreSQL deals with dates and times very nicely. See http://www.postgresql.org/docs/7.4/interactive/datatype-datetime.html, and you can also look at the source code. It uses the Julian calendar internally.
  • Haskell's standard time library is also nice, and has unlimited range and precision — that's right, they use bigints for the Julian date and an a ratio of bigints for the time. The library includes a bunch of useful date functions, including one for calculating the date Easter falls on. See http://hackage.haskell.org/package/time.
Dietrich Epp