gmtime_r和gmtime_s的区别。

时间:2022-12-17 11:47:59

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

这两个函数的区别是什么?我用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

我知道gmtime_r是线程安全的(但如果从同一个线程调用多个时间,则不安全),但是我不理解gmtime_s。

2 个解决方案

#1


14  

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(3)是一个标准的SUSv2函数。在windows环境中,你能找到的最接近gmtime_r()的是gmtime_s(),它的参数是反向的:

  • gmtime_r(const time_t*, struct tm*)
  • gmtime_r(const time_t *,struct tm *)
  • gmtime_s(struct tm*, const time_t*)
  • 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.

基本上,它们都将时间值转换为tm结构。然后,gmtime_r返回一个指向该结构的指针(如果失败,则返回NULL),而gmtime_s如果成功则返回0,如果失败,则返回一个errno_t。

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

tm结构有以下的身体,可以从上面列出的两个文档中看到:

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 */
};

#2


8  

gmtime_r and localtime_r are standard POSIX functions.

gmtime_r和localtime_r是标准POSIX函数。

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和localtime函数不是线程安全的或可重入的,因为它们使用一个静态区域来存储结果,所以gmtime_r和localtime_r将指针指向应该存储结果的位置。

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.)

gmtime_s和localtime_s是由微软引入的,现在是C11的一部分,尽管非微软的支持是有限的。(详见下文。)

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.)

他们的主要目的是安全。它们是作为微软的安全CRT(安全C运行时)的一部分添加的。据我所知,在微软的CRT中,线程安全并不是一个与gmtime和localtime有关的问题,因为这些函数的静态输出区域已经在每个线程中分配了。相反,添加了gmtime_s和localtime_s来执行安全CRT的参数验证。(换句话说,它们检查参数是否为NULL,在这种情况下,它们调用错误处理。)

#1


14  

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(3)是一个标准的SUSv2函数。在windows环境中,你能找到的最接近gmtime_r()的是gmtime_s(),它的参数是反向的:

  • gmtime_r(const time_t*, struct tm*)
  • gmtime_r(const time_t *,struct tm *)
  • gmtime_s(struct tm*, const time_t*)
  • 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.

基本上,它们都将时间值转换为tm结构。然后,gmtime_r返回一个指向该结构的指针(如果失败,则返回NULL),而gmtime_s如果成功则返回0,如果失败,则返回一个errno_t。

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

tm结构有以下的身体,可以从上面列出的两个文档中看到:

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 */
};

#2


8  

gmtime_r and localtime_r are standard POSIX functions.

gmtime_r和localtime_r是标准POSIX函数。

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和localtime函数不是线程安全的或可重入的,因为它们使用一个静态区域来存储结果,所以gmtime_r和localtime_r将指针指向应该存储结果的位置。

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.)

gmtime_s和localtime_s是由微软引入的,现在是C11的一部分,尽管非微软的支持是有限的。(详见下文。)

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.)

他们的主要目的是安全。它们是作为微软的安全CRT(安全C运行时)的一部分添加的。据我所知,在微软的CRT中,线程安全并不是一个与gmtime和localtime有关的问题,因为这些函数的静态输出区域已经在每个线程中分配了。相反,添加了gmtime_s和localtime_s来执行安全CRT的参数验证。(换句话说,它们检查参数是否为NULL,在这种情况下,它们调用错误处理。)