forked from NebulousLabs/Sia
/
timestamp.go
49 lines (39 loc) · 1.08 KB
/
timestamp.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
package types
// timestamp.go defines the timestamp type and implements sort.Interface
// interface for slices of timestamps.
import (
"time"
)
type (
// Timestamp is a Unix timestamp, i.e. the number of the seconds since Jan 1 1970.
Timestamp uint64
// TimestampSlice is an array of timestamps
TimestampSlice []Timestamp
)
// CurrentTimestamp returns the current time as a Timestamp.
func CurrentTimestamp() Timestamp {
return Timestamp(time.Now().Unix())
}
// Len is part of sort.Interface
func (ts TimestampSlice) Len() int {
return len(ts)
}
// Less is part of sort.Interface
func (ts TimestampSlice) Less(i, j int) bool {
return ts[i] < ts[j]
}
// Swap is part of sort.Interface
func (ts TimestampSlice) Swap(i, j int) {
ts[i], ts[j] = ts[j], ts[i]
}
// Clock allows clients to retrieve the current time.
type Clock interface {
Now() Timestamp
}
// StdClock is an implementation of Clock that retrieves the current time using
// the system time.
type StdClock struct{}
// Now retrieves the current timestamp.
func (c StdClock) Now() Timestamp {
return Timestamp(time.Now().Unix())
}