Link to home
Start Free TrialLog in
Avatar of aptalaptal
aptalaptal

asked on

Equivalent Delphi or Win32 API function of 'ctime' of MS VC++

char text[500];
strcpy(text, ctime((const long *)&pTBoxDataBuffer[i].dwTimeStamp) );
text[strlen(text)-1]=0; // remove final \n
printf(" %s \n", text);

i have to do the same what the code above does.

Does anybody know the Delphi equivalent of 'ctime' function of MS VC++?

    ctime((const long *)&pTBoxDataBuffer[i].dwTimeStamp)

'dwTimestamp' holds ' seconds since 01/01/70 00:00:00 GMT+0 ', called UTC.

Does anybody know Delphi equivalent or Win32 API of this 'ctime ' function?
It is urgent.
Thanks in advance for your help.

Avatar of TheNeil
TheNeil

What does the CTime routine do in MS VC++?

The Neil
What does the CTime routine do in MS VC++?

The Neil
Yo could see the win32.hlp's entry for getSystemTime:

The GetSystemTime function retrieves the current system date and time. The system time is expressed in Coordinated Universal Time (UTC).

VOID GetSystemTime(

    LPSYSTEMTIME  lpSystemTime       // address of system time structure  
   );


GL
Mike
I'm lost as to what you're ultimately trying to do. As I see it you're getting this value which holds the number of seconds from 1st Jan 1970, and then converts it to a string. Are you trying to store and output dates  since 1st Jan 1970? As far as I know there's no in built Delphi routine to do this (the seconds and 1970 bit anyway) but writing a routine to convert your integer value and add in the offset is pretty easy to do (ask if you want it writing)

The Neil
Avatar of aptalaptal

ASKER

TheNeil
would you write such a function for me to convert an integer number (holding seconds since 01/01/70 00:00:00 GMT + 0)?

then i will give you all the points.
I'll give it a try. Just to avoid confusion, you want a routine that is supplied with the number of seconds since 01/01/70 00:00:00 and returns a string containing the actual date (e.g. given 86461 (1 day 1 minute, and 1 second) it would return 02/01/70 00:01:01 as a string)

<Gulp> What have I gotten myself into now?

The Neil
edey,

i m not dealing with system time. i have the number of seconds from 01/01/70 00:00:00 GMT+0.

MS VC++ 5.0 and ,i guess, older versions of VC++ have 'ctime ' function to convert seconds into a stirng format like :

Thu Jan 20 19:36:11 2000 .

but thank you for your interest. :)
waiting for your any other help.
If you have the number of seconds since 1/1/70 00:00:00 GMT+0, why not just add this value to a date value, i.e.

dNewTime := StrToDate('01/01/1970')+
            nNumOfSeconds

Now dNewTime will contain the appropriate time.

TheNeil

you are thoroughly right :)

i have an integer value represents seconds from 01/01/1970 00:00:00 .

i need a function to convert that integer value (i mean seconds) into a string like

  seconds            string
948396971 ---> 20/01/2000  19:36:11


function SecToDateTime(second:integer):string;
begin
???
end;

does anybody do that or know a win32 API or object pascal function???

this is very important to me.
JoeBooth,

can you convert the number '948396971' (that is the seconds from 01/01/1970 00:00:00 ) into
this string : 20/01/2000  19:36:11 ?

  seconds     --->       string
 948396971 ---> '20/01/2000  19:36:11 '


this is what 'ctime' function in C++ does.
this is also what i want to do in Delphi.

thanks and waiting for your any suggestions.


JoeBooth,

can you convert the number '948396971' (that is the seconds from 01/01/1970 00:00:00 ) into
this string : 20/01/2000  19:36:11 ?

  seconds     --->       string
 948396971 ---> '20/01/2000  19:36:11 '


this is what 'ctime' function in C++ does.
this is also what i want to do in Delphi.

thanks and waiting for your any suggestions.


Joe,

948396971 is the number of seconds from 1st Jan 1970, the problem is, adding on the time UP TO the 1st Jan 1970

The Neil

Joe,

948396971 is the number of seconds from 1st Jan 1970, the problem is, adding on the time UP TO the 1st Jan 1970

The Neil

try this one:

function SecToDate(x:integer):string;
var x1Day,x1Hour,x1Min,x1Sec,date:TDateTime;
    yDay,yHour,yMin,ySec:integer;
   s:string;
begin
 x1Day:=StrToDateTime('02.01.1980')-StrToDateTime('01.01.1980');
 x1Hour:=StrToDateTime('01.01.1980 02:00:00')-StrToDateTime('01.01.1980 01:00:00');
 x1Min:=StrToDateTime('01.01.1980 01:01:00')-StrToDateTime('01.01.1980 01:00:00');
 x1Sec:=StrToDateTime('01.01.1980 01:00:01')-StrToDateTime('01.01.1980 01:00:00');
 yDay:=x div 86400;
 yHour:=(x - (yDay*86400)) div 3600;
 yMin:=((x - (yDay*86400)) - (yHour*3600)) div 60;
 ySec:=((x - (yDay*86400)) - (yHour*3600)) - (yMin*60);

 date:=StrToDateTime('01.01.1970 00:00:00')+
       yDay*x1Day+yHour*x1Hour+yMin*x1Min+ySec*x1Sec;
 Result:=FormatDateTime('dd.mm.yyyy hh:nn:ss',date);
end;

you have to ajust the dates and times to your time format

good luck
ASKER CERTIFIED SOLUTION
Avatar of TheNeil
TheNeil

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
The Neil - you dont like my function ? why ?
Egono

Who said I don't like your function? Admittedly I've sworn at you and your family since I spotted appearing ONE MINUTE before mine but it's small, neat, and elegant so you do gain some admiration. It's a bit confusing though and yours might fall over if the date/time format is different while mine will work on any machine (regardless of the date/time format).
Here I was all pleased that I'd got the damn thing working and you pop up with an alternative - grrrrr (I really fancied those points as well)

The Neil =:(
egono,

i tried your code but not working properly.

i gave 948396971, which is expected to display
20/01/2000 19:36:11, to your function .

but it returned 04/08/2901 19:36:11 which is wrong.


The Neil - I've wrote these function about a year ago and as I saw the question I remembered me - i just copied it from the old source

because you had so much work, I'd like to ask aptalaptal to share the points - is this ok ?
egono,

i tried your code but not working properly.

i gave 948396971, which is expected to display
20/01/2000 19:36:11, to your function .

but it returned 04/08/2901 19:36:11 which is wrong.


aptalaptal - did you change the date and time format ?

my functions works for me

ok grade TheNeils answer - as he said his function dosnt depend on date/time format

Aptalaptal

If you try my code then you'll see that 948396971 does return the correct value. Egono's code works on my machine but I think this may come down to the problem that I highlighted to him - the date/time format is potentially different on different machines.

The Neil
i think it would be unfair to TheNeil to divide the points.

sorry egono:(

but thank you for your help.

theneil worked hard on this question.
i am giving you the points.

thanks theneil. it works properly.

take care of yourselves, all who commented on this question.
bye until the next question.

take care:)
Thanks Aptalaptal (and I did work hard on this one). Have you accepted my comment as an answer or do you want me to submit another message as an answer?

All I can say Egono is sorry, when it came down to it, my code worked and your's didn't - next time it might be the other way round (probably will be knowing my code)

The Neil
that's life - congratulation TheNeil, you worked hard for it :)