从“今天”开始的时候,最好的方法是什么或“昨天”和Python的时间? [英] What's the best way to make a time from "Today" or "Yesterday" and a time in Python?

查看:145
本文介绍了从“今天”开始的时候,最好的方法是什么或“昨天”和Python的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python具有非常好的日期解析功能,但是通过今天创建一个新的日期并进行减法来识别日期时间的唯一方法,例如今天3:20 PM或昨天上午11:06。

Python has pretty good date parsing but is the only way to recognize a datetime such as "Today 3:20 PM" or "Yesterday 11:06 AM" by creating a new date today and doing subtractions?

推荐答案

我喜欢很多的图书馆,我看到越来越多的人使用,是 python-dateutil ,但不幸的是,它也不是其他传统的大日期时间解析器,来自Egenix的mxDateTime 可以解析tomorrow一词,尽管这两个库都具有非常强大的模糊解析器。

A library that I like a lot, and I'm seeing more and more people use, is python-dateutil but unfortunately neither it nor the other traditional big datetime parser, mxDateTime from Egenix can parse the word "tomorrow" in spite of both libraries having very strong "fuzzy" parsers.

我看到的唯一可以做到这一点的库是 magicdate 。示例:

The only library I've seen that can do this is magicdate. Examples:

>>> import magicdate
>>> magicdate.magicdate('today')
datetime.date(2009, 2, 15)
>>> magicdate.magicdate('tomorrow')
datetime.date(2009, 2, 16)
>>> magicdate.magicdate('yesterday')
datetime.date(2009, 2, 14)

不幸的是,这只返回datetime.date对象,所以不会包含时间部分,不能处理你的今天下午3:20的例子。

Unfortunately this only returns datetime.date objects, and so won't include time parts and can't handle your example of "Today 3:20 PM".

所以,你需要mxDateTime。示例:

So, you need mxDateTime for that. Examples:

>>> import mx.DateTime

>>> mx.DateTime.Parser.DateTimeFromString("Today 3:20 PM")
<mx.DateTime.DateTime object for '2009-02-15 15:20:00.00' at 28faa28>

>>> mx.DateTime.Parser.DateTimeFromString("Tomorrow 5:50 PM")
<mx.DateTime.DateTime object for '2009-02-15 17:50:00.00' at 2a86088>

编辑:mxDateTime.Parser只解析这些示例中的时间,忽略今天和明天。所以对于这种特殊情况,您需要使用magicdate的组合来获取日期和mxDateTime来获取时间。我的建议是使用python-dateutils或mxDateTime,只接受他们可以解析的字符串格式。

mxDateTime.Parser is only parsing the time in these examples and ignoring the words "today" and "tomorrow". So for this particular case you need to use a combo of magicdate to get the date and mxDateTime to get the time. My recommendation is to just use python-dateutils or mxDateTime and only accept the string formats they can parse.

编辑2:如在注释中所指出的,python-dateutil现在可以处理模糊的解析。我还发现了开发用于Chandler的 parsedatetime 模块,它与这个问题的查询:

EDIT 2: As noted in the comments it looks python-dateutil can now handle fuzzy parsing. I've also since discovered the parsedatetime module that was developed for use in Chandler and it works with the queries in this question:

>>> import parsedatetime.parsedatetime as pdt
>>> import parsedatetime.parsedatetime_consts as pdc
>>> c=pdc.Constants()
>>> p=pdt.Calendar(c)
>>> p.parse('Today 3:20 PM')
((2010, 3, 12, 15, 20, 0, 4, 71, -1), 3)
>>> p.parse('Yesterday 11:06 AM')
((2010, 3, 11, 11, 6, 0, 3, 70, -1), 3)

,以下是当前时间:

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2010, 3, 12, 15, 23, 35, 951652)

这篇关于从“今天”开始的时候,最好的方法是什么或“昨天”和Python的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆