summaryrefslogtreecommitdiff
path: root/paste/util/dateinterval.py
blob: 023bce4e5f89180129ee735f7771fcc93c5fcfc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
DateInterval.py

Convert interval strings (in the form of 1w2d, etc) to
seconds, and back again.  Is not exactly about months or
years (leap years in particular).

Accepts (y)ear, (b)month, (w)eek, (d)ay, (h)our, (m)inute, (s)econd.

Exports only timeEncode and timeDecode functions.
"""

import re

__all__ = ['interval_decode', 'interval_encode']

second = 1
minute = second*60
hour = minute*60
day = hour*24
week = day*7
month = day*30
year = day*365
timeValues = {
    'y': year,
    'b': month,
    'w': week,
    'd': day,
    'h': hour,
    'm': minute,
    's': second,
    }
timeOrdered = list(timeValues.items())
timeOrdered.sort(key=lambda x: x[1], reverse=True)


def interval_encode(seconds, include_sign=False):
    """Encodes a number of seconds (representing a time interval)
    into a form like 1h2d3s.

    >>> interval_encode(10)
    '10s'
    >>> interval_encode(493939)
    '5d17h12m19s'
    """
    s = ''
    orig = seconds
    seconds = abs(seconds)
    for char, amount in timeOrdered:
        if seconds >= amount:
            i, seconds = divmod(seconds, amount)
            s += '%i%s' % (i, char)
    if orig < 0:
        s = '-' + s
    elif not orig:
        return '0'
    elif include_sign:
        s = '+' + s
    return s

_timeRE = re.compile(r'[0-9]+[a-zA-Z]')
def interval_decode(s):
    """Decodes a number in the format 1h4d3m (1 hour, 3 days, 3 minutes)
    into a number of seconds

    >>> interval_decode('40s')
    40
    >>> interval_decode('10000s')
    10000
    >>> interval_decode('3d1w45s')
    864045
    """
    time = 0
    sign = 1
    s = s.strip()
    if s.startswith('-'):
        s = s[1:]
        sign = -1
    elif s.startswith('+'):
        s = s[1:]
    for match in allMatches(s, _timeRE):
        char = match.group(0)[-1].lower()
        if char not in timeValues:
            # @@: should signal error
            continue
        time += int(match.group(0)[:-1]) * timeValues[char]
    return time

# @@-sgd 2002-12-23 - this function does not belong in this module, find a better place.
def allMatches(source, regex):
    """Return a list of matches for regex in source
    """
    pos = 0
    end = len(source)
    rv = []
    match = regex.search(source, pos)
    while match:
        rv.append(match)
        match = regex.search(source, match.end() )
    return rv

if __name__ == '__main__':
    import doctest
    doctest.testmod()