Mercurial > p > roundup > code
comparison roundup/date.py @ 2098:18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
...(well, sqlite too, but that doesn't care).
Probably should use BOOLEAN instead of INTEGER for the Boolean props.
Need to fix a bizzaro MySQL error (gee, how unusual)
Need to finish MySQL migration from "version 1" database schemas.
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Mon, 22 Mar 2004 07:45:40 +0000 |
| parents | fc52d57c6c3e |
| children | 62ed6505cbec |
comparison
equal
deleted
inserted
replaced
| 2097:37ede7c5f5c5 | 2098:18addf2a8596 |
|---|---|
| 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 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" | 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, | 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
| 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 17 # | 17 # |
| 18 # $Id: date.py,v 1.60 2004-02-11 23:55:08 richard Exp $ | 18 # $Id: date.py,v 1.61 2004-03-22 07:45:39 richard Exp $ |
| 19 | 19 |
| 20 """Date, time and time interval handling. | 20 """Date, time and time interval handling. |
| 21 """ | 21 """ |
| 22 __docformat__ = 'restructuredtext' | 22 __docformat__ = 'restructuredtext' |
| 23 | 23 |
| 101 'offset' | 101 'offset' |
| 102 is the local time zone offset from GMT in hours. | 102 is the local time zone offset from GMT in hours. |
| 103 """ | 103 """ |
| 104 if type(spec) == type(''): | 104 if type(spec) == type(''): |
| 105 self.set(spec, offset=offset, add_granularity=add_granularity) | 105 self.set(spec, offset=offset, add_granularity=add_granularity) |
| 106 else: | 106 return |
| 107 elif hasattr(spec, 'tuple'): | |
| 108 spec = spec.tuple() | |
| 109 try: | |
| 107 y,m,d,H,M,S,x,x,x = spec | 110 y,m,d,H,M,S,x,x,x = spec |
| 111 frac = S - int(S) | |
| 108 ts = calendar.timegm((y,m,d,H+offset,M,S,0,0,0)) | 112 ts = calendar.timegm((y,m,d,H+offset,M,S,0,0,0)) |
| 109 self.year, self.month, self.day, self.hour, self.minute, \ | 113 self.year, self.month, self.day, self.hour, self.minute, \ |
| 110 self.second, x, x, x = time.gmtime(ts) | 114 self.second, x, x, x = time.gmtime(ts) |
| 115 # we lost the fractional part | |
| 116 self.second = self.second + frac | |
| 117 except: | |
| 118 raise ValueError, 'Unknown spec %r'%spec | |
| 111 | 119 |
| 112 usagespec='[yyyy]-[mm]-[dd].[H]H:MM[:SS][offset]' | 120 usagespec='[yyyy]-[mm]-[dd].[H]H:MM[:SS][offset]' |
| 113 def set(self, spec, offset=0, date_re=re.compile(r''' | 121 def set(self, spec, offset=0, date_re=re.compile(r''' |
| 114 ((?P<y>\d\d\d\d)([/-](?P<m>\d\d?)([/-](?P<d>\d\d?))?)? # yyyy[-mm[-dd]] | 122 ((?P<y>\d\d\d\d)([/-](?P<m>\d\d?)([/-](?P<d>\d\d?))?)? # yyyy[-mm[-dd]] |
| 115 |(?P<a>\d\d?)[/-](?P<b>\d\d?))? # or mm-dd | 123 |(?P<a>\d\d?)[/-](?P<b>\d\d?))? # or mm-dd |
| 123 ''' | 131 ''' |
| 124 | 132 |
| 125 m = serialised_re.match(spec) | 133 m = serialised_re.match(spec) |
| 126 if m is not None: | 134 if m is not None: |
| 127 # we're serialised - easy! | 135 # we're serialised - easy! |
| 128 self.year, self.month, self.day, self.hour, self.minute, \ | 136 g = m.groups() |
| 129 self.second = map(int, m.groups()[:6]) | 137 (self.year, self.month, self.day, self.hour, self.minute, |
| 138 self.second) = map(int, g[:6]) | |
| 130 return | 139 return |
| 131 | 140 |
| 132 # not serialised data, try usual format | 141 # not serialised data, try usual format |
| 133 m = date_re.match(spec) | 142 m = date_re.match(spec) |
| 134 if m is None: | 143 if m is None: |
| 138 | 147 |
| 139 if add_granularity: | 148 if add_granularity: |
| 140 _add_granularity(info, 'SMHdmyab') | 149 _add_granularity(info, 'SMHdmyab') |
| 141 | 150 |
| 142 # get the current date as our default | 151 # get the current date as our default |
| 143 y,m,d,H,M,S,x,x,x = time.gmtime(time.time()) | 152 ts = time.time() |
| 153 frac = ts - int(ts) | |
| 154 y,m,d,H,M,S,x,x,x = time.gmtime(ts) | |
| 155 # gmtime loses the fractional seconds | |
| 156 S = S + frac | |
| 144 | 157 |
| 145 if info['y'] is not None or info['a'] is not None: | 158 if info['y'] is not None or info['a'] is not None: |
| 146 if info['y'] is not None: | 159 if info['y'] is not None: |
| 147 y = int(info['y']) | 160 y = int(info['y']) |
| 148 m,d = (1,1) | 161 m,d = (1,1) |
| 165 | 178 |
| 166 if add_granularity: | 179 if add_granularity: |
| 167 S = S - 1 | 180 S = S - 1 |
| 168 | 181 |
| 169 # now handle the adjustment of hour | 182 # now handle the adjustment of hour |
| 183 frac = S - int(S) | |
| 170 ts = calendar.timegm((y,m,d,H,M,S,0,0,0)) | 184 ts = calendar.timegm((y,m,d,H,M,S,0,0,0)) |
| 171 self.year, self.month, self.day, self.hour, self.minute, \ | 185 self.year, self.month, self.day, self.hour, self.minute, \ |
| 172 self.second, x, x, x = time.gmtime(ts) | 186 self.second, x, x, x = time.gmtime(ts) |
| 187 # we lost the fractional part along the way | |
| 188 self.second = self.second + frac | |
| 173 | 189 |
| 174 if info.get('o', None): | 190 if info.get('o', None): |
| 175 try: | 191 try: |
| 176 self.applyInterval(Interval(info['o'], allowdate=0)) | 192 self.applyInterval(Interval(info['o'], allowdate=0)) |
| 177 except ValueError: | 193 except ValueError: |
| 290 if r: return r | 306 if r: return r |
| 291 return 0 | 307 return 0 |
| 292 | 308 |
| 293 def __str__(self): | 309 def __str__(self): |
| 294 """Return this date as a string in the yyyy-mm-dd.hh:mm:ss format.""" | 310 """Return this date as a string in the yyyy-mm-dd.hh:mm:ss format.""" |
| 295 return '%4d-%02d-%02d.%02d:%02d:%02d'%(self.year, self.month, self.day, | 311 return self.formal() |
| 296 self.hour, self.minute, self.second) | 312 |
| 313 def formal(self, sep='.', sec='%02d'): | |
| 314 f = '%%4d-%%02d-%%02d%s%%02d:%%02d:%s'%(sep, sec) | |
| 315 return f%(self.year, self.month, self.day, self.hour, self.minute, | |
| 316 self.second) | |
| 297 | 317 |
| 298 def pretty(self, format='%d %B %Y'): | 318 def pretty(self, format='%d %B %Y'): |
| 299 ''' print up the date date using a pretty format... | 319 ''' print up the date date using a pretty format... |
| 300 | 320 |
| 301 Note that if the day is zero, and the day appears first in the | 321 Note that if the day is zero, and the day appears first in the |
| 325 return '%4d%02d%02d%02d%02d%02d'%(self.year, self.month, | 345 return '%4d%02d%02d%02d%02d%02d'%(self.year, self.month, |
| 326 self.day, self.hour, self.minute, self.second) | 346 self.day, self.hour, self.minute, self.second) |
| 327 | 347 |
| 328 def timestamp(self): | 348 def timestamp(self): |
| 329 ''' return a UNIX timestamp for this date ''' | 349 ''' return a UNIX timestamp for this date ''' |
| 330 return calendar.timegm((self.year, self.month, self.day, self.hour, | 350 frac = self.second - int(self.second) |
| 351 ts = calendar.timegm((self.year, self.month, self.day, self.hour, | |
| 331 self.minute, self.second, 0, 0, 0)) | 352 self.minute, self.second, 0, 0, 0)) |
| 353 # we lose the fractional part | |
| 354 return ts + frac | |
| 332 | 355 |
| 333 class Interval: | 356 class Interval: |
| 334 ''' | 357 ''' |
| 335 Date intervals are specified using the suffixes "y", "m", and "d". The | 358 Date intervals are specified using the suffixes "y", "m", and "d". The |
| 336 suffix "w" (for "week") means 7 days. Time intervals are specified in | 359 suffix "w" (for "week") means 7 days. Time intervals are specified in |
