How to get time in millise in C ++, like java

In Java you can do this:

long now = (new Date()).getTime(); 

How can I do the same thing, but in C ++?

+6
c ++ time
source share
7 answers

In standard C ++ there is no such method (in standard C ++ there is only a second precision, not a millisecond). You can do this in non-portable ways, but since you did not specify, I assume you want a portable solution. Best of all, I would say this is a function to raise microsec_clock :: local_time () .

+9
source share

Since C ++ 0x is awesome

 namespace sc = std::chrono; auto time = sc::system_clock::now(); // get the current time auto since_epoch = time.time_since_epoch(); // get the duration since epoch // I don't know what system_clock returns // I think it uint64_t nanoseconds since epoch // Either way this duration_cast will do the right thing auto millis = sc::duration_cast<sc::milliseconds>(since_epoch); long now = millis.count(); // just like java (new Date()).getTime(); 

This works with gcc 4.4+. Compile it with --std=c++0x . I don't know if VS2010 implements std::chrono .

+11
source share

I like to have the time_ms function defined as such:

 // Used to measure intervals and absolute times typedef int64_t msec_t; // Get current time in milliseconds from the Epoch (Unix) // or the time the system started (Windows). msec_t time_ms(void); 

The implementation below should work on both Windows and Unix-like systems.

 #if defined(__WIN32__) #include <windows.h> msec_t time_ms(void) { return timeGetTime(); } #else #include <sys/time.h> msec_t time_ms(void) { struct timeval tv; gettimeofday(&tv, NULL); return (msec_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; } #endif 

Please note that the time returned by the Windows branch is equal to milliseconds from the moment the system starts, and the time returned by the Unix branch from the millisecond since 1970. Thus, if you use this code, rely only on the differences between the moments, and not on the absolute time itself.

+5
source share

The C ++ standard does not have a time function accurate to the second.

However, almost every operating system does this. Therefore, you need to write OS-specific code.

Win32:
GetSystemTime()
GetSystemTimeAsFileTime()

Unix / POSIX:
gettimeofday()
clock_gettime()

+3
source share

You can try this code (get from the source code of the StockFish chess engine (GPL)):

 #include <iostream> #include <stdio> #if !defined(_WIN32) && !defined(_WIN64) // Linux - Unix # include <sys/time.h> typedef timeval sys_time_t; inline void system_time(sys_time_t* t) { gettimeofday(t, NULL); } inline long long time_to_msec(const sys_time_t& t) { return t.tv_sec * 1000LL + t.tv_usec / 1000; } #else // Windows and MinGW # include <sys/timeb.h> typedef _timeb sys_time_t; inline void system_time(sys_time_t* t) { _ftime(t); } inline long long time_to_msec(const sys_time_t& t) { return t.time * 1000LL + t.millitm; } #endif int main() { sys_time_t t; system_time(&t); long long currentTimeMs = time_to_msec(t); std::cout << "currentTimeMs:" << currentTimeMs << std::endl; getchar(); // wait for keyboard input } 
+2
source share

Boost has a useful library for this:

http://www.boost.org/doc/libs/1_43_0/doc/html/date_time.html

ptime microsec_clock :: local_time () or ptime second_clock :: local_time ()

0
source share

Java:

 package com.company; public class Main { public static void main(String[] args) { System.out.println(System.currentTimeMillis()); } } 

c++:

 #include <iostream> #include <windows.h> __int64 currentTimeMillis() { FILETIME f; ::GetSystemTimeAsFileTime(&f); __int64 nano = (__int64(f.dwHighDateTime) << 32LL) + __int64(f.dwLowDateTime); return (nano - 116444736000000000LL) / 10000; } int main() { printf("%lli\n ", currentTimeMillis()); return 0; 

}

0
source share

All Articles