8000 experiment with alternative payload for NTP · rjwats/esp8266-react@aa6d6be · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit aa6d6be

Browse files
committed
experiment with alternative payload for NTP
1 parent 4917b38 commit aa6d6be

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

interface/.env.development

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Change the IP address to that of your ESP device to enable local development of the UI.
22
# Remember to also enable CORS in platformio.ini before uploading the code to the device.
3-
REACT_APP_HTTP_ROOT=http://192.168.0.88
4-
REACT_APP_WEB_SOCKET_ROOT=ws://192.168.0.88
3+
REACT_APP_HTTP_ROOT=http://192.168.0.24
4+
REACT_APP_WEB_SOCKET_ROOT=ws://192.168.0.24

lib/framework/NTPSettingsService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ void NTPSettingsService::configureNTP() {
7373

7474
void NTPSettingsService::configureTime(AsyncWebServerRequest* request, JsonVariant& json) {
7575
if (!sntp_enabled() && json.is<JsonObject>()) {
76-
String timeUtc = json["time_utc"];
7776
struct tm tm = {0};
78-
char* s = strptime(timeUtc.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
77+
String timeLocal = json["time_local"];
78+
char* s = strptime(timeLocal.c_str(), "%Y-%m-%dT%H:%M:%S", &tm);
7979
if (s != nullptr) {
8080
time_t time = mktime(&tm);
8181
struct timeval now = {.tv_sec = time};

lib/framework/NTPStatus.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,29 @@ NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) {
77
AuthenticationPredicates::IS_AUTHENTICATED));
88
}
99

10-
String toISOString(tm* time, bool incOffset) {
10+
/*
11+
* Formats the time using the format provided.
12+
*
13+
* Uses a 25 byte buffer, large enough to fit an ISO time string with offset.
14+
*/
15+
String formatTime(tm* time, const char* format) {
1116
char time_string[25];
12-
strftime(time_string, 25, incOffset ? "%FT%T%z" : "%FT%TZ", time);
17+
strftime(time_string, 25, format, time);
1318
return String(time_string);
1419
}
1520

21+
String toUTCTimeString(tm* time) {
22+
return formatTime(time, "%FT%TZ");
23+
}
24+
25+
String toLocalTimeString(tm* time) {
26+
return formatTime(time, "%FT%T");
27+
}
28+
29+
String offsetString(tm* time) {
30+
return formatTime(time, "%z");
31+
}
32+
1633
void NTPStatus::ntpStatus(AsyncWebServerRequest* request) {
1734
AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_NTP_STATUS_SIZE);
1835
JsonObject root = response->getRoot();
@@ -24,10 +41,12 @@ void NTPStatus::ntpStatus(AsyncWebServerRequest* request) {
2441
root["status"] = sntp_enabled() ? 1 : 0;
2542

2643
// the current time in UTC
27-
root["time_utc"] = toISOString(gmtime(&now), false);
44+
root["time_utc"] = toUTCTimeString(gmtime(&now));
2845

29-
// local time as ISO String with TZ
30-
root["time_local"] = toISOString(localtime(&now), true);
46+
// local time with offset separate
47+
struct tm* ltm = localtime(&now);
48+
root["time_local"] = toLocalTimeString(ltm);
49+
root["time_offset"] = offsetString(ltm);
3150

3251
// the sntp server name
3352
root["server"] = sntp_getservername(0);

0 commit comments

Comments
 (0)
0