[Webinar] Streamline your web hosting managementRegister Today


Time as a null-terminated string.

Posted on 1999-12-07
Medium Priority
Last Modified: 2012-06-21
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.

Question by:Richard2000
  • 2
LVL 86

Accepted Solution

jkr earned 200 total points
ID: 2261674
If you insist in using 'GetLocalTime()', the solution is

char acTime [ 256];

GetLocalTime ( &st);


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 */
             pActTime->tm_mon +1,

    strncpy( pcBuffer, acTimeStamp, MAX_TIMESTAMP +1);


Feel free to ask if you need more information!
LVL 86

Expert Comment

ID: 2261680
Oops, I almost forgot: You could of course also use '_strtime()'

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question