Reputation: 39881
THe code below returns number like 14517044
:
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds Milliseconds;
unsigned long res = std::chrono::duration_cast<Milliseconds>(Clock::now().time_since_epoch()).count();
printf("epoch %lu", res);
But http://www.epochconverter.com/ site shows numbers like 1426143327
, where as it is in seconds. So the epoch with Milliseconds has 8 digits and epoch with seconds has 10 digits?
What is wrong in my code?
Upvotes: 0
Views: 210
Reputation: 106066
Beware - not all computers use the same date/time for their epoch. Anyway, your problem is truncation of the value to 32 bits... it needs more.
#include <cstdio>
#include <iostream>
#include <chrono>
int main()
{
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds Milliseconds;
auto res = std::chrono::duration_cast<Milliseconds>(Clock::now().time_since_epoch()).count();
printf("garbage %lu\n", res); // garbage
printf("epoch printf %lld\n", (long long)res);
std::cout << "epoch cout " << res << '\n';
}
Output:
garbage 202650458
epoch printf 1426131792730
epoch cout 1426131792730
See it run here
Upvotes: 3