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
6 changes: 4 additions & 2 deletions humanfriendly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,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.datetime(*result)
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
16 changes: 16 additions & 0 deletions humanfriendly/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,22 @@ def test_parse_date(self):
with self.assertRaises(InvalidDate):
parse_date('2013-06-XY')

def test_parse_date_rejects_invalid_calendar_values(self):
"""Raise InvalidDate for dates that cannot be used as datetimes."""
for value in ['2023-02-29', '2024-02-30', '2024-04-31', '2024-00-10',
'2024-13-10', '2024-01-00', '0000-01-01', '10000-01-01',
'2024-01-01 24:00:00', '2024-01-01 12:60:00',
'2024-01-01 12:00:60']:
with self.assertRaises(InvalidDate):
parse_date(value)

def test_parse_date_preserves_valid_and_partial_dates(self):
"""Keep leap days and the existing defaults for omitted components."""
assert parse_date('2024-02-29') == (2024, 2, 29, 0, 0, 0)
assert parse_date('2024') == (2024, 1, 1, 0, 0, 0)
assert parse_date('2024-02') == (2024, 2, 1, 0, 0, 0)
assert parse_date('2024-02-29 23:59:59') == (2024, 2, 29, 23, 59, 59)

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