关键词搜索

源码搜索 ×
×

localtime居然不可重入,踩坑了

发布2022-06-26浏览618次

详情内容

最近踩坑了,全都拜localtime所赐,因为localtime居然不可重入:

  1. ubuntu@VM-0-15-ubuntu:~$ man localtime
  2. CTIME(3) Linux Programmer's Manual CTIME(3)
  3. NAME
  4. asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r - transform date and time to broken-
  5. down time or ASCII
  6. SYNOPSIS
  7. #include <time.h>
  8. char *asctime(const struct tm *tm);
  9. char *asctime_r(const struct tm *tm, char *buf);
  10. char *ctime(const time_t *timep);
  11. char *ctime_r(const time_t *timep, char *buf);
  12. struct tm *gmtime(const time_t *timep);
  13. struct tm *gmtime_r(const time_t *timep, struct tm *result);
  14. struct tm *localtime(const time_t *timep);
  15. struct tm *localtime_r(const time_t *timep, struct tm *result);
  16. time_t mktime(struct tm *tm);
  17. Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
  18. asctime_r(), ctime_r(), gmtime_r(), localtime_r():
  19. _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
  20. DESCRIPTION
  21. The ctime(), gmtime() and localtime() functions all take an argument of data type time_t, which represents calendar time.
  22. When interpreted as an absolute time value, it represents the number of seconds elapsed since the Epoch, 1970-01-01
  23. 00:00:00 +0000 (UTC).
  24. The asctime() and mktime() functions both take an argument representing broken-down time, which is a representation sepa[m
  25. rated into year, month, day, and so on.
  26. Broken-down time is stored in the structure tm, which is defined in <time.h> as follows:
  27. struct tm {
  28. int tm_sec; /* Seconds (0-60) */
  29. int tm_min; /* Minutes (0-59) */
  30. int tm_hour; /* Hours (0-23) */
  31. int tm_mday; /* Day of the month (1-31) */
  32. int tm_mon; /* Month (0-11) */
  33. int tm_year; /* Year - 1900 */
  34. int tm_wday; /* Day of the week (0-6, Sunday = 0) */
  35. int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
  36. int tm_isdst; /* Daylight saving time */
  37. };

真的需要小心一点。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载