PL/SQL sysdate to Unix epoch time in ms

主宰稳场 提交于 2020-01-06 13:03:27

问题


I have bunch of Oracle sysdate values which need to be converted to Unix epoch time in ms.

For example variable that has value

15-MAR-13

should convert to

1363351108398

in PL/SQL

How would one do that ?


回答1:


You can use this function. It also considers the time zone, because Unix epoche is 1970-01-01 00:00:00 UTC!

CREATE OR REPLACE FUNCTION GetEpoche(theTimestamp IN TIMESTAMP, timezone IN VARCHAR2 DEFAULT SESSIONTIMEZONE) RETURN NUMBER DETERMINISTIC IS

    timestampUTC TIMESTAMP;
    theInterval INTERVAL DAY(9) TO SECOND;
    epoche NUMBER;
BEGIN

    timestampUTC := FROM_TZ(theTimestamp, timezone) AT TIME ZONE 'UTC';    
    theInterval := TO_DSINTERVAL(timestampUTC - TO_TIMESTAMP('1970-01-01', 'YYYY-MM-DD') );
    epoche := EXTRACT(DAY FROM theInterval)*24*60*60 
        + EXTRACT(HOUR FROM theInterval)*60*60 
        + EXTRACT(MINUTE FROM theInterval)*60 
        + EXTRACT(SECOND FROM theInterval);
    RETURN ROUND(1000*epoche);
END GetEpoche;


来源:https://stackoverflow.com/questions/21382996/pl-sql-sysdate-to-unix-epoch-time-in-ms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!