不能减去offset-naive和offset-aware的datetimes [英] Can't subtract offset-naive and offset-aware datetimes
问题描述
我在PostgreSQL中有一个时区知道 timestamptz
字段。当我从表中抽取数据时,我想要减去时间,所以我可以得到它的年龄。
我遇到的问题是两个 datetime.datetime.now()
和 datetime.datetime.utcnow()
似乎返回时区不知道的时间戳,这导致我得到这个错误:
TypeError:不能减去offset-naive和offset-aware datetimes
有没有办法避免这种情况(最好没有使用第三方模块)。
编辑:感谢您的建议,但是尝试调整时区似乎给我错误..所以我只是要使用时区不知道的时间戳在PG,并始终插入使用:
NOW()AT TIME ZONE'UTC'
这样我的默认时间戳是UTC(即使这样做更麻烦)。
希望我最终可以找到一个修复。
你是否尝试删除时区感知?
从 http://pytz.sourceforge.net/
naive = dt.replace(tzinfo = None)
可能还需要添加时区转换。 / p>
I have a timezone aware timestamptz
field in PostgreSQL. When I pull data from the table, I then want to subtract the time right now so I can get it's age.
The problem I'm having is that both datetime.datetime.now()
and datetime.datetime.utcnow()
seem to return timezone unaware timestamps, which results in me getting this error:
TypeError: can't subtract offset-naive and offset-aware datetimes
Is there a way to avoid this (preferably without a third-party module being used).
EDIT: Thanks for the suggestions, however trying to adjust the timezone seems to give me errors.. so I'm just going to use timezone unaware timestamps in PG and always insert using:
NOW() AT TIME ZONE 'UTC'
That way all my timestamps are UTC by default (even though it's more annoying to do this).
Hopefully I can eventually find a fix for this.
have you tried to remove the timezone awareness?
from http://pytz.sourceforge.net/
naive = dt.replace(tzinfo=None)
may have to add time zone conversion as well.
这篇关于不能减去offset-naive和offset-aware的datetimes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!