"Thomas W" wrote:
[color=blue]
> I'm developing a web-application where the user sometimes has to[/color]
enter[color=blue]
> dates in plain text, allthough a format may be provided to give[/color]
clues.[color=blue]
> On the server side this piece of text has to be parsed into a[/color]
datetime[color=blue]
> python-object. Does anybody have any pointers on this?
>
> Besides the actual parsing, my main concern is the different locale
> date formats and how to be able to parse those strange us-like
> "month/day/year" compared to the clever and intuitive european-style
> "day/month/year" etc.
>
> I've searched google, but haven't found any good referances that[/color]
helped[color=blue]
> me solve this problem, especially with regards to the locale date
> format issues.
>
> Best regards,
> Thomas[/color]
Although it is not a solution to the general localization problem, you
may try the mx.DateTimeFrom() factory function
(
http://www.egenix.com/files/python/m....html#DateTime) for the
parsing part. I had also written some time ago a more robust and
customized version of such parser. The ambiguous us/european style
dates are disambiguated by the provided optional argument USA (False by
default <wink>). Below is the doctest and the documentation (with
epydoc tags); mail me offlist if you'd like to check it out.
George
#================================================= ======
def parseDateTime(string, USA=False, implyCurrentDate=False,
yearHeuristic=_20thcenturyHeuristic):
'''Tries to parse a string as a valid date and/or time.
It recognizes most common (and less common) date and time formats.
Examples:[color=blue][color=green][color=darkred]
>>> # doctest was run succesfully on...
>>> str(datetime.date.today())[/color][/color][/color]
'2005-05-16'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('21:23:39.91'))[/color][/color][/color]
'21:23:39.910000'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('16:15'))[/color][/color][/color]
'16:15:00'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('10am'))[/color][/color][/color]
'10:00:00'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('2:7:18.'))[/color][/color][/color]
'02:07:18'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('08:32:40 PM'))[/color][/color][/color]
'20:32:40'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('11:59pm'))[/color][/color][/color]
'23:59:00'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('12:32:9'))[/color][/color][/color]
'12:32:09'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('12:32:9', implyCurrentDate=True))[/color][/color][/color]
'2005-05-16 12:32:09'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('93/7/18'))[/color][/color][/color]
'1993-07-18'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('15.6.2001'))[/color][/color][/color]
'2001-06-15'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('6.15.2001'))[/color][/color][/color]
'2001-06-15'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('1980, November 20'))[/color][/color][/color]
'1980-11-20'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('4 Mar 79'))[/color][/color][/color]
'1979-03-04'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('July 4'))[/color][/color][/color]
'2005-07-04'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('15/08'))[/color][/color][/color]
'2005-08-15'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('5 Mar 3:45pm'))[/color][/color][/color]
'2005-03-05 15:45:00'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('01 02 2003'))[/color][/color][/color]
'2003-02-01'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('01 02 2003', USA=True))[/color][/color][/color]
'2003-01-02'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('3/4/92'))[/color][/color][/color]
'1992-04-03'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('3/4/92', USA=True))[/color][/color][/color]
'1992-03-04'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('12:32:09 1-2-2003'))[/color][/color][/color]
'2003-02-01 12:32:09'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('12:32:09 1-2-2003', USA=True))[/color][/color][/color]
'2003-01-02 12:32:09'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('3:45pm 5 12 2001'))[/color][/color][/color]
'2001-12-05 15:45:00'[color=blue][color=green][color=darkred]
>>> str(parseDateTime('3:45pm 5 12 2001', USA=True))[/color][/color][/color]
'2001-05-12 15:45:00'
@param USA: Disambiguates strings that are valid dates in both
(month,
day, year) and (day, month, year) order (e.g. 05/03/2002). If
True,
the first format is assumed.
@param implyCurrentDate: If True and the date is not given, the
current
date is implied.
@param yearHeuristic: If not None, a callable f(year) that
transforms the
value of the given year. The default heuristic transforms
2-digit
years to 4-digit years assuming they are in the 20th century::
lambda year: (year >= 100 and year
or year >= 10 and 1900 + year
or None)
The heuristic should return None if the year is not considered
valid.
If yearHeuristic is None, no year transformation takes place.
@return:
- C{datetime.date} if only the date is recognized.
- C{datetime.time} if only the time is recognized and
implyCurrentDate
is False.
- C{datetime.datetime} if both date and time are recognized.
@raise ValueError: If the string cannot be parsed successfully.
'''