Questa domanda è praticamente l'inverso di Convert Unixtime to Datetime SQL (Oracle).
Come dice Justin Cave:
Non ci sono funzioni integrate. Ma è relativamente facile scriverne una. Poiché un timestamp Unix è il numero di secondi trascorsi dal 1° gennaio 1970
Poiché sottrarre una data da un'altra data dà come risultato il numero di giorni tra loro, puoi fare qualcosa del genere:
create or replace function date_to_unix_ts( PDate in date ) return number is l_unix_ts number; begin l_unix_ts := ( PDate - date '1970-01-01' ) * 60 * 60 * 24; return l_unix_ts; end;
Essendo in secondi dal 1970, il numero di secondi frazionari è irrilevante. Puoi comunque richiamarla con un tipo di dato timestamp...
SQL> select date_to_unix_ts(systimestamp) from dual; DATE_TO_UNIX_TS(SYSTIMESTAMP) ----------------------------- 1345801660
In risposta al tuo commento, mi dispiace ma non vedo quel comportamento:
SQL> with the_dates as ( 2 select to_date('08-mar-12 01:00:00 am', 'dd-mon-yy hh:mi:ss am') as dt 3 from dual 4 union all 5 select to_date('08-mar-12', 'dd-mon-yy') 6 from dual ) 7 select date_to_unix_ts(dt) 8 from the_dates 9 ; DATE_TO_UNIX_TS(DT) ------------------- 1331168400 1331164800 SQL>
Ci sono 3.600 secondi di differenza, ovvero 1 ora.