github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/mru/timestampprovider.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package mru 26 27 import ( 28 "encoding/binary" 29 "time" 30 ) 31 32 // 33 var TimestampProvider timestampProvider = NewDefaultTimestampProvider() 34 35 // 36 type Timestamp struct { 37 Time uint64 // 38 } 39 40 // 41 const timestampLength = 8 42 43 // 44 type timestampProvider interface { 45 Now() Timestamp // 46 } 47 48 // 49 func (t *Timestamp) binaryGet(data []byte) error { 50 if len(data) != timestampLength { 51 return NewError(ErrCorruptData, "timestamp data has the wrong size") 52 } 53 t.Time = binary.LittleEndian.Uint64(data[:8]) 54 return nil 55 } 56 57 // 58 func (t *Timestamp) binaryPut(data []byte) error { 59 if len(data) != timestampLength { 60 return NewError(ErrCorruptData, "timestamp data has the wrong size") 61 } 62 binary.LittleEndian.PutUint64(data, t.Time) 63 return nil 64 } 65 66 type DefaultTimestampProvider struct { 67 } 68 69 // 70 func NewDefaultTimestampProvider() *DefaultTimestampProvider { 71 return &DefaultTimestampProvider{} 72 } 73 74 // 75 func (dtp *DefaultTimestampProvider) Now() Timestamp { 76 return Timestamp{ 77 Time: uint64(time.Now().Unix()), 78 } 79 }