向oracle高手求救,如何在oracle中取得unix下的long型时间值(即如何将一个date类型转换成unix下的整形时间)(100分)

  • 主题发起人 主题发起人 NewHuman
  • 开始时间 开始时间
N

NewHuman

Unregistered / Unconfirmed
GUEST, unregistred user!
向oracle高手求救,如何在oracle中取得unix下的long型时间值
(即如何将一个date类型转换成unix下的整形时间)
 
I can't remember where this was posted on the Net.
CREATE OR REPLACE FUNCTION Unix_To_Oracle_Date
(
p_unix_date IN NUMBER, -- Range of >= 0
p_num_hr_gmt_diff IN PLS_INTEGER -- Range of -24 to +24

) RETURN DATE AS


-- CONSTANTS

-- These 2 CONSTANTS below are of POSITIVE type to indicate that they
-- CANNOT be less than 1

c_SECONDS_IN_DAY CONSTANT POSITIVE := 86400;
c_HOURS_IN_DAY CONSTANT POSITIVE := 24;


c_UNIX_EPOCH_DATE_TIME_STRING CONSTANT VARCHAR2(50) := '01/01/1970 00:00:00';
c_DATE_FORMAT CONSTANT VARCHAR2(50) := 'MM/DD/YYYY HH24:MI:SS';

-- VARIABLES

v_sql_code PLS_INTEGER;
v_sql_error_message VARCHAR2(512);

-- The 2 variables have to be of NUMBER type because a PLS_INTEGER divided by
-- another PLS_INTEGER can result in a fraction, thus the result must be of
-- NUMBER type only

v_unix_seconds NUMBER;
v_gmt_time_gap NUMBER;

v_return_value DATE;

BEGIN

IF(NOT (p_num_hr_gmt_diff < (-c_HOURS_IN_DAY)) AND
NOT (p_num_hr_gmt_diff > (c_HOURS_IN_DAY))) THEN

v_unix_seconds := (p_unix_date / c_SECONDS_IN_DAY);
v_gmt_time_gap := (p_num_hr_gmt_diff / c_HOURS_IN_DAY);

v_return_value := TO_DATE(c_UNIX_EPOCH_DATE_TIME_STRING, c_DATE_FORMAT) +
v_unix_seconds + v_gmt_time_gap;
ELSE
v_return_value := NULL;
END IF;

RETURN(v_return_value);

EXCEPTION

WHEN OTHERS THEN
v_sql_code := SQLCODE;
v_sql_error_message := SQLERRM(v_sql_code);

DBMS_OUTPUT.PUT_LINE('OTHER ERROR');
DBMS_OUTPUT.PUT_LINE(v_sql_error_message);

END Unix_To_Oracle_Date;

 
接受答案了.
 
后退
顶部