enh: allow time like HH:MM in date strings. (#10034)

This commit is contained in:
Matthias Wolf 2019-02-13 11:05:00 +01:00 committed by Massimiliano Culpo
parent f65a1155e1
commit 861dd06bd1
2 changed files with 8 additions and 1 deletions

View file

@ -448,7 +448,8 @@ def pretty_string_to_date(date_str, now=None):
Args:
date_str (str): string representing a date. This string might be
in different format (like ``YYYY``, ``YYYY-MM``, ``YYYY-MM-DD``)
in different format (like ``YYYY``, ``YYYY-MM``, ``YYYY-MM-DD``,
``YYYY-MM-DD HH:MM``, ``YYYY-MM-DD HH:MM:SS``)
or be a *pretty date* (like ``yesterday`` or ``two months ago``)
Returns:
@ -467,6 +468,10 @@ def pretty_string_to_date(date_str, now=None):
pattern[re.compile(r'^\d{4}-\d{2}-\d{2}$')] = lambda x: datetime.strptime(
x, '%Y-%m-%d'
)
pattern[re.compile(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$')] = \
lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M')
pattern[re.compile(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$')] = \
lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
pretty_regex = re.compile(
r'(a|\d+)\s*(year|month|week|day|hour|minute|second)s?\s*ago')

View file

@ -82,6 +82,8 @@ def test_pretty_string_to_date_delta(now, delta, pretty_string):
('%Y', '2018'),
('%Y-%m', '2015-03'),
('%Y-%m-%d', '2015-03-28'),
('%Y-%m-%d %H:%M', '2015-03-28 11:12'),
('%Y-%m-%d %H:%M:%S', '2015-03-28 23:34:45'),
])
def test_pretty_string_to_date(format, pretty_string):
t1 = datetime.strptime(pretty_string, format)