Ticket #1942: query_future_exp.patch
| File query_future_exp.patch, 2.3 KB (added by shoffmann, 17 months ago) |
|---|
-
trac/util/datefmt.py
a b 269 269 return dt 270 270 271 271 272 _REL_FUTURE_RE = re.compile( 273 r'(?:(?:(?:in)|(?:\+))\s*(\d+\.?\d*)\s*' 274 r'(second|minute|hour|day|week|month|year|[hdwmy])s?\s*$)|' 275 r'(?:(\d+\.?\d*)\s*' 276 r'(second|minute|hour|day|week|month|year|[hdwmy])s?\s*' 277 r'(?:ahead))') 272 278 _REL_TIME_RE = re.compile( 273 279 r'(\d+\.?\d*)\s*' 274 280 r'(second|minute|hour|day|week|month|year|[hdwmy])s?\s*' … … 287 293 m=lambda v: timedelta(days=30 * v), 288 294 y=lambda v: timedelta(days=365 * v), 289 295 ) 290 _TIME_START_RE = re.compile(r'( this|last)\s*'296 _TIME_START_RE = re.compile(r'(next|this|last)\s*' 291 297 r'(second|minute|hour|day|week|month|year)$') 292 298 _time_starts = dict( 293 299 second=lambda now: now.replace(microsecond=0), … … 308 314 return now 309 315 if text == 'today': 310 316 return now.replace(second=0, minute=0, hour=0) 317 if text == 'tomorrow': 318 return now.replace(second=0, minute=0, hour=0) + timedelta(days=1) 311 319 if text == 'yesterday': 312 320 return now.replace(second=0, minute=0, hour=0) - timedelta(days=1) 313 match = _REL_ TIME_RE.match(text)321 match = _REL_FUTURE_RE.match(text) 314 322 if match: 315 (value, interval) = match.groups() 316 return now - _time_intervals[interval](float(value)) 323 value = match.group(1) or match.group(3) 324 interval = match.group(2) or match.group(4) 325 return now + _time_intervals[interval](float(value)) 326 else: 327 # relative time is past, if not matching future expression 328 match = _REL_TIME_RE.match(text) 329 if match: 330 (value, interval) = match.groups() 331 return now - _time_intervals[interval](float(value)) 317 332 match = _TIME_START_RE.match(text) 318 333 if match: 319 334 (which, start) = match.groups() … … 326 341 dt = dt.replace(year=dt.year - 1, month=12) 327 342 else: 328 343 dt -= _time_intervals[start](1) 344 elif which == 'next': 345 if start == 'month': 346 if dt.month < 12: 347 dt = dt.replace(month=dt.month + 1) 348 else: 349 dt = dt.replace(year=dt.year + 1, month=1) 350 else: 351 dt += _time_intervals[start](1) 329 352 return dt 330 353 return None 331 354
