Mercurial > p > roundup > code
comparison roundup/date.py @ 75:fa5aea4c212e
Fixed offset handling (shoulda read the spec a little better)
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Wed, 25 Jul 2001 04:09:34 +0000 |
| parents | 34491d9b91a0 |
| children | 0791d13baea7 |
comparison
equal
deleted
inserted
replaced
| 74:8233840961e3 | 75:fa5aea4c212e |
|---|---|
| 1 # $Id: date.py,v 1.3 2001-07-23 07:56:05 richard Exp $ | 1 # $Id: date.py,v 1.4 2001-07-25 04:09:34 richard Exp $ |
| 2 | 2 |
| 3 import time, re, calendar | 3 import time, re, calendar |
| 4 | 4 |
| 5 class Date: | 5 class Date: |
| 6 ''' | 6 ''' |
| 64 'offset' is the local time zone offset from GMT in hours. | 64 'offset' is the local time zone offset from GMT in hours. |
| 65 """ | 65 """ |
| 66 if type(spec) == type(''): | 66 if type(spec) == type(''): |
| 67 self.set(spec, offset=offset) | 67 self.set(spec, offset=offset) |
| 68 else: | 68 else: |
| 69 y,m,d,H,M,S,x,x,x = spec | |
| 70 ts = calendar.timegm((y,m,d,H+offset,M,S,0,0,0)) | |
| 69 self.year, self.month, self.day, self.hour, self.minute, \ | 71 self.year, self.month, self.day, self.hour, self.minute, \ |
| 70 self.second, x, x, x = spec | 72 self.second, x, x, x = time.gmtime(ts) |
| 71 self.offset = offset | |
| 72 | 73 |
| 73 def applyInterval(self, interval): | 74 def applyInterval(self, interval): |
| 74 ''' Apply the interval to this date | 75 ''' Apply the interval to this date |
| 75 ''' | 76 ''' |
| 76 t = (self.year + interval.year, | 77 t = (self.year + interval.year, |
| 137 if r: return r | 138 if r: return r |
| 138 return 0 | 139 return 0 |
| 139 | 140 |
| 140 def __str__(self): | 141 def __str__(self): |
| 141 """Return this date as a string in the yyyy-mm-dd.hh:mm:ss format.""" | 142 """Return this date as a string in the yyyy-mm-dd.hh:mm:ss format.""" |
| 142 return time.strftime('%Y-%m-%d.%T', (self.year, self.month, | 143 return time.strftime('%Y-%m-%d.%T', (self.year, self.month, self.day, |
| 143 self.day, self.hour, self.minute, self.second, 0, 0, 0)) | 144 self.hour, self.minute, self.second, 0, 0, 0)) |
| 144 | 145 |
| 145 def pretty(self): | 146 def pretty(self): |
| 146 ''' print up the date date using a pretty format... | 147 ''' print up the date date using a pretty format... |
| 147 ''' | 148 ''' |
| 148 return time.strftime('%e %B %Y', (self.year, self.month, | 149 return time.strftime('%e %B %Y', (self.year, self.month, |
| 160 if not m: | 161 if not m: |
| 161 raise ValueError, 'Not a date spec: [[yyyy-]mm-dd].[[h]h:mm[:ss]] [offset]' | 162 raise ValueError, 'Not a date spec: [[yyyy-]mm-dd].[[h]h:mm[:ss]] [offset]' |
| 162 info = m.groupdict() | 163 info = m.groupdict() |
| 163 | 164 |
| 164 # get the current date/time using the offset | 165 # get the current date/time using the offset |
| 165 y,m,d,H,M,S,x,x,x = time.gmtime(time.time()) | 166 y,m,d,H,M,S,x,x,x = time.gmtime() |
| 166 ts = calendar.timegm((y,m,d,H+offset,M,S,0,0,0)) | 167 |
| 168 # override year, month, day parts | |
| 169 if info['m'] is not None and info['d'] is not None: | |
| 170 m = int(info['m']) | |
| 171 d = int(info['d']) | |
| 172 if info['y'] is not None: y = int(info['y']) | |
| 173 H = M = S = 0 | |
| 174 | |
| 175 # override hour, minute, second parts | |
| 176 if info['H'] is not None and info['M'] is not None: | |
| 177 H = int(info['H']) - offset | |
| 178 M = int(info['M']) | |
| 179 S = 0 | |
| 180 if info['S'] is not None: S = int(info['S']) | |
| 181 | |
| 182 # now handle the adjustment of hour | |
| 183 ts = calendar.timegm((y,m,d,H,M,S,0,0,0)) | |
| 167 self.year, self.month, self.day, self.hour, self.minute, \ | 184 self.year, self.month, self.day, self.hour, self.minute, \ |
| 168 self.second, x, x, x = time.gmtime(ts) | 185 self.second, x, x, x = time.gmtime(ts) |
| 169 | |
| 170 if info['m'] is not None and info['d'] is not None: | |
| 171 self.month = int(info['m']) | |
| 172 self.day = int(info['d']) | |
| 173 if info['y'] is not None: | |
| 174 self.year = int(info['y']) | |
| 175 self.hour = self.minute = self.second = 0 | |
| 176 | |
| 177 if info['H'] is not None and info['M'] is not None: | |
| 178 self.hour = int(info['H']) | |
| 179 self.minute = int(info['M']) | |
| 180 if info['S'] is not None: | |
| 181 self.second = int(info['S']) | |
| 182 | 186 |
| 183 if info['o']: | 187 if info['o']: |
| 184 self.applyInterval(Interval(info['o'])) | 188 self.applyInterval(Interval(info['o'])) |
| 185 | 189 |
| 186 def __repr__(self): | 190 def __repr__(self): |
| 349 if __name__ == '__main__': | 353 if __name__ == '__main__': |
| 350 test() | 354 test() |
| 351 | 355 |
| 352 # | 356 # |
| 353 # $Log: not supported by cvs2svn $ | 357 # $Log: not supported by cvs2svn $ |
| 358 # Revision 1.3 2001/07/23 07:56:05 richard | |
| 359 # Storing only marshallable data in the db - no nasty pickled class references. | |
| 360 # | |
| 354 # Revision 1.2 2001/07/22 12:09:32 richard | 361 # Revision 1.2 2001/07/22 12:09:32 richard |
| 355 # Final commit of Grande Splite | 362 # Final commit of Grande Splite |
| 356 # | 363 # |
| 357 # Revision 1.1 2001/07/22 11:58:35 richard | 364 # Revision 1.1 2001/07/22 11:58:35 richard |
| 358 # More Grande Splite | 365 # More Grande Splite |
