-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuidc.go
More file actions
33 lines (26 loc) · 1.18 KB
/
uidc.go
File metadata and controls
33 lines (26 loc) · 1.18 KB
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
/*
Package uidc provides functions to generate simple time-and-random-based unique
identifiers.
It offers functions to generate 64-bit and 128-bit random identifiers in base-36
string format.
The generated IDs are not intended for cryptographic or security-related
purposes. Instead, they serve as simple unique identifiers for various use
cases.
*/
package uidc
import (
"strconv"
"time"
"github.com/Vonage/gosrvlib/pkg/random"
)
// NewID64 generates and returns a new base-36-string-formatted 64 bit unique ID based on time (high 32 bit) and a random number (low 32 bit).
// NOTE: the zero time is set to the 1st of January of 10 years ago.
func NewID64() string {
t := time.Now().UTC()
offset := time.Date(t.Year()-10, 1, 1, 0, 0, 0, 0, time.UTC).Unix() // [s] time starts 1st JAN 10 years ago
return strconv.FormatUint((((uint64)(t.Unix()-offset))<<32)+(uint64)(random.New(nil).RandUint32()), 36)
}
// NewID128 generates and returns a new base-36-string-formatted 128 bit unique ID based on time (high 64 bit) and a random number (low 64 bit).
func NewID128() string {
return strconv.FormatUint((uint64)(time.Now().UTC().UnixNano()), 36) + strconv.FormatUint(random.New(nil).RandUint64(), 36)
}