Mercurial > repos > guerler > hhblits
comparison lib/python3.8/site-packages/pip/_vendor/pytoml/utils.py @ 0:9e54283cc701 draft
"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
author | guerler |
---|---|
date | Mon, 27 Jul 2020 03:47:31 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9e54283cc701 |
---|---|
1 import datetime | |
2 import re | |
3 | |
4 rfc3339_re = re.compile(r'(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d+)?(?:Z|([+-]\d{2}):(\d{2}))') | |
5 | |
6 def parse_rfc3339(v): | |
7 m = rfc3339_re.match(v) | |
8 if not m or m.group(0) != v: | |
9 return None | |
10 return parse_rfc3339_re(m) | |
11 | |
12 def parse_rfc3339_re(m): | |
13 r = map(int, m.groups()[:6]) | |
14 if m.group(7): | |
15 micro = float(m.group(7)) | |
16 else: | |
17 micro = 0 | |
18 | |
19 if m.group(8): | |
20 g = int(m.group(8), 10) * 60 + int(m.group(9), 10) | |
21 tz = _TimeZone(datetime.timedelta(0, g * 60)) | |
22 else: | |
23 tz = _TimeZone(datetime.timedelta(0, 0)) | |
24 | |
25 y, m, d, H, M, S = r | |
26 return datetime.datetime(y, m, d, H, M, S, int(micro * 1000000), tz) | |
27 | |
28 | |
29 def format_rfc3339(v): | |
30 offs = v.utcoffset() | |
31 offs = int(offs.total_seconds()) // 60 if offs is not None else 0 | |
32 | |
33 if offs == 0: | |
34 suffix = 'Z' | |
35 else: | |
36 if offs > 0: | |
37 suffix = '+' | |
38 else: | |
39 suffix = '-' | |
40 offs = -offs | |
41 suffix = '{0}{1:02}:{2:02}'.format(suffix, offs // 60, offs % 60) | |
42 | |
43 if v.microsecond: | |
44 return v.strftime('%Y-%m-%dT%H:%M:%S.%f') + suffix | |
45 else: | |
46 return v.strftime('%Y-%m-%dT%H:%M:%S') + suffix | |
47 | |
48 class _TimeZone(datetime.tzinfo): | |
49 def __init__(self, offset): | |
50 self._offset = offset | |
51 | |
52 def utcoffset(self, dt): | |
53 return self._offset | |
54 | |
55 def dst(self, dt): | |
56 return None | |
57 | |
58 def tzname(self, dt): | |
59 m = self._offset.total_seconds() // 60 | |
60 if m < 0: | |
61 res = '-' | |
62 m = -m | |
63 else: | |
64 res = '+' | |
65 h = m // 60 | |
66 m = m - h * 60 | |
67 return '{}{:.02}{:.02}'.format(res, h, m) |