PostgreSQL Oracle兼容性
2016 Postgres大象会官方报名通道:点此报名
作者: digoal
背景
如何将一个时区的本地时间,转换为另一个时区的本地时间?
在Oracle中可以使用NEW_TIME这个函数实现这个目的。
NEW_TIME(ts, tz1, tz2)
NEW_TIME returns the date and time in time zone timezone2 when date and time in time zone timezone1 are date.
The arguments timezone1 and timezone2 can be any of these text strings:
AST, ADT: Atlantic Standard or Daylight Time
BST, BDT: Bering Standard or Daylight Time
CST, CDT: Central Standard or Daylight Time
EST, EDT: Eastern Standard or Daylight Time
GMT: Greenwich Mean Time
HST, HDT: Alaska-Hawaii Standard Time or Daylight Time.
MST, MDT: Mountain Standard or Daylight Time
NST: Newfoundland Standard Time
PST, PDT: Pacific Standard or Daylight Time
YST, YDT: Yukon Standard or Daylight Time
例子ALTER SESSION SET NLS_DATE_FORMAT ='DD-MON-YYYY HH24:MI:SS';
SELECT NEW_TIME(TO_DATE(
'11-10-99 01:23:45', 'MM-DD-YY HH24:MI:SS'),
'AST', 'PST') "New Date and Time" FROM DUAL;
New Date and Time
--------------------
09-NOV-1999 21:23:45
SELECT SYS_EXTRACT_UTC(TIMESTAMP '2000-03-28 11:30:00.00 -08:00')
FROM DUAL;
SYS_EXTRACT_UTC(TIMESTAMP'2000-03-2811:30:00.00-08:00')
-----------------------------------------------------------------
28-MAR-00 07.30.00 PM
PostgreSQL new_time
了解功能之后,PG就很容易实现new_time的函数了。CREATE OR REPLACE FUNCTION public.new_time(ts timestamp without time zone, tz1 text, tz2 text)
RETURNS timestamp without time zone
LANGUAGE plpgsql
STRICT
AS $function$
declare
res timestamp;
begin
execute 'set local timezone to '''|| tz2||'''';
-- raise notice '%', current_setting('timezone');
select (timestamptz(ts || tz1))::timestamp into res;
return res;
end;
$function$;
PostgreSQL的时区列表。postgres=# select * from pg_timezone_names ;
name | abbrev | utc_offset | is_dst
----------------------------------+--------+------------+--------
PST8PDT | PDT | -07:00:00 | t
Eire | IST | 01:00:00 | t
Antarctica/DumontDUrville | DDUT | 10:00:00 | f
Antarctica/Syowa | SYOT | 03:00:00 | f
Antarctica/McMurdo | NZST | 12:00:00 | f
Antarctica/Rothera | ROTT | -03:00:00 | f
Antarctica/Casey | AWST | 08:00:00 | f
Antarctica/Davis | DAVT | 07:00:00 | f
Antarctica/Macquarie | MIST | 11:00:00 | f
Antarctica/South_Pole | NZST | 12:00:00 | f
Antarctica/Troll | CEST | 02:00:00 | t
Antarctica/Mawson | MAWT | 05:00:00 | f
Antarctica/Palmer | CLST | -03:00:00 | t
Antarctica/Vostok | VOST | 06:00:00 | f
HST | HST | -10:00:00 | f
Iceland | GMT | 00:00:00 | f
CST6CDT | CDT | -05:00:00 | t
Kwajalein | MHT | 12:00:00 | f
GMT | GMT | 00:00:00 | f
Australia/Broken_Hill | ACST | 09:30:00 | f
Australia/Canberra | AEST | 10:00:00 | f
Australia/Currie | AEST | 10:00:00 | f
Australia/Yancowinna | ACST | 09:30:00 | f
Australia/Brisbane | AEST | 10:00:00 | f
Australia/West | AWST | 08:00:00 | f
......
Atlantic/Madeira | WEST | 01:00:00 | t
Egypt | EET | 02:00:00 | f
Canada/Central | CDT | -05:00:00 | t
Canada/Eastern | EDT | -04:00:00 | t
Canada/East-Saskatchewan | CST | -06:00:00 | f
Canada/Saskatchewan | CST | -06:00:00 | f
Canada/Pacific | PDT | -07:00:00 | t
Canada/Yukon | PDT | -07:00:00 | t
Canada/Atlantic | ADT | -03:00:00 | t
Canada/Newfoundland | NDT | -02:30:00 | t
Canada/Mountain | MDT | -06:00:00 | t
(589 rows)
PST -08:00 Pacific Standard Time
AST -04:00 Atlantic Standard Time (Canada)
最好直接使用时区postgres=# select new_time('1999-10-11 01:23:45', '-04', '-08');
NOTICE: +08
new_time
---------------------
1999-10-10 21:23:45
(1 row)
PostgreSQL SYS_EXTRACT_UTC
自定义函数如下CREATE OR REPLACE FUNCTION public.SYS_EXTRACT_UTC(ts timestamp with time zone)
RETURNS timestamp without time zone
LANGUAGE plpgsql
STRICT
AS $function$
declare
res timestamp;
begin
set local timezone to UTC;
select ts::timestamp into res;
return res;
end;
$function$;
验证postgres=# select SYS_EXTRACT_UTC('2000-03-28 11:30:00.00-08:00');
sys_extract_utc
---------------------
2000-03-28 19:30:00
(1 row)
扫描报名