如何找到当前时区 EST 或 EDT
How to find current timezone EST or EDT
Postgresql...如何找到当前时区 EST 或 EDT,我正在这样做但是期待一个适用于所有时区的简单解决方案。,
确切要求:如果我将 ET 作为本地时区,我需要当前时区和日光。
SELECT
(CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/New_York') - CURRENT_TIMESTAMP = '-04:00:00') then 'EDT'
WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/New_York') - CURRENT_TIMESTAMP = '-05:00:00') then 'EST'
ELSE ('OTH') END)
以下是我为此目的使用的函数:
CREATE OR REPLACE FUNCTION public.time_zone_tz_abbriv (
abbriv text
)
RETURNS text AS
$body$
SELECT
CASE WHEN 'ET' = THEN (CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/New_York') - CURRENT_TIMESTAMP) = '04:00:00' then 'EDT'
WHEN (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP AT TIME ZONE 'America/New_York')) = '05:00:00' then 'EST' END)
WHEN 'MT' = THEN (CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/Edmonton') - CURRENT_TIMESTAMP) = '06:00:00' then 'MDT'
WHEN (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP AT TIME ZONE 'America/Edmonton')) = '07:00:00' then 'MST' END)
WHEN 'PT' = THEN (CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/Los_Angeles') - CURRENT_TIMESTAMP) = '07:00:00' then 'PDT'
WHEN (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP AT TIME ZONE 'America/Los_Angeles')) = '08:00:00' then 'PST' END)
WHEN 'CT' = THEN (CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/Mexico_City') - CURRENT_TIMESTAMP) = '05:00:00' then 'CDT'
WHEN (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP AT TIME ZONE 'America/Mexico_City')) = '06:00:00' then 'CST' END)
END
$body$
LANGUAGE 'sql'
IMMUTABLE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
COST 100;
用法如下
SELECT TIME_ZONE_TZ_ABBRIV('ET');
Ans: EST or EDT
SELECT TIME_ZONE_TZ_ABBRIV('PT');
Ans: PST or PDT
SELECT TIME_ZONE_TZ_ABBRIV('MT');
Ans: MST or MDT
SELECT TIME_ZONE_TZ_ABBRIV('CT');
Ans: CST or CDT
只要你的要求确实是时区EDT/EST
、MDT/MST
等当前时间戳,你可以使用pg_timezone_names
.
它应该始终 return 基于您的系统时钟的正确缩写。
示例:
SELECT *
FROM pg_timezone_names
WHERE Name IN ('America/Edmonton','America/New_York')
此外,所有缩写都在 pg_timezone_abbrevs
中,与日期无关
示例:
SELECT *
from pg_timezone_abbrevs
WHERE Abbrev in ('EST','EDT')
Postgresql...如何找到当前时区 EST 或 EDT,我正在这样做但是期待一个适用于所有时区的简单解决方案。,
确切要求:如果我将 ET 作为本地时区,我需要当前时区和日光。
SELECT
(CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/New_York') - CURRENT_TIMESTAMP = '-04:00:00') then 'EDT'
WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/New_York') - CURRENT_TIMESTAMP = '-05:00:00') then 'EST'
ELSE ('OTH') END)
以下是我为此目的使用的函数:
CREATE OR REPLACE FUNCTION public.time_zone_tz_abbriv (
abbriv text
)
RETURNS text AS
$body$
SELECT
CASE WHEN 'ET' = THEN (CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/New_York') - CURRENT_TIMESTAMP) = '04:00:00' then 'EDT'
WHEN (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP AT TIME ZONE 'America/New_York')) = '05:00:00' then 'EST' END)
WHEN 'MT' = THEN (CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/Edmonton') - CURRENT_TIMESTAMP) = '06:00:00' then 'MDT'
WHEN (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP AT TIME ZONE 'America/Edmonton')) = '07:00:00' then 'MST' END)
WHEN 'PT' = THEN (CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/Los_Angeles') - CURRENT_TIMESTAMP) = '07:00:00' then 'PDT'
WHEN (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP AT TIME ZONE 'America/Los_Angeles')) = '08:00:00' then 'PST' END)
WHEN 'CT' = THEN (CASE WHEN ((CURRENT_TIMESTAMP AT TIME ZONE 'America/Mexico_City') - CURRENT_TIMESTAMP) = '05:00:00' then 'CDT'
WHEN (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP AT TIME ZONE 'America/Mexico_City')) = '06:00:00' then 'CST' END)
END
$body$
LANGUAGE 'sql'
IMMUTABLE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
COST 100;
用法如下
SELECT TIME_ZONE_TZ_ABBRIV('ET');
Ans: EST or EDT
SELECT TIME_ZONE_TZ_ABBRIV('PT');
Ans: PST or PDT
SELECT TIME_ZONE_TZ_ABBRIV('MT');
Ans: MST or MDT
SELECT TIME_ZONE_TZ_ABBRIV('CT');
Ans: CST or CDT
只要你的要求确实是时区EDT/EST
、MDT/MST
等当前时间戳,你可以使用pg_timezone_names
.
它应该始终 return 基于您的系统时钟的正确缩写。
示例:
SELECT *
FROM pg_timezone_names
WHERE Name IN ('America/Edmonton','America/New_York')
此外,所有缩写都在 pg_timezone_abbrevs
中,与日期无关
示例:
SELECT *
from pg_timezone_abbrevs
WHERE Abbrev in ('EST','EDT')