如何在 postgresql 中使用 GMT 时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1380460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:50:51  来源:igfitidea点击:

How do I use GMT times in postgresql?

postgresqltimezone

提问by Adam

We are running about in our java code with GregorianCalendar. We would like to persist these in our postgresql database as GMT +0 time in timestamp type columns, but we're not sure how to go about it. Should the fields be with time stamp or without? By a similar token which "current" time postgres function should we be using current_timestamp or localtimestamp?

我们正在使用GregorianCalendar运行我们的 Java 代码。我们希望将这些作为时间戳类型列中的 GMT +0 时间保留在我们的 postgresql 数据库中,但我们不确定如何去做。字段应该带时间戳还是不带?通过类似的标记,我们应该使用current_timestamp 还是 localtimestamp哪个“当前”时间 postgres 函数?

回答by Milen A. Radev

Start here - section 8.5.3. "Time Zones" from the documentation.

从这里开始 - 第 8.5.3 节。文档中的“时区”。

The difference between CURRENT_TIMESTAMP and LOCALTIMESTAMP is the typeof the result - the former returns "timestamp with time zone" and the latter - "timestamp":

CURRENT_TIMESTAMP 和 LOCALTIMESTAMP 的区别在于结果的类型——前者返回“timestamp with time zone”,后者返回“timestamp”:

milen=> select CURRENT_TIMESTAMP;
              now
-------------------------------
 2009-09-05 01:21:37.595704+03
(1 row)

milen=> select LOCALTIMESTAMP;
         timestamp
----------------------------
 2009-09-05 01:21:41.956355
(1 row)

It's another matter entirely at which time zone you want to see the current time. In this case the default time zone (that could set in several ways, all described in the section linked above) is important. Also you could use "AT TIME ZONE"in a query to get the current time at specific time zone without "fiddling" with the "timezone" setting:

您想在哪个时区查看当前时间完全是另一回事。在这种情况下,默认时区(可以通过多种方式设置,所有这些都在上面链接的部分中进行了描述)很重要。您也可以在查询中使用“AT TIME ZONE”来获取特定时区的当前时间,而无需“摆弄”“时区”设置:

milen=> select CURRENT_TIMESTAMP AT TIME ZONE 'UTC';
          timezone
----------------------------
 2009-09-04 22:21:44.418236
(1 row)