Skip to content

Commit

Permalink
Added three more sites
Browse files Browse the repository at this point in the history
  • Loading branch information
tidwall committed Sep 4, 2020
1 parent 251f94f commit caafade
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Retrieve the current time from remote servers.

It works by requesting timestamps from nine very popular hosts over https.
It works by requesting timestamps from twelve very popular hosts over https.
As soon as it gets at least three responses, it takes the two that have the
smallest difference in time. And from those two it picks the one that is
the oldest. Finally it ensures that the time is monotonic.
Expand Down
77 changes: 41 additions & 36 deletions rtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var sites = []string{
"facebook.com", "microsoft.com", "amazon.com", "google.com",
"youtube.com", "twitter.com", "reddit.com", "netflix.com",
"bing.com",
"bing.com", "twitch.tv", "myshopify.com", "wikipedia.org",
}

var rmu sync.Mutex
Expand All @@ -26,6 +26,10 @@ var rtime time.Time
// }
//
func Now() time.Time {
start := time.Now()
defer func() {
println(time.Since(start).String())
}()
res := make([]time.Time, 0, len(sites))
results := make(chan time.Time, len(sites))

Expand Down Expand Up @@ -55,46 +59,47 @@ func Now() time.Time {
return time.Time{}
case tm := <-results:
res = append(res, tm)

if len(res) >= 3 {
// We must have a minimum of three results. Find the two of three
// that have the least difference in time and take the smaller of
// the two.
type pair struct {
tm0 time.Time
tm1 time.Time
diff time.Duration
}
var list []pair
for i := 0; i < len(res); i++ {
for j := i + 1; j < len(res); j++ {
if i != j {
tm0, tm1 := res[i], res[j]
if tm0.After(tm1) {
tm0, tm1 = tm1, tm0
}
list = append(list, pair{tm0, tm1, tm1.Sub(tm0)})
if len(res) < 3 {
continue
}
// We must have a minimum of three results. Find the two of three
// that have the least difference in time and take the smaller of
// the two.
type pair struct {
tm0 time.Time
tm1 time.Time
diff time.Duration
}
var list []pair
for i := 0; i < len(res); i++ {
for j := i + 1; j < len(res); j++ {
if i != j {
tm0, tm1 := res[i], res[j]
if tm0.After(tm1) {
tm0, tm1 = tm1, tm0
}
list = append(list, pair{tm0, tm1, tm1.Sub(tm0)})
}
}
sort.Slice(list, func(i, j int) bool {
if list[i].diff < list[j].diff {
return true
}
if list[i].diff > list[j].diff {
return false
}
return list[i].tm0.Before(list[j].tm0)
})
res := list[0].tm0.Local()
// Ensure that the new time is after the previous time.
rmu.Lock()
defer rmu.Unlock()
if res.After(rtime) {
rtime = res
}
sort.Slice(list, func(i, j int) bool {
if list[i].diff < list[j].diff {
return true
}
if list[i].diff > list[j].diff {
return false
}
return rtime
return list[i].tm0.Before(list[j].tm0)
})
res := list[0].tm0.Local()
// Ensure that the new time is after the previous time.
rmu.Lock()
defer rmu.Unlock()
if res.After(rtime) {
rtime = res
}
println(time.Now().Sub(rtime).String())
return rtime
}
}
}

0 comments on commit caafade

Please sign in to comment.