|
| 1 | +# Copyright 2017 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import datetime |
| 16 | +import math |
| 17 | +import re |
| 18 | + |
| 19 | + |
| 20 | +class TimezoneInfo(datetime.tzinfo): |
| 21 | + def __init__(self, h, m): |
| 22 | + self._name = "UTC" |
| 23 | + if h != 0 and m != 0: |
| 24 | + self._name += "%+03d:%2d" % (h, m) |
| 25 | + self._delta = datetime.timedelta(hours=h, minutes=math.copysign(m, h)) |
| 26 | + |
| 27 | + def utcoffset(self, dt): |
| 28 | + return self._delta |
| 29 | + |
| 30 | + def tzname(self, dt): |
| 31 | + return self._name |
| 32 | + |
| 33 | + def dst(self, dt): |
| 34 | + return datetime.timedelta(0) |
| 35 | + |
| 36 | + |
| 37 | +UTC = TimezoneInfo(0, 0) |
| 38 | + |
| 39 | +# ref https://www.ietf.org/rfc/rfc3339.txt |
| 40 | +_re_rfc3339 = re.compile(r"(\d\d\d\d)-(\d\d)-(\d\d)" # Full time |
| 41 | + r"[ Tt]" # Separator |
| 42 | + r"(\d\d):(\d\d):(\d\d)([.,]\d+)?" # partial-time |
| 43 | + r"([zZ ]|[-+]\d\d?:\d\d)?", # time-offset |
| 44 | + re.VERBOSE + re.IGNORECASE) |
| 45 | +_re_timezone = re.compile(r"([-+])(\d\d?):?(\d\d)?") |
| 46 | + |
| 47 | + |
| 48 | +def parse_rfc3339(s): |
| 49 | + if type(s) == datetime.datetime: |
| 50 | + # no need to parse it, just make sure it has a timezone. |
| 51 | + if not s.tzinfo: |
| 52 | + return s.replace(tzinfo=UTC) |
| 53 | + return s |
| 54 | + groups = _re_rfc3339.search(s).groups() |
| 55 | + dt = [0] * 7 |
| 56 | + for x in range(6): |
| 57 | + dt[x] = int(groups[x]) |
| 58 | + if groups[6] is not None: |
| 59 | + dt[6] = int(groups[6]) |
| 60 | + tz = UTC |
| 61 | + if groups[7] is not None and groups[7] != 'Z' and groups[7] != 'z': |
| 62 | + tz_groups = _re_timezone.search(groups[7]).groups() |
| 63 | + hour = int(tz_groups[1]) |
| 64 | + minute = 0 |
| 65 | + if tz_groups[0] == "-": |
| 66 | + hour *= -1 |
| 67 | + if tz_groups[2]: |
| 68 | + minute = int(tz_groups[2]) |
| 69 | + tz = TimezoneInfo(hour, minute) |
| 70 | + return datetime.datetime( |
| 71 | + year=dt[0], month=dt[1], day=dt[2], |
| 72 | + hour=dt[3], minute=dt[4], second=dt[5], |
| 73 | + microsecond=dt[6], tzinfo=tz) |
| 74 | + |
| 75 | + |
| 76 | +def format_rfc3339(date_time): |
| 77 | + if date_time.tzinfo is None: |
| 78 | + date_time = date_time.replace(tzinfo=UTC) |
| 79 | + date_time = date_time.astimezone(UTC) |
| 80 | + return date_time.strftime('%Y-%m-%dT%H:%M:%SZ') |
0 commit comments