Daniel
Daniel

Reputation: 1263

C++ get time with milliseconds

I'm working on a project with MPI and I want to write a logging system.

I want to ask you guys what should I use to get the current time in milliseconds ?

Thanks

Upvotes: 3

Views: 22645

Answers (2)

eduffy
eduffy

Reputation: 40224

The MPI routine MPI_Wtime returns time in seconds, but usually has milisecond resolution. Call MPI_Wtick to make sure it turns 10e-3 or less.

Upvotes: 3

Cody Gray
Cody Gray

Reputation: 244702

If you want the current local date and time, call the GetLocalTime function.

If you want the current date and time in UTC format, call the GetSystemTime function.

Both of these functions accept a single parameter, a pointer to a SYSTEMTIME structure, which they fill in with the current date and time information.

After calling the function, you can read the time out of that structure. The member you're looking for would be wMilliseconds, which returns the current millisecond (somewhere between 0 and 999).

Sample code:

SYSTEMTIME st;
GetLocalTime(&st);
std::cout << std::setw(2) << st.wHour << ':'
          << std::setw(2) << st.wMinute << ':'
          << std::setw(2) << st.wSecond << '.'
          << std::setw(3) << st.wMilliseconds << '\n';

Upvotes: 16

Related Questions