github.com/aspring/packer@v0.8.1-0.20150629211158-9db281ac0f89/common/uuid/uuid.go (about)

     1  package uuid
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  // Generates a time ordered UUID. Top 32 bits are a timestamp,
    10  // bottom 96 are random.
    11  func TimeOrderedUUID() string {
    12  	unix := uint32(time.Now().UTC().Unix())
    13  
    14  	b := make([]byte, 12)
    15  	n, err := rand.Read(b)
    16  	if n != len(b) {
    17  		err = fmt.Errorf("Not enough entropy available")
    18  	}
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	return fmt.Sprintf("%08x-%04x-%04x-%04x-%04x%08x",
    23  		unix, b[0:2], b[2:4], b[4:6], b[6:8], b[8:])
    24  }