Django-'datetime.date'对象没有属性'tzinfo' [英] Django - 'datetime.date' object has no attribute 'tzinfo'

查看:517
本文介绍了Django-'datetime.date'对象没有属性'tzinfo'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来通知datetime时区的代码。我尝试使用Django文档中推荐的方法。

Here is my code that I use to make the datetime timezone aware. I tried to use the recommended approach from the Django docs.

tradeDay = day.trade_date + timedelta(hours=6)
td1 = pytz.timezone("Europe/London").localize(tradeDay, is_dst=None)
tradeDay = td1.astimezone(pytz.utc)

我得到tz_info错误。如何为tz_info属性设置日期?

I get the tz_info error. How can I datetime a tz_info attribute?


USE_TZ = settings.py

USE_TZ = True in settings.py


推荐答案

似乎 day.trade_date 实际上是 datetime.date 对象而不是 datetime.datetime ,因此尝试对其进行本地化将导致错误。

It looks as though day.trade_date is actually a datetime.date object rather than a datetime.datetime so trying to localize it will cause an error.

尝试转换 day.trade_date datetime.datetime 首先使用 combine() 。然后,您可以添加6个小时并对其进行本地化。

Try converting day.trade_date to a datetime.datetime first using combine(). You can then add 6 hours and localize it.

# Convert to a datetime first
tradeDate = datetime.combine(day.trade_date, datetime.min.time())

# Now the date can be localized
tradeDay = tradeDate + timedelta(hours=6)
td1 = pytz.timezone("Europe/London").localize(tradeDay, is_dst=None)
tradeDay = td1.astimezone(pytz.utc)

这篇关于Django-'datetime.date'对象没有属性'tzinfo'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆