gpt4 book ai didi

python - PostgreSQL 中的 SQL 时间戳

转载 作者:行者123 更新时间:2023-11-29 13:17:08 30 4
gpt4 key购买 nike

我试图了解 PostgreSQL 保存 timestamp 数据类型的原始方式。根据我使用的客户端,我得到 2 个不同的结果:

<强>1。 psql

# SELECT date_incorporated FROM client;

date_incorporated
------------------------
2017-06-14 19:42:15-04

<强>2。 records python模块

rows = db.query('SELECT date_incorporated FROM client')
print(rows[0])
# {"date_incorporated": "2017-06-14T19:42:15-04:00"}

由于 psql 接口(interface)和 records模块都应该给我返回原始数据,我不明白为什么两个都给我返回他们存储的不同格式的时间戳。

到目前为止,我看到的两个区别是 records 中日期和时间中间的 T版本,以及它在字符串末尾显示时区的不同方式

其中之一正在改变它吗?哪个显示真实数据?

最佳答案

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

All timezone-aware dates and times are stored internally in UTC. They are converted to local time in the zone specified by the TimeZone configuration parameter before being displayed to the client.

https://www.postgresql.org/docs/current/static/datatype-datetime.html#DATATYPE-DATETIME-OUTPUT

The output format of the date/time types can be set to one of the four styles ISO 8601, SQL (Ingres), traditional POSTGRES (Unix date format), or German. The default is the ISO format.

如:

t=# select now();
now
-------------------------------
2017-11-29 09:07:31.716276+00
(1 row)

t=# set datestyle to SQL;
SET
t=# select now();
now
--------------------------------
11/29/2017 09:07:52.341416 UTC
(1 row)

所以时间是保存的,而不是返回的方式。至少不是真的。您可以在某种程度上控制它如何返回给您的客户。 psql 不处理时间。但是 python 可以。我相信不是记录而是python本身

https://en.wikipedia.org/wiki/ISO_8601

T is the time designator that precedes the time components of the representation.

T 绝对不是由 postgres 本身添加的(除非你故意用 to_char 格式化日期)

关于python - PostgreSQL 中的 SQL 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47546388/

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