Skip to content

Commit 52a23de

Browse files
committed
made localtime call thread_safe for systems that do not have localtime_r
1 parent 1baa0e7 commit 52a23de

File tree

8 files changed

+56
-31
lines changed

8 files changed

+56
-31
lines changed

hardware/OpenZWave.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,7 +3430,11 @@ void COpenZWave::GetNodeValuesJson(const unsigned int homeID, const int nodeID,
34303430
std::string i_label = m_pManager->GetValueLabel(*ittValue);
34313431
std::string i_units = m_pManager->GetValueUnits(*ittValue);
34323432
std::string i_help = m_pManager->GetValueHelp(*ittValue);
3433-
char *szDate = asctime(localtime(&ittCmds->second.m_LastSeen));
3433+
3434+
struct tm timeinfo;
3435+
localtime_r(&ittCmds->second.m_LastSeen, &timeinfo);
3436+
3437+
char *szDate = asctime(&timeinfo);
34343438
root["result"][index]["config"][ivalue]["index"] = i_index;
34353439
root["result"][index]["config"][ivalue]["label"] = i_label;
34363440
root["result"][index]["config"][ivalue]["units"] = i_units;
@@ -3453,7 +3457,11 @@ void COpenZWave::GetNodeValuesJson(const unsigned int homeID, const int nodeID,
34533457
std::string i_label = m_pManager->GetValueLabel(*ittValue);
34543458
std::string i_units = m_pManager->GetValueUnits(*ittValue);
34553459
std::string i_help = m_pManager->GetValueHelp(*ittValue);
3456-
char *szDate = asctime(localtime(&ittCmds->second.m_LastSeen));
3460+
3461+
struct tm timeinfo;
3462+
localtime_r(&ittCmds->second.m_LastSeen, &timeinfo);
3463+
3464+
char *szDate = asctime(&timeinfo);
34573465
root["result"][index]["config"][ivalue]["index"] = i_index;
34583466
root["result"][index]["config"][ivalue]["label"] = i_label;
34593467
root["result"][index]["config"][ivalue]["units"] = i_units;

hardware/TE923Tool.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,13 @@ int CTE923Tool::get_te923_memdata( Te923DataSet_t *data )
430430
} else
431431
adr = data->__src;
432432

433-
time_t tm = time( NULL );
434-
struct tm *timeinfo = localtime( &tm );
435-
int sysyear = timeinfo->tm_year;
436-
int sysmon = timeinfo->tm_mon;
433+
time_t tm = mytime( NULL );
434+
struct tm timeinfo;
435+
localtime_r(&tm, &timeinfo);
436+
437+
= localtime( &tm );
438+
int sysyear = timeinfo.tm_year;
439+
int sysmon = timeinfo.tm_mon;
437440
unsigned char readretries=0;
438441
do
439442
{

main/Logger.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ void CLogger::Log(const _eLogLevel level, const char* logline, ...)
8989
// Get a timestamp
9090
struct timeval tv;
9191
gettimeofday(&tv, NULL);
92-
struct tm *tm;
93-
tm = localtime(&tv.tv_sec);
92+
93+
struct tm timeinfo;
94+
localtime_r(&tv.tv_sec, &timeinfo);
9495

9596
// create a time stamp string for the log message
9697
snprintf(szDate, sizeof(szDate), "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
@@ -100,7 +101,6 @@ void CLogger::Log(const _eLogLevel level, const char* logline, ...)
100101
// Get a timestamp
101102
SYSTEMTIME time;
102103
::GetLocalTime(&time);
103-
104104
// create a time stamp string for the log message
105105
sprintf_s(szDate, sizeof(szDate), "%04d-%02d-%02d %02d:%02d:%02d.%03d ", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);
106106
#endif

main/SQLHelper.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7127,13 +7127,12 @@ bool CSQLHelper::CheckDate(const std::string &sDate, int& d, int& m, int& y)
71277127
t.tm_isdst = -1;
71287128

71297129
time_t when = mktime(&t);
7130-
const struct tm *norm = localtime(&when);
7131-
if (norm == NULL)
7132-
return false;
7130+
struct tm norm;
7131+
localtime_r(&when, &norm);
71337132

7134-
return (norm->tm_mday == d &&
7135-
norm->tm_mon == m - 1 &&
7136-
norm->tm_year == y - 1900);
7133+
return (norm.tm_mday == d &&
7134+
norm.tm_mon == m - 1 &&
7135+
norm.tm_year == y - 1900);
71377136
}
71387137
return false;
71397138
}

main/WebServer.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,6 @@ namespace http {
19851985
localtime_r(&pNode->m_LastSeen, &loctime);
19861986
strftime(szDate, 80, "%Y-%m-%d %X", &loctime);
19871987

1988-
//char *szDate = asctime(localtime(&pNode->m_LastSeen));
19891988
root["result"][ii]["LastUpdate"] = szDate;
19901989

19911990
//Add configuration parameters here
@@ -12749,7 +12748,11 @@ namespace http {
1274912748
root["result"][ii]["Temperature"] = szTemp;
1275012749
}
1275112750
root["result"][ii]["Days"] = itt->Days;
12752-
char *pDate = asctime(localtime(&itt->startTime));
12751+
12752+
struct tm timeinfo;
12753+
localtime_r(&itt->startTime, &timeinfo);
12754+
12755+
char *pDate = asctime(&timeinfo);
1275312756
if (pDate != NULL)
1275412757
{
1275512758
pDate[strlen(pDate) - 1] = 0;

main/localtime_r.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
#include "localtime_r.h"
33

44
time_t m_lasttime=time(NULL);
5+
boost::mutex TimeMutex_;
56

67
#ifndef localtime_r
78
struct tm *localtime_r(const time_t *timep, struct tm *result)
89
{
10+
boost::lock_guard<boost::mutex> l(TimeMutex_);
911
#ifdef localtime_s
1012
localtime_s(timep, result);
1113
#else
12-
struct tm *s = localtime (timep);
14+
struct tm *s = localtime(timep);
1315
if (s == NULL)
1416
return NULL;
15-
*result = *s;
17+
memcpy(result, s, sizeof(struct tm));
1618
#endif
1719
return result;
1820
}

main/mainworker.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,13 +1404,14 @@ void MainWorker::DecodeRXMessage(const CDomoticzHardwareBase *pHardware, const u
14041404
// Get a timestamp
14051405
struct timeval tv;
14061406
gettimeofday(&tv, NULL);
1407-
struct tm *tm;
1408-
tm = localtime(&tv.tv_sec);
1407+
1408+
struct tm timeinfo;
1409+
localtime_r(&tv.tv_sec, &timeinfo);
14091410

14101411
// create a time stamp string for the log message
14111412
snprintf(szDate, sizeof(szDate), "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
1412-
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1413-
tm->tm_hour, tm->tm_min, tm->tm_sec, (int)tv.tv_usec / 1000);
1413+
timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,
1414+
timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, (int)tv.tv_usec / 1000);
14141415
#else
14151416
// Get a timestamp
14161417
SYSTEMTIME time;
@@ -9744,8 +9745,12 @@ bool MainWorker::SwitchModal(const std::string &idx, const std::string &status,
97449745
WriteToHardware(HardwareID,(const char*)&tsen,sizeof(tsen.EVOHOME1));
97459746

97469747
// convert now to string form
9747-
time_t now = time(0);
9748-
char *szDate = asctime(localtime(&now));
9748+
time_t now = mytime(NULL);
9749+
9750+
struct tm timeinfo;
9751+
localtime_r(&now, &timeinfo);
9752+
9753+
char *szDate = asctime(&timeinfo);
97499754
szDate[strlen(szDate)-1]=0;
97509755

97519756
WriteMessageStart();
@@ -10477,13 +10482,14 @@ void MainWorker::SetInternalSecStatus()
1047710482
// Get a timestamp
1047810483
struct timeval tv;
1047910484
gettimeofday(&tv, NULL);
10480-
struct tm *tm;
10481-
tm = localtime(&tv.tv_sec);
10485+
10486+
struct tm timeinfo;
10487+
localtime_r(&tv.tv_sec, &timeinfo);
1048210488

1048310489
// create a time stamp string for the log message
1048410490
snprintf(szDate, sizeof(szDate), "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
10485-
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10486-
tm->tm_hour, tm->tm_min, tm->tm_sec, (int)tv.tv_usec / 1000);
10491+
timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,
10492+
timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, (int)tv.tv_usec / 1000);
1048710493
#else
1048810494
// Get a timestamp
1048910495
SYSTEMTIME time;

smtpclient/SMTPClient.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <sstream>
66

77
#include "../main/Logger.h"
8+
#include "../main/localtime_r.h"
89
#include "../webserver/Base64.h"
910

1011
// Part of the Message Construction is taken from jwSMTP library
@@ -245,8 +246,11 @@ const std::string SMTPClient::MakeMessage()
245246
time_t t;
246247
time(&t);
247248
char timestring[128] = "";
248-
249-
if (strftime(timestring, 127, "Date: %a, %d %b %Y %H:%M:%S %Z", localtime(&t))) { // got the date
249+
250+
struct tm timeinfo;
251+
localtime_r(&t, &timeinfo);
252+
253+
if (strftime(timestring, 127, "Date: %a, %d %b %Y %H:%M:%S %Z", &timeinfo)) { // got the date
250254
ret += timestring;
251255
ret += "\r\n";
252256
}

0 commit comments

Comments
 (0)