Skip to content

Commit

Permalink
Add a function to auto lookup our timezone using our public ip
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstroud committed Oct 28, 2024
1 parent 6e6aee8 commit 8dceabd
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "LittleFS.h"
#include "comms.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#ifdef LEGACY_SETTINGS_MIGRATION
// Filenames for legacy user configuation, replaced by single file.
Expand Down Expand Up @@ -51,6 +52,38 @@ bool enableNTP = false;
unsigned long lastRebootAt = 0;
int32_t savedDoorUpdateAt = 0;

int get_tz()
{
WiFiClient client;
HTTPClient http;

if (http.begin(client, "http://ip-api.com/csv/?fields=timezone,offset"))
{ // HTTP

// start connection and send HTTP header
int httpCode = http.GET();

// httpCode will be negative on error
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
String payload = http.getString();
char * timezone = (char*)payload.c_str();
char * ch = strchr(timezone, ',');
*ch = '\0';
ch++;
int offset = atoi(ch);
offset /= 3600;
RINFO("Setting timezone to %s, %d", timezone, offset);
return offset;
}
}
http.end();
}
return 0;
}

void time_is_set(bool from_sntp)
{
RINFO("Clock set from NTP server: %d", from_sntp ? 1 : 0);
Expand Down

0 comments on commit 8dceabd

Please sign in to comment.