什么语言有最简单,最强大的日期解析?

时间:2022-03-11 11:40:42

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.

我知道java有SimpleDateFormat似乎相当强大,但你需要提前知道格式才能正确使用它。 TCL的时钟扫描功能似乎是我见过的最简单,最强大的功能。

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"

例如时钟扫描“1/08/2009 12:33:01 AM”将与时钟扫描一样有效“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?

编辑:好的,删除它必须是一个内置功能的想法。 Perl和Python是最好的吗?

5 个解决方案

#1


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

Python没有内置的库,但优秀的dateutil库提供了一个非常适应的parse()方法。

From simple (assuming today is 9/25):

从简单(假设今天是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

#2


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://metacpan.org/pod/Date::Parse):

Perl的Date :: Parse模块(我不知道它可以被认为是内置于该语言,但它是一个CPAN模块,这对我来说足够好)已经为我节省了无数个小时的数据转换项目。从文档(http://metacpan.org/pod/Date::Parse):

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 

#3


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.'

日期的问题是一直存在的国际化问题 - 即使是一个看着“2009年8月1日12:33:01 AM”的人也会确定它应该是1月8日或8月1日。

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.

我已经看到日期/时间解析器将接受大量的格式,并为您提供各种实际意义的选项。您还可以根据区域设置为各种格式分配优先级。不幸的是我现在找不到它。它被编写为C ++库。我不知道任何语言可以处理这种内置的东西。

#4


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

我喜欢SQL Server,在2008年它一直达到100纳秒的精度

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

所有可用的样式,请参见此处: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

你会使用ISO8601(yyyy-mm-ddThh:mi:ss.mmm)这是一种不依赖语言或语言环境的安全格式

#5


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.

Java的SimpleDateFormat做得很好。是的我知道,这不是自动的,但为什么会这样?猜测确切的值总是不好的做法IMO。

#1


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

Python没有内置的库,但优秀的dateutil库提供了一个非常适应的parse()方法。

From simple (assuming today is 9/25):

从简单(假设今天是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

#2


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://metacpan.org/pod/Date::Parse):

Perl的Date :: Parse模块(我不知道它可以被认为是内置于该语言,但它是一个CPAN模块,这对我来说足够好)已经为我节省了无数个小时的数据转换项目。从文档(http://metacpan.org/pod/Date::Parse):

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 

#3


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.'

日期的问题是一直存在的国际化问题 - 即使是一个看着“2009年8月1日12:33:01 AM”的人也会确定它应该是1月8日或8月1日。

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.

我已经看到日期/时间解析器将接受大量的格式,并为您提供各种实际意义的选项。您还可以根据区域设置为各种格式分配优先级。不幸的是我现在找不到它。它被编写为C ++库。我不知道任何语言可以处理这种内置的东西。

#4


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

我喜欢SQL Server,在2008年它一直达到100纳秒的精度

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

所有可用的样式,请参见此处: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

你会使用ISO8601(yyyy-mm-ddThh:mi:ss.mmm)这是一种不依赖语言或语言环境的安全格式

#5


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.

Java的SimpleDateFormat做得很好。是的我知道,这不是自动的,但为什么会这样?猜测确切的值总是不好的做法IMO。