Convert dates TDatetime < - > Unix  

Send By: Gaspar Fernández
Web : http://www.totaki.com
Email: blakeyed@totaki.com
Date: 15/04/03

Tip accessed 553 times

 


I propose a simple but interesting source code source, to convert date and hour time unix to TDatetime and vice versa.

The date Unix is the number of seconds lapsed from January 1 1970, at 12 in the night so:


   function 1_Ene_1970: TDateTime;
   begin
     Result := EncodeDate(1970, 1, 1);
   end;



With this function we obtain the date in TDateTime of January of 1970, 1 and the result is 25569.0, so:


   const SegundosPorDia = 86400;

   function UnixToTDateTime(Unix: Int64): TDateTime;
   begin
     Result := 25569.0 + Unix / SegundosPorDia;
   end;

   function DateTimeToUnix(DT: TDateTime): Int64;
   begin
     Result := Trunc(SegundosPorDia * (DT - 25569.0));
   end;