Skip to content

Instantly share code, notes, and snippets.

@GregIngelmo
Created April 7, 2015 03:33
Show Gist options
  • Save GregIngelmo/4b1d5a50b1f1f31fb659 to your computer and use it in GitHub Desktop.
Save GregIngelmo/4b1d5a50b1f1f31fb659 to your computer and use it in GitHub Desktop.
UUID precision test
package main
import (
"crypto/rand"
"fmt"
"io"
"net"
"sync/atomic"
"time"
)
var timeBase = time.Date(1582, time.October, 15, 0, 0, 0, 0, time.UTC).Unix()
var hardwareAddr []byte
var clockSeq uint32
type UUID [16]byte
// Version extracts the version of this UUID variant. The RFC 4122 describes
// five kinds of UUIDs.
func (u UUID) Version() int {
return int(u[6] & 0xF0 >> 4)
}
// Timestamp extracts the timestamp information from a time based UUID
// (version 1).
func (u UUID) Timestamp() int64 {
if u.Version() != 1 {
return 0
}
return int64(uint64(u[0])<<24|uint64(u[1])<<16|
uint64(u[2])<<8|uint64(u[3])) +
int64(uint64(u[4])<<40|uint64(u[5])<<32) +
int64(uint64(u[6]&0x0F)<<56|uint64(u[7])<<48)
}
// Time is like Timestamp, except that it returns a time.Time.
func (u UUID) Time() time.Time {
if u.Version() != 1 {
return time.Time{}
}
t := u.Timestamp()
sec := t / 1e7
nsec := (t % 1e7) * 100
return time.Unix(sec+timeBase, nsec).UTC()
}
// 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
}
func init() {
if interfaces, err := net.Interfaces(); err == nil {
for _, i := range interfaces {
if i.Flags&net.FlagLoopback == 0 && len(i.HardwareAddr) > 0 {
hardwareAddr = i.HardwareAddr
break
}
}
}
if hardwareAddr == nil {
// If we failed to obtain the MAC address of the current computer,
// we will use a randomly generated 6 byte sequence instead and set
// the multicast bit as recommended in RFC 4122.
hardwareAddr = make([]byte, 6)
_, err := io.ReadFull(rand.Reader, hardwareAddr)
if err != nil {
panic(err)
}
hardwareAddr[0] = hardwareAddr[0] | 0x01
}
// initialize the clock sequence with a random number
var clockSeqRand [2]byte
io.ReadFull(rand.Reader, clockSeqRand[:])
clockSeq = uint32(clockSeqRand[1])<<8 | uint32(clockSeqRand[0])
}
func main() {
tOrig := time.Now().UTC()
uuid := UUIDFromTime(tOrig)
tConv := uuid.Time()
// tConv will be missing to decimal places off of nanoseconds
fmt.Printf("original: %s\n", tOrig.Format(time.RFC3339Nano))
fmt.Printf("converted: %s\n", tConv.Format(time.RFC3339Nano))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment