选择缩写时区

时间:2019-07-02 18:22:49

标签: oracle timezone timestamp-with-timezone

我正在尝试编写查询以返回给定区域的当前缩写时区。例如...

“美国/纽约” 'America / Los_Angeles'

我希望将它们转换为...

'EDT' “ PDT”

我可以使用本地时区实现

select TO_CHAR(CAST(SYSDATE AS TIMESTAMP WITH LOCAL TIME ZONE),'TZD') from dual;

哪个返回“ PDT”。但是我需要它来返回任何区域的时区。希望有类似...

select TO_CHAR(CAST(SYSDATE AS TIMESTAMP AT TIME ZONE 'America/Los_Angeles'),'TZD') from dual;

但是Oracle不喜欢这种语法。知道如何获得它吗?

2 个答案:

答案 0 :(得分:3)

AT TIME ZONE是一个时间戳修饰符,不属于CAST表达式。

因此:

select to_char(cast(sysdate as timestamp) at time zone 'America/Los_Angeles', 'TZD')
from dual;

或更简单:

select to_char(systimestamp at time zone 'America/Los_Angeles', 'TZD') from dual;

这是获取所有当前时区的查询:

select distinct tzname, to_char(systimestamp at time zone tzname, 'TZD') as zone
from v$timezone_names
order by tzname;

答案 1 :(得分:0)

您认为什么是“缩写时区”?时区缩写没有通用的标准,并且其中许多没有区别,请参见Time zone Abbreviations或以下示例:

SELECT tzname, tzabbrev
FROM v$timezone_names
WHERE tzname = 'America/Los_Angeles';

+----------------------------+
|TZNAME             |TZABBREV|
+----------------------------+
|America/Los_Angeles|LMT     |
|America/Los_Angeles|PST     |
|America/Los_Angeles|PDT     |
|America/Los_Angeles|PWT     |
|America/Los_Angeles|PPT     |
+----------------------------+

反之亦然:

SELECT tzname, tzabbrev, TZ_OFFSET(tzname)
FROM v$timezone_names
WHERE tzabbrev = 'EST'
order by 3;

+----------------------------------------------+
|TZNAME             |TZABBREV|TZ_OFFSET(TZNAME)|
+----------------------------------------------+
|Australia/Yancowinn|EST     |+09:30           |
|Australia/Broken_Hi|EST     |+09:30           |
|Australia/ACT      |EST     |+10:00           |
|Australia/Brisbane |EST     |+10:00           |
|Australia/Canberra |EST     |+10:00           |
|Australia/Queenslan|EST     |+10:00           |
|Australia/Hobart   |EST     |+10:00           |
|Australia/Lindeman |EST     |+10:00           |
|Australia/Victoria |EST     |+10:00           |
|Australia/Melbourne|EST     |+10:00           |
|Australia/NSW      |EST     |+10:00           |
|Australia/Tasmania |EST     |+10:00           |
|Australia/Sydney   |EST     |+10:00           |
|Australia/Currie   |EST     |+10:00           |
|Australia/Lord_Howe|EST     |+10:30           |
|Australia/LHI      |EST     |+10:30           |
|Antarctica/Macquari|EST     |+11:00           |
|America/Moncton    |EST     |-03:00           |
|America/Antigua    |EST     |-04:00           |
|America/Detroit    |EST     |-04:00           |
|America/Fort_Wayne |EST     |-04:00           |
|America/Grand_Turk |EST     |-04:00           |
|America/Indiana/Ind|EST     |-04:00           |
|America/Indiana/Mar|EST     |-04:00           |
|America/Indiana/Pet|EST     |-04:00           |
|America/Indiana/Vev|EST     |-04:00           |
|America/Indiana/Vin|EST     |-04:00           |
|America/Indiana/Win|EST     |-04:00           |
|America/Indianapoli|EST     |-04:00           |
|America/Iqaluit    |EST     |-04:00           |
|America/Kentucky/Lo|EST     |-04:00           |
|America/Kentucky/Mo|EST     |-04:00           |
|America/Louisville |EST     |-04:00           |
|America/Montreal   |EST     |-04:00           |
|America/Nassau     |EST     |-04:00           |
|America/New_York   |EST     |-04:00           |
|America/Nipigon    |EST     |-04:00           |
|America/Pangnirtung|EST     |-04:00           |
|America/Santo_Domin|EST     |-04:00           |
|America/Thunder_Bay|EST     |-04:00           |
|America/Toronto    |EST     |-04:00           |
|Canada/Eastern     |EST     |-04:00           |
|EST5EDT            |EST     |-04:00           |
|US/East-Indiana    |EST     |-04:00           |
|US/Eastern         |EST     |-04:00           |
|US/Michigan        |EST     |-04:00           |
|US/Central         |EST     |-05:00           |
|Jamaica            |EST     |-05:00           |
|America/Cancun     |EST     |-05:00           |
|America/Cayman     |EST     |-05:00           |
|America/Chicago    |EST     |-05:00           |
|America/Coral_Harbo|EST     |-05:00           |
|America/Indiana/Kno|EST     |-05:00           |
|America/Indiana/Tel|EST     |-05:00           |
|America/Jamaica    |EST     |-05:00           |
|America/Knox_IN    |EST     |-05:00           |
|America/Atikokan   |EST     |-05:00           |
|America/Menominee  |EST     |-05:00           |
|America/Merida     |EST     |-05:00           |
|America/Panama     |EST     |-05:00           |
|America/Port-au-Pri|EST     |-05:00           |
|America/Rankin_Inle|EST     |-05:00           |
|America/Resolute   |EST     |-05:00           |
|CST                |EST     |-05:00           |
|EST                |EST     |-05:00           |
|US/Indiana-Starke  |EST     |-05:00           |
|America/Managua    |EST     |-06:00           |
|America/Cambridge_B|EST     |-06:00           |
+----------------------------------------------+

注意,TZD的结果不仅取决于地区,而且取决于时间:

SELECT 
    TO_CHAR(SYSTIMESTAMP AT TIME ZONE 'America/Los_Angeles', 'TZD') AS ZTD_SUMMER, 
    TO_CHAR((SYSTIMESTAMP + NUMTODSINTERVAL(150, 'DAY')) AT TIME ZONE 'America/Los_Angeles', 'TZD') as ZTD_WINTER
FROM dual;

+------------------------------+
|ZTD_SUMMER         |ZTD_WINTER|
+------------------------------+
|PDT                |PST       |
+------------------------------+