Link to home
Start Free TrialLog in
Avatar of kagami01
kagami01

asked on

How to get in C++ this time format HH.MM.SS?

Hello Experts,
how can i get this time format?
for ex. 17.22.34

i need a string as result
Avatar of Infinity08
Infinity08
Flag of Belgium image

You can use strftime :

        http://www.cplusplus.com/reference/clibrary/ctime/strftime.html
time_t now = time(0);
char timestamp[10] = "";
strftime (timestamp, 10,"%H.%M.%S", localtime(&now));

Open in new window

Avatar of wayside
wayside

#include <sys/types.h>
#include <sys/timeb.h>

struct tm *current_time;

current_time = localtime(time());

char timebuf[20] = { 0 } ;

strftime(timebuf, 20, "%H.%M.%S", current_time);

Avatar of kagami01

ASKER

i can use even the results of strftime even with asctime??
>> i can use even the results of strftime even with asctime??

asctime is another (fixed) way of formatting a time into a string. strftime allows you to choose the format, so you'll need strftime as I showed.
so how to convert the output of strftime in a string??
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

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
Or if you mean you want it as an std::string, you can always create one from the timestamp buffer :
time_t now = time(0);
char timestamp_buf[10] = "";
strftime (timestamp_buf, 10,"%H.%M.%S", localtime(&now));
 
std::string timestamp = std::string(timestamp_buf);
std::cout << timestamp << std::endl;

Open in new window