You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django+FactoryBoy写入PostgreSQL时datetime未正确转换为UTC的问题排查

Django+FactoryBoy写入PostgreSQL时datetime未正确转换为UTC的问题排查

Okay, this is a weird one. I've got a Django model that I'm writing to Postgres via FactoryBoy for testing.

contract = ContractFactory(        
    effective_date=datetime.datetime(2023, 1, 1, tzinfo=pytz.timezone('US/Central'))
)

I'm in US/Central, and I expect the database to store the object in UTC. The datetime in Postgres should be 2023-01-01 06:00:00 UTC since the difference between timezones is 6 hours.

If I fetch the object back and print the date, it's wrong!

2023-01-01 05:51:00+00:00

If I fetch it from Postgres directly, it's also wrong:

2023-01-01 05:51:00.000000 +00:00

I checked the raw time in the database and it's correct. Postgres is running in a Docker container, and if I do a select now() the time is just fine in UTC.

Where could this be coming from?

备注:内容来源于stack exchange,提问作者mj_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 17:53:09