BrainStone
BrainStone

Reputation: 3195

C++ Equivalent for GetLocalTime in Linux (with milliseconds!)

I have been searching for over an hour but I simply seem to not be able to find the solution!

I am looking for a function that gives me a similar struct as GetLocalTime on Windows does. The important thing for me is that this struct has hours, minutes, seconds and milliseconds.

localtime() does not include milliseconds and therefore I cannot use it!

I would apprechiate a solution that uses the standard library or another very small library since I am working on a Raspberry Pi and connot use large libraries like boost!

Upvotes: 2

Views: 7540

Answers (3)

Bruno Bonnefont
Bruno Bonnefont

Reputation: 121

You can use a combination of:

  • clock_gettime(CLOCK_REALTIME); returns local time, up to millisecond (of course constrained by the actual clock resolution); does not care of local timezone information, returns UTC time. Just use millisecond information (from tv_nsec field).
  • time(); returns local time, up to the second - no millisecond - also UTC time. time() results (a time_t) is easy to convert to the final format.

  • then convert time() result using localtime_r(); this sets up a structure very similar to Windows SYSTEMTIME; result is up to the second, and takes into account local timezone information.

  • finally set up the millisecond field using clock_gettime() results.

These routines are documented, not deprecated, portable.
You may need to call tzset() once (this sets the timezone information - a C global variable - from operating system environment - probably a heavy operation).

Upvotes: 1

Vitalii
Vitalii

Reputation: 4793

As it was mentioned above, there are not direct equivalent. If you can use C++ 11, <chrono> header allows to get the same result, but not in single call. You can use high_resolution_clock to get current Unix time in milliseconds, then you can get localtime C function to get time without milliseconds, and use current Unix time in milleseconds to find milliseconds count. It looks like you will have to write your own GetLocalTime implementation, but with C++ 11 it will not be complex.

Upvotes: 2

GetLocalTime is not a usual Linux function.

Read time(7), you probably want clock_gettime(2), or (as commented by Joachim Pileborg), the older gettimeofday(2)

If you need some struct giving all of hours, minutes, seconds, milliseconds you have to code that yourself using localtime(3) and explicitly computing the millisecond part.

Something like the below code is printing the time with milliseconds

 struct timespec ts = {0,0};
 struct tm tm = {};
 char timbuf[64];
 if (clock_gettime(CLOCK_REALTIME, &ts))
    { perror("clock_gettime"), exit(EXIT_FAILURE);};
 time_t tim = ts.tv_sec;
 if (localtime(&tim, &tm))
    { perror("localtime"), exit(EXIT_FAILURE);};
 if (strftime(timbuf, sizeof(timbuf), "%D %T", &tm))
     { perror("strftime"), exit(EXIT_FAILURE);};
 printf("%s.%03d\n", timbuf, (int)(ts.tv_nsec/1000000));

Upvotes: 1

Related Questions