This commit is contained in:
azw 2023-08-20 15:29:28 +08:00
parent 9377182985
commit 8716d301c6
3 changed files with 57 additions and 4 deletions

View File

@ -104,3 +104,9 @@ func MakeListHead () *ListHead{
l.Init(nil)
return l
}
func NewListHead () *ListHead{
l := new(ListHead)
l.Init(nil)
return l
}

View File

@ -130,7 +130,3 @@ func FormatUnixDateEx(sec int64) string {
strTime := time.Unix(sec, 0).Format("20060102")
return strTime
}
func TraverseSlice[T any](arr []T, cb func (T)) {
}

51
uuid.go Normal file
View File

@ -0,0 +1,51 @@
package q5
import (
"fmt"
"time"
)
const Q5_EPOCH = 1419120000000;
const MACHINE_ID_BIT_NUM = 12
const SEQUNCE_ID_BIT_NUM = 10
const MAX_MACHINE_ID = (1 << MACHINE_ID_BIT_NUM) - 1
const MAX_SEQUNCE_ID = (1 << SEQUNCE_ID_BIT_NUM) - 1
type Uuid struct {
machineId int32
sequenceId int32
lastGenerateTick int64
}
func (this *Uuid) Generate() int64 {
var value int64
tick := GetTickCount()
if tick == this.lastGenerateTick {
this.sequenceId++
if this.sequenceId >= MAX_SEQUNCE_ID {
for tick <= this.lastGenerateTick {
time.Sleep(time.Millisecond * 1)
tick = GetTickCount()
}
this.sequenceId = 0
}
} else {
this.sequenceId = 0
}
this.lastGenerateTick = tick
// 保留后41位时间
value = (tick - Q5_EPOCH) << 22
//中间12位是机器ID
value |= int64((int64(this.machineId) & int64(MAX_MACHINE_ID)) << SEQUNCE_ID_BIT_NUM)
//最后10位是sequenceID
value |= int64(this.sequenceId & MAX_SEQUNCE_ID)
return value
}
func (this *Uuid) SetMachineId(macId int32) {
if macId > MAX_MACHINE_ID || macId < 1 {
panic(fmt.Sprintf("Uuid.SetMachineId error %d", macId))
}
this.machineId = macId
}