Even though many of the c programmers are using time_t data type to express a date format in the oracle database. It is nearly hard to convert each other. Thus, I made a function to convert it.
(Actually, time _t data type in an oracle is like as a long type in the c programming)
(1) First, you need to get a beginning of the second with a following program.
(compile and run it)
#include <stdio.h>
#include <time.h>
int main() {
struct tm tt;
tt.tm_year = 1970-1900; // The number of years since 1900
tt.tm_mon = 0;
tt.tm_mday = 1;
tt.tm_hour = 0;
tt.tm_min = 0;
tt.tm_sec=0;
tt.tm_isdst = -1;
int tx = mktime(&tt);
printf("%d", tx);
}
#include <time.h>
int main() {
struct tm tt;
tt.tm_year = 1970-1900; // The number of years since 1900
tt.tm_mon = 0;
tt.tm_mday = 1;
tt.tm_hour = 0;
tt.tm_min = 0;
tt.tm_sec=0;
tt.tm_isdst = -1;
int tx = mktime(&tt);
printf("%d", tx);
}
in Korea: -32400
time_t is the number of seconds since the Epoch, 1970-01-01 00:00. (UTC)
(2) Put the result of (1) to the red marks.
86400: 60*60*24 (sec*min*hour)
-- This Function Is For DATE -> time_t (second)
CREATE OR REPLACE FUNCTION DATE_TO_SECOND (v_date DATE) RETURN NUMBER AS
BEGIN
RETURN
trunc((v_date-to_date('19700101','YYYYMMDD'))*86400-32400) ;
END;
/
-- This Function Is For time_t (second) -> DATE
CREATE OR REPLACE FUNCTION SECOND_TO_DATE (v_sec NUMBER) RETURN DATE AS
BEGIN
RETURN
numtodsinterval((v_sec+32400) , 'second')+to_date('19700101','YYYYMMDD') ;
END;
/
CREATE OR REPLACE FUNCTION DATE_TO_SECOND (v_date DATE) RETURN NUMBER AS
BEGIN
RETURN
trunc((v_date-to_date('19700101','YYYYMMDD'))*86400-32400) ;
END;
/
-- This Function Is For time_t (second) -> DATE
CREATE OR REPLACE FUNCTION SECOND_TO_DATE (v_sec NUMBER) RETURN DATE AS
BEGIN
RETURN
numtodsinterval((v_sec+32400) , 'second')+to_date('19700101','YYYYMMDD') ;
END;
/