将GMT转换为新西兰时间

时间:2017-10-25 04:04:05

标签: oracle oracle11g

如何在Oracle 11G中将GMT日期转换为Newzealand日期时间格式。

TO_CHAR(TO_DATE('date','DD-MM-YY HH: MI: SS')+13/24,'DD-MON-YYYY HH24: MI: SS')

但是,这不会像夏令时那样正常,但是在正常情况下我需要将其设为+12。

是否有相同的Oracle功能?

2 个答案:

答案 0 :(得分:0)

这取决于日期列的数据类型。

  • 案例TIMESTAMP WITH LOCAL TIME ZONE

    在运行查询之前,使用ALTER SESSION SET TIME_ZONE = 'NZ';更改会话时区。

  • 案例TIMESTAMP WITH TIME ZONE

    运行SELECT date_col AT TIME ZONE 'NZ' FROM ...

  • 案例TIMESTAMP

    运行SELECT FROM_TZ(date_col, 'UTC') AT TIME ZONE 'NZ' FROM ...

  • 案例DATE

    运行SELECT FROM_TZ(CAST(date_col AS TIMESTAMP), 'UTC') AT TIME ZONE 'NZ' FROM ...

  • 案例VARCHAR2

    这将是一个非常糟糕的设计。将数据类型更改为上述类型之一(最好是TIMESTAMP WITH TIME ZONE)。解决方法是使用SELECT TO_TIMESTAMP_TZ(date_col||' UTC', 'DD-MM-YY HH24: MI: SS TZR') AT TIME ZONE 'NZ' FROM ...

注意,在您的问题中,您使用日期格式DD-MM-YY HH: MI: SS,这是正确的吗? HH表示12小时格式的小时,如果缺少AM/PM指示符,则这是不明确的。年份也应始终由四位数字提供。

答案 1 :(得分:0)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE dates ( dt ) As
-- London on British Summer Time, NZ on winter time
SELECT DATE '2017-09-23' + INTERVAL '3' HOUR FROM DUAL UNION ALL
-- London on British Summer Time, NZ on Daylight Savings Time
SELECT DATE '2017-09-24' + INTERVAL '3' HOUR FROM DUAL UNION ALL
-- London on British Summer Time, NZ on Daylight Savings Time
SELECT DATE '2017-10-25' + INTERVAL '3' HOUR FROM DUAL UNION ALL
-- London on Daylight Savings Time, NZ on Daylight Savings Time
SELECT DATE '2017-10-29' + INTERVAL '3' HOUR FROM DUAL;

查询1

如果您的时间存储在伦敦当地时间(尊重BST / DST):

SELECT dt AS London_time,
       CAST(
         FROM_TZ(
           CAST( dt AS TIMESTAMP ),
           'Europe/London'
         ) AT TIME ZONE 'Pacific/Auckland'
         AS DATE
       )
       AS Auckland_Time
FROM   dates

<强> Results

|          LONDON_TIME |        AUCKLAND_TIME |
|----------------------|----------------------|
| 2017-09-23T03:00:00Z | 2017-09-23T14:00:00Z |
| 2017-09-24T03:00:00Z | 2017-09-24T15:00:00Z |
| 2017-10-25T03:00:00Z | 2017-10-25T15:00:00Z |
| 2017-10-29T03:00:00Z | 2017-10-29T16:00:00Z |

查询2

如果您的时间存储为UTC:

SELECT dt AS London_time,
       CAST(
         FROM_TZ(
           CAST( dt AS TIMESTAMP ),
           'UTC'
         ) AT TIME ZONE 'Pacific/Auckland'
         AS DATE
       )
       AS Auckland_Time
FROM   dates

<强> Results

|          LONDON_TIME |        AUCKLAND_TIME |
|----------------------|----------------------|
| 2017-09-23T03:00:00Z | 2017-09-23T15:00:00Z |
| 2017-09-24T03:00:00Z | 2017-09-24T16:00:00Z |
| 2017-10-25T03:00:00Z | 2017-10-25T16:00:00Z |
| 2017-10-29T03:00:00Z | 2017-10-29T16:00:00Z |

(注意:如果您希望将值作为CAST( .. AS DATE )数据类型,则可以删除外部TIMESTAMP WITH TIME ZONE。)