Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-09-30
7
Medium Priority
?
1,974 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22606311
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
ID: 22606426
#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
ID: 22606631
i can use even the results of strftime even with asctime??
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 53

Expert Comment

by:Infinity08
ID: 22606977
>> 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
ID: 22607076
so how to convert the output of strftime in a string??
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 560 total points
ID: 22607111
>> 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
ID: 22607132
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

704 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