1 // Copyright 2014 Google Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package uuid 6 7 import ( 8 "encoding/binary" 9 10 guuid "github.com/google/uuid" 11 ) 12 13 // A Time represents a time as the number of 100's of nanoseconds since 15 Oct 14 // 1582. 15 type Time = guuid.Time 16 17 // GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and 18 // clock sequence as well as adjusting the clock sequence as needed. An error 19 // is returned if the current time cannot be determined. 20 func GetTime() (Time, uint16, error) { return guuid.GetTime() } 21 22 // ClockSequence returns the current clock sequence, generating one if not 23 // already set. The clock sequence is only used for Version 1 UUIDs. 24 // 25 // The uuid package does not use global static storage for the clock sequence or 26 // the last time a UUID was generated. Unless SetClockSequence a new random 27 // clock sequence is generated the first time a clock sequence is requested by 28 // ClockSequence, GetTime, or NewUUID. (section 4.2.1.1) sequence is generated 29 // for 30 func ClockSequence() int { return guuid.ClockSequence() } 31 32 // SetClockSequence sets the clock sequence to the lower 14 bits of seq. Setting to 33 // -1 causes a new sequence to be generated. 34 func SetClockSequence(seq int) { guuid.SetClockSequence(seq) } 35 36 // Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in 37 // uuid. It returns false if uuid is not valid. The time is only well defined 38 // for version 1 and 2 UUIDs. 39 func (uuid UUID) Time() (Time, bool) { 40 if len(uuid) != 16 { 41 return 0, false 42 } 43 time := int64(binary.BigEndian.Uint32(uuid[0:4])) 44 time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32 45 time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48 46 return Time(time), true 47 } 48 49 // ClockSequence returns the clock sequence encoded in uuid. It returns false 50 // if uuid is not valid. The clock sequence is only well defined for version 1 51 // and 2 UUIDs. 52 func (uuid UUID) ClockSequence() (int, bool) { 53 if len(uuid) != 16 { 54 return 0, false 55 } 56 return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff, true 57 } 58