PL / SQL sysdate以ms为单位的Unix纪元时间

时间:2014-01-27 14:09:21

标签: oracle plsql unix-timestamp

我有一堆Oracle sysdate值需要转换为以ms为单位的Unix纪录时间。

例如具有值

的变量

15-MAR-13

应转换为

1363351108398
PL / SQL中的

如何做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以使用此功能。它还考虑时区,因为Unix epoche是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;