Time as a null-terminated string.

How do I get the local time in the format HH:MM:SS into a null-terminated string using MS Visual C++ 4?  I know how to use GetLocalTime() to fill up the structure, but how do I convert the hours, minutes and seconds into a simple null-terminated string separated by colons and append the terminating zero?  Could you please post some C++ source code which will do this.

Thanks
Richard2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
If you insist in using 'GetLocalTime()', the solution is

SYSTEMTIME st;
char acTime [ 256];

GetLocalTime ( &st);

  sprintf(acTime,"%d:%d:%d\n",st.wHour,st.wMinute,st.wSecond);


Or you could use

BOOL GetTimeStamp( char *pcBuffer, unsigned short usBufSize)
{
 time_t         Sec70;
 struct tm     *pActTime;

 char           acTimeStamp[MAX_TIMESTAMP +1];

    if ( MAX_TIMESTAMP +1 < usBufSize ) return(FALSE);

    time( &Sec70); /* Get time in seconds since 0:00am 1.1.1970 */

    pActTime = localtime( &Sec70); /* put it in a more reasonable format */

    sprintf( acTimeStamp,       /* arrange the interesting values in a string */
             "%.2d%.2d%.2d%.2d%.2d%.2d",
             pActTime->tm_year,
             pActTime->tm_mon +1,
             pActTime->tm_mday,
             pActTime->tm_hour,
             pActTime->tm_min,
             pActTime->tm_sec);

    strncpy( pcBuffer, acTimeStamp, MAX_TIMESTAMP +1);

 return(TRUE);
}

Feel free to ask if you need more information!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Oops, I almost forgot: You could of course also use '_strtime()'
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.