divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package uuid |
| 6 | |
| 7 | import ( |
| 8 | "encoding/binary" |
| 9 | ) |
| 10 | |
| 11 | // NewUUID returns a Version 1 UUID based on the current NodeID and clock |
| 12 | // sequence, and the current time. If the NodeID has not been set by SetNodeID |
| 13 | // or SetNodeInterface then it will be set automatically. If the NodeID cannot |
| 14 | // be set NewUUID returns nil. If clock sequence has not been set by |
| 15 | // SetClockSequence then it will be set automatically. If GetTime fails to |
| 16 | // return the current NewUUID returns nil and an error. |
| 17 | // |
| 18 | // In most cases, New should be used. |
| 19 | func NewUUID() (UUID, error) { |
| 20 | nodeMu.Lock() |
| 21 | if nodeID == zeroID { |
| 22 | setNodeInterface("") |
| 23 | } |
| 24 | nodeMu.Unlock() |
| 25 | |
| 26 | var uuid UUID |
| 27 | now, seq, err := GetTime() |
| 28 | if err != nil { |
| 29 | return uuid, err |
| 30 | } |
| 31 | |
| 32 | timeLow := uint32(now & 0xffffffff) |
| 33 | timeMid := uint16((now >> 32) & 0xffff) |
| 34 | timeHi := uint16((now >> 48) & 0x0fff) |
| 35 | timeHi |= 0x1000 // Version 1 |
| 36 | |
| 37 | binary.BigEndian.PutUint32(uuid[0:], timeLow) |
| 38 | binary.BigEndian.PutUint16(uuid[4:], timeMid) |
| 39 | binary.BigEndian.PutUint16(uuid[6:], timeHi) |
| 40 | binary.BigEndian.PutUint16(uuid[8:], seq) |
| 41 | copy(uuid[10:], nodeID[:]) |
| 42 | |
| 43 | return uuid, nil |
| 44 | } |