Hi Eugene,
Is there an embedded function to generate UNIX time – seconds that have elapsed since midnight, January 1, 1970?
Regards.
UNIX time function?
Re: UNIX time function?
Hi,
coarse variant (with LocalTimeOffset)
DateTimeToTimeStamp1970(Now)
coarse variant (with LocalTimeOffset)
Code: Select all
function TimeStamp1970ToDateTime(aTimeStamp: Integer): TDateTime;
begin
Result := (aTimeStamp / (24 * 60 * 60)) + EncodeDate(1970, 1, 1)
end;
function DateTimeToTimeStamp1970(aDateTime: TDateTime): Integer;
begin
Result := Round((aDateTime - EncodeDate(1970, 1, 1)) * 24 * 60 * 60)
end;
DateTimeToTimeStamp1970(Now)
-
- Posts: 98
- Joined: Sat May 08, 2010 8:12 pm
Re: UNIX time function?
Hi,Eugene wrote:Hi,
coarse variant (with LocalTimeOffset)
DateTimeToTimeStamp1970(Now)Code: Select all
function TimeStamp1970ToDateTime(aTimeStamp: Integer): TDateTime; begin Result := (aTimeStamp / (24 * 60 * 60)) + EncodeDate(1970, 1, 1) end; function DateTimeToTimeStamp1970(aDateTime: TDateTime): Integer; begin Result := Round((aDateTime - EncodeDate(1970, 1, 1)) * 24 * 60 * 60) end;
Thank you for your assistance.
I was thinking about something similar and before seeing your answer I came up with the following:
Code: Select all
function DateTimeToUnixTimestamp(dtValue: TDateTime): integer;
const
iGMTOffset = 2; // GMT +2
var
h, m, s, ms: integer;
begin
DecodeTime(dtValue,h,m,s,ms);
Result := Int(dtValue - EncodeDate(1970, 1, 1)) * 86400 + (h - iGMTOffset) * 3600 + m * 60 + s;
end;
For now I haven’t thought about the reverse function but if I come up with something new I’ll posted it.
Regards