ntptime – simple NTP client

This module provides a small client for the Network Time Protocol (NTP), useful for retrieving the current UTC time from an internet time server and optionally setting the on-board real-time clock. It performs a single UDP request to an NTP server and decodes the returned 64-bit timestamp.

This module relies on a working network connection. Its 32-bit on-the-wire timestamp is automatically corrected for the 2036 NTP rollover, allowing the implementation to remain valid until February 2160.

Functions

ntptime.time() int

Query the configured NTP server (host) and return the current UTC time as an integer number of seconds since the MicroPython epoch.

The MicroPython epoch is detected at runtime using time.gmtime(). Both the 1970 (Unix) and 2000 epochs are supported; any other epoch raises Exception.

Network errors propagate to the caller; the underlying socket is always closed before returning.

ntptime.settime() None

Fetch the current time using time() and set the on-board RTC accordingly via machine.RTC.

Note that MicroPython has no time-zone support: the RTC is always set in UTC.

Constants

ntptime.host: str

The hostname of the NTP server to query. Defaults to "pool.ntp.org". Re-assign at runtime to override, e.g. ntptime.host = "time.google.com".

ntptime.timeout: int

Socket timeout in seconds applied to the NTP UDP request. Defaults to 1. Increase on slow or high-latency networks.