[Tutor] working with time spans ?

Michele Alzetta michele.alzetta at aliceposta.it
Fri Jun 4 18:50:57 EDT 2004

```Thanks to Danny Yoo's suggestion I got to this, might be interesting:

from time import mktime
class TimeSpan(object):
def __init__(self, startimetuple, endtimetuple):
self.starttime = mktime(startimetuple)
self.endtime = mktime(endtimetuple)
def __contains__(self, timespan):
return (self.starttime <= timespan.starttime) and \
(self.endtime >= timespan.endtime)

periodstart = (2004, 6, 01, 8, 0, 0, 0, 0, 0)
periodend = (2004, 7, 01, 8, 0, 0, 0, 0, 0)
shiftperiod = TimeSpan(periodend,periodstart)

shift1start = (2004, 6, 15, 8, 0, 0, 0, 0, 0)
shift1end = (2004, 6, 15, 20, 0, 0, 0, 0, 0)
shift1 = TimeSpan(shift1start,shift1end)

shift2start = (2004, 7, 15, 8, 0, 0, 0, 0, 0)
shift2end = (2004, 7, 15, 20, 0, 0, 0, 0, 0)
shift2 = TimeSpan(shift2start,shift2end)

shift1 in shiftperiod
True

shift2 in shiftperiod
False

which ought to come in pretty handy for my purposes.

Actually all I'm interested in are years, months, days, hours and
minutes so the tuples with 9 elements are a bit of a nuisance.

Would it be a good idea to make TimeSpan inherit from time and
change the mktime method for instance ?

--
Michele

```