Collected time from network is 4 hours forward from local time

Hi, i used this code to get the time from the wifi hotspot i set in my laptop. ESP32 connects to the hotspot and collects the time and show it in the LCD that connected to the ESP32. But time shown in the display is 4 hours forward from the laptop time. laptop time is 3:52PM and esp shows 7:52.

Is there a way to set the time automatically without changing the GMT offset? because people from different places will have different offset GMT, they need to change the code to get correct time. And that's a trouble.


const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = -14400;
const int   daylightOffset_sec = 0;


void setupWIFITime(){
  tft.setCursor(0, 30);
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextColor(ST77XX_RED);
  tft.setTextSize(1);
  // Connect to Wi-Fi
  Serial.print("Connecting to ");
  tft.println("Connecting to ");
  tft.println(ssid);
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    tft.print(".");
  }
  Serial.println("");
  tft.println("");
  tft.println("WiFi connected.");
  Serial.println("WiFi connected.");

  // Init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  watchFace();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

Does this forum discussion help: Getting Time from NTP service using NODEMCU 1.0 (ESP- 12E)?

Without a schematic, I can only take a SWAG and say you need a button and some code. During setup() have the user push a button to change the offset. If no button for a few seconds assume it is correct. Save that value in EEPROM. This will allow you to use an existing button assuming one exists.

Yes, absolutely

people in different timeszones will need to do a change according to their location. I see no other realistic alternative how you could do that autmatically.

You could offer a webpage / settings on the ESP where the people should enter the right timeszone. Basically the same what you will need to let the user enter his WIFI credentials.

The code would need some way of determining where, in the many timezones in the World it is located at the time the code is run.

Use a GPS and a large geofence map of the Worlds timezones ?

or - IP based lookup table, or one of the "geolocation" services - but i don't know if this is working really well for large countries with several time zones...

In some projects, I use a 3G/4G modem (SIM expired - as long as it can connect to the network, no traffic), to get the local time from the cell carrier.

It’s always localised and DST adjusted - easy,

@noiasca Currently the hotspot that used by the esp is my laptop. And why the esp time is different then the laptop time? doesn't it getting the time from the local network?

Also i need this because, if there is no way then people have to recompile the code with the correct GMT offset. or i have to use a settings page to set the time then don't have to use the wifif to set the time! but that's a headache.