GitHunt
SU

SuaveIV/nu_script_time_sync

A Nushell script that checks whether your system clock has drifted, using the Time.now API.

time-sync.nu

Nushell
Time.now API

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.nu

Install as a command

Use it as a module in your config.nu to use it anywhere:

use /path/to/time-sync.nu

Then run it:

time-sync
time-sync --max-offset 10sec
time-sync --raw

This prints a formatted report:

image

For a one-line summary, use -1:

image

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

  1. Records the time before and after the API call to measure round-trip latency.
  2. Pulls UTC time from https://time.now/developer/api/ip.
  3. Compares the network time to your local time.
  4. 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

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.

World Time API by Time.Now

Languages

Nushell100.0%

Contributors

MIT License
Created March 11, 2026
Updated March 13, 2026