How to get the current timestamp in milliseconds since 1970 exactly the way Java gets

In Java, we can use System.currentTimeMillis() to get the current timestamp in milliseconds from a point in time that is -

the difference, in milliseconds, between the current time and midnight, January 1, 1970 UTC.

In C ++, how to get the same thing?

I am currently using this to get the current timestamp -

 struct timeval tp; gettimeofday(&tp, NULL); long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds cout << ms << endl; 

Does it look right or not?

+104
c ++ timestamp
Oct 24 '13 at 1:08
source share
6 answers

If you have access to C ++ 11 libraries, check out the std::chrono . You can use it to get milliseconds since Unix Epoch:

 #include <chrono> // ... using namespace std::chrono; milliseconds ms = duration_cast< milliseconds >( system_clock::now().time_since_epoch() ); 
+187
Oct 24 '13 at 1:28
source share

use <sys/time.h>

 struct timeval tp; gettimeofday(&tp, NULL); long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; 

refer to this .

+34
Oct 24 '13 at 1:18
source share

If you use gettimeofday, you need to cast longer, otherwise you will get an overflow and therefore not a real number of milliseconds from the era: long int msint = tp.tv_sec * 1000 + tp.tv_usec / 1000; will give you a number like 767990892, which is 8 days after the era; -).

 int main(int argc, char* argv[]) { struct timeval tp; gettimeofday(&tp, NULL); long long mslong = (long long) tp.tv_sec * 1000L + tp.tv_usec / 1000; //get current timestamp in milliseconds std::cout << mslong << std::endl; } 
+12
Dec 09 '14 at 6:42
source share

This answer is very similar to Oz. Uses <chrono> for C ++ - I have not received it from Oz. though...

I took the original snippet at the bottom of this page and slightly modified it to become a full-fledged console application. I love to use this little thing. This is great if you do a lot of scripting and need a reliable tool in Windows to get the era in milliseconds without resorting to using VB or some less modern, less readable code.

 #include <chrono> #include <iostream> int main() { unsigned __int64 now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); std::cout << now << std::endl; return 0; } 
+12
Jan 11 '17 at 4:53 on
source share

Starting with C ++ 11 you can use std::chrono :

  • get current system time: std::chrono::system_clock::now()
  • get time from era: .time_since_epoch()
  • translate base unit to milliseconds: duration_cast<milliseconds>(d)
  • translate std::chrono::milliseconds to an integer ( uint64_t to avoid overflow)
 #include <chrono> #include <cstdint> #include <iostream> uint64_t timeSinceEpochMillisec() { using namespace std::chrono; return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); } int main() { std::cout << timeSinceEpochMillisec() << std::endl; return 0; } 
0
May 13 '19 at 7:39
source share

Turn on <ctime> and use the time function.

-21
Oct 24 '13 at 1:10
source share



All Articles