Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed Mar 25, 2018
0 parents commit 04f3ad3
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions htpdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"errors"
"flag"
"fmt"
"log"
"net/http"
"time"
)

func timeDiff(url string) (float64, float64, error) {
t0 := time.Now()
resp, err := http.Head(url)
delta := time.Since(t0).Seconds()
if err != nil {
return 0, 0, err
}

date := resp.Header.Get("Date")
if date == "" {
return 0, 0, errors.New("Date header is missing")
}
t1, err := time.Parse(time.RFC1123, date)
if err != nil {
return 0, 0, err
}

theta := t1.Sub(t0).Seconds() + 0.5 - delta/2
return theta, delta, nil
}

func main() {
n := flag.Uint("n", 10, "Number of requests")
h := flag.String("u", "https://google.com", "Host URL")
flag.Parse()

var sumT, sumD float64
for i := uint(0); i < *n; i++ {
theta, delta, err := timeDiff(*h)
if err != nil {
log.Fatal(err)
}
sumT += theta / delta
sumD += 1 / delta
}
fmt.Printf("offset %.5f sec\n", sumT/sumD)
}

0 comments on commit 04f3ad3

Please # to comment.