SuaveIV/nu_script_time_sync
A Nushell script that checks whether your system clock has drifted, using the Time.now API.
time-sync.nu
A Nushell script to check if your system clock is drifting. It queries the Time.now API, compares the result to your local time, and tells you if your clock is out of sync.
Why I wrote this
System clocks drift. Most of the time NTP handles it, but when it fails (suspended VMs, weird service configs, air-gapped machines), it fails silently and messes up your logs. I wanted a quick sanity check without having to remember the flags for timedatectl or parse ntpq output.
Usage
Run it once
nu time-sync.nuInstall as a command
Use it as a module in your config.nu to use it anywhere:
use /path/to/time-sync.nuThen run it:
time-sync
time-sync --max-offset 10sec
time-sync --rawThis prints a formatted report:
For a one-line summary, use -1:
Flags
| Flag | Default | What it does |
|---|---|---|
--max-offset |
5sec |
Allowed drift before the clock is marked out of sync |
--max-rtt |
2sec |
Network latency cutoff. Above this, the check is flagged as unreliable |
--one-line -1 |
(none) | Single-line output: IN SYNC 14:32:07 → 14:32:07 312ms |
--raw -r |
(none) | Returns a raw record instead of text. Best for scripting. |
Scripting
time-sync --raw | if not $in.synced { print "Clock drift detected!" }The --raw flag returns a record with these fields: local, network, drift, rtt, synced, reliable, timezone.
How it works
- Records the time before and after the API call to measure round-trip latency.
- Pulls UTC time from
https://time.now/developer/api/ip. - Compares the network time to your local time.
- Reports the drift, RTT, and sync status.
If the network is slow (RTT above --max-rtt), the script flags the result as unreliable. High latency inflates the apparent drift, so a slow API response doesn't actually mean your clock is wrong.
Requirements
- Nushell
- Internet access
Notes
- The script uses optional cell paths (
?) to parse the API response. If the API changes its JSON shape slightly, the script shouldn't crash. - Network errors surface as clean messages instead of raw Nu stack traces.