简体   繁体   English

从 timestamptz 中提取日期部分

[英]Extract date part from timestamptz

I am trying to extract the hour from a timestamp with a timezone.我正在尝试从带有时区的时间戳中提取小时。 However, my times are coming up incorrectly.但是,我的时间不正确。

Here's an example, I am using Dbeaver with my timezone set to EST:这是一个示例,我正在使用 Dbeaver 并将时区设置为 EST:

SELECT '2020-01-24 14:27:12' AT TIME ZONE 'US/Pacific' as foo,
EXTRACT(HOUR FROM foo) as ex,
DATE_PART('HOUR', foo::timestamp) as dp

RETURNS:退货:

foo                 |ex |dp
2020-01-24 17:27:12 |22 | 22
  • Why is my time coming up 3 hours ahead, it should be 3 hours behind?为什么我的时间提前了3小时,它应该晚了3小时?
  • Extract and DATE_PART don't seem to get me the hour I would like. ExtractDATE_PART似乎没有让我得到我想要的时间。 It looks like it's taking 17 as EST and then converting it to UTC.看起来它以 17 作为 EST,然后将其转换为 UTC。 Here's what I am expecting to get:这是我期望得到的:
foo                 |ex |dp
2020-01-24 11:27:12 |11 | 11

Check if your timezone is set to EST:检查您的时区是否设置为 EST:

SELECT current_setting('TIMEZONE');

or with:或与:

show timezone;

If it is not you can set it like this:如果不是,你可以这样设置:

set timezone to est;

AS shown in this DEMO如本演示中所示

If that is not working try with convert_timezone如果这不起作用,请尝试使用convert_timezone

select convert_timezone('US/Pacific', '2020-01-24 14:27:12')

And exploring the mater on hand I have found this fact:探索手头的材料,我发现了这个事实:

Note Amazon Redshift doesn't validate POSIX-style time zone specifications, so it is possible to set the time zone to an invalid value. Note Amazon Redshift 不会验证 POSIX 样式的时区规范,因此可以将时区设置为无效值。 For example, the following command doesn't return an error, even though it sets the time zone to an invalid value.例如,以下命令不会返回错误,即使它将时区设置为无效值。

set timezone to 'xxx36';将时区设置为“xxx36”;

from this source: https://docs.aws.amazon.com/redshift/latest/dg/CONVERT_TIMEZONE.html来自这个来源: https : //docs.aws.amazon.com/redshift/latest/dg/CONVERT_TIMEZONE.html

'AT TIME ZONE' does not work like you are expecting. 'AT TIME ZONE' 不能像您预期的那样工作。 Use convert_timezone() instead.请改用 convert_timezone() 。

SELECT
  '2020-01-24 14:27:12' AT TIME ZONE 'US/Pacific' as foo,  
    -- expected '2020-01-24 6:27:12' got '2020-01-24 22:27:12+00'
  convert_timezone('UTC', 'US/Pacific', CAST('2020-01-24 14:27:12' AS TIMESTAMP WITHOUT TIME ZONE)) as bar 
    -- expected '2020-01-24 6:27:12' got '2020-01-24 06:27:12'
  ;

'AT TIME ZONE' interprets the timestamp as being relative to the specified time zone and converts it to a TIMESTAMPTZ offset to UTC. 'AT TIME ZONE' 将时间戳解释为相对于指定时区并将其转换为相对于 UTC 的 TIMESTAMPTZ 偏移量。 That is in the above example it converts from US/Pacific to UTC, not the other way around.在上面的示例中,它从 US/Pacific 转换为 UTC,而不是相反。

It works perfectly fine for me (using dbVisualizer).它对我来说非常好(使用 dbVisualizer)。

The issue is with your SQL Client.问题在于您的 SQL 客户端。

SQL clients often impose formatting that impacts the values you see. SQL 客户端通常会施加影响您看到的值的格式。 You can test this by converting values to Text before sending them to your SQL client:您可以通过在将值发送到 SQL 客户端之前将值转换为文本来测试这一点:

SELECT
  '2020-01-24 14:27:12' AT TIME ZONE 'US/Pacific' as foo,
  foo::text as t,
  EXTRACT(HOUR FROM foo) as ex,
  DATE_PART('HOUR', foo::timestamp) as dp

For me, this results in:对我来说,这导致:

2020-01-24 22:27:12+00  2020-01-24 22:27:12+00  22.0    22.0

Try it in your SQL client and see what happens.在您的 SQL 客户端中尝试一下,看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM