Mega Code Archive

 
Categories / Delphi / System
 

Converting TDateTime to a UNIX Timestamp

Title: Converting TDateTime to a UNIX Timestamp Question: How can I convert a UNIX timestamp (seconds since January 1, 1970) to a TDateTime? Answer: You can use the following functions to convert a UNIX Timestamp to a TDateTime and vice versa. I found this on a web site after much searching. I have changed the functions to take and return an actual TDateTime rather than a string. function UNIXTimeToDateTime(UnixTime: LongWord): TDateTime; var TimeZoneInformation: TTimeZoneInformation; begin GetTimeZoneInformation(TimeZoneInformation); Result := StrToDate('01/01/1970') + (UnixTime/(24*3600)) - ((TimeZoneInformation.Bias + TimeZoneInformation.DaylightBias) / (24 * 60)); end; function DateTimeToUNIXTime(DelphiTime : TDateTime): LongWord; var MyTimeZoneInformation: TTimeZoneInformation; begin GetTimeZoneInformation(MyTimeZoneInformation); Result := round(DelphiTime - StrToDate('01/01/1970') + ((MyTimeZoneInformation.Bias) / (24 * 60))) * (24 * 3600); end;