20

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

Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
  • possible duplicate of [What is the Windows equivalent of the Unix function gmtime\_r?](http://stackoverflow.com/questions/12044519/what-is-the-windows-equivalent-of-the-unix-function-gmtime-r) – o11c Jun 30 '15 at 03:09

2 Answers2

23

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 */
};
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
  • Regarding gmtime_s: as from what I can see, the Windows the standard localtime and gmtime functions are already thread-safe, so yes – Natan Streppel Sep 27 '13 at 13:55
  • as for regarding gmtime_r, [taken from here](http://linux.die.net/man/3/gmtime_r): "The four functions asctime(), ctime(), gmtime() and localtime() return a pointer to static data and hence are not thread-safe. Thread-safe versions asctime_r(), ctime_r(), gmtime_r() and localtime_r() are specified by SUSv2" – Natan Streppel Sep 27 '13 at 14:12
  • I know that on Windows that functions are thread safe ( they use thread local storage ) and I know that on posix they're not, so posix provide `*_r` alternative. Now I'm using MinGW 4.8.0 (posix, dw2) that provides both `*_r` and `*_s`. MinGW links to msvcrt.dll (on Windows Xp is at version 7.0.2600.5512 ). So I'm not sure – Elvis Dukaj Sep 27 '13 at 14:27
  • @Streppel: Just one note: I'm pretty sure SUS and POSIX are two different (if related) things. – Tim Čas Feb 11 '15 at 17:30
11

gmtime_r and localtime_r are standard POSIX functions.

Their main purpose is thread safety (reentrancy). The basic gmtime and localtime functions aren't thread-safe or reentrant, because they use a single static area to store their results, so gmtime_r and localtime_r take pointers to where the results should be stored.

gmtime_s and localtime_s were introduced by Microsoft and are now part of C11, although non-Microsoft support is limited. (See here for further discussion.)

Their main purpose is security. They were added as part of Microsoft's Secure CRT (Secure C Run-Time). From what I understand, thread safety isn't an issue with gmtime and localtime in Microsoft's CRT, since these functions' static output areas are already allocated per thread. Instead, gmtime_s and localtime_s were added to do the Secure CRT's parameter validation. (In other words, they check if their parameters are NULL, in which case they invoke error handling.)

Community
  • 1
  • 1
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • `gmtime_s` and `localtime_s` are in C11 too. –  Oct 20 '16 at 07:47
  • @FaTony - I had forgotten that. Thank you for the info; I updated my answer. – Josh Kelley Oct 20 '16 at 12:53
  • While documented as not thread-safe, `gmtime` and `localtime` are commonly implemented using thread-local storage and thus are effectively thread-safe. However, they are not reentrant. – user686249 Dec 06 '21 at 11:00
  • Just looked at the glibc implementation and apparently I recalled incorrectly. The storage is not allocated thread-locally. – user686249 Dec 06 '21 at 11:27