Solved

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

Posted on 2008-09-30
7
1,734 Views
Last Modified: 2012-06-27
Hello Experts,
how can i get this time format?
for ex. 17.22.34

i need a string as result
0
Comment
Question by:kagami01
  • 4
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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

0
 
LVL 14

Expert Comment

by:wayside
Comment Utility
#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);

0
 

Author Comment

by:kagami01
Comment Utility
i can use even the results of strftime even with asctime??
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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.
0
 

Author Comment

by:kagami01
Comment Utility
so how to convert the output of strftime in a string??
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 140 total points
Comment Utility
>> so how to convert the output of strftime in a string??

strftime already places its output in a string. In my example, the string is called timestamp. You can output it by adding this for example :
time_t now = time(0);

char timestamp[10] = "";

strftime (timestamp, 10,"%H.%M.%S", localtime(&now));
 

std::cout << timestamp << std::endl;                     // <--- this

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now