1515# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717#
18- # $Id: date.py,v 1.52 2003-04-21 14:29:39 kedder Exp $
18+ # $Id: date.py,v 1.53 2003-04-22 20:53:54 kedder Exp $
1919
2020__doc__ = """
2121Date, time and time interval handling.
@@ -59,6 +59,8 @@ class Date:
5959 "11-07.09:32:43" means <Date yyyy-11-07.14:32:43>
6060 "14:25" means <Date yyyy-mm-dd.19:25:00>
6161 "8:47:11" means <Date yyyy-mm-dd.13:47:11>
62+ "2003" means <Date 2003-01-01.00:00:00>
63+ "2003-06" means <Date 2003-06-01.00:00:00>
6264 "." means "right now"
6365
6466 The Date class should understand simple date expressions of the form
@@ -89,6 +91,7 @@ class Date:
8991 minute, second) is the serialisation format returned by the serialise()
9092 method, and is accepted as an argument on instatiation.
9193 '''
94+
9295 def __init__ (self , spec = '.' , offset = 0 , add_granularity = 0 ):
9396 """Construct a date given a specification and a time zone offset.
9497
@@ -104,8 +107,10 @@ def __init__(self, spec='.', offset=0, add_granularity=0):
104107 self .year , self .month , self .day , self .hour , self .minute , \
105108 self .second , x , x , x = time .gmtime (ts )
106109
110+ usagespec = '[yyyy]-[mm]-[dd].[H]H:MM[:SS][offset]'
107111 def set (self , spec , offset = 0 , date_re = re .compile (r'''
108- (((?P<y>\d\d\d\d)[/-])?(?P<m>\d\d?)?[/-](?P<d>\d\d?))? # [yyyy-]mm-dd
112+ ((?P<y>\d\d\d\d)([/-](?P<m>\d\d?)([/-](?P<d>\d\d?))?)? # yyyy[-mm[-dd]]
113+ |(?P<a>\d\d?)[/-](?P<b>\d\d?))? # or mm-dd
109114 (?P<n>\.)? # .
110115 (((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d))?)? # hh:mm:ss
111116 (?P<o>.+)? # offset
@@ -125,24 +130,27 @@ def set(self, spec, offset=0, date_re=re.compile(r'''
125130 # not serialised data, try usual format
126131 m = date_re .match (spec )
127132 if m is None :
128- raise ValueError , _ ('Not a date spec: [[yyyy-]mm-dd].'
129- '[[h]h:mm[:ss]][offset]' )
133+ raise ValueError , _ ('Not a date spec: %s' % self .usagespec )
130134
131135 info = m .groupdict ()
132136
133137 if add_granularity :
134- _add_granularity (info , 'SMHdmy ' )
138+ _add_granularity (info , 'SMHdmyab ' )
135139
136140 # get the current date as our default
137141 y ,m ,d ,H ,M ,S ,x ,x ,x = time .gmtime (time .time ())
138142
139- # override year, month, day parts
140- if info ['m' ] is not None and info ['d' ] is not None :
141- m = int (info ['m' ])
142- d = int (info ['d' ])
143+ if info ['y' ] is not None or info ['a' ] is not None :
143144 if info ['y' ] is not None :
144145 y = int (info ['y' ])
145- # time defaults to 00:00:00 GMT - offset (local midnight)
146+ m ,d = (1 ,1 )
147+ if info ['m' ] is not None :
148+ m = int (info ['m' ])
149+ if info ['d' ] is not None :
150+ d = int (info ['d' ])
151+ if info ['a' ] is not None :
152+ m = int (info ['a' ])
153+ d = int (info ['b' ])
146154 H = - offset
147155 M = S = 0
148156
@@ -165,8 +173,7 @@ def set(self, spec, offset=0, date_re=re.compile(r'''
165173 try :
166174 self .applyInterval (Interval (info ['o' ], allowdate = 0 ))
167175 except ValueError :
168- raise ValueError , _ ('Not a date spec: [[yyyy-]mm-dd].'
169- '[[h]h:mm[:ss]][offset]' )
176+ raise ValueError , _ ('Not a date spec: %s' % self .usagespec )
170177
171178 def addInterval (self , interval ):
172179 ''' Add the interval to this date, returning the date tuple
0 commit comments