github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/native/time.go (about)

     1  package native
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/unicornultrafoundation/go-helios/common/bigendian"
     7  )
     8  
     9  type (
    10  	// Timestamp is a UNIX nanoseconds timestamp
    11  	Timestamp uint64
    12  )
    13  
    14  // Bytes gets the byte representation of the index.
    15  func (t Timestamp) Bytes() []byte {
    16  	return bigendian.Uint64ToBytes(uint64(t))
    17  }
    18  
    19  // BytesToTimestamp converts bytes to timestamp.
    20  func BytesToTimestamp(b []byte) Timestamp {
    21  	return Timestamp(bigendian.BytesToUint64(b))
    22  }
    23  
    24  func FromUnix(t int64) Timestamp {
    25  	return Timestamp(int64(t) * int64(time.Second))
    26  }
    27  
    28  // Unix returns t as a Unix time, the number of seconds elapsed
    29  // since January 1, 1970 UTC. The result does not depend on the
    30  // location associated with t.
    31  func (t Timestamp) Unix() int64 {
    32  	return int64(t) / int64(time.Second)
    33  }
    34  
    35  func (t Timestamp) Time() time.Time {
    36  	return time.Unix(int64(t)/int64(time.Second), int64(t)%int64(time.Second))
    37  }
    38  
    39  // MaxTimestamp return max value.
    40  func MaxTimestamp(x, y Timestamp) Timestamp {
    41  	if x > y {
    42  		return x
    43  	}
    44  	return y
    45  }