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
另外Oracle还有一个函数SYS_EXTRACT_UTC,是将一个timestamptz转换成UTC时区的本地时间:
https://docs.oracle.com/cd/B12037_01/server.101/b10759/functions152.htm
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)
验证对应oracle的例子: https://www.postgresql.org/docs/7.2/static/timezones.html
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>+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)
2016 Postgres大象会官方报名通道:http://www.huodongxing.com/event/8352217821400
扫描报名
http://nalei.zjtcbmw.cn/haibei/
http://taiying.njtcbmw.cn/ycjz/
http://huilong.sctcbmw.cn/changde/
https://longhu.tiancebbs.cn/
http://km.lstcxxw.cn/guizhou/
https://aihuishou.tiancebbs.cn/flqcq/
http://taiying.njtcbmw.cn/shjinshan/
http://taiying.njtcbmw.cn/bygjz/
http://cf.lstcxxw.cn/jsqz/
http://huaguang.jxtcbmw.cn/tczh/
https://cpqqita.tiancebbs.cn/
https://dapuxian.tiancebbs.cn/
http://ouyu.hftcbmw.cn/jjsy/
http://gx.lztcxxw.cn/chongmingxian/
https://shou.tiancebbs.cn/
http://yz.cqtcxxw.cn/qhxn/
https://honglan.tiancebbs.cn/daxinganling/
http://yz.cqtcxxw.cn/djjz/
http://jinqiang.ahtcbmw.cn/cqzx/
http://huaguang.jxtcbmw.cn/dp/
http://js.sytcxxw.cn/lijiang/
https://congjiang.tiancebbs.cn/
https://fenlei.tiancebbs.cn/sfc/
https://slshanyang.tiancebbs.cn/
https://nanming.tiancebbs.cn/
http://huilong.sctcbmw.cn/nanchong/
http://shengshun.njtcbmw.cn/xian/
http://jinqiang.ahtcbmw.cn/yantai/
http://wogao.ahtcbmw.cn/wanning/
http://bjtcxxw.cn/pgq/
https://awati.tiancebbs.cn/
http://yz.cqtcxxw.cn/ms/
http://cf.lstcxxw.cn/hdq/
http://huilong.sctcbmw.cn/shuangyashan/
https://zulin.tiancebbs.cn/sh/3603.html
https://changzhi.tiancebbs.cn/qths/462118.html
https://aihuishou.tiancebbs.cn/sh/1313.html
https://zulin.tiancebbs.cn/sh/4863.html
https://sz.tiancebbs.cn/pgjgsc/451005.html
https://sh.tiancebbs.cn/hjzl/468466.html
https://sz.tiancebbs.cn/qitafuwu/57903.html
https://zulin.tiancebbs.cn/sh/782.html
https://www.tiancebbs.cn/ershoufang/472200.html
https://www.tiancebbs.cn/ershoufang/470876.html
https://hg.tiancebbs.cn/qths/468073.html
https://www.tiancebbs.cn/wangzhantuiguang/440810.html
https://www.tiancebbs.cn/ershoufang/473672.html
https://www.tiancebbs.cn/ershoufang/472140.html
https://www.tiancebbs.cn/ershoufang/467946.html
https://zulin.tiancebbs.cn/sh/4850.html
https://zulin.tiancebbs.cn/sh/2066.html
https://aihuishou.tiancebbs.cn/sh/2139.html
https://klmy.tiancebbs.cn/qths/460244.html
https://su.tiancebbs.cn/hjzl/471088.html
https://www.tiancebbs.cn/ershoufang/474485.html
https://wz.tiancebbs.cn/qths/469264.html
https://changshushi.tiancebbs.cn/hjzl/462851.html
https://www.tiancebbs.cn/ershoufang/470274.html
https://zulin.tiancebbs.cn/sh/1711.html
https://su.tiancebbs.cn/hjzl/469216.html
https://aihuishou.tiancebbs.cn/sh/2209.html
https://heyuan.tiancebbs.cn/qths/463325.html
https://zulin.tiancebbs.cn/sh/1470.html
https://aihuishou.tiancebbs.cn/sh/4063.html
https://zulin.tiancebbs.cn/sh/4815.html
https://zulin.tiancebbs.cn/sh/4425.html
https://zulin.tiancebbs.cn/sh/3646.html
https://zulin.tiancebbs.cn/sh/1219.html
https://zulin.tiancebbs.cn/sh/810.html
https://www.tiancebbs.cn/ershouwang/468822.html
https://aihuishou.tiancebbs.cn/sh/1088.html
https://sz.tiancebbs.cn/pgjgsc/419897.html
https://sr.tiancebbs.cn/qths/454649.html
https://www.tiancebbs.cn/ershoufang/469273.html
https://lc.tiancebbs.cn/qths/470431.html
https://taicang.tiancebbs.cn/hjzl/463765.html
https://www.tiancebbs.cn/ershouwang/467654.html
https://zulin.tiancebbs.cn/sh/2246.html
https://sh.tiancebbs.cn/hjzl/459983.html
https://xg.tiancebbs.cn/qths/470504.html
https://aihuishou.tiancebbs.cn/sh/3974.html
https://zulin.tiancebbs.cn/sh/2437.html
https://zulin.tiancebbs.cn/sh/857.html
https://zulin.tiancebbs.cn/sh/3584.html
https://zulin.tiancebbs.cn/sh/647.html
高情商的表达不满语句:https://www.nanss.com/yulu/1913.html 特别委屈想哭的句子:https://www.nanss.com/yulu/1923.html 饿了发朋友圈:https://www.nanss.com/wenan/1868.html 儿子上幼儿园的感慨:https://www.nanss.com/wenan/1910.html 工作生活两不误美句:https://www.nanss.com/gongzuo/1716.html 写给天堂的老公的一段话:https://www.nanss.com/shenghuo/1756.html 最经典的酒吧短语:https://www.nanss.com/wenan/1854.html 决定放弃一个人的网名:https://www.nanss.com/mingcheng/1925.html 感叹人生聚散的句子:https://www.nanss.com/yulu/1697.html 快手网名女生霸气:https://www.nanss.com/mingcheng/1633.html 最有意义的12岁寄语励志:https://www.nanss.com/yulu/1630.html 鼓励孩子们努力上进的句子:https://www.nanss.com/xuexi/1962.html 拟人的句子:https://www.nanss.com/xuexi/1964.html 表达欢迎回来的话语:https://www.nanss.com/yulu/1973.html 我想陪你的句子:https://www.nanss.com/yulu/1946.html 阳光少年的句子励志:https://www.nanss.com/xuexi/1601.html emo语句关于爱情:https://www.nanss.com/yulu/1597.html 个性名称:https://www.nanss.com/mingcheng/1805.html 给男生取一个可爱的绰号:https://www.nanss.com/mingcheng/1698.html 住院发的文案:https://www.nanss.com/wenan/1782.html 一晃十二年的感悟:https://www.nanss.com/yuedu/1634.html 女人第一根白头发感悟:https://www.nanss.com/yulu/1757.html 好看的符号网名:https://www.nanss.com/mingcheng/1824.html 国家安全教育日顺口溜简短:https://www.nanss.com/shenghuo/1591.html 适合小侄子出生时发的说说:https://www.nanss.com/wenan/1592.html 高智商的骂领导的话:https://www.nanss.com/gongzuo/1933.html 现实又很扎心的句子:https://www.nanss.com/yulu/1954.html 土味情话大全:https://www.nanss.com/yulu/1969.html 自己不努力靠谁都没用的句子:https://www.nanss.com/yulu/1972.html 不期待不依赖的句子:https://www.nanss.com/wenan/1897.html