39 lines
718 B
Go
39 lines
718 B
Go
package usecase
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"crypto/rand"
|
||
|
|
"encoding/binary"
|
||
|
|
"fmt"
|
||
|
|
"sync/atomic"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type SystemClock struct{}
|
||
|
|
|
||
|
|
func (SystemClock) Now() time.Time {
|
||
|
|
return time.Now().UTC()
|
||
|
|
}
|
||
|
|
|
||
|
|
type UUIDGenerator struct{}
|
||
|
|
|
||
|
|
func (UUIDGenerator) NewID() (string, error) {
|
||
|
|
var value [16]byte
|
||
|
|
if _, err := rand.Read(value[:]); err != nil {
|
||
|
|
now := uint64(time.Now().UnixNano())
|
||
|
|
binary.BigEndian.PutUint64(value[:8], now)
|
||
|
|
binary.BigEndian.PutUint64(value[8:], fallbackUUID.Add(1))
|
||
|
|
}
|
||
|
|
value[6] = (value[6] & 0x0f) | 0x40
|
||
|
|
value[8] = (value[8] & 0x3f) | 0x80
|
||
|
|
return fmt.Sprintf(
|
||
|
|
"%08x-%04x-%04x-%04x-%012x",
|
||
|
|
value[0:4],
|
||
|
|
value[4:6],
|
||
|
|
value[6:8],
|
||
|
|
value[8:10],
|
||
|
|
value[10:16],
|
||
|
|
), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
var fallbackUUID atomic.Uint64
|