python了解人类友好(简明英语)日期规范

bob*_*bah 0 python datetime

是否有与整合现有的Python包datetime和理解人类友好的日期指定格式,有点类似如何/usr/bin/dateGNU Linux不对?

$ date -d 'today'
Thu May 28 14:01:15 XXX 2015
$ date -d 'tomorrow'
Fri May 29 14:01:23 XXX 2015
$ date -d 'this Sunday'
Sun May 31 00:00:00 XXX 2015
$ date -d 'Wednesday next week'
Wed Jun 10 00:00:00 XXX 2015
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 5

parsedatetime 可能会有所帮助:

import datetime as DT
import parsedatetime as pdt

p = pdt.Calendar()
for text in ('today', 'tomorrow', 'this Sunday', 'Wednesday next week', 'next week Wednesday', ):
    timetuple, flag = p.parse(text)
    date = DT.datetime(*timetuple[:6])
    print('{:20} --> {}'.format(text, date))
Run Code Online (Sandbox Code Playgroud)

产量

today                --> 2015-05-28 09:00:00
tomorrow             --> 2015-05-29 09:00:00
this Sunday          --> 2015-05-31 09:13:46
Wednesday next week  --> 2015-06-01 09:00:00
next week Wednesday  --> 2015-06-03 09:00:00
Run Code Online (Sandbox Code Playgroud)

但请注意Wednesday next week不正确解析.