Link to home
Start Free TrialLog in
Avatar of ItsMe
ItsMe

asked on

Convert time to Unix Timestamp ?

hi! how can i convert the delphi time to a unix timestamp ?
any units out there ?

thanks
ItsMe
ASKER CERTIFIED SOLUTION
Avatar of zebada
zebada

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ferruccio Accalai
these function already exist in ..\source\rtl\common\DateUtils.pas

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

F68 ;-)


Avatar of zebada
zebada

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

Avatar of ItsMe

ASKER

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
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
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 ;-)
Avatar of ItsMe

ASKER

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
Probably time to close this question and ask a new one!
Avatar of ItsMe

ASKER

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
Avatar of ItsMe

ASKER

so can anyone tell me where the diffrence of 2 hrs comes from ?
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.
Is it a possible timezone problem?
Is it a possible daylight savings issue?
Avatar of ItsMe

ASKER

hi zebada ! i tried
"ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime" and rebootet the server.
its still the same. wired
Are the system times the same when you are running Windows as when you are running Linux?
Avatar of ItsMe

ASKER

yep they're. i synchronize them with a german timeserver.
I'm all out of ideas then :( sorry.