Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Handle date with over 999999 microseconds #643

Merged
merged 3 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Handle date with over 999999 microseconds
  • Loading branch information
matthewdooler committed Aug 19, 2019
commit 18a1bfbacab0031aca0f9c8e099057a0d801d660
15 changes: 12 additions & 3 deletions arrow/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import absolute_import, unicode_literals

import re
from datetime import datetime
from datetime import datetime, timedelta

from dateutil import tz

Expand Down Expand Up @@ -275,16 +275,25 @@ def _build_datetime(parts):
elif am_pm == "am" and hour == 12:
hour = 0

microsecond = parts.get("microsecond", 0)
matthewdooler marked this conversation as resolved.
Show resolved Hide resolved
if microsecond == 1000000:
microsecond = 0
second_increment = 1
else:
second_increment = 0

increment = timedelta(seconds=second_increment)

return datetime(
year=parts.get("year", 1),
month=parts.get("month", 1),
day=parts.get("day", 1),
hour=hour,
minute=parts.get("minute", 0),
second=parts.get("second", 0),
microsecond=parts.get("microsecond", 0),
microsecond=microsecond,
tzinfo=parts.get("tzinfo"),
)
) + increment

def _parse_multiformat(self, string, formats):

Expand Down
6 changes: 6 additions & 0 deletions tests/parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ def test_parse_subsecond_rounding(self):
self.assertEqual(self.parser.parse(string, format), self.expected)
self.assertEqual(self.parser.parse_iso(string), self.expected)

# overflow (zero out the subseconds and increment the seconds)
matthewdooler marked this conversation as resolved.
Show resolved Hide resolved
self.expected = datetime(2013, 1, 1, 12, 30, 46, 000000)
matthewdooler marked this conversation as resolved.
Show resolved Hide resolved
string = "2013-01-01 12:30:45.9999995"
matthewdooler marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(self.parser.parse(string, format), self.expected)
self.assertEqual(self.parser.parse_iso(string), self.expected)

def test_map_lookup_keyerror(self):

with self.assertRaises(parser.ParserError):
Expand Down