Path: blob/main/vendor/github.com/google/uuid/version1.go
2875 views
// Copyright 2016 Google Inc. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34package uuid56import (7"encoding/binary"8)910// NewUUID returns a Version 1 UUID based on the current NodeID and clock11// sequence, and the current time. If the NodeID has not been set by SetNodeID12// or SetNodeInterface then it will be set automatically. If the NodeID cannot13// be set NewUUID returns nil. If clock sequence has not been set by14// SetClockSequence then it will be set automatically. If GetTime fails to15// return the current NewUUID returns nil and an error.16//17// In most cases, New should be used.18func NewUUID() (UUID, error) {19var uuid UUID20now, seq, err := GetTime()21if err != nil {22return uuid, err23}2425timeLow := uint32(now & 0xffffffff)26timeMid := uint16((now >> 32) & 0xffff)27timeHi := uint16((now >> 48) & 0x0fff)28timeHi |= 0x1000 // Version 12930binary.BigEndian.PutUint32(uuid[0:], timeLow)31binary.BigEndian.PutUint16(uuid[4:], timeMid)32binary.BigEndian.PutUint16(uuid[6:], timeHi)33binary.BigEndian.PutUint16(uuid[8:], seq)3435nodeMu.Lock()36if nodeID == zeroID {37setNodeInterface("")38}39copy(uuid[10:], nodeID[:])40nodeMu.Unlock()4142return uuid, nil43}444546