0

I'm working on an ESP8266 module that needs to fetch date data from worldtimeapi, everything works fine but the problem is How do I strip of the time field off the datetime JSON response without loop.

Note: Arduino JSON library was used in deserializing the response data and it seems the library only supports char data type.

void loop ()
{
 // Initialize the request object
 HTTPClient request;
 
// Make a http request
 request.begin(dateTime);
 delay(2000);
 // Check the request status code.
int responseCode = request.GET(); //Get the api host response code.
 Serial.print("Response code is: ");
 Serial.println(responseCode);
 if (responseCode == 200); // If the server returned OK
 {
 String responseData = request.getString();
 Serial.println("Response from the server is:");
 Serial.println(responseData);
 // Deserialize the JSON response data
 StaticJsonDocument<715> result; // Declare data storage to stack
 
 //Check for error during data deserialization
 DeserializationError error = deserializeJson(result, responseData);
 
 if (error) 
 {
 Serial.print(F("Response deserialization failed with reason: "));
 Serial.println(error.f_str());
 return;
 }
 else
 {
 char abbreviation = result["abbreviation"]; // Returns the current time zone abbreviation
 char timezone = result["timezone"]; // Returns the current time zone 
 char datetime = result["datetime"]; // "2021年03月12日T17:04:0
 char date = datetime.remove(9,22); // This line throws error : request for member 'remove' in 'datetime', which is of non-class type 'char'
 
 
 Serial.print("Current time is ");
 Serial.print(datetime);
 Serial.print(" ");
 Serial.print(abbreviation);
 Serial.print(" "); 
 Serial.println(timezone);
 delay(2000);
 }
 
 }
}
asked Mar 13, 2021 at 19:56
1
  • why without loop? Commented Mar 13, 2021 at 20:05

1 Answer 1

2

You can use strtok to split the string on the T character:

char *datepart = strtok(datetime, "T");
char *timepart = strtok(NULL, "T");
Serial.print("The current date is: ");
Serial.println(datepart);
Serial.print("The current time is: ");
Serial.println(timepart);

An alternative is to just find the T and turn it into the NULL terminating character:

char *T = strchr(datetime, 'T');
*T = 0;

If you need to copy the string data into a temporary string first you can do:

char temp[strlen(datetime)+1];
strcpy(temp, datetime);

Then operate on temp instead of datetime.

answered Mar 13, 2021 at 19:58
9
  • I need to get rid of T17:04:0. Commented Mar 13, 2021 at 20:04
  • Then just use datepart. Commented Mar 13, 2021 at 20:06
  • How do I do that in C++, I think datepart is a SQL syntax. Commented Mar 13, 2021 at 20:14
  • I have shown you how to do it. Commented Mar 13, 2021 at 20:14
  • 1
    You may need to copy the string content to a temporary work string first then. Commented Mar 13, 2021 at 20:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.