From e73b5e85c88e3ff03d40df350702f273f431e2cc Mon Sep 17 00:00:00 2001 From: Luc <8822552+luc-github@users.noreply.github.com> Date: Sun, 11 Sep 2022 16:37:44 +0800 Subject: [PATCH] Use iso-8601 to set time but time zone like suggested by @terjeio for [ESP800/140] Simplify string parsing to set date/time using strptime --- docs/Commands.md | 4 +- esp3d/src/core/espcmd/ESP0.cpp | 4 +- esp3d/src/core/espcmd/ESP140.cpp | 4 +- esp3d/src/core/espcmd/ESP800.cpp | 2 +- esp3d/src/modules/time/time_server.cpp | 76 +++----------------------- 5 files changed, 16 insertions(+), 74 deletions(-) diff --git a/docs/Commands.md b/docs/Commands.md index 7ac96ab4..dd3df46d 100644 --- a/docs/Commands.md +++ b/docs/Commands.md @@ -71,7 +71,7 @@ The json format is { `[ESP131] json= pwd=` * Sync / Set / Get current time - `[ESP140] json= pwd=` + `[ESP140] json= pwd=` * Get/Set display/set boot delay in ms / Verbose boot `[ESP150]pwd=` @@ -284,7 +284,7 @@ label can be: light/framesize/quality/contrast/brightness/saturation/gainceiling `[ESP790]= json= pwd=` * FW Informations - `[ESP800]json= pwd=` + `[ESP800]json= pwd= ` * Get state / Set Enable / Disable Serial Communication `[ESP900] json= pwd=` diff --git a/esp3d/src/core/espcmd/ESP0.cpp b/esp3d/src/core/espcmd/ESP0.cpp index ff722496..074cfb41 100644 --- a/esp3d/src/core/espcmd/ESP0.cpp +++ b/esp3d/src/core/espcmd/ESP0.cpp @@ -59,7 +59,7 @@ const char * help[]= {"[ESP] (id) - display this help", "[ESP131](Port) - display/set Telnet port", #endif //TELNET_FEATURE #if defined(TIMESTAMP_FEATURE) - "[ESP140](SYNC) (srv1=xxxx) (srv2=xxxx) (srv3=xxxx) (zone=xxx) (dst=YES/NO) (time=YYYY-MM-DD#H24:MM:SS) (SYNC) (NOW)- sync/display/set current time/time servers", + "[ESP140](SYNC) (srv1=xxxx) (srv2=xxxx) (srv3=xxxx) (zone=xxx) (dst=YES/NO) (time=YYYY-MM-DDTHH:mm:ss) (SYNC) (NOW)- sync/display/set current time/time servers", #endif //TIMESTAMP_FEATURE "[ESP150](delay=time) (verbose=ON/OFF)- display/set boot delay in ms / Verbose boot", #if defined(WS_DATA_FEATURE) @@ -147,7 +147,7 @@ const char * help[]= {"[ESP] (id) - display this help", "[ESP780](path) - List Global Filesystem", "[ESP790](Action)=(path) - rmdir / remove / mkdir / exists / create on Global Filesystem (path)", #endif //GLOBAL_FILESYSTEM_FEATURE - "[ESP800](time=YYYY-MM-DD-HH-MM-SS) - display FW Informations in plain/JSON", + "[ESP800](time=YYYY-MM-DDTHH:mm:ss)(version=3.0.0-a11)(setup=0/1) - display FW Informations /set time", #if COMMUNICATION_PROTOCOL != SOCKET_SERIAL "[ESP900](ENABLE/DISABLE) - display/set serial state", "[ESP901] - display/set serial baud rate", diff --git a/esp3d/src/core/espcmd/ESP140.cpp b/esp3d/src/core/espcmd/ESP140.cpp index 1b6321c1..baf7f483 100644 --- a/esp3d/src/core/espcmd/ESP140.cpp +++ b/esp3d/src/core/espcmd/ESP140.cpp @@ -26,12 +26,12 @@ #include "../../modules/time/time_server.h" #define COMMANDID 140 //Sync / Set / Get current time -//[ESP140] NOW json= pwd= +//[ESP140] NOW json= pwd= bool Commands::ESP140(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) { bool noError = true; bool json = has_tag (cmd_params, "json"); - String response; + String response="ok"; String parameter; bool hasParam = false; int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead diff --git a/esp3d/src/core/espcmd/ESP800.cpp b/esp3d/src/core/espcmd/ESP800.cpp index ff3fe013..2aafecc3 100644 --- a/esp3d/src/core/espcmd/ESP800.cpp +++ b/esp3d/src/core/espcmd/ESP800.cpp @@ -46,7 +46,7 @@ //get fw version firmare target and fw version //eventually set time with pc time //output is JSON or plain text according parameter -//[ESP800]json= +//[ESP800]json= bool Commands::ESP800(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) { bool noError = true; diff --git a/esp3d/src/modules/time/time_server.cpp b/esp3d/src/modules/time/time_server.cpp index 40965195..b7f51bec 100644 --- a/esp3d/src/modules/time/time_server.cpp +++ b/esp3d/src/modules/time/time_server.cpp @@ -151,81 +151,23 @@ const char * TimeServer::current_time(time_t t) return stmp.c_str(); } +//the string date time need to be iso-8601 +//the time zone part will be ignored bool TimeServer::setTime(const char* stime) { String stmp = stime; - String substmp; struct tm tmstruct; struct timeval time_val = {0, 0}; - int pos2; - //make uniform separators - stmp.replace("#","-"); - stmp.replace(":","-"); - //Search Year - int pos = stmp.indexOf("-"); - if (pos == -1) { - return false; - } - substmp = stmp.substring(0,pos); - if (substmp.length()!=4) { - return false; - } - pos2=pos; - tmstruct.tm_year = substmp.toInt() - 1900; - //Search Month - pos = stmp.indexOf("-",pos2+1); - if (pos == -1) { - return false; - } - substmp = stmp.substring(pos2+1,pos); - if ((substmp.toInt() > 11) || (substmp.toInt() <0 )) { - return false; - } - pos2=pos; - tmstruct.tm_mon = substmp.toInt() - 1; - //Search day - pos = stmp.indexOf("-",pos2+1); - if (pos == -1) { - return false; - } - substmp = stmp.substring(pos2+1,pos); - if ((substmp.toInt() > 31) || (substmp.toInt() <1 )) { - return false; - } - pos2=pos; - tmstruct.tm_mday = substmp.toInt(); - - //Search hour - pos = stmp.indexOf("-", pos2+1); - if (pos == -1) { - return false; - } - substmp = stmp.substring(pos2+1,pos); - if ((substmp.toInt() > 23) || (substmp.toInt() <0 )) { - - return false; - } - pos2=pos; - tmstruct.tm_hour = substmp.toInt(); - - //Search min - pos = stmp.indexOf("-", pos2+1); - if (pos == -1) { - return false; - } - substmp = stmp.substring(pos2+1,pos); - if ((substmp.toInt() > 59) || (substmp.toInt() < 0 )) { - return false; - } - tmstruct.tm_min = substmp.toInt(); - //Search sec - substmp = stmp.substring(pos+1); - if ((substmp.toInt() > 59) || (substmp.toInt() < 0 )) { - return false; + memset(&tmstruct, 0, sizeof(struct tm)); + if (strptime(stime,"%Y-%m-%dT%H:%M:%S", &tmstruct)==nullptr) { + //allow not to set seconds for lazy guys typing command line + if (strptime(stime,"%Y-%m-%dT%H:%M", &tmstruct)==nullptr) { + return false; + } } tmstruct.tm_isdst = 0; //ignore dst - tmstruct.tm_sec = substmp.toInt(); time_val.tv_sec = mktime (&tmstruct); + //try to setTime if(settimeofday(&time_val,0) == -1) { return false; }