@@ -151,81 +151,23 @@ const char * TimeServer::current_time(time_t t)
151
151
return stmp.c_str ();
152
152
}
153
153
154
+ // the string date time need to be iso-8601
155
+ // the time zone part will be ignored
154
156
bool TimeServer::setTime (const char * stime)
155
157
{
156
158
String stmp = stime;
157
- String substmp;
158
159
struct tm tmstruct;
159
160
struct timeval time_val = {0 , 0 };
160
- int pos2;
161
- // make uniform separators
162
- stmp.replace (" #" ," -" );
163
- stmp.replace (" :" ," -" );
164
- // Search Year
165
- int pos = stmp.indexOf (" -" );
166
- if (pos == -1 ) {
167
- return false ;
168
- }
169
- substmp = stmp.substring (0 ,pos);
170
- if (substmp.length ()!=4 ) {
171
- return false ;
172
- }
173
- pos2=pos;
174
- tmstruct.tm_year = substmp.toInt () - 1900 ;
175
- // Search Month
176
- pos = stmp.indexOf (" -" ,pos2+1 );
177
- if (pos == -1 ) {
178
- return false ;
179
- }
180
- substmp = stmp.substring (pos2+1 ,pos);
181
- if ((substmp.toInt () > 11 ) || (substmp.toInt () <0 )) {
182
- return false ;
183
- }
184
- pos2=pos;
185
- tmstruct.tm_mon = substmp.toInt () - 1 ;
186
- // Search day
187
- pos = stmp.indexOf (" -" ,pos2+1 );
188
- if (pos == -1 ) {
189
- return false ;
190
- }
191
- substmp = stmp.substring (pos2+1 ,pos);
192
- if ((substmp.toInt () > 31 ) || (substmp.toInt () <1 )) {
193
- return false ;
194
- }
195
- pos2=pos;
196
- tmstruct.tm_mday = substmp.toInt ();
197
-
198
- // Search hour
199
- pos = stmp.indexOf (" -" , pos2+1 );
200
- if (pos == -1 ) {
201
- return false ;
202
- }
203
- substmp = stmp.substring (pos2+1 ,pos);
204
- if ((substmp.toInt () > 23 ) || (substmp.toInt () <0 )) {
205
-
206
- return false ;
207
- }
208
- pos2=pos;
209
- tmstruct.tm_hour = substmp.toInt ();
210
-
211
- // Search min
212
- pos = stmp.indexOf (" -" , pos2+1 );
213
- if (pos == -1 ) {
214
- return false ;
215
- }
216
- substmp = stmp.substring (pos2+1 ,pos);
217
- if ((substmp.toInt () > 59 ) || (substmp.toInt () < 0 )) {
218
- return false ;
219
- }
220
- tmstruct.tm_min = substmp.toInt ();
221
- // Search sec
222
- substmp = stmp.substring (pos+1 );
223
- if ((substmp.toInt () > 59 ) || (substmp.toInt () < 0 )) {
224
- return false ;
161
+ memset (&tmstruct, 0 , sizeof (struct tm ));
162
+ if (strptime (stime," %Y-%m-%dT%H:%M:%S" , &tmstruct)==nullptr ) {
163
+ // allow not to set seconds for lazy guys typing command line
164
+ if (strptime (stime," %Y-%m-%dT%H:%M" , &tmstruct)==nullptr ) {
165
+ return false ;
166
+ }
225
167
}
226
168
tmstruct.tm_isdst = 0 ; // ignore dst
227
- tmstruct.tm_sec = substmp.toInt ();
228
169
time_val.tv_sec = mktime (&tmstruct);
170
+ // try to setTime
229
171
if (settimeofday (&time_val,0 ) == -1 ) {
230
172
return false ;
231
173
}
0 commit comments