-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoffset.go
65 lines (53 loc) · 1.63 KB
/
offset.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package wal
import (
"strconv"
"time"
"github.com/pkg/errors"
)
// Offset represents the offset of a data chunk within a write-ahead logger.
type Offset int64
// ZeroOffset holds the value of the oldest-possible offset within a
// write-ahead logger.
var ZeroOffset = Offset(0)
// NewOffset returns a new Offset for the current time.
// This is a shorthand for:
//
// NewOffsetTime(time.Now())
//
func NewOffset() Offset {
return NewOffsetTime(time.Now())
}
// NewOffsetTime returns a new Offset for the given time.Time.
func NewOffsetTime(t time.Time) Offset {
return Offset(t.UnixNano())
}
// ParseOffset returns an offset parsed from s.
func ParseOffset(s string) (Offset, error) {
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return ZeroOffset, errors.Wrap(err, "parse offset")
}
return Offset(n), nil
}
// Before reports whether the offset o is older than b.
func (o Offset) Before(b Offset) bool {
return time.Unix(0, int64(o)).Before(time.Unix(0, int64(b)))
}
// After reports whether the offset o is newer than b.
func (o Offset) After(b Offset) bool {
return time.Unix(0, int64(o)).Before(time.Unix(0, int64(b)))
}
// Equal reports whether the offset o is the same as b.
func (o Offset) Equal(b Offset) bool {
return time.Unix(0, int64(o)).Equal(time.Unix(0, int64(b)))
}
// Within reports whether a <= o <= b.
func (o Offset) Within(a, b Offset) bool {
n := int64(o)
return int64(a) <= n && n <= int64(b)
}
// String implements the fmt.Stringer interface, and provides a means for
// representing an offset that can be later parsed with ParseOffset.
func (o Offset) String() string {
return strconv.FormatInt(int64(o), 10)
}