github.com/NebulousLabs/Sia@v1.3.7/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 is a Unix timestamp, i.e. the number of the seconds since Jan 1 1970.
    12  	Timestamp uint64
    13  	// TimestampSlice is an array of timestamps
    14  	TimestampSlice []Timestamp
    15  )
    16  
    17  // CurrentTimestamp returns the current time as a Timestamp.
    18  func CurrentTimestamp() Timestamp {
    19  	return Timestamp(time.Now().Unix())
    20  }
    21  
    22  // Len is part of sort.Interface
    23  func (ts TimestampSlice) Len() int {
    24  	return len(ts)
    25  }
    26  
    27  // Less is part of sort.Interface
    28  func (ts TimestampSlice) Less(i, j int) bool {
    29  	return ts[i] < ts[j]
    30  }
    31  
    32  // Swap is part of sort.Interface
    33  func (ts TimestampSlice) Swap(i, j int) {
    34  	ts[i], ts[j] = ts[j], ts[i]
    35  }
    36  
    37  // Clock allows clients to retrieve the current time.
    38  type Clock interface {
    39  	Now() Timestamp
    40  }
    41  
    42  // StdClock is an implementation of Clock that retrieves the current time using
    43  // the system time.
    44  type StdClock struct{}
    45  
    46  // Now retrieves the current timestamp.
    47  func (c StdClock) Now() Timestamp {
    48  	return Timestamp(time.Now().Unix())
    49  }