@@ -7,12 +7,29 @@ NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) {
7
7
AuthenticationPredicates::IS_AUTHENTICATED));
8
8
}
9
9
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) {
11
16
char time_string[25 ];
12
- strftime (time_string, 25 , incOffset ? " %FT%T%z " : " %FT%TZ " , time);
17
+ strftime (time_string, 25 , format , time);
13
18
return String (time_string);
14
19
}
15
20
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
+
16
33
void NTPStatus::ntpStatus (AsyncWebServerRequest* request) {
17
34
AsyncJsonResponse* response = new AsyncJsonResponse (false , MAX_NTP_STATUS_SIZE);
18
35
JsonObject root = response->getRoot ();
@@ -24,10 +41,12 @@ void NTPStatus::ntpStatus(AsyncWebServerRequest* request) {
24
41
root[" status" ] = sntp_enabled () ? 1 : 0 ;
25
42
26
43
// the current time in UTC
27
- root[" time_utc" ] = toISOString (gmtime (&now), false );
44
+ root[" time_utc" ] = toUTCTimeString (gmtime (&now));
28
45
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);
31
50
32
51
// the sntp server name
33
52
root[" server" ] = sntp_getservername (0 );
0 commit comments