Skip to content

Commit

Permalink
Add support for midnight at the end of day
Browse files Browse the repository at this point in the history
  • Loading branch information
ceorcham committed Oct 31, 2019
1 parent c9c2f11 commit bc3e318
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
17 changes: 16 additions & 1 deletion arrow/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,21 @@ def _build_datetime(parts):
elif am_pm == "am" and hour == 12:
hour = 0

# Support for midnight at the end of day
if hour == 24:
if parts.get("minute"):
raise ParserError("Midnight at the end of day must not contain minutes")
if parts.get("second"):
raise ParserError("Midnight at the end of day must not contain seconds")
if parts.get("microsecond"):
raise ParserError(
"Midnight at the end of day must not contain microseconds"
)
hour = 0
day_increment = 1
else:
day_increment = 0

# account for rounding up to 1000000
microsecond = parts.get("microsecond", 0)
if microsecond == 1000000:
Expand All @@ -431,7 +446,7 @@ def _build_datetime(parts):
else:
second_increment = 0

increment = timedelta(seconds=second_increment)
increment = timedelta(days=day_increment, seconds=second_increment)

return (
datetime(
Expand Down
38 changes: 38 additions & 0 deletions tests/parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,44 @@ def test_iso8601_basic_format(self):
with self.assertRaises(ParserError):
self.parser.parse_iso("20180517T1055213Z")

def test_midnight_end_day(self):
self.assertEqual(
self.parser.parse_iso("2019-10-30T24:00:00"),
datetime(2019, 10, 31, 0, 0, 0, 0),
)
self.assertEqual(
self.parser.parse_iso("2019-10-30T24:00"),
datetime(2019, 10, 31, 0, 0, 0, 0),
)
self.assertEqual(
self.parser.parse_iso("2019-10-30T24:00:00.0"),
datetime(2019, 10, 31, 0, 0, 0, 0),
)
self.assertEqual(
self.parser.parse_iso("2019-10-31T24:00:00"),
datetime(2019, 11, 1, 0, 0, 0, 0),
)
self.assertEqual(
self.parser.parse_iso("2019-12-31T24:00:00"),
datetime(2020, 1, 1, 0, 0, 0, 0),
)
self.assertEqual(
self.parser.parse_iso("2019-12-31T23:59:59.9999999"),
datetime(2020, 1, 1, 0, 0, 0, 0),
)

with self.assertRaises(ParserError):
self.parser.parse_iso("2019-12-31T24:01:00")

with self.assertRaises(ParserError):
self.parser.parse_iso("2019-12-31T24:00:01")

with self.assertRaises(ParserError):
self.parser.parse_iso("2019-12-31T24:00:00.1")

with self.assertRaises(ParserError):
self.parser.parse_iso("2019-12-31T24:00:00.999999")


class TzinfoParserTests(Chai):
def setUp(self):
Expand Down

0 comments on commit bc3e318

Please sign in to comment.