Anonymous avatar Anonymous committed 7ae4722

Started implementation of datetime parsing, using a very basic approach for now.

Comments (0)

Files changed (2)

 """
 
 from datetime import date, datetime, time, timedelta, tzinfo
+import re
 
 from babel.core import Locale
 from babel.util import default_locale, UTC
     return parse_pattern(format).apply(time, locale)
 
 def parse_date(string, locale=LC_TIME):
-    raise NotImplementedError
+    """Parse a date from a string.
+    
+    This function uses the date format for the locale as a hint to determine
+    the order in which the date fields appear in the string.
+    
+    >>> parse_date('4/1/04', locale='en_US')
+    datetime.date(2004, 4, 1)
+    >>> parse_date('01.04.2004', locale='de_DE')
+    datetime.date(2004, 4, 1)
+    
+    :param string: the string containing the date
+    :param locale: a `Locale` object or a locale identifier
+    :return: the parsed date
+    :rtype: `date`
+    """
+    # TODO: try ISO format first?
+    format = get_date_format(locale=locale).pattern.lower()
+    year_idx = format.index('y')
+    month_idx = format.index('m')
+    if month_idx < 0:
+        month_idx = format.index('l')
+    day_idx = format.index('d')
+
+    indexes = [(year_idx, 'Y'), (month_idx, 'M'), (day_idx, 'D')]
+    indexes.sort()
+    indexes = dict([(item[1], idx) for idx, item in enumerate(indexes)])
+
+    # FIXME: this currently only supports numbers, but should also support month
+    #        names, both in the requested locale, and english
+
+    numbers = re.findall('(\d+)', string)
+    year = numbers[indexes['Y']]
+    if len(year) == 2:
+        year = 2000 + int(year)
+    else:
+        year = int(year)
+    month = int(numbers[indexes['M']])
+    day = int(numbers[indexes['D']])
+    if month > 12:
+        month, day = day, month
+    return date(year, month, day)
 
 def parse_datetime(string, locale=LC_TIME):
     raise NotImplementedError
 
 def parse_time(string, locale=LC_TIME):
-    raise NotImplementedError
+    """Parse a tiem from a string.
+    
+    This function uses the time format for the locale as a hint to determine
+    the order in which the time fields appear in the string.
+    
+    >>> parse_time('15:30:00', locale='en_US')
+    datetime.time(15, 30)
+    
+    :param string: the string containing the time
+    :param locale: a `Locale` object or a locale identifier
+    :return: the parsed time
+    :rtype: `time`
+    """
+    # TODO: try ISO format first?
+    format = get_time_format(locale=locale).pattern.lower()
+    hour_idx = format.index('h')
+    if hour_idx < 0:
+        hour_idx = format.index('k')
+    min_idx = format.index('m')
+    sec_idx = format.index('s')
+
+    indexes = [(hour_idx, 'H'), (min_idx, 'M'), (sec_idx, 'S')]
+    indexes.sort()
+    indexes = dict([(item[1], idx) for idx, item in enumerate(indexes)])
+
+    # FIXME: support 12 hour clock, and 0-based hour specification
+
+    numbers = re.findall('(\d+)', string)
+    hour = int(numbers[indexes['H']])
+    minute = int(numbers[indexes['M']])
+    second = int(numbers[indexes['S']])
+    return time(hour, minute, second)
 
 
 class DateTimePattern(object):
 this support is based on the assumption that there will be only one specific
 locale used per process (at least simultaneously.) Also, it doesn't provide
 access to other kinds of locale data, such as the localized names of countries,
-languages, or time zones, with are frequently needed in web-based applications.
+languages, or time-zones, which are frequently needed in web-based applications.
 
 For these requirements, Babel includes data extracted from the `Common Locale
 Data Repository (CLDR) <http://unicode.org/cldr/>`_, and provides a number of
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.