gmtime_r 和 gmtime_s 的区别 [英] difference between gmtime_r and gmtime_s
问题描述
这两个函数有什么区别?我使用的是 MinGW 4.8.0.
Which is the difference between these two functions? I'm using MinGW 4.8.0.
我知道 gmtime_r
是线程安全的(但如果从同一线程多次调用则不安全)但我不明白 gmtime_s
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(3)
是一个标准SUSv2函数.在 Windows 环境中,最接近 gmtime_r()
的是 gmtime_s()
,它的参数颠倒了:
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*)
基本上,它们都将时间值转换为 tm
结构.gmtime_r
然后返回指向该结构的指针(如果失败则返回 NULL
),而 gmtime_s
如果成功则返回 0
,和 errno_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
结构具有以下主体,从上面列出的两个文档中可以看出:
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 */
};
这篇关于gmtime_r 和 gmtime_s 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!