嗨,我遇到麻烦时两小时前在一个类似的功能我get_timestamp =>获取GMT时间两小时前的Python 2.7
def get_timestamp():
"""Return correctly formatted timestamp"""
return strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
def two_hours_ago():
"""Return correctly formatted timestamp"""
last = (date.today() - timedelta(hours=1))
return strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()-'2HOURS')
我尝试这样做:
def two_hours_ago():
"""Return correctly formatted timestamp"""
today = date.today()
yesterday = (today - timedelta(hours=2))
print(yesterday.timetuple())
return strftime("%Y-%m-%dT%H:%M:%SZ", yesterday.timetuple())
UPDATE 谢谢 黄燕浩
我正在寻找亚马逊MWS的ISO格式返回2小时的间隔,我用下面的函数返回格式正确的时间。
def two_hours_ago():
"""Return correctly formatted timestamp"""
now = datetime.now()
two_hours_ago = (now - timedelta(hours=2))
return two_hours_ago.strftime("%Y-%m-%dT%H:%M:%SZ")
def now():
"""Return correctly formatted timestamp"""
now = datetime.now()
two_hours_ago = (now - timedelta(hours=0))
return two_hours_ago.strftime("%Y-%m-%dT%H:%M:%SZ")
+1
一个'date'对象没有时间。所以'今天 - timedelta(小时= 2)'是无意义的。 –
+2
如果你想使用UTC日期和时间,你可以使用'datetime.datetime.utcnow()'。 –
+0
_I试过这个_它做了什么,它是如何不符合你的期望的? –