Skip to content
Open
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: 6 additions & 3 deletions humanfriendly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ def parse_date(datestring):
:param datestring: The date/time string to parse.
:returns: A tuple with the numbers ``(year, month, day, hour, minute,
second)`` (all numbers are integers).
:raises: :exc:`InvalidDate` when the date cannot be parsed.
:raises: :exc:`InvalidDate` when the date cannot be parsed or is not a
valid calendar date.

Supported date/time formats:

Expand Down Expand Up @@ -582,10 +583,12 @@ def parse_date(datestring):
if len(tokens) >= 2:
date_parts = list(map(int, tokens[0].split('-'))) + [1, 1]
time_parts = list(map(int, tokens[1].split(':'))) + [0, 0, 0]
return tuple(date_parts[0:3] + time_parts[0:3])
result = tuple(date_parts[0:3] + time_parts[0:3])
else:
year, month, day = (list(map(int, datestring.split('-'))) + [1, 1])[0:3]
return (year, month, day, 0, 0, 0)
result = (year, month, day, 0, 0, 0)
datetime.date(*result[:3])
return result
except Exception:
msg = "Invalid date! (expected 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS' but got: %r)"
raise InvalidDate(format(msg, datestring))
Expand Down
19 changes: 19 additions & 0 deletions humanfriendly/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,25 @@ def test_parse_date(self):
with self.assertRaises(InvalidDate):
parse_date('2013-06-XY')

def test_parse_date_invalid_calendar_dates(self):
"""Reject impossible dates with and without a time component."""
for date in ('2023-02-29', '2024-02-30', '2024-04-31',
'2024-00-10', '2024-13-01', '2024-01-00', '2024-01-32'):
for suffix in ('', ' 12:34:56'):
with self.assertRaises(InvalidDate):
parse_date(date + suffix)

def test_parse_date_calendar_boundaries(self):
"""Keep leap days, month ends, and existing partial-date defaults."""
for date, parts in (('2024-02-29', (2024, 2, 29)),
('2000-02-29', (2000, 2, 29)),
('2024-04-30', (2024, 4, 30)),
('2024-12-31', (2024, 12, 31)),
('2024', (2024, 1, 1)),
('2024-02', (2024, 2, 1))):
self.assertEqual(parse_date(date), parts + (0, 0, 0))
self.assertEqual(parse_date(date + ' 12:34:56'), parts + (12, 34, 56))

def test_format_size(self):
"""Test :func:`humanfriendly.format_size()`."""
self.assertEqual('0 bytes', format_size(0))
Expand Down