Code

c74b85d6a6068a83659363e83f4757843beae078
[roundup.git] / roundup / date.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 # $Id: date.py,v 1.15 2002-01-05 02:27:00 richard Exp $
20 __doc__ = """
21 Date, time and time interval handling.
22 """
24 import time, re, calendar
26 class Date:
27     '''
28     As strings, date-and-time stamps are specified with the date in
29     international standard format (yyyy-mm-dd) joined to the time
30     (hh:mm:ss) by a period ("."). Dates in this form can be easily compared
31     and are fairly readable when printed. An example of a valid stamp is
32     "2000-06-24.13:03:59". We'll call this the "full date format". When
33     Timestamp objects are printed as strings, they appear in the full date
34     format with the time always given in GMT. The full date format is
35     always exactly 19 characters long. 
37     For user input, some partial forms are also permitted: the whole time
38     or just the seconds may be omitted; and the whole date may be omitted
39     or just the year may be omitted. If the time is given, the time is
40     interpreted in the user's local time zone. The Date constructor takes
41     care of these conversions. In the following examples, suppose that yyyy
42     is the current year, mm is the current month, and dd is the current day
43     of the month; and suppose that the user is on Eastern Standard Time.
45       "2000-04-17" means <Date 2000-04-17.00:00:00>
46       "01-25" means <Date yyyy-01-25.00:00:00>
47       "2000-04-17.03:45" means <Date 2000-04-17.08:45:00>
48       "08-13.22:13" means <Date yyyy-08-14.03:13:00>
49       "11-07.09:32:43" means <Date yyyy-11-07.14:32:43>
50       "14:25" means <Date yyyy-mm-dd.19:25:00>
51       "8:47:11" means <Date yyyy-mm-dd.13:47:11>
52       "." means "right now"
54     The Date class should understand simple date expressions of the form
55     stamp + interval and stamp - interval. When adding or subtracting
56     intervals involving months or years, the components are handled
57     separately. For example, when evaluating "2000-06-25 + 1m 10d", we
58     first add one month to get 2000-07-25, then add 10 days to get
59     2000-08-04 (rather than trying to decide whether 1m 10d means 38 or 40
60     or 41 days).
62     Example usage:
63         >>> Date(".")
64         <Date 2000-06-26.00:34:02>
65         >>> _.local(-5)
66         "2000-06-25.19:34:02"
67         >>> Date(". + 2d")
68         <Date 2000-06-28.00:34:02>
69         >>> Date("1997-04-17", -5)
70         <Date 1997-04-17.00:00:00>
71         >>> Date("01-25", -5)
72         <Date 2000-01-25.00:00:00>
73         >>> Date("08-13.22:13", -5)
74         <Date 2000-08-14.03:13:00>
75         >>> Date("14:25", -5)
76         <Date 2000-06-25.19:25:00>
77     '''
78     def __init__(self, spec='.', offset=0):
79         """Construct a date given a specification and a time zone offset.
81           'spec' is a full date or a partial form, with an optional
82                  added or subtracted interval. Or a date 9-tuple.
83         'offset' is the local time zone offset from GMT in hours.
84         """
85         if type(spec) == type(''):
86             self.set(spec, offset=offset)
87         else:
88             y,m,d,H,M,S,x,x,x = spec
89             ts = calendar.timegm((y,m,d,H+offset,M,S,0,0,0))
90             self.year, self.month, self.day, self.hour, self.minute, \
91                 self.second, x, x, x = time.gmtime(ts)
93     def applyInterval(self, interval):
94         ''' Apply the interval to this date
95         '''
96         t = (self.year + interval.year,
97              self.month + interval.month,
98              self.day + interval.day,
99              self.hour + interval.hour,
100              self.minute + interval.minute,
101              self.second + interval.second, 0, 0, 0)
102         self.year, self.month, self.day, self.hour, self.minute, \
103             self.second, x, x, x = time.gmtime(calendar.timegm(t))
105     def __add__(self, other):
106         """Add an interval to this date to produce another date."""
107         t = (self.year + other.sign * other.year,
108             self.month + other.sign * other.month,
109             self.day + other.sign * other.day,
110             self.hour + other.sign * other.hour,
111             self.minute + other.sign * other.minute,
112             self.second + other.sign * other.second, 0, 0, 0)
113         return Date(time.gmtime(calendar.timegm(t)))
115     # XXX deviates from spec to allow subtraction of dates as well
116     def __sub__(self, other):
117         """ Subtract:
118              1. an interval from this date to produce another date.
119              2. a date from this date to produce an interval.
120         """
121         if isinstance(other, Date):
122             # TODO this code will fall over laughing if the dates cross
123             # leap years, phases of the moon, ....
124             a = calendar.timegm((self.year, self.month, self.day, self.hour,
125                 self.minute, self.second, 0, 0, 0))
126             b = calendar.timegm((other.year, other.month, other.day, other.hour,
127                 other.minute, other.second, 0, 0, 0))
128             diff = a - b
129             if diff < 0:
130                 sign = -1
131                 diff = -diff
132             else:
133                 sign = 1
134             S = diff%60
135             M = (diff/60)%60
136             H = (diff/(60*60))%60
137             if H>1: S = 0
138             d = (diff/(24*60*60))%30
139             if d>1: H = S = M = 0
140             m = (diff/(30*24*60*60))%12
141             if m>1: H = S = M = 0
142             y = (diff/(365*24*60*60))
143             if y>1: d = H = S = M = 0
144             return Interval((y, m, d, H, M, S), sign=sign)
145         t = (self.year - other.sign * other.year,
146              self.month - other.sign * other.month,
147              self.day - other.sign * other.day,
148              self.hour - other.sign * other.hour,
149              self.minute - other.sign * other.minute,
150              self.second - other.sign * other.second, 0, 0, 0)
151         return Date(time.gmtime(calendar.timegm(t)))
153     def __cmp__(self, other):
154         """Compare this date to another date."""
155         for attr in ('year', 'month', 'day', 'hour', 'minute', 'second'):
156             r = cmp(getattr(self, attr), getattr(other, attr))
157             if r: return r
158         return 0
160     def __str__(self):
161         """Return this date as a string in the yyyy-mm-dd.hh:mm:ss format."""
162         return '%4d-%02d-%02d.%02d:%02d:%02d'%(self.year, self.month, self.day,
163             self.hour, self.minute, self.second)
165     def pretty(self):
166         ''' print up the date date using a pretty format...
167         '''
168         return time.strftime('%e %B %Y', (self.year, self.month,
169             self.day, self.hour, self.minute, self.second, 0, 0, 0))
171     def set(self, spec, offset=0, date_re=re.compile(r'''
172               (((?P<y>\d\d\d\d)-)?((?P<m>\d\d)-(?P<d>\d\d))?)? # yyyy-mm-dd
173               (?P<n>\.)?                                       # .
174               (((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d))?)?    # hh:mm:ss
175               (?P<o>.+)?                                       # offset
176               ''', re.VERBOSE)):
177         ''' set the date to the value in spec
178         '''
179         m = date_re.match(spec)
180         if not m:
181             raise ValueError, _('Not a date spec: [[yyyy-]mm-dd].[[h]h:mm[:ss]]'
182                 '[offset]')
183         info = m.groupdict()
185         # get the current date/time using the offset
186         y,m,d,H,M,S,x,x,x = time.gmtime(time.time())
188         # override year, month, day parts
189         if info['m'] is not None and info['d'] is not None:
190             m = int(info['m'])
191             d = int(info['d'])
192             if info['y'] is not None: y = int(info['y'])
193             H = M = S = 0
195         # override hour, minute, second parts
196         if info['H'] is not None and info['M'] is not None:
197             H = int(info['H']) - offset
198             M = int(info['M'])
199             S = 0
200             if info['S'] is not None: S = int(info['S'])
202         # now handle the adjustment of hour
203         ts = calendar.timegm((y,m,d,H,M,S,0,0,0))
204         self.year, self.month, self.day, self.hour, self.minute, \
205             self.second, x, x, x = time.gmtime(ts)
207         if info['o']:
208             self.applyInterval(Interval(info['o']))
210     def __repr__(self):
211         return '<Date %s>'%self.__str__()
213     def local(self, offset):
214         """Return this date as yyyy-mm-dd.hh:mm:ss in a local time zone."""
215         t = (self.year, self.month, self.day, self.hour + offset, self.minute,
216              self.second, 0, 0, 0)
217         self.year, self.month, self.day, self.hour, self.minute, \
218             self.second, x, x, x = time.gmtime(calendar.timegm(t))
220     def get_tuple(self):
221         return (self.year, self.month, self.day, self.hour, self.minute,
222             self.second, 0, 0, 0)
224 class Interval:
225     '''
226     Date intervals are specified using the suffixes "y", "m", and "d". The
227     suffix "w" (for "week") means 7 days. Time intervals are specified in
228     hh:mm:ss format (the seconds may be omitted, but the hours and minutes
229     may not).
231       "3y" means three years
232       "2y 1m" means two years and one month
233       "1m 25d" means one month and 25 days
234       "2w 3d" means two weeks and three days
235       "1d 2:50" means one day, two hours, and 50 minutes
236       "14:00" means 14 hours
237       "0:04:33" means four minutes and 33 seconds
239     Example usage:
240         >>> Interval("  3w  1  d  2:00")
241         <Interval 22d 2:00>
242         >>> Date(". + 2d") - Interval("3w")
243         <Date 2000-06-07.00:34:02>
244     '''
245     def __init__(self, spec, sign=1):
246         """Construct an interval given a specification."""
247         if type(spec) == type(''):
248             self.set(spec)
249         else:
250             self.sign = sign
251             self.year, self.month, self.day, self.hour, self.minute, \
252                 self.second = spec
254     def __cmp__(self, other):
255         """Compare this interval to another interval."""
256         for attr in ('year', 'month', 'day', 'hour', 'minute', 'second'):
257             r = cmp(getattr(self, attr), getattr(other, attr))
258             if r: return r
259         return 0
260         
261     def __str__(self):
262         """Return this interval as a string."""
263         sign = {1:'+', -1:'-'}[self.sign]
264         l = [sign]
265         if self.year: l.append('%sy'%self.year)
266         if self.month: l.append('%sm'%self.month)
267         if self.day: l.append('%sd'%self.day)
268         if self.second:
269             l.append('%d:%02d:%02d'%(self.hour, self.minute, self.second))
270         elif self.hour or self.minute:
271             l.append('%d:%02d'%(self.hour, self.minute))
272         return ' '.join(l)
274     def set(self, spec, interval_re = re.compile('''
275             \s*
276             (?P<s>[-+])?         # + or -
277             \s*
278             ((?P<y>\d+\s*)y)?    # year
279             \s*
280             ((?P<m>\d+\s*)m)?    # month
281             \s*
282             ((?P<w>\d+\s*)w)?    # week
283             \s*
284             ((?P<d>\d+\s*)d)?    # day
285             \s*
286             (((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d))?)?   # time
287             \s*
288             ''', re.VERBOSE)):
289         ''' set the date to the value in spec
290         '''
291         self.year = self.month = self.week = self.day = self.hour = \
292             self.minute = self.second = 0
293         self.sign = 1
294         m = interval_re.match(spec)
295         if not m:
296             raise ValueError, _('Not an interval spec: [+-] [#y] [#m] [#w] '
297                 '[#d] [[[H]H:MM]:SS]')
299         info = m.groupdict()
300         for group, attr in {'y':'year', 'm':'month', 'w':'week', 'd':'day',
301                 'H':'hour', 'M':'minute', 'S':'second'}.items():
302             if info[group] is not None:
303                 setattr(self, attr, int(info[group]))
305         if self.week:
306             self.day = self.day + self.week*7
308         if info['s'] is not None:
309             self.sign = {'+':1, '-':-1}[info['s']]
311     def __repr__(self):
312         return '<Interval %s>'%self.__str__()
314     def pretty(self):
315         ''' print up the date date using one of these nice formats..
316         '''
317         if self.year or self.month > 2:
318             return None
319         if self.month or self.day > 13:
320             days = (self.month * 30) + self.day
321             if days > 28:
322                 if int(days/30) > 1:
323                     return _('%(number)s months')%{'number': int(days/30)}
324                 else:
325                     return _('1 month')
326             else:
327                 return _('%(number)s weeks')%{'number': int(days/7)}
328         if self.day > 7:
329             return _('1 week')
330         if self.day > 1:
331             return _('%(number)s days')%{'number': self.day}
332         if self.day == 1 or self.hour > 12:
333             return _('yesterday')
334         if self.hour > 1:
335             return _('%(number)s hours')%{'number': self.hour}
336         if self.hour == 1:
337             if self.minute < 15:
338                 return _('an hour')
339             quart = self.minute/15
340             if quart == 2:
341                 return _('1 1/2 hours')
342             return _('1 %(number)s/4 hours')%{'number': quart}
343         if self.minute < 1:
344             return _('just now')
345         if self.minute == 1:
346             return _('1 minute')
347         if self.minute < 15:
348             return _('%(number)s minutes')%{'number': self.minute}
349         quart = int(self.minute/15)
350         if quart == 2:
351             return _('1/2 an hour')
352         return _('%(number)s/4 hour')%{'number': quart}
354     def get_tuple(self):
355         return (self.year, self.month, self.day, self.hour, self.minute,
356             self.second)
359 def test():
360     intervals = ("  3w  1  d  2:00", " + 2d", "3w")
361     for interval in intervals:
362         print '>>> Interval("%s")'%interval
363         print `Interval(interval)`
365     dates = (".", "2000-06-25.19:34:02", ". + 2d", "1997-04-17", "01-25",
366         "08-13.22:13", "14:25")
367     for date in dates:
368         print '>>> Date("%s")'%date
369         print `Date(date)`
371     sums = ((". + 2d", "3w"), (".", "  3w  1  d  2:00"))
372     for date, interval in sums:
373         print '>>> Date("%s") + Interval("%s")'%(date, interval)
374         print `Date(date) + Interval(interval)`
376 if __name__ == '__main__':
377     test()
380 # $Log: not supported by cvs2svn $
381 # Revision 1.14  2001/11/22 15:46:42  jhermann
382 # Added module docstrings to all modules.
384 # Revision 1.13  2001/09/18 22:58:37  richard
386 # Added some more help to roundu-admin
388 # Revision 1.12  2001/08/17 03:08:11  richard
389 # fixed prettification of intervals of 1 week
391 # Revision 1.11  2001/08/15 23:43:18  richard
392 # Fixed some isFooTypes that I missed.
393 # Refactored some code in the CGI code.
395 # Revision 1.10  2001/08/07 00:24:42  richard
396 # stupid typo
398 # Revision 1.9  2001/08/07 00:15:51  richard
399 # Added the copyright/license notice to (nearly) all files at request of
400 # Bizar Software.
402 # Revision 1.8  2001/08/05 07:46:12  richard
403 # Changed date.Date to use regular string formatting instead of strftime -
404 # win32 seems to have problems with %T and no hour... or something...
406 # Revision 1.7  2001/08/02 00:27:04  richard
407 # Extended the range of intervals that are pretty-printed before actual dates
408 # are displayed.
410 # Revision 1.6  2001/07/31 09:54:18  richard
411 # Fixed the 2.1-specific gmtime() (no arg) call in roundup.date. (Paul Wright)
413 # Revision 1.5  2001/07/29 07:01:39  richard
414 # Added vim command to all source so that we don't get no steenkin' tabs :)
416 # Revision 1.4  2001/07/25 04:09:34  richard
417 # Fixed offset handling (shoulda read the spec a little better)
419 # Revision 1.3  2001/07/23 07:56:05  richard
420 # Storing only marshallable data in the db - no nasty pickled class references.
422 # Revision 1.2  2001/07/22 12:09:32  richard
423 # Final commit of Grande Splite
425 # Revision 1.1  2001/07/22 11:58:35  richard
426 # More Grande Splite
429 # vim: set filetype=python ts=4 sw=4 et si