Skip to content

Commit c035af8

Browse files
committed
Add getTimeFromString() function to convert an input string into a Schedule timestamp
1 parent 394da9a commit c035af8

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/utility/time/TimeService.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,66 @@ unsigned long TimeService::getLocalTime()
108108
return utc + _timezone_offset;
109109
}
110110

111+
unsigned long TimeService::getTimeFromString(const String& input)
112+
{
113+
struct tm t =
114+
{
115+
0 /* tm_sec */,
116+
0 /* tm_min */,
117+
0 /* tm_hour */,
118+
0 /* tm_mday */,
119+
0 /* tm_mon */,
120+
0 /* tm_year */,
121+
0 /* tm_wday */,
122+
0 /* tm_yday */,
123+
0 /* tm_isdst */
124+
};
125+
126+
char s_month[16];
127+
int month, day, year, hour, min, sec;
128+
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
129+
static const int expected_length = 20;
130+
static const int expected_parameters = 6;
131+
132+
if(input == nullptr || input.length() != expected_length)
133+
{
134+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s invalid input length", __FUNCTION__);
135+
return 0;
136+
}
137+
138+
int scanned_parameters = sscanf(input.c_str(), "%d %s %d %d:%d:%d", &year, s_month, &day, &hour, &min, &sec);
139+
140+
if(scanned_parameters != expected_parameters)
141+
{
142+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s invalid input parameters number", __FUNCTION__);
143+
return 0;
144+
}
145+
146+
char * s_month_position = strstr(month_names, s_month);
147+
148+
if(s_month_position == nullptr || strlen(s_month) != 3) {
149+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s invalid month name, use %s", __FUNCTION__, month_names);
150+
return 0;
151+
}
152+
153+
month = (s_month_position - month_names) / 3;
154+
155+
if(month < 0 || month > 11 || day < 1 || day > 31 || year < 1900 || hour < 0 ||
156+
hour > 24 || min < 0 || min > 60 || sec < 0 || sec > 60) {
157+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s invalid date values", __FUNCTION__);
158+
return 0;
159+
}
160+
161+
t.tm_mon = month;
162+
t.tm_mday = day;
163+
t.tm_year = year - 1900;
164+
t.tm_hour = hour;
165+
t.tm_min = min;
166+
t.tm_sec = sec;
167+
t.tm_isdst = -1;
168+
169+
return mktime(&t);
170+
}
111171
/**************************************************************************************
112172
* PRIVATE MEMBER FUNCTIONS
113173
**************************************************************************************/

src/utility/time/TimeService.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#include <Arduino_ConnectionHandler.h>
2626

27-
2827
#ifdef ARDUINO_ARCH_SAMD
2928
#include <RTCZero.h>
3029
#endif
@@ -49,6 +48,10 @@ class TimeService
4948
unsigned long getTime();
5049
unsigned long getLocalTime();
5150
void setTimeZoneData(long offset, unsigned long valid_until);
51+
/* Helper function to convert an input String into a UNIX timestamp.
52+
* The input String format must be as follow "2021 Nov 01 17:00:00"
53+
*/
54+
static unsigned long getTimeFromString(const String& input);
5255

5356
private:
5457

0 commit comments

Comments
 (0)