简体   繁体   English

将日期从 UTC 时区转换为 PST 时区(包括 DST 因素)

[英]Convert a date from UTC timezone to PST Time zone (including DST factor)

I want to convert a date in UTC timezone to PST timezone.我想将 UTC 时区的日期转换为 PST 时区。 The NEW_TIME doesn't take into account the DST (Daylight Saving Time) factor, so I tried using CAST and TO_TIMESTAMP_TZ functions but both gave incorrect time ( difference of around 5 hrs 30 min). NEW_TIME 没有考虑 DST(夏令时)因素,所以我尝试使用 CAST 和 TO_TIMESTAMP_TZ 函数,但两者都给出了不正确的时间(大约 5 小时 30 分钟的差异)。 Not sure why.不知道为什么。

SELECT TO_CHAR(TO_TIMESTAMP_TZ(max(end_date) AT TIME ZONE 'PST')
      ,'DD-MON-YYYY HH24:MI:SS')
FROM table1
WHERE NAME= 'FIRST';
SELECT TO_CHAR(CAST((max(end_date) AT TIME ZONE 'PST') AS DATE )
      ,'DD-MON-YYYY HH24:MI:SS')
FROM table1
WHERE NAME= 'FIRST';

Instead of 'PST' I also tried using 'US/PACIFIC', but it too gave the same result.除了“PST”,我还尝试使用“US/PACIFIC”,但它也给出了相同的结果。

max(end_date) is: 2021-03-15 07:17:16 (in UTC) max(end_date) 为:2021-03-15 07:17:16(UTC 时间)

The query is returning (incorrect time returned): 14-MAR-2021 18:47:16查询正在返回(返回的时间不正确):14-MAR-2021 18:47:16

The time that it should (correct time expected): 15-MAR-2021 00:17:16它应该的时间(预期的正确时间):15-MAR-2021 00:17:16

Can anyone please help in correcting my query or any other function that can convert the date from UTC to PST time zone (keeping the DST factor in mind).任何人都可以帮助纠正我的查询或任何其他可以将日期从 UTC 转换为 PST 时区的 function(记住 DST 因素)。

You have 6 hours time difference, so I'm going to assume that you are in the same time zone as Asia/Dacca and have set up my session using:您有 6 小时的时差,所以我假设您与Asia/Dacca位于同一时区,并使用以下命令设置了我的 session:

ALTER SESSION SET TIME_ZONE='Asia/Dacca';

Now, if I create table1 with the data type TIMESTAMP WITH TIME ZONE :现在,如果我使用数据类型TIMESTAMP WITH TIME ZONE创建table1

CREATE TABLE table1 (
  name     VARCHAR2(20),
  end_date TIMESTAMP WITH TIME ZONE
);

INSERT INTO table1 ( name, end_date ) VALUES ( 'FIRST', TIMESTAMP '2021-03-15 07:17:16 UTC' );

Then your query (you do not need to use TO_TIMESTAMP_TZ on a column that is already a TIMESTAMP WITH TIME ZONE column):然后您的查询(您不需要在已经是TIMESTAMP WITH TIME ZONE列的列上使用TO_TIMESTAMP_TZ ):

SELECT TO_CHAR(
         max(end_date) AT TIME ZONE 'PST',
         'DD-MON-YYYY HH24:MI:SS'
       ) AS pst_end_date
FROM   table1
WHERE  NAME = 'FIRST';

Outputs:输出:

 | | PST_END_DATE | PST_END_DATE | |:------------------- | |:------------------- | | | 15-MAR-2021 00:17:16 | 2021 年 3 月 15 日 00:17:16 |

and works!和作品!


However, if you store end_date using a TIMESTAMP (without time zone):但是,如果您使用TIMESTAMP (无时区)存储end_date

CREATE TABLE table1 (
  name     VARCHAR2(20),
  end_date TIMESTAMP
);

INSERT INTO table1 ( name, end_date ) VALUES ( 'FIRST', TIMESTAMP '2021-03-15 07:17:16' );

Then:然后:

SELECT TO_CHAR(
         max(end_date) AT TIME ZONE 'PST',
         'DD-MON-YYYY HH24:MI:SS'
       ) AS pst_end_date
FROM   table1
WHERE  NAME = 'FIRST';

Outputs:输出:

 | | PST_END_DATE | PST_END_DATE | |:------------------- | |:------------------- | | | 14-MAR-2021 18:17:16 | 2021 年 3 月 14 日 18:17:16 |

Which replicates your issue.这复制了您的问题。

This is because the database does not know the time zone of the data and will implicitly assume that it is the same as the database/session time zone and we've set that to Asia/Dacca and not UTC .这是因为数据库不知道数据的时区,并且会隐含地假设它与数据库/会话时区相同,我们已将其设置为Asia/Dacca而不是UTC Instead we need to explicitly tell the database to use the UTC time zone for the conversion:相反,我们需要明确告诉数据库使用UTC时区进行转换:

SELECT TO_CHAR(
         FROM_TZ(max(end_date), 'UTC') AT TIME ZONE 'PST',
         'DD-MON-YYYY HH24:MI:SS'
       ) AS pst_end_date
FROM   table1
WHERE  NAME = 'FIRST';

Which outputs:哪个输出:

 | | PST_END_DATE | PST_END_DATE | |:------------------- | |:------------------- | | | 15-MAR-2021 00:17:16 | 2021 年 3 月 15 日 00:17:16 |

If your column has the DATE data type:如果您的列具有DATE数据类型:

CREATE TABLE table1 (
  name     VARCHAR2(20),
  end_date DATE
);

INSERT INTO table1 ( name, end_date ) VALUES ( 'FIRST', TIMESTAMP '2021-03-15 07:17:16' );

Then you can use the same query with an added CAST :然后,您可以使用添加了CAST的相同查询:

SELECT TO_CHAR(
         FROM_TZ(CAST(max(end_date) AS TIMESTAMP), 'UTC') AT TIME ZONE 'PST',
         'DD-MON-YYYY HH24:MI:SS'
       ) AS pst_end_date
FROM   table1
WHERE  NAME = 'FIRST';

db<>fiddle here db<> 在这里摆弄

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

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