github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/swarm/storage/feed/timestampprovider.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package feed
    18  
    19  import (
    20  	"encoding/binary"
    21  	"encoding/json"
    22  	"time"
    23  )
    24  
    25  // TimestampProvider sets the time source of the feeds package
    26  var TimestampProvider timestampProvider = NewDefaultTimestampProvider()
    27  
    28  // Timestamp encodes a point in time as a Unix epoch
    29  type Timestamp struct {
    30  	Time uint64 `json:"time"` // Unix epoch timestamp, in seconds
    31  }
    32  
    33  // 8 bytes uint64 Time
    34  const timestampLength = 8
    35  
    36  // timestampProvider interface describes a source of timestamp information
    37  type timestampProvider interface {
    38  	Now() Timestamp // returns the current timestamp information
    39  }
    40  
    41  // binaryGet populates the timestamp structure from the given byte slice
    42  func (t *Timestamp) binaryGet(data []byte) error {
    43  	if len(data) != timestampLength {
    44  		return NewError(ErrCorruptData, "timestamp data has the wrong size")
    45  	}
    46  	t.Time = binary.LittleEndian.Uint64(data[:8])
    47  	return nil
    48  }
    49  
    50  // binaryPut Serializes a Timestamp to a byte slice
    51  func (t *Timestamp) binaryPut(data []byte) error {
    52  	if len(data) != timestampLength {
    53  		return NewError(ErrCorruptData, "timestamp data has the wrong size")
    54  	}
    55  	binary.LittleEndian.PutUint64(data, t.Time)
    56  	return nil
    57  }
    58  
    59  // UnmarshalJSON implements the json.Unmarshaller interface
    60  func (t *Timestamp) UnmarshalJSON(data []byte) error {
    61  	return json.Unmarshal(data, &t.Time)
    62  }
    63  
    64  // MarshalJSON implements the json.Marshaller interface
    65  func (t *Timestamp) MarshalJSON() ([]byte, error) {
    66  	return json.Marshal(t.Time)
    67  }
    68  
    69  // DefaultTimestampProvider is a TimestampProvider that uses system time
    70  // as time source
    71  type DefaultTimestampProvider struct {
    72  }
    73  
    74  // NewDefaultTimestampProvider creates a system clock based timestamp provider
    75  func NewDefaultTimestampProvider() *DefaultTimestampProvider {
    76  	return &DefaultTimestampProvider{}
    77  }
    78  
    79  // Now returns the current time according to this provider
    80  func (dtp *DefaultTimestampProvider) Now() Timestamp {
    81  	return Timestamp{
    82  		Time: uint64(time.Now().Unix()),
    83  	}
    84  }