aboutsummaryrefslogtreecommitdiff
path: root/version1.go
diff options
context:
space:
mode:
authorPaul Borman <borman@google.com>2015-06-03 14:39:07 -0700
committerPaul Borman <borman@google.com>2015-06-03 14:39:07 -0700
commit2a573a0b4d3d5c1114b3ff01456b48c68e0e658e (patch)
tree1a560a31fdddf99bd3514ea9f45ff7125052120d /version1.go
parented3ca8a15a931b141440a7e98e4f716eec255f7d (diff)
downloadgoogle-uuid-2a573a0b4d3d5c1114b3ff01456b48c68e0e658e.tar.gz
Move code up one directory
Diffstat (limited to 'version1.go')
-rw-r--r--version1.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/version1.go b/version1.go
new file mode 100644
index 0000000..0127eac
--- /dev/null
+++ b/version1.go
@@ -0,0 +1,41 @@
+// Copyright 2011 Google Inc. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package uuid
+
+import (
+ "encoding/binary"
+)
+
+// NewUUID returns a Version 1 UUID based on the current NodeID and clock
+// sequence, and the current time. If the NodeID has not been set by SetNodeID
+// or SetNodeInterface then it will be set automatically. If the NodeID cannot
+// be set NewUUID returns nil. If clock sequence has not been set by
+// SetClockSequence then it will be set automatically. If GetTime fails to
+// return the current NewUUID returns nil.
+func NewUUID() UUID {
+ if nodeID == nil {
+ SetNodeInterface("")
+ }
+
+ now, seq, err := GetTime()
+ if err != nil {
+ return nil
+ }
+
+ uuid := make([]byte, 16)
+
+ time_low := uint32(now & 0xffffffff)
+ time_mid := uint16((now >> 32) & 0xffff)
+ time_hi := uint16((now >> 48) & 0x0fff)
+ time_hi |= 0x1000 // Version 1
+
+ binary.BigEndian.PutUint32(uuid[0:], time_low)
+ binary.BigEndian.PutUint16(uuid[4:], time_mid)
+ binary.BigEndian.PutUint16(uuid[6:], time_hi)
+ binary.BigEndian.PutUint16(uuid[8:], seq)
+ copy(uuid[10:], nodeID)
+
+ return uuid
+}