-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace code.google.com/p/go-uuid with TimeUUID from gocql
- Loading branch information
1 parent
947c776
commit 62434fb
Showing
2 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) 2012 The gocql Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// The uuid package can be used to generate and parse universally unique | ||
// identifiers, a standardized format in the form of a 128 bit number. | ||
// | ||
// http://tools.ietf.org/html/rfc4122 | ||
package uuid | ||
|
||
import ( | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type UUID [16]byte | ||
|
||
var timeBase = time.Date(1582, time.October, 15, 0, 0, 0, 0, time.UTC).Unix() | ||
var hardwareAddr []byte | ||
var clockSeq uint32 | ||
|
||
// TimeUUID generates a new time based UUID (version 1) using the current | ||
// time as the timestamp. | ||
func TimeUUID() UUID { | ||
return UUIDFromTime(time.Now()) | ||
} | ||
|
||
// UUIDFromTime generates a new time based UUID (version 1) as described in | ||
// RFC 4122. This UUID contains the MAC address of the node that generated | ||
// the UUID, the given timestamp and a sequence number. | ||
func UUIDFromTime(aTime time.Time) UUID { | ||
var u UUID | ||
|
||
utcTime := aTime.In(time.UTC) | ||
t := uint64(utcTime.Unix()-timeBase)*10000000 + uint64(utcTime.Nanosecond()/100) | ||
u[0], u[1], u[2], u[3] = byte(t>>24), byte(t>>16), byte(t>>8), byte(t) | ||
u[4], u[5] = byte(t>>40), byte(t>>32) | ||
u[6], u[7] = byte(t>>56)&0x0F, byte(t>>48) | ||
|
||
clock := atomic.AddUint32(&clockSeq, 1) | ||
u[8] = byte(clock >> 8) | ||
u[9] = byte(clock) | ||
|
||
copy(u[10:], hardwareAddr) | ||
|
||
u[6] |= 0x10 // set version to 1 (time based uuid) | ||
u[8] &= 0x3F // clear variant | ||
u[8] |= 0x80 // set to IETF variant | ||
|
||
return u | ||
} | ||
|
||
// String returns the UUID in it's canonical form, a 32 digit hexadecimal | ||
// number in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. | ||
func (u UUID) String() string { | ||
var offsets = [...]int{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34} | ||
const hexString = "0123456789abcdef" | ||
r := make([]byte, 36) | ||
for i, b := range u { | ||
r[offsets[i]] = hexString[b>>4] | ||
r[offsets[i]+1] = hexString[b&0xF] | ||
} | ||
r[8] = '-' | ||
r[13] = '-' | ||
r[18] = '-' | ||
r[23] = '-' | ||
return string(r) | ||
|
||
} |
62434fb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattrobenolt Watch out! You must respect the license. See #3508
62434fb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aviau oops. 😞
This is why I'm not a lawyer!