Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual Set Time not accounting for DST Properly (Fix attached) #331

Open
Tshetrim opened this issue Apr 23, 2024 · 1 comment
Open

Manual Set Time not accounting for DST Properly (Fix attached) #331

Tshetrim opened this issue Apr 23, 2024 · 1 comment

Comments

@Tshetrim
Copy link

Tshetrim commented Apr 23, 2024

File: https://github.com/rjwats/esp8266-react/blob/master/lib/framework/NTPSettingsService.cpp#L74

Hello, raising an issue with timezones and accounting for DST (Daylight Savings Time) on ESP32

For some reason, tm tm_isdst is set to a value (0 or 1) leading the conversion in mktime to not automatically calculate isdst and instead use that overriden value.

The impact was that when using this function, DST was not properly being accounted for leading to incorrect set time.

Solution:
Add:
tm.tm_isdst = -1;

Setting tm.tm_isdst to -1 before calling mktime causes it to do this calculation using the posix timezone (assuming its set) allowing for correct time setting.

See solution below:

void NTPSettingsService::configureTime(AsyncWebServerRequest* request, JsonVariant& json) {
  if (!sntp_enabled() && json.is<JsonObject>()) {
    struct tm tm = {0};
    tm.tm_isdst = -1;
    String timeLocal = json["local_time"];
    char* s = strptime(timeLocal.c_str(), "%Y-%m-%dT%H:%M:%S", &tm);
    if (s != nullptr) {
      time_t time = mktime(&tm);
      struct timeval now = {.tv_sec = time};
      settimeofday(&now, nullptr);
      AsyncWebServerResponse* response = request->beginResponse(200);
      request->send(response);
      return;
    }
  }
  AsyncWebServerResponse* response = request->beginResponse(400);
  request->send(response);
}

I realize this project is not maintained anymore but just wanted to leave this fix here incase anyone else encountered it (took me a bit to pindown the root cause).

@proddy
Copy link

proddy commented Apr 23, 2024

+1

Thta's what I did too.

https://github.com/emsesp/EMS-ESP32/blob/1487f30c433e6fc87ecbba42c04f9f9ea483dd4f/lib/framework/NTPSettingsService.cpp#L67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants