我试图从值中减去一个日期值datetime.today()来计算多久以前的东西.但它抱怨说:
TypeError: can't subtract offset-naive and offset-aware datetimes
Run Code Online (Sandbox Code Playgroud)
该值datetime.today()似乎不是"时区感知",而我的其他日期值是.如何获得值datetime.today()是时区的?现在它给我时间在当地时间,恰好是PST,即UTC-8hrs.最坏的情况是,有没有办法我可以手动输入时区值到datetime返回的对象datetime.today()并将其设置为UTC-8?当然,理想的解决方案是让它自动知道时区.
我有以下方法:
# last_updated is a datetime() object, representing the last time this program ran
def time_diff(last_updated):
day_period = last_updated.replace(day=last_updated.day + 1,
hour=1,
minute=0,
second=0,
microsecond=0)
delta_time = day_period - last_updated
hours = delta_time.seconds // 3600
# make sure a period of 24hrs have passed before shuffling
if hours >= 24:
print "hello"
else:
print "do nothing"
Run Code Online (Sandbox Code Playgroud)
我想知道从那以后24小时过去了last_updated,我怎么能这样做last_updated呢?