• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 647
  • Last Modified:

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.

0
aptalaptal
Asked:
aptalaptal
  • 10
  • 9
  • 5
  • +2
1 Solution
 
TheNeilCommented:
What does the CTime routine do in MS VC++?

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

The Neil
0
 
edeyCommented:
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
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
TheNeilCommented:
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
0
 
aptalaptalAuthor Commented:
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.
0
 
TheNeilCommented:
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
0
 
aptalaptalAuthor Commented:
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.
0
 
JoeBoothCommented:
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.

0
 
aptalaptalAuthor Commented:
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.
0
 
aptalaptalAuthor Commented:
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.


0
 
aptalaptalAuthor Commented:
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.


0
 
TheNeilCommented:
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

0
 
TheNeilCommented:
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

0
 
egonoCommented:
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
0
 
TheNeilCommented:
Aptalaptal

Hopefully this is what you're after. What it does is tobreak the seconds down into days, hours, minutes, and seconds. It then uses the number of days to calculate the years involved, and finally the months. The complexity comes in in the form of leap years but this handles them (or seems to)

function SecToDateTime(iNew : LONGINT): STRING;
VAR
  dtNew      : TDateTime;
  years      : INTEGER;
  months     : INTEGER;
  days       : INTEGER;
  hours      : INTEGER;
  mins       : INTEGER;
  secs       : INTEGER;
  Day_Count  : INTEGER;
  n          : INTEGER;
begin
  days := iNew DIV (24 * 60 * 60);
  iNew := iNew - (days * 24 * 60 * 60);
  hours := iNew DIV (60 * 60);
  iNew := iNew - (hours * 60 * 60);
  mins := iNew DIV 60;
  secs := iNew MOD 60;

  //Hours, mins, and secs are fine now - just need to work out the months and years
  //BUT we have to allow for leap years...

  n := 0;
  Years := 0;
  REPEAT
    n := n + 1;
    IF ((n + 1970) MOD 4) = 0
    THEN
      Day_Count := 366
    ELSE
      Day_Count := 365;

    IF days > Day_Count
    THEN
    BEGIN
      Years := Years + 1;
      days := days - Day_Count;
    END;
  UNTIL (Days <= Day_Count);

  IF ((Years - 2) MOD 4) = 0  //Is THIS year a leap year?
  THEN
  BEGIN
    IF Days < (31 + 29)    //If we HAVEN'T gone past February 29th then we're
    THEN                   //a day short (the above REPEAT loop will have taken
      Days := Days + 1;    //one too many off) so add a day back on
  END;

  //Days is now between 1 and 365 (or 366 in the case of a leap year)
  Months := 0;
  Day_Count := 31;
  FOR n := 1 TO 12    //Iterate through the months until we find the one we need
  DO                  //taking days off as we progress through the year
  BEGIN
    IF (Days <= Day_Count)
    THEN
    BEGIN
      IF Months = 0
      THEN
        Months := n;
    END
    ELSE
      Days := Days - Day_Count;

    CASE (n + 1) OF
      9, 4, 6, 11 : Day_Count := Day_Count + 30;
      1, 3, 5, 7, 8, 10, 12 : Day_Count := Day_Count + 31;
      2 : IF ((Years + 1970) MOD 4) = 0     //Handle leap years
          THEN
            Day_Count := Day_Count + 29
          ELSE
            Day_Count := Day_Count + 28;
    END;
  END;

  dtNew := EncodeDate(years + 1970, months, days + 1) + EncodeTime(hours, mins, secs, 0);
  Result := DateTimeToStr(dtNew);
end;

The Neil
0
 
egonoCommented:
The Neil - you dont like my function ? why ?
0
 
TheNeilCommented:
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 =:(
0
 
aptalaptalAuthor Commented:
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.


0
 
egonoCommented:
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 ?
0
 
aptalaptalAuthor Commented:
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.


0
 
egonoCommented:
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

0
 
TheNeilCommented:
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
0
 
aptalaptalAuthor Commented:
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.
0
 
aptalaptalAuthor Commented:
bye until the next question.

take care:)
0
 
TheNeilCommented:
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
0
 
egonoCommented:
that's life - congratulation TheNeil, you worked hard for it :)
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 10
  • 9
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now