Skip to content

Commit da93905

Browse files
committed
getTimeFromString use sscanf
1 parent 7489163 commit da93905

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/utility/time/TimeService.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
#include "TimeService.h"
2323

2424
#include <time.h>
25-
#include <iomanip>
26-
#include <sstream>
25+
2726
#include "NTPUtils.h"
2827

2928
/**************************************************************************************
@@ -107,15 +106,35 @@ unsigned long TimeService::getLocalTime()
107106

108107
unsigned long TimeService::getTimeFromString(const String& timeString)
109108
{
110-
struct tm t;
111-
std::istringstream ss(timeString.c_str());
112-
ss >> std::get_time(&t, "%Y %b %d %H:%M:%S");
113-
114-
if(!ss) {
115-
return 0;
116-
} else {
117-
return mktime(&t);
118-
}
109+
char s_month[5];
110+
int month, day, year, hour, minutes, seconds;
111+
struct tm t =
112+
{
113+
0 /* tm_sec */,
114+
0 /* tm_min */,
115+
0 /* tm_hour */,
116+
0 /* tm_mday */,
117+
0 /* tm_mon */,
118+
0 /* tm_year */,
119+
0 /* tm_wday */,
120+
0 /* tm_yday */,
121+
0 /* tm_isdst */
122+
};
123+
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
124+
125+
sscanf(timeString.c_str(), "%d %s %d %d:%d:%d", &year, s_month, &day, &hour, &minutes, &seconds);
126+
127+
month = (strstr(month_names, s_month) - month_names) / 3;
128+
129+
t.tm_mon = month;
130+
t.tm_mday = day;
131+
t.tm_year = year - 1900;
132+
t.tm_hour = hour;
133+
t.tm_min = minutes;
134+
t.tm_sec = seconds;
135+
t.tm_isdst = -1;
136+
137+
return mktime(&t);
119138
}
120139

121140
/**************************************************************************************

src/utility/time/TimeService.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
**************************************************************************************/
2424

2525
#include <Arduino_ConnectionHandler.h>
26-
#include <string>
2726

2827

2928
#ifdef ARDUINO_ARCH_SAMD

0 commit comments

Comments
 (0)