Skip to content

API Reference

chumicro_ntp

chumicro_ntp

Runner-shaped SNTP client for CircuitPython, MicroPython, and CPython.

NTPClient

Runner-shaped SNTP client over an injected UDP socket.

Parameters:

Name Type Description Default
socket object | None

Non-blocking UDP object with sendto/recvfrom_into/close/setblocking.

None
server str

NTP server hostname. Defaults to "pool.ntp.org".

'pool.ntp.org'
port int

NTP server UDP port. Defaults to 123.

123
timeout_ms int

Tick budget for the recv side. Defaults to 5000.

5000
ticks object | None

Optional tick source (chumicro_timing.ticks shape); defaults to the real clock.

None

Raises:

Type Description
ValueError

timeout_ms is non-positive.

busy property

True between :meth:query and result completion.

from_config(config, *, radio=None, socket=None, transport_factory=None, ticks=None, **constructor_kwargs) classmethod

Build an :class:NTPClient from runtime config.

Parameters:

Name Type Description Default
config object

Mapping-like runtime config; reads ntp.server, ntp.port, and ntp.timeout_ms, all optional.

required
radio object | None

CircuitPython radio handle for the default UDP factory.

None
socket object | None

Pre-built non-blocking UDP socket; skips the factory.

None
transport_factory object | None

Callable returning the UDP socket on first use.

None
ticks object | None

Optional tick source, forwarded to the constructor.

None
**constructor_kwargs object

Any other constructor knob, passed through verbatim; an explicit keyword wins over its config-derived value.

{}

Raises:

Type Description
RuntimeError

No socket or factory was given and chumicro_sockets.sockets_factory is unavailable.

query()

Issue a single SNTP query.

Returns:

Name Type Description
An NTPResult

class:NTPResult the caller polls.

Raises:

Type Description
RuntimeError

A query is already in flight (busy).

check(now_ms)

Return True when the runner should call :meth:handle.

Parameters:

Name Type Description Default
now_ms int

Current tick value (unused; required by the runner).

required

handle(now_ms)

Drain one tick of work for the in-flight query.

Parameters:

Name Type Description Default
now_ms int

Current tick value, used for timeout detection.

required

cancel()

Abort an in-flight query.

Returns:

Type Description
bool

True if a query was in flight, False if the client was idle.

close()

Close the UDP socket.

A factory-built client reopens on the next :meth:query; a client constructed with socket= raises RuntimeError from query after close, since it has no way to rebuild the socket.

NTPError

Bases: OSError

SNTP exchange failed.

NTPResult

Handle for a single in-flight SNTP exchange.

Parameters:

Name Type Description Default
ticks_started_ms int

Tick value when the request was issued.

required

unix_seconds property

Server's transmit timestamp converted to Unix-epoch seconds.

Raises:

Type Description
Exception

Re-raises the stored :attr:error when the exchange failed.

RuntimeError

The exchange has not finished yet.