1515# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717#
18- # $Id: date.py,v 1.23 2002-07-18 23 :07:08 richard Exp $
18+ # $Id: date.py,v 1.24 2002-08-21 07 :07:27 richard Exp $
1919
2020__doc__ = """
2121Date, time and time interval handling.
@@ -75,6 +75,10 @@ class Date:
7575 <Date 2000-08-14.03:13:00>
7676 >>> Date("14:25", -5)
7777 <Date 2000-06-25.19:25:00>
78+
79+ The date format 'yyyymmddHHMMSS' (year, month, day, hour,
80+ minute, second) is the serialisation format returned by the serialise()
81+ method, and is accepted as an argument on instatiation.
7882 '''
7983 def __init__ (self , spec = '.' , offset = 0 ):
8084 """Construct a date given a specification and a time zone offset.
@@ -210,17 +214,23 @@ def pretty(self):
210214 return str
211215
212216 def set (self , spec , offset = 0 , date_re = re .compile (r'''
213- (((?P<y>\d\d\d\d)-)?((?P<m>\d\d?)-(?P<d>\d\d?))?)? # yyyy-mm-dd
214- (?P<n>\.)? # .
215- (((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d))?)? # hh:mm:ss
216- (?P<o>.+)? # offset
217- ''' , re .VERBOSE )):
217+ (((?P<y>\d\d\d\d)-)?((?P<m>\d\d?)-(?P<d>\d\d?))?)? # yyyy-mm-dd
218+ (?P<n>\.)? # .
219+ (((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d))?)? # hh:mm:ss
220+ (?P<o>.+)? # offset
221+ ''' , re .VERBOSE ), serialised_re = re .compile ('''
222+ (?P<y>\d{4})(?P<m>\d{2})(?P<d>\d{2}) # yyyymmdd
223+ (?P<H>\d{2})(?P<M>\d{2})(?P<S>\d{2}) # HHMMSS
224+ ''' , re .VERBOSE )):
218225 ''' set the date to the value in spec
219226 '''
220- m = date_re .match (spec )
227+ m = serialised_re .match (spec )
221228 if not m :
222- raise ValueError , _ ('Not a date spec: [[yyyy-]mm-dd].[[h]h:mm[:ss]]'
223- '[offset]' )
229+ m = date_re .match (spec )
230+ if not m :
231+ raise ValueError , _ ('Not a date spec: [[yyyy-]mm-dd].'
232+ '[[h]h:mm[:ss]][offset]' )
233+
224234 info = m .groupdict ()
225235
226236 # get the current date/time using the offset
@@ -245,7 +255,7 @@ def set(self, spec, offset=0, date_re=re.compile(r'''
245255 self .year , self .month , self .day , self .hour , self .minute , \
246256 self .second , x , x , x = time .gmtime (ts )
247257
248- if info [ 'o' ] :
258+ if info . get ( 'o' , None ) :
249259 self .applyInterval (Interval (info ['o' ]))
250260
251261 def __repr__ (self ):
@@ -262,6 +272,10 @@ def get_tuple(self):
262272 return (self .year , self .month , self .day , self .hour , self .minute ,
263273 self .second , 0 , 0 , 0 )
264274
275+ def serialise (self ):
276+ return '%4d%02d%02d%02d%02d%02d' % (self .year , self .month ,
277+ self .day , self .hour , self .minute , self .second )
278+
265279class Interval :
266280 '''
267281 Date intervals are specified using the suffixes "y", "m", and "d". The
@@ -290,6 +304,10 @@ class Interval:
290304 days (or over/underflow from hours/mins/secs) will do that, and
291305 days-per-month and leap years are accounted for. Leap seconds are not.
292306
307+ The interval format 'syyyymmddHHMMSS' (sign, year, month, day, hour,
308+ minute, second) is the serialisation format returned by the serialise()
309+ method, and is accepted as an argument on instatiation.
310+
293311 TODO: more examples, showing the order of addition operation
294312 '''
295313 def __init__ (self , spec , sign = 1 ):
@@ -311,7 +329,7 @@ def __cmp__(self, other):
311329 r = cmp (getattr (self , attr ), getattr (other , attr ))
312330 if r : return r
313331 return 0
314-
332+
315333 def __str__ (self ):
316334 """Return this interval as a string."""
317335 sign = {1 :'+' , - 1 :'-' }[self .sign ]
@@ -325,35 +343,32 @@ def __str__(self):
325343 l .append ('%d:%02d' % (self .hour , self .minute ))
326344 return ' ' .join (l )
327345
328- def set (self , spec , interval_re = re .compile ('''
329- \s*
330- (?P<s>[-+])? # + or -
331- \s*
332- ((?P<y>\d+\s*)y)? # year
333- \s*
334- ((?P<m>\d+\s*)m)? # month
335- \s*
336- ((?P<w>\d+\s*)w)? # week
337- \s*
338- ((?P<d>\d+\s*)d)? # day
339- \s*
340- (((?P<H>\d+):(?P<M>\d+))?(:(?P<S>\d+))?)? # time
341- \s*
342- ''' , re .VERBOSE )):
346+ def set (self , spec , interval_re = re .compile ('''
347+ \s*(?P<s>[-+])? # + or -
348+ \s*((?P<y>\d+\s*)y)? # year
349+ \s*((?P<m>\d+\s*)m)? # month
350+ \s*((?P<w>\d+\s*)w)? # week
351+ \s*((?P<d>\d+\s*)d)? # day
352+ \s*(((?P<H>\d+):(?P<M>\d+))?(:(?P<S>\d+))?)? # time
353+ \s*''' , re .VERBOSE ), serialised_re = re .compile ('''
354+ (?P<s>[+-])(?P<y>\d{4})(?P<m>\d{2})(?P<d>\d{2})
355+ (?P<H>\d{2})(?P<M>\d{2})(?P<S>\d{2})''' , re .VERBOSE )):
343356 ''' set the date to the value in spec
344357 '''
345358 self .year = self .month = self .week = self .day = self .hour = \
346359 self .minute = self .second = 0
347360 self .sign = 1
348- m = interval_re .match (spec )
361+ m = serialised_re .match (spec )
349362 if not m :
350- raise ValueError , _ ('Not an interval spec: [+-] [#y] [#m] [#w] '
351- '[#d] [[[H]H:MM]:SS]' )
363+ m = interval_re .match (spec )
364+ if not m :
365+ raise ValueError , _ ('Not an interval spec: [+-] [#y] [#m] [#w] '
366+ '[#d] [[[H]H:MM]:SS]' )
352367
353368 info = m .groupdict ()
354369 for group , attr in {'y' :'year' , 'm' :'month' , 'w' :'week' , 'd' :'day' ,
355370 'H' :'hour' , 'M' :'minute' , 'S' :'second' }.items ():
356- if info [ group ] is not None :
371+ if info . getr ( group , None ) is not None :
357372 setattr (self , attr , int (info [group ]))
358373
359374 if self .week :
@@ -416,8 +431,12 @@ def pretty(self):
416431 return s
417432
418433 def get_tuple (self ):
419- return (self .year , self .month , self .day , self .hour , self .minute ,
420- self .second )
434+ return (self .sign , self .year , self .month , self .day , self .hour ,
435+ self .minute , self .second )
436+
437+ def serialise (self ):
438+ return '%s%4d%02d%02d%02d%02d%02d' % (self .sign , self .year , self .month ,
439+ self .day , self .hour , self .minute , self .second )
421440
422441
423442def test ():
@@ -442,6 +461,9 @@ def test():
442461
443462#
444463# $Log: not supported by cvs2svn $
464+ # Revision 1.23 2002/07/18 23:07:08 richard
465+ # Unit tests and a few fixes.
466+ #
445467# Revision 1.22 2002/07/14 06:05:50 richard
446468# . fixed the date module so that Date(". - 2d") works
447469#
0 commit comments