github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/storage/mru/timestampprovider.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:50</date>
    10  //</624342683908837376>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package mru
    29  
    30  import (
    31  	"encoding/binary"
    32  	"time"
    33  )
    34  
    35  //
    36  var TimestampProvider timestampProvider = NewDefaultTimestampProvider()
    37  
    38  //
    39  type Timestamp struct {
    40  Time uint64 //
    41  }
    42  
    43  //
    44  const timestampLength = 8
    45  
    46  //
    47  type timestampProvider interface {
    48  Now() Timestamp //
    49  }
    50  
    51  //
    52  func (t *Timestamp) binaryGet(data []byte) error {
    53  	if len(data) != timestampLength {
    54  		return NewError(ErrCorruptData, "timestamp data has the wrong size")
    55  	}
    56  	t.Time = binary.LittleEndian.Uint64(data[:8])
    57  	return nil
    58  }
    59  
    60  //
    61  func (t *Timestamp) binaryPut(data []byte) error {
    62  	if len(data) != timestampLength {
    63  		return NewError(ErrCorruptData, "timestamp data has the wrong size")
    64  	}
    65  	binary.LittleEndian.PutUint64(data, t.Time)
    66  	return nil
    67  }
    68  
    69  type DefaultTimestampProvider struct {
    70  }
    71  
    72  //
    73  func NewDefaultTimestampProvider() *DefaultTimestampProvider {
    74  	return &DefaultTimestampProvider{}
    75  }
    76  
    77  //
    78  func (dtp *DefaultTimestampProvider) Now() Timestamp {
    79  	return Timestamp{
    80  		Time: uint64(time.Now().Unix()),
    81  	}
    82  }
    83