查询时区 - 数据库服务器客户机具有不同的时区 [英] Query timezone - db server & client machine have different time zones

查看:226
本文介绍了查询时区 - 数据库服务器客户机具有不同的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(以下是 PostgreSQL中的时区存储时间戳



我使用PostgreSQL 9.x,数据库服务器的默认时区为 +9 例如,它有一个表格,其中包含一列 timezone 类型。



一个Java程序在与数据库相同的服务器上,它从数据库查询数据。数据库所在的服务器和Java服务器程序的客户端(浏览器)都位于时区 -8



我的问题是:




  • 客户端(浏览器)将显示原始的日期时间或调整的日期时间为9 - (-8) = 17小时前?

  • 如果我想在浏览器客户端中获得与原始数据相同的数据,是否应该更改Java服务器机器或客户端机器的时区?

    (我知道使db和它的服务器具有相同的时区是更好的,但是我们假设现在由于某种原因而不是相同的。)


解决方案

总是获取相同的数据在浏览器客户端中作为db原始值。只有许多不同的方式来显示相同​​的时间点(在不同的时区)。两个示例( timestamptz 值的文本表示):

 '2012 -03-05 20:00:00 + 03'
'2012-03-05 18:00:00 + 01'

相同



就Postgres而言,服务器的时区设置完全与此无关。唯一相关的事情是设置会话。唯一改变的是显示的值。它总是在同一时间点。



所以只需在会话中设置时区以获得相应的文本表示:





另一个选项是 AT TIME ZONE 构造。要获得时间戳作为本地 没有时区的时间戳

  SELECT my_timestamptz_column AT TIME ZONE'-8'; 

处理包含时区类型列的表时,这可能是更好的方法,因为每个行的时区可以更改。



同样,所有这一切都在参考答案中: p>


(This is a follow-up to: Time zone storage in PostgreSQL timestamps)

I am using PostgreSQL 9.x, the db server has default timezone as +9 for example, it has a table that contains a column of timezone type.

There is a Java program on the same server as the db, it query data from the the database. Both the server that the db resides, and the client (a browser) of the Java server program is in timezone -8.

My questions are:

  • Will the client (brower) show the original datetime or adjusted datetime with 9 - (-8) = 17 hours earlier?
  • If I want to get the same data in browser client as the db original value, should I change the timezone of Java server machine or the Client machine, or both?
    (I know it's better to make the db & its server have the same timezone, but let's assume they are not setting the same for now for some reason.)

解决方案

You always get the same data in browser client as the db original value. There are just many different ways to display the same point in time (at different time zones). Two examples (text representations of a timestamptz value):

'2012-03-05 20:00:00+03'
'2012-03-05 18:00:00+01'

Same value.

As far as Postgres is concerned, the time zone setting of the server is completely irrelevant for this. The only relevant thing is the setting of the session. And the only thing that changes is the display of the value. It's always the same point in time.

So just set the time zone in your session to get the according text representation:

Another option would be the AT TIME ZONE construct. To get a timestamp as local timestamp without time zone:

SELECT my_timestamptz_column AT TIME ZONE '-8';

When dealing with a table that contains a column of timezone type, this might be the better approach, since the time zone can change for every row.

Again, all of this is in the reference answer:

这篇关于查询时区 - 数据库服务器客户机具有不同的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆