Oracle PL/SQLとUNIXタイムスタンプ

Oracleのdate型とUNIXタイムスタンプを相互に変換するPL/SQL関数が必要になった。
作ってみた。

create or replace function to_unix_timestamp(dt date) return number is
begin
    return (dt - to_date('1970-01-01','YYYY-MM-DD')) * 86400;
end;
/
create or replace function from_unix_timestamp(ts number) return date is
begin
    return to_date(trunc(ts / 86400, 0) + 2440588, 'J') + (mod(ts, 86400) / 86400);
end;
/
select to_unix_timestamp(sysdate) from dual;
select to_char(from_unix_timestamp(to_unix_timestamp(sysdate)),'YYYY-MM-DD HH24:MI:SS') from dual;
This entry was posted in Code, Tech and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>