github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/internal/utils/tso/tso.go (about) 1 // Licensed to the LF AI & Data foundation under one 2 // or more contributor license agreements. See the NOTICE file 3 // distributed with this work for additional information 4 // regarding copyright ownership. The ASF licenses this file 5 // to you under the Apache License, Version 2.0 (the 6 // "License"); you may not use this file except in compliance 7 // with the License. You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package tso 18 19 import "time" 20 21 const ( 22 logicalBits = 18 23 logicalBitsMask = (1 << logicalBits) - 1 24 ) 25 26 // ComposeTS returns a timestamp composed of physical part and logical part 27 func ComposeTS(physical, logical int64) uint64 { 28 return uint64((physical << logicalBits) + logical) 29 } 30 31 // ComposeTSByTime returns a timestamp composed of physical time.Time and logical time 32 func ComposeTSByTime(physical time.Time, logical int64) uint64 { 33 return ComposeTS(physical.UnixNano()/int64(time.Millisecond), logical) 34 } 35 36 // GetCurrentTime returns the current timestamp 37 func GetCurrentTime() uint64 { 38 return ComposeTSByTime(time.Now(), 0) 39 } 40 41 // ParseTS parses the ts to (physical,logical). 42 func ParseTS(ts uint64) (time.Time, uint64) { 43 logical := ts & logicalBitsMask 44 physical := ts >> logicalBits 45 physicalTime := time.Unix(int64(physical/1000), int64(physical)%1000*time.Millisecond.Nanoseconds()) 46 return physicalTime, logical 47 } 48 49 // ParseHybridTs parses the ts to (physical, logical), physical part is of utc-timestamp format. 50 func ParseHybridTs(ts uint64) (int64, int64) { 51 logical := ts & logicalBitsMask 52 physical := ts >> logicalBits 53 return int64(physical), int64(logical) 54 }