I have tried this and other codes I found online but they did not work. My IDE is Xcode. Edit: When I tried the code on the link, the variable long long microseconds always returned 0. I would like to print the timestamp in this manner: (hour:minute:microseconds). For example, 15:17:09:134613464312.
Asked
Active
Viewed 1,352 times
0
-
*How* did it now work? What problems did you have? What errors, if any? If it build correctly, without warning, what result did you expect and what did you get? – Some programmer dude Jun 29 '15 at 05:38
-
1*"the did not work"* - what happened when you tried them? Did you get a compiler error message (and if so what), or a dubious runtime value? BTW /- for the linked solution, you'll need a C++11-capable compiler with C++11 support enabled. For "and other codes" - you should also describe them? What's the use of people here wasting their time to provide you with the same answer you've already tried? If you show what you've tried, they might see your mistake and offer some targeted advice. – Tony Delroy Jun 29 '15 at 05:38
-
Please check this. http://stackoverflow.com/questions/22203319/c-c-microsecond-timestamp – Satish Chalasani Jun 29 '15 at 05:38
2 Answers
1
Expanding on the previous answer here's what the OP was asking:
#include <iostream>
#include <chrono>
#include <ctime>
std::string ts() {
using namespace std;
using namespace std::chrono;
using days = duration<int, ratio_multiply<hours::period, ratio<24> >::type>;
high_resolution_clock::time_point now = high_resolution_clock::now();
high_resolution_clock::duration tp = now.time_since_epoch();
days d = duration_cast<days>(tp);
tp -= d;
hours h = duration_cast<hours>(tp);
tp -= h;
minutes m = duration_cast<minutes>(tp);
tp -= m;
seconds s = duration_cast<seconds>(tp);
tp -= s;
milliseconds ms = duration_cast<milliseconds>(tp);
tp -= ms;
microseconds us = duration_cast<microseconds>(tp);
tp -= us;
nanoseconds ns = duration_cast<nanoseconds>(tp);
tp -= ns;
std::ostringstream oss;
oss << std::setfill('0')
<< std::setw(2) << h.count() << ":"
<< std::setw(2) << m.count() << ":"
<< std::setw(2) << s.count() << "."
<< std::setw(3) << ms.count()
<< std::setw(3) << us.count()
<< std::setw(3) << ns.count();
return oss.str();
}
void main ()
{
std::cout << ts() << std::endl;
std::cout << ts() << std::endl;
}
//output:
//22:43:00.295605757
//22:43:00.295640327

L8Cod3r
- 60
- 6
0
The method you use is correct.
If you the code between the two now() calls lasts less than a microseconds, microseconds will always be zero, since the number is not a floating point. If you have always zero, that means you need higher resolution (try nanoseconds).
Btw, if you simply want timestamp, you don't want to use this code since it is done to compute elapsed time between two time points. You can try something like this:
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>
(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
Which is really a timestamp and not a duration.
EDIT: To have the current microseconds count, you can do something like this:
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std;
using namespace std::chrono;
using days = duration<int, ratio_multiply<hours::period, ratio<24> >::type>;
int main() {
system_clock::time_point now = system_clock::now();
system_clock::duration tp = now.time_since_epoch();
days d = duration_cast<days>(tp);
tp -= d;
hours h = duration_cast<hours>(tp);
tp -= h;
minutes m = duration_cast<minutes>(tp);
tp -= m;
seconds s = duration_cast<seconds>(tp);
tp -= s;
cout << tp.count() << "\n";
return 0;
}
This will print the current microseconds count.
Hours:Minutes:Seconds is pretty easy.

Baptiste Wicht
- 7,472
- 7
- 45
- 110
-
Thank you for the quick answer. I would like to show it in this manner: (hour:minute:microseconds). For example, 15:17:09:134613464312. I would appreciate it if you also could tell me how to do it. I am sorry for not being precise. – stacktest Jun 29 '15 at 06:31
-
You want a timestamp or the current time ? It is not really the same thing. If you want to display a timestamp, there is no point in display hours since it is more that 45 years. – Baptiste Wicht Jun 29 '15 at 06:44
-
I am sorry for the confusion. I want the precise current time as in the example. – stacktest Jun 29 '15 at 06:52