source: publico/il.spdo/trunk/Paste-1.7.5.1-py2.6.egg/paste/util/dateinterval.py @ 5327

Última Alteração nesse arquivo desde 5327 foi 5327, incluída por fabianosantos, 8 anos atrás

Import inicial.

File size: 2.4 KB
Linha 
1"""
2DateInterval.py
3
4Convert interval strings (in the form of 1w2d, etc) to
5seconds, and back again.  Is not exactly about months or
6years (leap years in particular).
7
8Accepts (y)ear, (b)month, (w)eek, (d)ay, (h)our, (m)inute, (s)econd.
9
10Exports only timeEncode and timeDecode functions. 
11"""
12
13import re
14
15__all__ = ['interval_decode', 'interval_encode']
16
17second = 1
18minute = second*60
19hour = minute*60
20day = hour*24
21week = day*7
22month = day*30
23year = day*365
24timeValues = {
25    'y': year,
26    'b': month,
27    'w': week,
28    'd': day,
29    'h': hour,
30    'm': minute,
31    's': second,
32    }
33timeOrdered = timeValues.items()
34timeOrdered.sort(lambda a, b: -cmp(a[1], b[1]))
35   
36def interval_encode(seconds, include_sign=False):
37    """Encodes a number of seconds (representing a time interval)
38    into a form like 1h2d3s.
39
40    >>> interval_encode(10)
41    '10s'
42    >>> interval_encode(493939)
43    '5d17h12m19s'
44    """
45    s = ''
46    orig = seconds
47    seconds = abs(seconds)
48    for char, amount in timeOrdered:
49        if seconds >= amount:
50            i, seconds = divmod(seconds, amount)
51            s += '%i%s' % (i, char)
52    if orig < 0:
53        s = '-' + s
54    elif not orig:
55        return '0'
56    elif include_sign:
57        s = '+' + s
58    return s
59
60_timeRE = re.compile(r'[0-9]+[a-zA-Z]')
61def interval_decode(s):
62    """Decodes a number in the format 1h4d3m (1 hour, 3 days, 3 minutes)
63    into a number of seconds
64
65    >>> interval_decode('40s')
66    40
67    >>> interval_decode('10000s')
68    10000
69    >>> interval_decode('3d1w45s')
70    864045
71    """
72    time = 0
73    sign = 1
74    s = s.strip()
75    if s.startswith('-'):
76        s = s[1:]
77        sign = -1
78    elif s.startswith('+'):
79        s = s[1:]
80    for match in allMatches(s, _timeRE):
81        char = match.group(0)[-1].lower()
82        if not timeValues.has_key(char):
83            # @@: should signal error
84            continue
85        time += int(match.group(0)[:-1]) * timeValues[char]
86    return time
87
88# @@-sgd 2002-12-23 - this function does not belong in this module, find a better place.
89def allMatches(source, regex):
90    """Return a list of matches for regex in source
91    """
92    pos = 0
93    end = len(source)
94    rv = []
95    match = regex.search(source, pos)
96    while match:
97        rv.append(match)
98        match = regex.search(source, match.end() )
99    return rv
100
101if __name__ == '__main__':
102    import doctest
103    doctest.testmod()
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.