1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """DNS TTL conversion."""
19
20 import dns.exception
21 from ._compat import long
22
23
24 -class BadTTL(dns.exception.SyntaxError):
25 """DNS TTL value is not well-formed."""
26
27
29 """Convert the text form of a TTL to an integer.
30
31 The BIND 8 units syntax for TTLs (e.g. '1w6d4h3m10s') is supported.
32
33 *text*, a ``text``, the textual TTL.
34
35 Raises ``dns.ttl.BadTTL`` if the TTL is not well-formed.
36
37 Returns an ``int``.
38 """
39
40 if text.isdigit():
41 total = long(text)
42 else:
43 if not text[0].isdigit():
44 raise BadTTL
45 total = long(0)
46 current = long(0)
47 for c in text:
48 if c.isdigit():
49 current *= 10
50 current += long(c)
51 else:
52 c = c.lower()
53 if c == 'w':
54 total += current * long(604800)
55 elif c == 'd':
56 total += current * long(86400)
57 elif c == 'h':
58 total += current * long(3600)
59 elif c == 'm':
60 total += current * long(60)
61 elif c == 's':
62 total += current
63 else:
64 raise BadTTL("unknown unit '%s'" % c)
65 current = 0
66 if not current == 0:
67 raise BadTTL("trailing integer")
68 if total < long(0) or total > long(2147483647):
69 raise BadTTL("TTL should be between 0 and 2^31 - 1 (inclusive)")
70 return total
71