问题
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