Skip to content

Instantly share code, notes, and snippets.

@bryanjhv
Created June 2, 2023 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanjhv/1f1afaaf16b21d96341a25b7a44aefdb to your computer and use it in GitHub Desktop.
Save bryanjhv/1f1afaaf16b21d96341a25b7a44aefdb to your computer and use it in GitHub Desktop.
package main
import (
"encoding/binary"
"fmt"
"net"
"time"
)
type Packet struct {
Flags uint8
_ [39]uint8
TxTimeSec uint32
TxTimeFrac uint32
}
func main() {
conn, _ := net.Dial("udp", "time.nist.gov:123")
defer conn.Close()
buf := &Packet{Flags: 0b1110011}
_ = binary.Write(conn, binary.BigEndian, buf)
_ = binary.Read(conn, binary.BigEndian, buf)
sec := float64(buf.TxTimeSec) - 2208988800
nsec := (int64(buf.TxTimeFrac) * 1e9) >> 32
fmt.Println(time.Unix(int64(sec), nsec))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment