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 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
33 changes: 23 additions & 10 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,15 +275,28 @@ def _build_datetime(parts):
elif am_pm == "am" and hour == 12:
hour = 0

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),
tzinfo=parts.get("tzinfo"),
# account for rounding up to 1000000
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=microsecond,
tzinfo=parts.get("tzinfo"),
)
+ increment
)

def _parse_multiformat(self, string, formats):
Expand Down
23 changes: 23 additions & 0 deletions tests/parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,29 @@ 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
# regression tests for issue #636
self.expected = datetime(2013, 1, 1, 12, 30, 46)
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)

self.expected = datetime(2013, 1, 1, 12, 31, 0)
string = "2013-01-01 12:30:59.9999999"
self.assertEqual(self.parser.parse(string, format), self.expected)
self.assertEqual(self.parser.parse_iso(string), self.expected)

self.expected = datetime(2013, 1, 2, 0, 0, 0)
string = "2013-01-01 23:59:59.9999999"
self.assertEqual(self.parser.parse(string, format), self.expected)
self.assertEqual(self.parser.parse_iso(string), self.expected)

# 6 digits should remain unrounded
self.expected = datetime(2013, 1, 1, 12, 30, 45, 999999)
string = "2013-01-01 12:30:45.999999"
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