2022-04-15 09:42:07 +02:00
|
|
|
package rnd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
const (
|
|
|
|
PrefixNone = byte(0)
|
|
|
|
PrefixMixed = byte('*')
|
|
|
|
)
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
// GenerateUID returns a unique id with prefix as string.
|
|
|
|
func GenerateUID(prefix byte) string {
|
2024-01-05 16:31:07 +01:00
|
|
|
return generateUID(prefix, time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateUID returns a unique id with prefix as string at a given time.
|
|
|
|
func generateUID(prefix byte, t time.Time) string {
|
2022-04-15 09:42:07 +02:00
|
|
|
result := make([]byte, 0, 16)
|
|
|
|
result = append(result, prefix)
|
2024-01-05 16:31:07 +01:00
|
|
|
result = append(result, strconv.FormatInt(t.UTC().Unix(), 36)[0:6]...)
|
2023-12-12 18:42:50 +01:00
|
|
|
result = append(result, Base36(9)...)
|
2022-04-15 09:42:07 +02:00
|
|
|
|
|
|
|
return string(result)
|
|
|
|
}
|