-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
59 lines (45 loc) · 1.46 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package uuid7
import (
"crypto/rand"
"encoding/binary"
"math"
"math/big"
"time"
)
type UUID [16]byte
func MustUUID7() UUID {
uuid, err := UUID7()
if err != nil {
panic(err)
}
return uuid
}
func UUID7() (UUID, error) {
return FromTime(time.Now())
}
func FromTime(uuidTime time.Time) (UUID, error) {
milliseconds := uint64(uuidTime.UnixNano() / int64(time.Millisecond))
nanoseconds := uint64(uuidTime.UnixNano() % int64(time.Millisecond))
// Calculate the 12-bit sub-millisecond precision time
subMillisecond := float64(nanoseconds) / float64(time.Millisecond)
precisionBitsValue := uint16(math.Floor(subMillisecond * 4096))
// Initialize a buffer to hold the big-endian bytes
var uuidBytes UUID
// Version field set to 0b0111 (7)
version := uint16(7)
// Manually construct the first 64-bit field
// 48 bits for the timestamp, 4 bits for version, and 12 bits for sub-ms time
sixtyFourBitField := (milliseconds << 16) | ((uint64(version) << 12) & 0xF000) | uint64(precisionBitsValue)
binary.BigEndian.PutUint64(uuidBytes[0:], sixtyFourBitField)
// Generate a 64-bit random number
randomValue, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
return UUID{}, err
}
// Clear the top two bits and set them to 0b10
random64 := randomValue.Uint64()
random64 &= math.MaxUint64 >> 2 // Clear top two bits
random64 |= uint64(2) << 62 // Set top bits to 0b10
binary.BigEndian.PutUint64(uuidBytes[8:], random64)
return uuidBytes, nil
}