showing the initial timestamp format
目前我正在研究元数据库。我的时间戳类似于“2017 年 6 月 23 日星期五上午 8:43”。我需要将此时间戳从 utc 转换为 ist 形式。
代码是这样的
<小时/>SELECT CAST("public"."locations"."timestamp" AS date) AS "timestamp"
FROM "public"."locations"
GROUP BY CAST("public"."locations"."timestamp" AS date)
ORDER BY CAST("public"."locations"."timestamp" AS date) ASC
最佳答案
在 PostgreSQL1 中,假设一个简化的场景,专注于将时间戳转换为 IST
就是这个:
CREATE TABLE locations
(
id serial PRIMARY KEY,
your_timestamp timestamp with time zone
) ;
注意:不要使用timestamp
作为列名称,以避免混淆。 timestamp
已经是 type
.
INSERT INTO locations
(your_timestamp)
VALUES
('2017-01-01 10:00:00 UTC'),
('2017-01-01 10:00:00 Europe/Rome'),
('2017-01-01 10:00:00 America/New_York'),
('2017-01-01 10:00:00 IST') ;
您将转换所有这些时间(不仅是已经输入的 UTC 时间,或者连接到数据库的客户端的本地时区),您将使用:
SELECT
id,
your_timestamp,
your_timestamp AT TIME ZONE 'UTC' AS timestamp_utc,
your_timestamp AT TIME ZONE 'IST' AS timestamp_ist
FROM
locations ;
id | your_timestamp | timestamp_utc | timestamp_ist -: | :--------------------- | :------------------ | :------------------ 1 | 2017-01-01 10:00:00+00 | 2017-01-01 10:00:00 | 2017-01-01 12:00:00 2 | 2017-01-01 09:00:00+00 | 2017-01-01 09:00:00 | 2017-01-01 11:00:00 3 | 2017-01-01 15:00:00+00 | 2017-01-01 15:00:00 | 2017-01-01 17:00:00 4 | 2017-01-01 08:00:00+00 | 2017-01-01 08:00:00 | 2017-01-01 10:00:00
您可以在 dbfiddle here 处查看。
<小时/>引用文献:
- 关于
AT TIME ZONE
的 PostgreSQL 文档
1 我不知道 MySQL 的等效项。我希望您实际上正在使用 PostgreSQL,因为您的标识符是 "double quoted"
而不是`backtick quoted`
就像在 MySQL 中一样。
关于mysql - 使用 sql 查询将元数据库中预配置的时间戳从 utc 格式转换为 ist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45379876/