diff options
author | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2020-09-05 11:57:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-05 11:57:10 +0200 |
commit | 2163e81fea7aac048ee023ae8a5b2f01762d45a4 (patch) | |
tree | 755d5f9d4fb9f359c0d1c9e7b289bc35553f807a /drivers | |
parent | c4865921ec8f2a4f5d3440b2b2abe5dbe289a4b9 (diff) | |
parent | 4b3aec50c01ea079f4c303e2986484b4771dc918 (diff) |
Merge pull request #41763 from madmiraal/fix-linux-time
Make OS_Unix time calls thread safe.
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/unix/os_unix.cpp | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 3c212cadb8..6b98a344dc 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -171,52 +171,53 @@ double OS_Unix::get_unix_time() const { OS::Date OS_Unix::get_date(bool utc) const { time_t t = time(nullptr); - struct tm *lt; + struct tm lt; if (utc) { - lt = gmtime(&t); + gmtime_r(&t, <); } else { - lt = localtime(&t); + localtime_r(&t, <); } Date ret; - ret.year = 1900 + lt->tm_year; + ret.year = 1900 + lt.tm_year; // Index starting at 1 to match OS_Unix::get_date // and Windows SYSTEMTIME and tm_mon follows the typical structure // of 0-11, noted here: http://www.cplusplus.com/reference/ctime/tm/ - ret.month = (Month)(lt->tm_mon + 1); - ret.day = lt->tm_mday; - ret.weekday = (Weekday)lt->tm_wday; - ret.dst = lt->tm_isdst; + ret.month = (Month)(lt.tm_mon + 1); + ret.day = lt.tm_mday; + ret.weekday = (Weekday)lt.tm_wday; + ret.dst = lt.tm_isdst; return ret; } OS::Time OS_Unix::get_time(bool utc) const { time_t t = time(nullptr); - struct tm *lt; + struct tm lt; if (utc) { - lt = gmtime(&t); + gmtime_r(&t, <); } else { - lt = localtime(&t); + localtime_r(&t, <); } Time ret; - ret.hour = lt->tm_hour; - ret.min = lt->tm_min; - ret.sec = lt->tm_sec; + ret.hour = lt.tm_hour; + ret.min = lt.tm_min; + ret.sec = lt.tm_sec; get_time_zone_info(); return ret; } OS::TimeZoneInfo OS_Unix::get_time_zone_info() const { time_t t = time(nullptr); - struct tm *lt = localtime(&t); + struct tm lt; + localtime_r(&t, <); char name[16]; - strftime(name, 16, "%Z", lt); + strftime(name, 16, "%Z", <); name[15] = 0; TimeZoneInfo ret; ret.name = name; char bias_buf[16]; - strftime(bias_buf, 16, "%z", lt); + strftime(bias_buf, 16, "%z", <); int bias; bias_buf[15] = 0; sscanf(bias_buf, "%d", &bias); |