Skip to content

Instantly share code, notes, and snippets.

@mengzhuo
Last active April 15, 2018 05:41
Show Gist options
  • Save mengzhuo/8acbd88b71d0a88c6844a22f0f39093f to your computer and use it in GitHub Desktop.
Save mengzhuo/8acbd88b71d0a88c6844a22f0f39093f to your computer and use it in GitHub Desktop.
Test NTP offset from NIST
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/beevik/ntp"
)
func main() {
var (
err error
nist *ntp.Response
)
for {
nist, err = ntp.Query("time.nist.gov")
if err != nil {
continue
}
break
}
targetList := []target{
{7, "time%d.aliyun.com"},
{4, "time%d.google.com"},
{7, "time%d.apple.com"},
}
for _, t := range targetList {
ali := make([]*ntp.Response, t.count)
for i := 1; i < t.count; i++ {
ali[i-1], err = ntp.Query(fmt.Sprintf(t.formater, i))
if err != nil {
log.Print(err)
continue
}
}
for i, ao := range ali {
if ao == nil {
continue
}
io.WriteString(os.Stdout,
fmt.Sprintf("%s,%s\n", fmt.Sprintf(t.formater, i+1),
(ao.ClockOffset - nist.ClockOffset).String()))
}
}
}
type target struct {
count int
formater string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment