本文介绍了PostgreSQL时区转换'EST'!='-05'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在x86_64-pc-linux-gnu上运行PostgreSQL 9.6.6,并且我的时区设置为'UTC'。

I am running PostgreSQL 9.6.6 on x86_64-pc-linux-gnu and my time zone is set to 'UTC'.

有人知道为什么结果吗?以下 SELECT 语句是否不同?

Does anyone know why the results of the following SELECT statements are different?

A)

SELECT timezone('EST', '2017-12-21');
    timezone       
---------------------
2017-12-20 19:00:00

B)

SELECT timezone('-05', '2017-12-21');
      timezone       
---------------------
2017-12-21 05:00:00

根据 pg_timezone_names -05 应该与 EST 具有相同的偏移量...有什么想法吗?谢谢。

According to the pg_timezone_names table -05 should have the same offset as EST... Any thoughts? Thanks.

推荐答案

并进一步:

当您将时区设置为 EST -您声明您的客户处于EST时间区域,因此返回时间将根据您的tz进行调整:

when you set timezone to 'EST' - you declare that your client is in EST time zone, thus returned time will be adjusted for your tz:

t=# select '2017-12-21'::timestamptz;
      timestamptz
------------------------
 2017-12-21 00:00:00-05
(1 row)

pg_timezone_names中的间隔匹配utc_offset 和等号 -05 ,因此可以正常使用。如果您将时区设置为‘-05’(实际上,在EST中比世界标准时间要少5个小时)。

the interval match utc_offset from pg_timezone_names and isequal -05, so it works as expected. (indeed in EST will be 5 hours less then UTC) same result if you set timezone to '-05'.

-05 EST 的结果相同设置时区,如文档中所述。

Both -05 and EST give same result for SET TIMEZONE as described in docs.

现在,您在使用 interval时回答与文档一致

也遵循以下规则:

t=# select '2017-12-21'::timestamptz at time zone 'EST';
      timezone
---------------------
 2017-12-20 19:00:00
(1 row)

t=# select '2017-12-21'::timestamptz at time zone interval '-05:00';
      timezone
---------------------
 2017-12-20 19:00:00
(1 row)

但是文档还说:

在文本情况下,可以用第8.5.3节中描述的
中的任何一种方式指定时区名称。


  • 在pg_timezone_names中列出了公认的时区名称

  • 公认的缩写在pg_timezone_abbrevs
  • 中列出
  • POSIX样式的时区规范,格式为STDoffset或STDoffsetDST

  • recognized time zone names are listed in the pg_timezone_names
  • recognized abbreviations are listed in the pg_timezone_abbrevs
  • POSIX-style time zone specifications of the form STDoffset or STDoffsetDST

(格式化我的)

最后:



TL; DR



简而言之-当您将 $ -05定义为<$ c的文本(非间隔)输入时$ c> timezone()函数或 AT TIME ZONE 指令(实际上是相同的)Postgres认为这是尝试使用POSIX样式的时区然后反转符号,因此您得到相反的结果...

TL;DR

So in short - when you define '-05' as text (not interval) input for timezone() function or AT TIME ZONE directive (effectively same) Postgres thinks this is an attempt to use POSIX style time zone and thus inverts sign, thus you get "opposite" result...

此记录的反转的简单演示:

a simple demonstration of this documented inversion:

t=# select '2017-12-21'::timestamptz at time zone '05';
      timezone
---------------------
 2017-12-20 19:00:00
(1 row)

这篇关于PostgreSQL时区转换'EST'!='-05'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:12