Created
June 2, 2023 14:54
-
-
Save bryanjhv/1f1afaaf16b21d96341a25b7a44aefdb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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