ducky/devices
2015-11-15
Parent:b6494e1a499e
ducky/devices/vendor/code.google.com/p/go-uuid/uuid/version1.go
Add ToMap and ToSlice helpers. Add helper functions that convert a map of ID-keyed Devices into a slice, and back. We'll use these a lot (especially with GetMany) as shorthand helper functions.
| paddy@0 | 1 // Copyright 2011 Google Inc. All rights reserved. |
| paddy@0 | 2 // Use of this source code is governed by a BSD-style |
| paddy@0 | 3 // license that can be found in the LICENSE file. |
| paddy@0 | 4 |
| paddy@0 | 5 package uuid |
| paddy@0 | 6 |
| paddy@0 | 7 import ( |
| paddy@0 | 8 "encoding/binary" |
| paddy@0 | 9 ) |
| paddy@0 | 10 |
| paddy@0 | 11 // NewUUID returns a Version 1 UUID based on the current NodeID and clock |
| paddy@0 | 12 // sequence, and the current time. If the NodeID has not been set by SetNodeID |
| paddy@0 | 13 // or SetNodeInterface then it will be set automatically. If the NodeID cannot |
| paddy@0 | 14 // be set NewUUID returns nil. If clock sequence has not been set by |
| paddy@0 | 15 // SetClockSequence then it will be set automatically. If GetTime fails to |
| paddy@0 | 16 // return the current NewUUID returns nil. |
| paddy@0 | 17 func NewUUID() UUID { |
| paddy@0 | 18 if nodeID == nil { |
| paddy@0 | 19 SetNodeInterface("") |
| paddy@0 | 20 } |
| paddy@0 | 21 |
| paddy@0 | 22 now, seq, err := GetTime() |
| paddy@0 | 23 if err != nil { |
| paddy@0 | 24 return nil |
| paddy@0 | 25 } |
| paddy@0 | 26 |
| paddy@0 | 27 uuid := make([]byte, 16) |
| paddy@0 | 28 |
| paddy@0 | 29 time_low := uint32(now & 0xffffffff) |
| paddy@0 | 30 time_mid := uint16((now >> 32) & 0xffff) |
| paddy@0 | 31 time_hi := uint16((now >> 48) & 0x0fff) |
| paddy@0 | 32 time_hi |= 0x1000 // Version 1 |
| paddy@0 | 33 |
| paddy@0 | 34 binary.BigEndian.PutUint32(uuid[0:], time_low) |
| paddy@0 | 35 binary.BigEndian.PutUint16(uuid[4:], time_mid) |
| paddy@0 | 36 binary.BigEndian.PutUint16(uuid[6:], time_hi) |
| paddy@0 | 37 binary.BigEndian.PutUint16(uuid[8:], seq) |
| paddy@0 | 38 copy(uuid[10:], nodeID) |
| paddy@0 | 39 |
| paddy@0 | 40 return uuid |
| paddy@0 | 41 } |