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