-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime-sync.nu
More file actions
114 lines (97 loc) · 3.53 KB
/
time-sync.nu
File metadata and controls
114 lines (97 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env nu
# Check system time accuracy using the official Time.now API.
export def main [
--max-offset: duration = 5sec # Threshold for 'out of sync' status
--max-rtt: duration = 2sec # Maximum latency allowed for a reliable check
--raw (-r) # Output raw record for automation
--one-line (-1) # Compact single-line output
]: [
nothing -> record # Logic path for --raw
nothing -> nothing # Logic path for terminal display
nothing -> string # Logic path for --one-line
] {
let start_time = (date now)
let network_data = fetch-network-time
let end_time = (date now)
let rtt = ($end_time - $start_time)
# Defensive Programming: Handle potential nulls in typed flags
let ctx = {
rtt: $rtt,
threshold: ($max_offset | default 5sec),
rtt_limit: ($max_rtt | default 2sec)
}
let report = calculate-drift $network_data $ctx
# Use if/else expression to satisfy multi-signature return paths
if $raw {
$report
} else if $one_line {
display-one-line $report
} else {
display-report $report
}
}
# --- Helper Functions ---
def fetch-network-time []: nothing -> record {
let api = "https://time.now/developer/api/ip"
try {
http get --max-time 5sec $api
} catch {
error make {
msg: "Network unreachable"
label: {
text: "Request failed here"
span: (metadata $api).span
}
help: "Check your internet connection or the Time.now service status."
}
}
}
def calculate-drift [
data: record,
ctx: record
]: nothing -> record {
let local = (date now)
# Defensive Extraction with optional cellpath '?'
let network = ($data.utc_datetime?
| default ($data.datetime?)
| default ($local | into string)
| into datetime)
let drift = ($network - $local | math abs)
{
local: $local,
network: $network,
drift: $drift,
rtt: $ctx.rtt,
synced: ($drift < $ctx.threshold),
reliable: ($ctx.rtt < $ctx.rtt_limit),
timezone: ($data.timezone? | default Unknown)
}
}
def display-one-line [report: record]: nothing -> nothing {
let status_color = if $report.synced { "green_bold" } else { "red_bold" }
let status = if $report.synced { "IN SYNC" } else { "OUT OF SYNC" }
let local = ($report.local | format date %H:%M:%S)
let network = ($report.network | format date %H:%M:%S)
print $"(ansi $status_color)($status)(ansi reset) ($local) → ($network) ($report.drift)"
}
def display-report [report: record]: nothing -> nothing {
let status_color = if $report.synced { "green_bold" } else { "red_bold" }
# Create the clickable link using the correct 'ansi link' syntax
let header_link = ("https://time.now" | ansi link --text "World Time API by Time.Now")
# Escape parentheses \( \) to prevent Nushell execution
let reliability_note = if $report.reliable {
$"(ansi green)Reliable \(Low Latency\)(ansi reset)"
} else {
$"(ansi yellow_italic)Unreliable \(High Latency: ($report.rtt)\)(ansi reset)"
}
print $"
(ansi $status_color)--- ($header_link) ---(ansi reset)
Status: (if $report.synced { 'IN SYNC' } else { 'OUT OF SYNC' })
Reliability: ($reliability_note)
Timezone: ($report.timezone)
Local: ($report.local | format date %H:%M:%S)
Network: ($report.network | format date %H:%M:%S)
Drift: ($report.drift)
RTT: ($report.rtt)
"
}