return "pm"
+class ParseError(ValueError):
+ pass
+
+
def parse_date(string, locale=LC_TIME):
"""Parse a date from a string.
:param string: the string containing the date
:param locale: a `Locale` object or a locale identifier
"""
+ numbers = re.findall(r'(\d+)', string)
+ if not numbers:
+ raise ParseError("No numbers were found in input")
+
# TODO: try ISO format first?
format = get_date_format(locale=locale).pattern.lower()
year_idx = format.index('y')
# FIXME: this currently only supports numbers, but should also support month
# names, both in the requested locale, and english
- numbers = re.findall(r'(\d+)', string)
year = numbers[indexes['Y']]
if len(year) == 2:
year = 2000 + int(year)
:return: the parsed time
:rtype: `time`
"""
+ numbers = re.findall(r'(\d+)', string)
+ if not numbers:
+ raise ParseError("No numbers were found in input")
+
# TODO: try ISO format first?
format = get_time_format(locale=locale).pattern.lower()
hour_idx = format.index('h')
if 'pm' in string.lower():
hour_offset = 12
- numbers = re.findall(r'(\d+)', string)
-
# Parse up to three numbers from the string.
minute = second = 0
hour = int(numbers[indexes['H']]) + hour_offset
assert dates.parse_time(input, locale='en_US') == expected
+@pytest.mark.parametrize('case', ['', 'a', 'aaa'])
+@pytest.mark.parametrize('func', [dates.parse_date, dates.parse_time])
+def test_parse_errors(case, func):
+ with pytest.raises(dates.ParseError):
+ func(case, locale='en_US')
+
+
def test_datetime_format_get_week_number():
format = dates.DateTimeFormat(date(2006, 1, 8), Locale.parse('de_DE'))
assert format.get_week_number(6) == 1