Subject Re: [firebird-support] How to convert TIMESTAMP to unix timestamp (number of seconds since epoch)
Author Anderson Farias
Hi,

>> I need to convert a timestamp to a unix timestamp in a procedure. I don't
>> seem to find a good way to that. Is there a UDF library that can do this
>> for me.


Another option is to use an UDF... FreeAdhocUDF has an AGEINSECONDS function
which returns the number of seconds between 2 timestamps. Being 1st
timestamp the unix-time EPOCH (1-jan-1970 midnight) and the other your date
you have the result you need. If using FB 2.1 *no* need for a UDF, you can
use DATEDIFF internal function. examples:

select F_AGEINSECONDS('1/1/1970 00:00:00', current_timestamp) from
rdb$database;

or (FB 2.1)

select DATEDIFF(second, timestamp '1/1/1970 00:00:00', current_timestamp)
from rdb$database;


inside your procedure:

declare epoch timestamp = '1970-01-01 00:00:00'
declare unix_time1 integer
declare unix_time2 integer

unix_time1 = F_AGEINSECONDS(epoch, current_timestamp)
unix_time2 = datediff(second, epoch, current_timestamp)


regards,
Anderson Farias