Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: parse microseconds in data-time fields #80

Merged
merged 1 commit into from
Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions kubernetes_asyncio/config/dateutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def dst(self, dt):
_re_timezone = re.compile(r"([-+])(\d\d?):?(\d\d)?")


MICROSEC_PER_SEC = 1000000


def parse_rfc3339(s):
if isinstance(s, datetime.datetime):
# no need to parse it, just make sure it has a timezone.
Expand All @@ -55,8 +58,10 @@ def parse_rfc3339(s):
dt = [0] * 7
for x in range(6):
dt[x] = int(groups[x])
us = 0
if groups[6] is not None:
dt[6] = int(groups[6])
partial_sec = float(groups[6].replace(",", "."))
us = int(MICROSEC_PER_SEC * partial_sec)
tz = UTC
if groups[7] is not None and groups[7] != 'Z' and groups[7] != 'z':
tz_groups = _re_timezone.search(groups[7]).groups()
Expand All @@ -70,7 +75,7 @@ def parse_rfc3339(s):
return datetime.datetime(
year=dt[0], month=dt[1], day=dt[2],
hour=dt[3], minute=dt[4], second=dt[5],
microsecond=dt[6], tzinfo=tz)
microsecond=us, tzinfo=tz)


def format_rfc3339(date_time):
Expand Down
31 changes: 23 additions & 8 deletions kubernetes_asyncio/config/dateutil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,39 @@

class DateUtilTest(unittest.TestCase):

def _parse_rfc3339_test(self, st, y, m, d, h, mn, s):
def _parse_rfc3339_test(self, st, y, m, d, h, mn, s, us):
actual = parse_rfc3339(st)
expected = datetime(y, m, d, h, mn, s, 0, UTC)
expected = datetime(y, m, d, h, mn, s, us, UTC)
self.assertEqual(expected, actual)

def test_parse_rfc3339(self):
self._parse_rfc3339_test("2017-07-25T04:44:21Z",
2017, 7, 25, 4, 44, 21)
2017, 7, 25, 4, 44, 21, 0)
self._parse_rfc3339_test("2017-07-25 04:44:21Z",
2017, 7, 25, 4, 44, 21)
2017, 7, 25, 4, 44, 21, 0)
self._parse_rfc3339_test("2017-07-25T04:44:21",
2017, 7, 25, 4, 44, 21)
2017, 7, 25, 4, 44, 21, 0)
self._parse_rfc3339_test("2017-07-25T04:44:21z",
2017, 7, 25, 4, 44, 21)
2017, 7, 25, 4, 44, 21, 0)
self._parse_rfc3339_test("2017-07-25T04:44:21+03:00",
2017, 7, 25, 1, 44, 21)
2017, 7, 25, 1, 44, 21, 0)
self._parse_rfc3339_test("2017-07-25T04:44:21-03:00",
2017, 7, 25, 7, 44, 21)
2017, 7, 25, 7, 44, 21, 0)

self._parse_rfc3339_test("2017-07-25T04:44:21,005Z",
2017, 7, 25, 4, 44, 21, 5000)
self._parse_rfc3339_test("2017-07-25T04:44:21.005Z",
2017, 7, 25, 4, 44, 21, 5000)
self._parse_rfc3339_test("2017-07-25 04:44:21.0050Z",
2017, 7, 25, 4, 44, 21, 5000)
self._parse_rfc3339_test("2017-07-25T04:44:21.5",
2017, 7, 25, 4, 44, 21, 500000)
self._parse_rfc3339_test("2017-07-25T04:44:21.005z",
2017, 7, 25, 4, 44, 21, 5000)
self._parse_rfc3339_test("2017-07-25T04:44:21.005+03:00",
2017, 7, 25, 1, 44, 21, 5000)
self._parse_rfc3339_test("2017-07-25T04:44:21.005-03:00",
2017, 7, 25, 7, 44, 21, 5000)

def test_format_rfc3339(self):
self.assertEqual(
Expand Down