Convert time to Unix Timestamp ?

ItsMe
ItsMe used Ask the Experts™
on
hi! how can i convert the delphi time to a unix timestamp ?
any units out there ?

thanks
ItsMe
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
unixtime := Trunc((EncodeDate(y,m,d)+EncodeTime(h,m,s,0)-25569)*86400);

Or if you already have your date/time in a TDateTime use:
unixtime := Trunc((datetime-25569)*86400);

Paul
Ferruccio AccalaiSenior developer, analyst and customer assistance

Commented:
these function already exist in ..\source\rtl\common\DateUtils.pas

function DateTimeToUnix(const AValue: TDateTime): Int64;
function UnixToDateTime(const AValue: Int64): TDateTime;

F68 ;-)


Commented:
I thought they did, couldn't remember what they were called - best to use the built in ones :)
I wonder how (or even if) they convert the following "non-existant" dates...
Sep 3rd 1752 through Sep 13th 1752...

Paul

Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
hi !
i'm using delphi 4 standard. cannot find the functions
there. could you send me the unit ? thanks for your
code zebada. does it works diffrent from the unit?

ItsMe

Commented:
It's pretty easy to figure it out:

Delphi (V2 and later) stores date-times as a double (TDateTime)
The whole part is the number of days since 30-Dec-1899
The fractional part is the fraction of the time of day.

A unix date-time is stored as the number of seconds since midnight 1-Jan-1970.

So to convert from Delphi to Unix you need to:
1. subtract the number of days between 30-Dec-1899 and 1-Jan-1970. (which is 25569)
2. multiply by 86400 (the number of seconds in one day)
3. Truncate because Unix date times are accurate only to 1 second.

Regards
Paul
Ferruccio AccalaiSenior developer, analyst and customer assistance

Commented:
Well, DateUtils cames since D5 so the best is to use the Zebada function....

BTW:
const
SecsPerDay = 86400;
UnixDateDelta = 25569;

function DateTimeToUnix(const AValue: TDateTime): Integer;
begin
  Result := Round((AValue - UnixDateDelta) * SecsPerDay); //in D6 is delared like the zebada's one but the result is rounded, not truncated...
end;

and the reversed is

function UnixToDateTime(const AValue: Integer): TDateTime;
begin
  Result := AValue / SecsPerDay + UnixDateDelta;
end;

Nothing more then zebada. Just to add some notices...
F68 ;-)

Author

Commented:
i tried the functions but the results differ from the values stored in my db. i wrote
some datetimestamps with php4 to a mysql database. i didn't work much with
TDateTime in delphi yet. in php there is a function which can create a datetimestamp
from a string. i tried StrToDate ('31/08/2003'); but this will only result the date.
how can i calculate a time, too ? and how do i set the format of the date to
german ?

thanks
ItsMe

Commented:
Probably time to close this question and ask a new one!

Author

Commented:
hi again !
the Convert functions result diffrent values than php !

in php

1st September, 8:00 AM is 1062396000

but your function returns 1062403200

whats wrong with it ? i created the date by

DateSeparator := '/';
tagstart := StrToDateTime ('01/09/03 08:00:00');
(german date format)


please help!
thanks

Author

Commented:
so can anyone tell me where the diffrence of 2 hrs comes from ?

Commented:
It works fine for converting dates between C on Linux and Delphi on Windows.
I have not used php so I'm not much help there.

Commented:
Is it a possible timezone problem?
Is it a possible daylight savings issue?

Author

Commented:
hi zebada ! i tried
"ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime" and rebootet the server.
its still the same. wired

Commented:
Are the system times the same when you are running Windows as when you are running Linux?

Author

Commented:
yep they're. i synchronize them with a german timeserver.

Commented:
I'm all out of ideas then :( sorry.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial