PG中文社区 /
mdi-home
首页 社区新闻 中文文档 加入ACE {{ item.text }} 登录
mdi-home 首页 mdi-chat-processing 社区新闻 mdi-book-open-variant 中文文档 mdi-account-multiple-check 加入ACE mdi-file-multiple-outline 相关资料 mdi-blank {{item.text}} mdi-exit-to-app 退出账号
PostgreSQL Oracle兼容性

原作者:digoal/德哥  创作时间:2016-10-17 12:51:07+08  
doudou586 发布于2016-10-17 12:51:07           评论: 0   浏览: 39091   顶: 11668  踩: 11744 

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

扫描报名


/images/news/2016/pgconf2016_qrcode.jpg


/images/news/2016/pgconf2016_plus_logo_cn.png


评论:0   浏览: 39091                   顶: 11668  踩: 11744 

请在登录后发表评论,否则无法保存。


发表评论:
加入我们
QQ群1:5276420
QQ群2:3336901
QQ群3:254622631
文档群:150657323
文档翻译平台:按此访问
社区邮件列表:按此订阅
商业支持
扫码关注
加入我们
QQ群1:5276420
QQ群2:3336901
QQ群3:254622631
文档群:150657323
文档翻译平台:按此访问
社区邮件列表:按此订阅
商业支持
扫码关注
© PostgreSQL中文社区 ... (自2010年起)