forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skew.go
50 lines (41 loc) · 1.39 KB
/
skew.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package lease
import (
"time"
)
// Skew holds information about a remote writer's idea of the current time.
type Skew struct {
// LastWrite is the most recent remote time known to have been written
// by the skewed writer.
LastWrite time.Time
// Beginning should be the latest known local time before LastWrite
// was read.
Beginning time.Time
// End should be the earliest known local time after LastWrite
// was read.
End time.Time
}
// Earliest returns the earliest local time after which we can be confident
// that the remote writer will agree the supplied time is in the past.
func (skew Skew) Earliest(remote time.Time) (local time.Time) {
if skew.isZero() {
return remote
}
delta := remote.Sub(skew.LastWrite)
return skew.Beginning.Add(delta)
}
// Latest returns the latest local time after which we can be confident that
// the remote writer will agree the supplied time is in the past.
func (skew Skew) Latest(remote time.Time) (local time.Time) {
if skew.isZero() {
return remote
}
delta := remote.Sub(skew.LastWrite)
return skew.End.Add(delta)
}
// isZero lets us shortcut Earliest and Latest when the skew represents a
// perfect unskewed clock (such as for a local writer).
func (skew Skew) isZero() bool {
return skew.LastWrite.IsZero() && skew.Beginning.IsZero() && skew.End.IsZero()
}