@@ -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 **************************************************************************************/
0 commit comments