views:

150

answers:

5

I know java has the SimpleDateFormat which seems fairly powerful, but you need to know the format ahead of time to use it correctly.
TCL's clock scan function seems to be the easiest and most powerful I've seen.

e.g. clock scan "1/08/2009 12:33:01 AM" will work just as well as
clock scan "8-Jan-2009 12:33:01"

EDIT: Okay, removing the idea that it has to be a built-in feature. Are Perl and Python the best available?

+2  A: 

Perl's Date::Parse module (I don't know that it can be considered builtin to the language, but it's a CPAN module so that's good enough for me) has saved me countless hours on data conversion projects. From the documentation (http://search.cpan.org/~gbarr/TimeDate-1.16/lib/Date/Parse.pm):

Below is a sample list of dates that are known to be parsable with Date::Parse:

1995:01:24T09:08:17.1823213           ISO-8601
1995-01-24T09:08:17.1823213
Wed, 16 Jun 94 07:29:35 CST           Comma and day name are optional 
Thu, 13 Oct 94 10:13:13 -0700
Wed, 9 Nov 1994 09:50:32 -0500 (EST)  Text in ()'s will be ignored.
21 dec 17:05                          Will be parsed in the current time zone
21-dec 17:05
21/dec 17:05
21/dec/93 17:05
1999 10:02:18 "GMT"
16 Nov 94 22:28:20 PST
earino
A: 

The problem with dates is the ever-present internationalization issues - even a human looking at "1/08/2009 12:33:01 AM" is going to be sure that it should be Jan 8th or August 1st.'

I have seen date/time parsers that will accept a ton of formats and present you with various options for what is actually meant. You can also assign priorities to the various formats based on locale. Unfortunately I can't find it now. It was written as a C++ library. I don't know of any language that could handle this sort of thing built-in.

Eclipse
you would use ISO8601 in that case yyyy-mm-ddThh:mi:ss.mmm I always use that
SQLMenace
But it's absolutely useless in trying to parse dates from various different places.
Eclipse
A: 

I like SQL Server's, in 2008 it goes all the way to 100 nanosecond precision

for all the available styles, see here: http://msdn.microsoft.com/en-us/library/ms187928.aspx

you would use ISO8601 (yyyy-mm-ddThh:mi:ss.mmm) this is one of the safe formats that don't depend on language or locale

SQLMenace
I was thinking more in terms of general use scripting or programming languages.
Scott Hoffman
+2  A: 

Python doesn't have a library built in, but the excellent dateutil library provides a parse() method that's pretty accommodating.

From simple (assuming today is 9/25):

>>> parse("Thu Sep 25 2003")
datetime.datetime(2003, 9, 25, 0, 0)

>>> parse("Sep 25 2003")
datetime.datetime(2003, 9, 25, 0, 0)

>>> parse("Sep 2003", default=DEFAULT)
datetime.datetime(2003, 9, 25, 0, 0)

>>> parse("Sep", default=DEFAULT)
datetime.datetime(2003, 9, 25, 0, 0)

>>> parse("2003", default=DEFAULT)
datetime.datetime(2003, 9, 25, 0, 0)

To ambigous:

>>> parse("10-09-2003")
datetime.datetime(2003, 10, 9, 0, 0)

>>> parse("10-09-2003", dayfirst=True)
datetime.datetime(2003, 9, 10, 0, 0)

>>> parse("10-09-03")
datetime.datetime(2003, 10, 9, 0, 0)

>>> parse("10-09-03", yearfirst=True)
datetime.datetime(2010, 9, 3, 0, 0)

To all over the board:

>>> parse("Wed, July 10, '96")
datetime.datetime(1996, 7, 10, 0, 0)

>>> parse("1996.07.10 AD at 15:08:56 PDT", ignoretz=True)
datetime.datetime(1996, 7, 10, 15, 8, 56)

>>> parse("Tuesday, April 12, 1952 AD 3:30:42pm PST", ignoretz=True)
datetime.datetime(1952, 4, 12, 15, 30, 42)

>>> parse("November 5, 1994, 8:15:30 am EST", ignoretz=True)
datetime.datetime(1994, 11, 5, 8, 15, 30)

>>> parse("3rd of May 2001")
datetime.datetime(2001, 5, 3, 0, 0)

>>> parse("5:50 A.M. on June 13, 1990")
datetime.datetime(1990, 6, 13, 5, 50)

Take a look at the documentation for it here:

http://labix.org/python-dateutil#head-c0e81a473b647dfa787dc11e8c69557ec2c3ecd2

Eric Palakovich Carr
A: 

Java's SimpleDateFormat is quite well done. Yes I know, it's not automatic but why should it be? Guessing exact values is always bad practice IMO.

Esko
Guessing is bad practice? Wouldn't that depend on the circumstance? I mean, either you force an input mask if you have that capability, or you have to know the data that you're parsing. It's either the language itself, or the developer, yes?
Scott Hoffman