DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ntp_service.cpp File Reference

SNTP wall-clock time sync implementation (DETWS_ENABLE_NTP). More...

#include "ntp_service.h"
#include "shared_primitives/http_date.h"
#include <time.h>

Go to the source code of this file.

Functions

void detws_ntp_set_test_epoch (time_t epoch)
 
bool detws_ntp_begin (const char *tz, const char *server1, const char *server2)
 Start the SNTP client.
 
bool detws_ntp_synced ()
 True once a plausible wall-clock time has been obtained from SNTP.
 
time_t detws_ntp_epoch ()
 Current Unix epoch seconds, or 0 if not yet synced (or disabled).
 
size_t detws_ntp_http_date (char *out, size_t out_cap)
 Format the current time as an RFC 7231 IMF-fixdate (HTTP Date).
 
uint32_t ntp_time_source (void)
 NTP as a time source for the multi-source registry (services/time_source).
 

Detailed Description

SNTP wall-clock time sync implementation (DETWS_ENABLE_NTP).

Definition in file ntp_service.cpp.

Function Documentation

◆ detws_ntp_set_test_epoch()

void detws_ntp_set_test_epoch ( time_t  epoch)

Definition at line 58 of file ntp_service.cpp.

◆ detws_ntp_begin()

bool detws_ntp_begin ( const char *  tz = nullptr,
const char *  server1 = "pool.ntp.org",
const char *  server2 = "time.nist.gov" 
)

Start the SNTP client.

Returns immediately; the first sync arrives asynchronously (poll detws_ntp_synced()). Call once after the WiFi link is up.

Parameters
tzPOSIX TZ string (e.g. "UTC0", "EST5EDT,M3.2.0,M11.1.0"). Pass nullptr for UTC.
server1Primary NTP server (default "pool.ntp.org").
server2Secondary NTP server (default "time.nist.gov").
Returns
true if the client was started; false if disabled at compile time.

Definition at line 63 of file ntp_service.cpp.

◆ detws_ntp_synced()

bool detws_ntp_synced ( )

True once a plausible wall-clock time has been obtained from SNTP.

Checks that the system clock has advanced past 2021-01-01, which only happens after a successful sync (the RTC starts at the epoch on a cold boot).

Definition at line 70 of file ntp_service.cpp.

◆ detws_ntp_epoch()

time_t detws_ntp_epoch ( )

Current Unix epoch seconds, or 0 if not yet synced (or disabled).

Definition at line 74 of file ntp_service.cpp.

Referenced by detws_ntp_http_date(), and ntp_time_source().

◆ detws_ntp_http_date()

size_t detws_ntp_http_date ( char *  out,
size_t  out_cap 
)

Format the current time as an RFC 7231 IMF-fixdate (HTTP Date).

Writes e.g. "Sun, 06 Nov 1994 08:49:37 GMT" into out. Always GMT.

Parameters
outDestination buffer (>= 30 bytes recommended).
out_capCapacity of out.
Returns
Number of characters written (excluding the null), or 0 if time is not yet available / disabled.

Definition at line 78 of file ntp_service.cpp.

References detws_http_date(), and detws_ntp_epoch().

◆ ntp_time_source()

uint32_t ntp_time_source ( void  )

NTP as a time source for the multi-source registry (services/time_source).

Register with detws_time_source_add("ntp", priority, ntp_time_source) so the aggregated detws_time_now() (and thus the HTTP Date header when DETWS_ENABLE_TIME_SOURCE is set) can be fed by NTP alongside an RTC / GPS. Returns the current epoch, or 0 when not synced.

Definition at line 88 of file ntp_service.cpp.

References detws_ntp_epoch().