github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/types/timestamp.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package types 16 17 import ( 18 "context" 19 "time" 20 21 "github.com/dolthub/dolt/go/store/hash" 22 ) 23 24 const ( 25 timestampNumBytes = 15 26 timestampFormat = "2006-01-02 15:04:05.999999" 27 ) 28 29 type Timestamp time.Time 30 31 func (v Timestamp) Value(ctx context.Context) (Value, error) { 32 return v, nil 33 } 34 35 func (v Timestamp) Equals(other Value) bool { 36 v2, ok := other.(Timestamp) 37 if !ok { 38 return false 39 } 40 41 return time.Time(v).Equal(time.Time(v2)) 42 } 43 44 func (v Timestamp) Less(nbf *NomsBinFormat, other LesserValuable) (bool, error) { 45 if v2, ok := other.(Timestamp); ok { 46 return time.Time(v).Before(time.Time(v2)), nil 47 } 48 return TimestampKind < other.Kind(), nil 49 } 50 51 func (v Timestamp) Hash(nbf *NomsBinFormat) (hash.Hash, error) { 52 return getHash(v, nbf) 53 } 54 55 func (v Timestamp) isPrimitive() bool { 56 return true 57 } 58 59 func (v Timestamp) WalkValues(ctx context.Context, cb ValueCallback) error { 60 return nil 61 } 62 63 func (v Timestamp) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error { 64 return nil 65 } 66 67 func (v Timestamp) typeOf() (*Type, error) { 68 return PrimitiveTypeMap[TimestampKind], nil 69 } 70 71 func (v Timestamp) Kind() NomsKind { 72 return TimestampKind 73 } 74 75 func (v Timestamp) valueReadWriter() ValueReadWriter { 76 return nil 77 } 78 79 func (v Timestamp) writeTo(w nomsWriter, nbf *NomsBinFormat) error { 80 data, err := time.Time(v).MarshalBinary() 81 if err != nil { 82 return err 83 } 84 85 err = TimestampKind.writeTo(w, nbf) 86 if err != nil { 87 return err 88 } 89 90 w.writeRaw(data) 91 return nil 92 } 93 94 func (v Timestamp) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) { 95 t, err := b.ReadTimestamp() 96 if err != nil { 97 return nil, err 98 } 99 return Timestamp(t), nil 100 101 } 102 103 func (v Timestamp) skip(nbf *NomsBinFormat, b *binaryNomsReader) { 104 b.skipBytes(timestampNumBytes) 105 } 106 107 func (v Timestamp) String() string { 108 return time.Time(v).UTC().Format(timestampFormat) 109 } 110 111 func (v Timestamp) HumanReadableString() string { 112 return v.String() 113 }