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

[feature] automatically detect timezone of clock via internet #35

Open
techniccontroller opened this issue Jan 5, 2025 · 2 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@techniccontroller
Copy link
Owner

Possible solution:

  1. Get via timezone string via API request: https://networkengineering.stackexchange.com/questions/23761/how-to-get-local-time-from-internet

  2. Convert timezone string into offset: https://github.com/bxparks/AceTime

@techniccontroller techniccontroller self-assigned this Jan 6, 2025
@techniccontroller techniccontroller added the enhancement New feature or request label Jan 6, 2025
@BramWerbrouck
Copy link

Can this do the trick?

void getTimeZone() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin("http://ip-api.com/json/");
    int httpCode = http.GET();

    if (httpCode == 200) { // HTTP OK
      String payload = http.getString();
      Serial.println("Received data:");
      Serial.println(payload);

      int tzIndex = payload.indexOf("\"timezone\":\"");
      if (tzIndex != -1) {
        int tzStart = tzIndex + 12; 
        int tzEnd = payload.indexOf("\"", tzStart);
        String timezone = payload.substring(tzStart, tzEnd);
        Serial.println("Timezone: " + timezone);
      }
    } else {
      Serial.println("Error: " + String(httpCode));
    }
    http.end();
  }
}

@techniccontroller
Copy link
Owner Author

Thank you very much. I have already used another API in a dev branch, but this one is much more efficient.
I will most likely release a new code version this weekend, including this feature.

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

When branches are created from issues, their pull requests are automatically linked.

2 participants