如何检查日期时间是否早于20秒 [英] How to check if datetime is older than 20 seconds
问题描述
这是我第一次来这里,希望我在正确的位置发布此问题.:)
This is my first time here so I hope I post this question at the right place. :)
我需要为我的脚本构建泛洪控制,但是我不擅长使用UTC和其他东西进行日期到时间的转换.我希望你能帮助我.我正在将Google App Engine与Python配合使用.我在DataStore数据库中有一个datetime属性,应该检查它是否早于20秒,然后继续.
I need to build flood control for my script but I'm not good at all this datetime to time conversions with UTC and stuff. I hope you can help me out. I'm using the Google App Engine with Python. I've got a datetimeproperty at the DataStore database which should be checked if it's older than 20 seconds, then proceed.
有人可以帮我吗?
所以在半伪装中:
q = db.GqlQuery("SELECT * FROM Kudo WHERE fromuser = :1", user)
lastplus = q.get()
if lastplus.date is older than 20 seconds:
print"Go!"
推荐答案
您可以使用datetime.timedelta数据类型,如下所示:
You can use the datetime.timedelta datatype, like this:
import datetime
lastplus = q.get()
if lastplus.date < datetime.datetime.now()-datetime.timedelta(seconds=20):
print "Go"
在此处了解更多信息: http://docs.python.org/library/datetime.html
Read more about it here: http://docs.python.org/library/datetime.html
干杯
菲利普
这篇关于如何检查日期时间是否早于20秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!