Page 1 of 1
fetching local time in Arduino
Posted: Tue Mar 23, 2021 9:31 pm
by reza_neam
Hi,
Currently, I am using pool.ntp.org to fetch the time. As you know it only returns the GMT time and if I want to convert it to the local time I need to know the offset.
I am wondering is there any way to find the offset based on the IP address of the ESP32 module?
Re: fetching local time in Arduino
Posted: Wed Mar 24, 2021 4:40 pm
by lbernstone
Re: fetching local time in Arduino
Posted: Wed Mar 24, 2021 7:48 pm
by reza_neam
Hi Ibernstone,
Thank you for your reply. I really found it thoughtful.
i found another API to that directly gives the offset value.
see
http://worldtimeapi.org/api/ip
Re: fetching local time in Arduino
Posted: Wed Mar 24, 2021 8:38 pm
by reza_neam
So, I can minimize it to this
Code: Select all
HTTPClient http;
const char *ntpServer = "pool.ntp.org";
const char *timeApiUrl = "http://worldtimeapi.org/api/ip";
http.begin(timeApiUrl);
int httpCode = http.GET();
if (httpCode != 200)
{
printf("Unable to fetch the time info\r\n");
configTime(0, 0, ntpServer);
return;
}
String response = http.getString();
long offset = response.substring(response.indexOf("\"raw_offset\":") + 13, response.indexOf(",\"timezone\":")).toFloat();
http.end();
configTime(offset, 0, ntpServer);
Re: fetching local time in Arduino
Posted: Wed Mar 24, 2021 9:13 pm
by lbernstone
Hopefully someday we will be rid of the curse that is Daylight Savings Time and we can use a simple method to set time!