比较日期时间和日期 [英] comparing datetime with date

查看:71
本文介绍了比较日期时间和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶地发现

I was very surprised to discover that

import datetime
x = datetime.date( 2004,9,14)
y = datetime.datetime(2004,9,14,6,43,15)
print x == y
import datetime
x = datetime.date(2004, 9, 14)
y = datetime.datetime(2004, 9, 14, 6, 43, 15)
print x == y



正确


这两个对象如何被认为是平等的?是否有*通用*方式

来测试日期!=日期时间以及4.5!= 4.6?


Donnal Walter

阿肯色州儿童医院


True

How can these two objects be considered equal? Is there a *general* way
to test for date != datetime as well as 4.5 != 4.6?

Donnal Walter
Arkansas Children''s Hospital

推荐答案

Donnal Walter写道:
Donnal Walter wrote:
我很惊讶发现
I was very surprised to discover that
>>>导入日期时间
>>> x = datetime.date(2004,9,14)
>>> y = datetime.datetime(2004,9,14,6,43,15)
>>> print x == y
>>> import datetime
>>> x = datetime.date(2004, 9, 14)
>>> y = datetime.datetime(2004, 9, 14, 6, 43, 15)
>>> print x == y

True

这两个对象如何被认为是平等的?

True

How can these two objects be considered equal?




猜猜:


http://docs.python中。 org / lib / datetime-datetime.html

它说"注意:为了阻止比较而不是回落到

比较对象地址的默认方案,日期时间比较

通常会引发TypeError,如果另一个comparand也不是日期时间

对象。但是,如果另一个

comparand具有timetuple属性,则返回NotImplemented。这个钩子给出了其他类型的日期

对象实现混合类型比较的机会。 """

(日期类的文档说的相同)。


我怀疑这仅仅意味着datetime.date * *使用这个

行为来提供一个有意义的混合类型比较,

到_it_。

是否有*通用*方式
测试日期!=日期时间以及4.5!= 4.6?



Guessing:

In http://docs.python.org/lib/datetime-datetime.html
it says this """Note: In order to stop comparison from falling back to
the default scheme of comparing object addresses, datetime comparison
normally raises TypeError if the other comparand isn''t also a datetime
object. However, NotImplemented is returned instead if the other
comparand has a timetuple attribute. This hook gives other kinds of date
objects a chance at implementing mixed-type comparison. """
(the docs for the date class say the same).

I suspect this means simply that datetime.date *does* use this
behaviour to provide a mixed-type comparison that makes sense,
to _it_.
Is there a *general* way
to test for date != datetime as well as 4.5 != 4.6?




无法说,对不起。


-Peter



Couldn''t say, sorry.

-Peter


Donnal Walter写道:
Donnal Walter wrote:
我很惊讶地发现
I was very surprised to discover that
>>>导入日期时间
>>> x = datetime.date(2004,9,14)
>>> y = datetime.datetime(2004,9,14,6,43,15)
>>> print x == y
>>> import datetime
>>> x = datetime.date(2004, 9, 14)
>>> y = datetime.datetime(2004, 9, 14, 6, 43, 15)
>>> print x == y

True




我认为这完全合法 - 日期对象应该只关心

对象它被比较包含某种有效的日期信息,并且

限制它的比较。


问题是你试图比较两种不同的东西在这里 - 如果它处理这个问题它的实现如此,因为没有规范的

方式比较两个甚至不是结构等价的东西。


目前的实现方式选择了一种方法 - 这就像其他人想到的那样好,并且肯定有一些便利优势

它的一面。

这两个对象如何被认为是平等的?是否有*通用*方式来测试日期!= datetime以及4.5!= 4.6?



I think thats perfectly legal - a date object should care only about if the
object it is compared to contains some sort of valid date information, and
restrict its comparision to that.

The matter is that you try to compare two different things here - so its up
to the implementation if how it deals with this, as there is no canonical
way to compare two things that aren''t even structural equivalent.

The current implementation chose one way to do it - and thats as good as any
other one could think of, and certainly has some convenience advantage on
its side.
How can these two objects be considered equal? Is there a *general* way
to test for date != datetime as well as 4.5 != 4.6?




尝试从你的日期时间对象日期,时间部分为零 -

然后你会得到你想要的东西。


-

问候,


Diez B. Roggisch



Try making a datetime-object from your date, with zeros for the time part -
then you''ll get what you want.

--
Regards,

Diez B. Roggisch


Diez B. Roggisch写道:
Diez B. Roggisch wrote:
Donnal Walter写道:
Donnal Walter wrote:
这两个对象如何被认为是平等的?是否有*通用*方式来测试日期!= datetime以及4.5!= 4.6?
How can these two objects be considered equal? Is there a *general* way
to test for date != datetime as well as 4.5 != 4.6?



尝试用你的日期创建一个datetime-object,用零对于时间部分 -
然后你会得到你想要的东西。



Try making a datetime-object from your date, with zeros for the time part -
then you''ll get what you want.




在尝试提供我的第一个答案时,我想到了建议
这个,但似乎没有一种简单的方法。

看起来以下是我能做的最好的事情('x''

是一个datetime.date对象):


z = datetime.datetime(x.year,x.month,x.day)


我刚发现timetuple(),这意味着可以用糟糕的形式写成




z = datetime.datetime(* x.timetuple()[:3])


但这并不是更好,而且更加难以理解。


回答Donnal但是,上面的问题,看来这可能是一个合理的方法:



While trying to provide my first answer, I thought of suggesting
this, but there doesn''t appear to be a simple way of doing it.
It looked like the following was the best I could do (where ''x''
is a datetime.date object):

z = datetime.datetime(x.year, x.month, x.day)

I just found timetuple(), which means this could be written
in the awful form:

z = datetime.datetime(*x.timetuple()[:3])

but that''s hardly better, and definitely more inscrutable.

To answer Donnal''s question above, however, it appears this
might be a reasonable approach:

x.timetuple()== y.timetuple()
x.timetuple() == y.timetuple()



错误


不确定现在有更好的方法。


-Peter


False

Not sure there''s a better approach readily available.

-Peter


这篇关于比较日期时间和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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