Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between gmtime_r and gmtime_s

Which is the difference between these two functions? I'm using MinGW 4.8.0.

I know that gmtime_r is thread safe ( but not secure if called multiple time from the same thread) but I don't understand gmtime_s

like image 716
Elvis Dukaj Avatar asked Sep 27 '13 13:09

Elvis Dukaj


People also ask

What is Gmtime_r?

The gmtime_r() function breaks down the time value, in seconds, and stores it in result. result is a pointer to the tm structure, defined in <time. h>. The value time is usually obtained by a call to the time() function.

Is Gmtime_r thread-safe?

The gmtime_r() function is thread-safe and returns values in a user-supplied buffer instead of possibly using a static data area that may be overwritten by each call.

What is gmtime() function?

Description. The gmtime() function breaks down the time value, in seconds, and stores it in a tm structure, defined in <time. h>.

What is C++ Gmtime?

The gmtime() function in C++ converts the given time since epoch to calendar time which is expressed as UTC time rather than local time. The gmtime() is defined in <ctime> header file.


1 Answers

The difference is that gmtime_r(3) is a standard SUSv2 function. The closest you can find to gmtime_r() on a windows environment is gmtime_s(), which has its arguments reversed:

  • gmtime_r(const time_t*, struct tm*)
  • gmtime_s(struct tm*, const time_t*)

Basically, they both convert a time value to a tm structure. gmtime_r then return a pointer to this structure (or NULL if failed), whereas gmtime_s returns 0 if successful, and a errno_t in case of failure.

The tm structure has the following body, as can be seen from both docs listed above:

struct tm {
    int tm_sec;         /* seconds */
    int tm_min;         /* minutes */
    int tm_hour;        /* hours */
    int tm_mday;        /* day of the month */
    int tm_mon;         /* month */
    int tm_year;        /* year */
    int tm_wday;        /* day of the week */
    int tm_yday;        /* day in the year */
    int tm_isdst;       /* daylight saving time */
};
like image 179
Natan Streppel Avatar answered Sep 22 '22 10:09

Natan Streppel