【问题标题】:PostgreSQL output timestamp with time zone带有时区的 PostgreSQL 输出时间戳
【发布时间】:2014-01-24 08:17:12
【问题描述】:

尝试在 PostgreSQL 中输出 timestamp with time zone
我已经尝试了几个参数,前两个很好。

(1):

select time at time zone 'UTC' from sf where id='365498691097858048';

它输出:

2013-08-08 15:44:13

(2):

select time at time zone 'PDT' from sf where id='365498691097858048';

输出

2013-08-08 08:44:13

但是第三个让我很困惑:

(3):

select time at time zone <b>'UTC-07'</b> from sf where id='365498691097858048';
2013-08-08 <b>22:44:13</b>

'UTC-07'和'PDT'一样,为什么输出完全不同?

【问题讨论】:

  • time 应该是一个列的名称 .. 什么? timestamp 还是 timestamp with time zone?我找不到你的表定义。无论哪种方式,都不要使用保留字 time 作为列名 - 对于不同数据类型的列更不用说。
  • 也许 PostgreSQL 看到 UTC-07,解析到 UTC 并进入“我知道那个时区”,忽略 -07 后缀。如果是这样,我无法在我的 9.3 安装中重现它 - 你运行的是什么 PostgreSQL 版本? (始终在问题中包含您的 PostgreSQL 版本)。
  • @ErwinBrandstetter ,您好,这是带时区的时间戳。

标签: postgresql timezone timestamp


【解决方案1】:

发生这种情况是因为当您使用不带名称的时区时 PostgreSQL 符合 POSIX 标准。

来自IANA Timezone database

# Be consistent with POSIX TZ settings in the Zone names,
# even though this is the opposite of what many people expect.
# POSIX has positive signs west of Greenwich, but many people expect
# positive signs east of Greenwich.  For example, TZ='Etc/GMT+4' uses
# the abbreviation "-04" and corresponds to 4 hours behind UT
# (i.e. west of Greenwich) even though many people would expect it to
# mean 4 hours ahead of UT (i.e. east of Greenwich).

来自 POSIX 规范 as documented for GNU libc(强调我的):

偏移量指定您必须添加到本地时间以获得协调世界时值的时间值。它的语法类似于 [+|-]hh[:毫米[:ss]]。如果当地时区在本初子午线以西,则为正,如果在东,则为负。小时必须介于 0 和 24 之间,分钟和秒必须介于 0 和 59 之间。

【讨论】:

    猜你喜欢
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    相关资源
    最近更新 更多