PostgreSQL时区转换'EST'!='-05' [英] postgresql timezone conversion 'EST' != '-05'

查看:132
本文介绍了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.

推荐答案

https://www.postgresql.org/docs/current/static/view-pg-timezone-names.html


视图pg_timezone_names提供了由SET TIMEZONE识别的
的时区名称列表

The view pg_timezone_names provides a list of time zone names that are recognized by SET TIMEZONE

并进一步:


utc_offset interval与UTC的偏移量(正向是格林威治以东)

utc_offset interval Offset from UTC (positive means east of Greenwich)

当您将时区设置为 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时回答与文档一致 https: //www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT


在这些表达式中,可以将所需的时区指定为
作为文本字符串(例如'PST')或间隔(例如
INTERVAL'-08:00')。 / p>

In these expressions, the desired time zone zone can be specified either as a text string (e.g., 'PST') or as an interval (e.g., INTERVAL '-08:00').

也遵循以下规则:

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节中描述的
中的任何一种方式指定时区名称。

In the text case, a time zone name can be specified in any of the ways described in Section 8.5.3.

https://www.postgresql.org/docs/current/static/datatype-datetime.html#DATATYPE-TIMEZONES


PostgreSQL允许您以三种不同的形式指定时区:

PostgreSQL allows you to specify time zones in three different forms:


  • 在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

(格式化我的)

最后:


应该警惕POSIX样式的时区功能可能导致到
静默接受虚假输入...要记住的另一个问题是,格林威治标准时间 west
使用POSIX时区名称中的
,正偏移量。 PostgreSQL在其他任何地方都遵循ISO-8601
约定,正时区偏移量是格林威治的



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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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