什么語言有最簡單,最強大的日期解析? - What language has the easiest and most robust date parsing? -开发者知识库

什么語言有最簡單,最強大的日期解析? - What language has the easiest and most robust date parsing? -开发者知识库,第1张

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

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复