github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/types/timestamp.go (about) 1 package types 2 3 // timestamp.go defines the timestamp type and implements sort.Interface 4 // interface for slices of timestamps. 5 6 import ( 7 "time" 8 ) 9 10 type ( 11 Timestamp uint64 12 TimestampSlice []Timestamp 13 ) 14 15 // CurrentTimestamp returns the current time as a Timestamp. 16 func CurrentTimestamp() Timestamp { 17 return Timestamp(time.Now().Unix()) 18 } 19 20 // Len is part of sort.Interface 21 func (ts TimestampSlice) Len() int { 22 return len(ts) 23 } 24 25 // Less is part of sort.Interface 26 func (ts TimestampSlice) Less(i, j int) bool { 27 return ts[i] < ts[j] 28 } 29 30 // Swap is part of sort.Interface 31 func (ts TimestampSlice) Swap(i, j int) { 32 ts[i], ts[j] = ts[j], ts[i] 33 } 34 35 // Clock allows clients to retrieve the current time. 36 type Clock interface { 37 Now() Timestamp 38 } 39 40 // StdClock is an implementation of Clock that retrieves the current time using 41 // the system time. 42 type StdClock struct{} 43 44 // Now retrieves the current timestamp. 45 func (c StdClock) Now() Timestamp { 46 return Timestamp(time.Now().Unix()) 47 }