您好,我正在编写一个依赖于时间的程序,并观察到一些我不太清楚的日期时间对象的奇怪行为。我正在处理/遇到问题的代码是......
now = datetime.now()
time_changed_state = some datettime object previously initialized
time_delay = some integer
time_arrival = time_changed_state + timedelta(minutes=time_delay)
if now < time_arrival:
do something
elif now >= time_arrival:
do something different
我一直在处理测试用例,以确保代码按照我希望的方式运行,但似乎没有。
当time_delay = 0时,我发现了奇怪的行为,我知道现在将是&gt; = time_arrival,因为time_changed_state是在此函数调用之前初始化的日期时间对象,现在已在函数内初始化。但是,“执行某些操作”代码正在执行,而不是“执行不同的代码”。
非常感谢!
答案 0 :(得分:1)
我已经编辑了你的代码,这是你期望的吗?
from datetime import datetime
from datetime import timedelta
now = datetime.now()
time_changed_state = now - timedelta(hours=2)
time_delay = 0
time_arrival = time_changed_state + timedelta(minutes=time_delay)
if now < time_arrival:
print 'something'
elif now >= time_arrival:
print 'something different'