diff --git a/roundup/date.py b/roundup/date.py
index 95777a33b4ca27eceb5506558576c46e1b1f60ab..49151768aedaf563b78e76c689702609803b671d 100644 (file)
--- a/roundup/date.py
+++ b/roundup/date.py
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#
-# $Id: date.py,v 1.52 2003-04-21 14:29:39 kedder Exp $
+# $Id: date.py,v 1.59 2003-12-05 03:28:38 richard Exp $
__doc__ = """
Date, time and time interval handling.
"11-07.09:32:43" means <Date yyyy-11-07.14:32:43>
"14:25" means <Date yyyy-mm-dd.19:25:00>
"8:47:11" means <Date yyyy-mm-dd.13:47:11>
+ "2003" means <Date 2003-01-01.00:00:00>
+ "2003-06" means <Date 2003-06-01.00:00:00>
"." means "right now"
The Date class should understand simple date expressions of the form
minute, second) is the serialisation format returned by the serialise()
method, and is accepted as an argument on instatiation.
'''
+
def __init__(self, spec='.', offset=0, add_granularity=0):
"""Construct a date given a specification and a time zone offset.
self.year, self.month, self.day, self.hour, self.minute, \
self.second, x, x, x = time.gmtime(ts)
+ usagespec='[yyyy]-[mm]-[dd].[H]H:MM[:SS][offset]'
def set(self, spec, offset=0, date_re=re.compile(r'''
- (((?P<y>\d\d\d\d)[/-])?(?P<m>\d\d?)?[/-](?P<d>\d\d?))? # [yyyy-]mm-dd
+ ((?P<y>\d\d\d\d)([/-](?P<m>\d\d?)([/-](?P<d>\d\d?))?)? # yyyy[-mm[-dd]]
+ |(?P<a>\d\d?)[/-](?P<b>\d\d?))? # or mm-dd
(?P<n>\.)? # .
(((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d))?)? # hh:mm:ss
(?P<o>.+)? # offset
# not serialised data, try usual format
m = date_re.match(spec)
if m is None:
- raise ValueError, _('Not a date spec: [[yyyy-]mm-dd].'
- '[[h]h:mm[:ss]][offset]')
+ raise ValueError, _('Not a date spec: %s' % self.usagespec)
info = m.groupdict()
if add_granularity:
- _add_granularity(info, 'SMHdmy')
+ _add_granularity(info, 'SMHdmyab')
# get the current date as our default
y,m,d,H,M,S,x,x,x = time.gmtime(time.time())
- # override year, month, day parts
- if info['m'] is not None and info['d'] is not None:
- m = int(info['m'])
- d = int(info['d'])
+ if info['y'] is not None or info['a'] is not None:
if info['y'] is not None:
y = int(info['y'])
- # time defaults to 00:00:00 GMT - offset (local midnight)
+ m,d = (1,1)
+ if info['m'] is not None:
+ m = int(info['m'])
+ if info['d'] is not None:
+ d = int(info['d'])
+ if info['a'] is not None:
+ m = int(info['a'])
+ d = int(info['b'])
H = -offset
M = S = 0
try:
self.applyInterval(Interval(info['o'], allowdate=0))
except ValueError:
- raise ValueError, _('Not a date spec: [[yyyy-]mm-dd].'
- '[[h]h:mm[:ss]][offset]')
+ raise ValueError, _('Not a date spec: %s' % self.usagespec)
def addInterval(self, interval):
''' Add the interval to this date, returning the date tuple
# now cope with under- and over-flow
# first do the time
while (second < 0 or second > 59 or minute < 0 or minute > 59 or
- hour < 0 or hour > 59):
+ hour < 0 or hour > 23):
if second < 0: minute -= 1; second += 60
elif second > 59: minute += 1; second -= 60
if minute < 0: hour -= 1; minute += 60
elif minute > 59: hour += 1; minute -= 60
if hour < 0: day -= 1; hour += 24
- elif hour > 59: day += 1; hour -= 24
+ elif hour > 23: day += 1; hour -= 24
# fix up the month so we're within range
while month < 1 or month > 12:
if month > 12: year += 1; month -= 12
# now do the days, now that we know what month we're in
- mdays = calendar.mdays
- if month == 2 and calendar.isleap(year): month_days = 29
- else: month_days = mdays[month]
- while month < 1 or month > 12 or day < 0 or day > month_days:
+ def get_mdays(year, month):
+ if month == 2 and calendar.isleap(year): return 29
+ else: return calendar.mdays[month]
+
+ while month < 1 or month > 12 or day < 1 or day > get_mdays(year,month):
# now to day under/over
- if day < 0: month -= 1; day += month_days
- elif day > month_days: month += 1; day -= month_days
+ if day < 1:
+ # When going backwards, decrement month, then increment days
+ month -= 1
+ day += get_mdays(year,month)
+ elif day > get_mdays(year,month):
+ # When going forwards, decrement days, then increment month
+ day -= get_mdays(year,month)
+ month += 1
# possibly fix up the month so we're within range
while month < 1 or month > 12:
- if month < 1: year -= 1; month += 12
+ if month < 1: year -= 1; month += 12 ; day += 31
if month > 12: year += 1; month -= 12
- # re-figure the number of days for this month
- if month == 2 and calendar.isleap(year): month_days = 29
- else: month_days = mdays[month]
return (year, month, day, hour, minute, second, 0, 0, 0)
+ def differenceDate(self, other):
+ "Return the difference between this date and another date"
+
def applyInterval(self, interval):
''' Apply the interval to this date
'''
assert isinstance(other, Date), 'May only subtract Dates or Intervals'
- # TODO this code will fall over laughing if the dates cross
- # leap years, phases of the moon, ....
+ return self.dateDelta(other)
+
+ def dateDelta(self, other):
+ """ Produce an Interval of the difference between this date
+ and another date. Only returns days:hours:minutes:seconds.
+ """
+ # Returning intervals larger than a day is almost
+ # impossible - months, years, weeks, are all so imprecise.
a = calendar.timegm((self.year, self.month, self.day, self.hour,
self.minute, self.second, 0, 0, 0))
b = calendar.timegm((other.year, other.month, other.day,
other.hour, other.minute, other.second, 0, 0, 0))
diff = a - b
- if diff < 0:
+ if diff > 0:
sign = 1
- diff = -diff
else:
sign = -1
+ diff = -diff
S = diff%60
M = (diff/60)%60
- H = (diff/(60*60))%60
- if H>1: S = 0
- d = (diff/(24*60*60))%30
- if d>1: H = S = M = 0
- m = (diff/(30*24*60*60))%12
- if m>1: H = S = M = 0
- y = (diff/(365*24*60*60))
- if y>1: d = H = S = M = 0
- return Interval((y, m, d, H, M, S), sign=sign)
+ H = (diff/(60*60))%24
+ d = diff/(24*60*60)
+ return Interval((0, 0, d, H, M, S), sign=sign)
def __cmp__(self, other):
"""Compare this date to another date."""
return '%4d%02d%02d%02d%02d%02d'%(self.year, self.month,
self.day, self.hour, self.minute, self.second)
+ def timestamp(self):
+ ''' return a UNIX timestamp for this date '''
+ return calendar.timegm((self.year, self.month, self.day, self.hour,
+ self.minute, self.second, 0, 0, 0))
+
class Interval:
'''
Date intervals are specified using the suffixes "y", "m", and "d". The
raise TypeError, "Can't add %r"%other
def __div__(self, other):
- ''' Divide this interval by an int value.
+ """ Divide this interval by an int value.
Can't divide years and months sensibly in the _same_
calculation as days/time, so raise an error in that situation.
- '''
+ """
try:
other = float(other)
except TypeError:
'''
if self.year:
if self.year == 1:
- return _('1 year')
+ s = _('1 year')
else:
- return _('%(number)s years')%{'number': self.year}
+ s = _('%(number)s years')%{'number': self.year}
elif self.month or self.day > 13:
days = (self.month * 30) + self.day
if days > 28:
if self.sign < 0:
s = s + _(' ago')
else:
- s = _('in') + s
+ s = _('in ') + s
return s
def get_tuple(self):
self.day, self.hour, self.minute, self.second)
def fixTimeOverflow(time):
- ''' Handle the overflow in the time portion (H, M, S) of "time":
+ """ Handle the overflow in the time portion (H, M, S) of "time":
(sign, y,m,d,H,M,S)
Overflow and underflow will at most affect the _days_ portion of
the date. We do not overflow days to months as we don't know _how_
to, generally.
- '''
+ """
# XXX we could conceivably use this function for handling regular dates
# XXX too - we just need to interrogate the month/year for the day
# XXX overflow...