【问题标题】:How to check if datetime is older than 20 seconds如何检查日期时间是否超过 20 秒
【发布时间】:2011-01-24 18:54:13
【问题描述】:

这是我第一次来这里,所以我希望我在正确的地方发布这个问题。 :)

我需要为我的脚本构建洪水控制,但我并不擅长使用 UTC 和其他东西进行日期时间到时间的转换。我希望你能帮助我。 我正在使用带有 Python 的 Google App Engine。我在 DataStore 数据库中有一个 datetime 属性,应该检查它是否超过 20 秒,然后继续。

有人可以帮帮我吗?

所以在半伪:

q = db.GqlQuery("SELECT * FROM Kudo WHERE fromuser = :1", user)
lastplus = q.get()

if lastplus.date is older than 20 seconds:
print"Go!"

【问题讨论】:

标签: python google-app-engine datetime time


【解决方案1】:

您可以使用 datetime.timedelta 数据类型,如下所示:

import datetime
lastplus = q.get()
if lastplus.date < datetime.datetime.now()-datetime.timedelta(seconds=20):
    print "Go"

在此处了解更多信息:http://docs.python.org/library/datetime.html

干杯,

菲利普

【讨论】:

    【解决方案2】:

    试试这个:

    from datetime import timedelta, datetime
    if lastplus.date < datetime.utcnow() + timedelta(seconds = -20):
        print "fee fie fo foo!"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 2017-08-27
      • 2021-07-03
      • 2015-12-23
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多