显示初始时间戳格式
目前我正在研究元数据库。我的时间戳是这样的"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
引用:
- 关于
AT TIME ZONE
的PostgreSQL文档
1我不知道 MySQL 等效项。我希望你真的在使用PostgreSQL,因为你的标识符是"double quoted"
的,而不是像MySQL那样`backtick quoted`
。