Edgewall Software

Ticket #1942: query_future_exp.patch

File query_future_exp.patch, 2.3 KB (added by shoffmann, 17 months ago)

add future expressions to TracQuery

  • trac/util/datefmt.py

    a b  
    269269    return dt 
    270270 
    271271 
     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))') 
    272278_REL_TIME_RE = re.compile( 
    273279    r'(\d+\.?\d*)\s*' 
    274280    r'(second|minute|hour|day|week|month|year|[hdwmy])s?\s*' 
     
    287293    m=lambda v: timedelta(days=30 * v), 
    288294    y=lambda v: timedelta(days=365 * v), 
    289295) 
    290 _TIME_START_RE = re.compile(r'(this|last)\s*' 
     296_TIME_START_RE = re.compile(r'(next|this|last)\s*' 
    291297                            r'(second|minute|hour|day|week|month|year)$') 
    292298_time_starts = dict( 
    293299    second=lambda now: now.replace(microsecond=0), 
     
    308314        return now 
    309315    if text == 'today': 
    310316        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) 
    311319    if text == 'yesterday': 
    312320        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) 
    314322    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)) 
    317332    match = _TIME_START_RE.match(text) 
    318333    if match: 
    319334        (which, start) = match.groups() 
     
    326341                    dt = dt.replace(year=dt.year - 1, month=12) 
    327342            else: 
    328343                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) 
    329352        return dt 
    330353    return None 
    331354