// A RFC 868 (Time protocol) client package main import ( "fmt" "flag" "time" "net" "os" "encoding/binary" ) func main() { var ( port = "37" thetime int64 response = make([]byte, 4) ) if flag.NArg() != 1 { fmt.Printf("Usage: time-rfc-868 host\n") os.Exit(1) } host := flag.Arg(0) remote := host + ":" + port con, error := net.Dial("tcp4", "", remote) if error != nil { fmt.Printf("Cannot connect to %s: %s\n", host, error) os.Exit(1) } defer con.Close() in, error := con.Read(response) if error != nil || in != 4 { fmt.Printf("Error reading data: %s, in: %d\n", error, in) os.Exit(2) } // RFC 868 sets the epoch in 1900 but Go's libraries use 1970 theoffsettime, error := time.Parse(time.RFC3339, "1900-01-01T00:00:00Z") theoffsetseconds := theoffsettime.Seconds() thetime = int64(binary.BigEndian.Uint32(response)) theparsedtime := time.SecondsToUTC(thetime + theoffsetseconds) fmt.Printf("Time at %s is %d (which is %s)\n", remote, thetime, theparsedtime) }