gpt4 book ai didi

python - 时间戳值改变 Django

转载 作者:行者123 更新时间:2023-11-29 14:33:38 25 4
gpt4 key购买 nike

存储在数据库中的时间戳是正确的,但在查询时时间值正在改变。

models.py
aed = models.CharField(max_length=100)
gbp = models.CharField(max_length=100)
timestamp = models.DateTimeField(auto_now= False,auto_now_add=True)

数据库存储值

2018-01-10 13:17:05.107542+05:30
2018-01-10 13:18:01.803399+05:30
2018-01-10 13:19:01.873091+05:30
2018-01-10 13:20:04.476807+05:30
2018-01-10 13:21:01.913048+05:30

我的查询值

2018-01-10 07:47:05.107542+00:00
2018-01-10 07:47:05
2018-01-10 07:49:01.873091+00:00
2018-01-10 07:49:01
2018-01-10 07:51:01.913048+00:00
2018-01-10 07:51:01
2018-01-10 07:48:01.803399+00:00
2018-01-10 07:48:01
2018-01-10 07:50:04.476807+00:00
2018-01-10 07:50:04

时代变了。

我的设置.py

TIME_ZONE =  'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

最佳答案

https://www.postgresql.org/docs/current/static/datatype-datetime.html

For timestamp with time zone, the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone. If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's TimeZone parameter, and is converted to UTC using the offset for the timezone zone.

When a timestamp with time zone value is output, it is always converted from UTC to the current timezone zone, and displayed as local time in that zone.

格式化我的。

您看到的是不同的客户端时区 - 您用来直接查询数据库的时区使用 +05:30 而 django 时区 - UTC

关于python - 时间戳值改变 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48183177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com