Skip to content

Commit

Permalink
Handle cascading rounding.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Nov 28, 2023
1 parent 1e0a3f2 commit 240eb7c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 5 additions & 3 deletions ops/_private/timeconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def parse_rfc3339(s: str) -> datetime.datetime:

microsecond = round(float(sfrac or '0') * 1000000)
carry, microsecond = divmod(microsecond, 1000000)
ss = int(ss) + carry

return datetime.datetime(int(y), int(m), int(d), int(hh), int(mm), ss,
microsecond=microsecond, tzinfo=tz)
parsed = datetime.datetime(int(y), int(m), int(d), int(hh), int(mm), int(ss),
microsecond=microsecond, tzinfo=tz)
if carry:
return parsed + datetime.timedelta(seconds=carry)
return parsed
3 changes: 3 additions & 0 deletions test/test_private.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def test_parse_rfc3339(self):
self.assertEqual(timeconv.parse_rfc3339('2006-08-28T13:20:00.99999999Z'),
datetime.datetime(2006, 8, 28, 13, 20, 1, 0, tzinfo=utc))

self.assertEqual(timeconv.parse_rfc3339('2006-12-31T23:59:59.99999999Z'),
datetime.datetime(2007, 1, 1, 0, 0, 0, 0, tzinfo=utc))

tzinfo = datetime.timezone(datetime.timedelta(hours=-11, minutes=-30))
self.assertEqual(timeconv.parse_rfc3339('2020-12-25T13:45:50.123456789-11:30'),
datetime.datetime(2020, 12, 25, 13, 45, 50, 123457, tzinfo=tzinfo))
Expand Down

0 comments on commit 240eb7c

Please sign in to comment.