SQL Postgre 中的时间戳SQL

SQL Timestamp in PostgreSQL

我试图了解 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 接口和 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.

EG:

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 会。不是 records 我相信而是 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 格式化日期)