FObersteiner/ntp_client
NTP client CLI in Zig
Command line app to query an NTP server, e.g. to verify your OS clock setting. Or get the time independent of your OS clock. Or mess with your local NTP server.
zig build -Dexe [--release=[safe|small|fast]]
# build and run, debug: zig build -Dexe run
# library tests: zig build test
Currently targets SNTP (RFC4330), does not implement the full NTP spec. src/ntp.zig can be used independently in other projects; it is exposed via this project's build.zig and build.zig.zon files. Other dependencies of the binary are lazy, i.e. they won't be fetched if you use only the library in another project.
Usage: ntp_client [options]
Options:
-s, --server NTP server to query (default: pool.ntp.org)
-v, --protocol-version NTP protocol version, 3 or 4 (default: 4)
-4, --ipv4 use IPv4 instead of the default IPv6
--src-ip IP address to use for sending the query (default: 0::0 / IPv6 auto-select)
--src-port UDP port to use for sending the query (default: 0 / any port)
--dst-port UDP port of destination server (default: 123)
-z, --timezone Timezone to use in console output (default: UTC)
-j, --json Print result as a JSON string
-i, --interval Interval for repeated queries in seconds (default: null / one-shot operation)
-a, --all Query all IP addresses found for a given server URL (default: false / stop after first)
-h, --help Show this help and exit
zig build run -Dexe --release=safe -- -4 -z local
---***---
Server name: "pool.ntp.org"
Server address: "178.63.67.56:123"
---
LI=0 VN=4 Mode=4 Stratum=2 Poll=0 (0 s) Precision=-25 (29 ns)
ID: 0xDAA2684D
Server root dispersion: 686 us, root delay: 31036 us
---
Server last synced : 2025-06-18T17:04:47.182320188+02:00
T1, packet created : 2025-06-18T17:09:33.295124493+02:00
T2, server received : 2025-06-18T17:09:33.286877204+02:00
T3, server replied : 2025-06-18T17:09:33.286925770+02:00
T4, reply received : 2025-06-18T17:09:33.316002877+02:00
(timezone displayed: Europe/Berlin)
---
Offset to timserver: -0.019 s (-18663 us)
Round-trip delay: 0.021 s (20829 us)
---
Result flags: 0 (OK)
---***---
Developed & tested mostly on Debian Linux, on an x86 machine. Windows worked last time I tested (build.zig links libc for this), Mac OS might work (can't test this).
I try to track Zig master with this package; might not compile with older versions.
MIT. See the LICENSE file in the root directory of the repository.