数据库中是否有任何可能性,无论何时我都应该存储为 GMT 时间戳。如果是这样,请分享给我。
最佳答案
对于 PostgreSQL,您的表应该有一个类型为 timestamp with time zone
的列(timestamptz
简称)像这样:
CREATE TABLE tbl (id serial, ts timestamptz);
如果您要存储的内容(我们称之为 my_ts
)是...
一个local
timestamp without time zone
,即它与您客户端中timezone
的当前设置一致:
按原样插入,所有内容都在内部自动保存为 UTC:INSERT INTO tbl (ts) VALUES my_ts'
带时区的时间戳
:
按原样插入,所有内容都在内部自动保存为 UTC:INSERT INTO tbl (ts) VALUES my_ts;
没有时区的时间戳
,但来自另一个时区:
比如说,您从 伦敦 的奥运会中获得了timestamp without time zone
值,但您的客户认为您处于不同的时区。使用AT TIME ZONE
转换时间戳构造:INSERT INTO tbl (ts) VALUES my_ts AT TIME ZONE 'Europe/London';
如果您使用时区名称(而不是时区缩写,在本例中为 BST
),则会自动应用 DST(夏令时)规则。详情在此related question .
更多关于 timestamp handling in PostgreSQL in this related answer .
现在,如果你想将 tbl.ts
显示为 UTC (~ GMT) timestamp without time zone
,不管你当前的时区如何,像这样检索值:
SELECT ts AT TIME ZONE 'UTC' FROM tbl;
请注意,AT TIME ZONE
应用于 timestamptz
与应用于 timestamp
时有不同的效果! Read the manual carefully .
关于mysql - 数据库中的 GMT 时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12071783/