5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-30 06:11:36 +00:00
matterbridge/vendor/github.com/pborman/uuid/time.go

58 lines
2.1 KiB
Go
Raw Normal View History

2016-04-10 21:39:38 +00:00
// Copyright 2014 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"
2020-08-09 22:29:54 +00:00
guuid "github.com/google/uuid"
2016-04-10 21:39:38 +00:00
)
// A Time represents a time as the number of 100's of nanoseconds since 15 Oct
// 1582.
2020-08-09 22:29:54 +00:00
type Time = guuid.Time
2016-04-10 21:39:38 +00:00
// GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and
// clock sequence as well as adjusting the clock sequence as needed. An error
// is returned if the current time cannot be determined.
2020-08-09 22:29:54 +00:00
func GetTime() (Time, uint16, error) { return guuid.GetTime() }
2016-04-10 21:39:38 +00:00
// ClockSequence returns the current clock sequence, generating one if not
// already set. The clock sequence is only used for Version 1 UUIDs.
//
// The uuid package does not use global static storage for the clock sequence or
// the last time a UUID was generated. Unless SetClockSequence a new random
// clock sequence is generated the first time a clock sequence is requested by
// ClockSequence, GetTime, or NewUUID. (section 4.2.1.1) sequence is generated
// for
2020-08-09 22:29:54 +00:00
func ClockSequence() int { return guuid.ClockSequence() }
2016-04-10 21:39:38 +00:00
2020-10-19 21:40:00 +00:00
// SetClockSequence sets the clock sequence to the lower 14 bits of seq. Setting to
2016-04-10 21:39:38 +00:00
// -1 causes a new sequence to be generated.
2020-08-09 22:29:54 +00:00
func SetClockSequence(seq int) { guuid.SetClockSequence(seq) }
2016-04-10 21:39:38 +00:00
// Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in
// uuid. It returns false if uuid is not valid. The time is only well defined
// for version 1 and 2 UUIDs.
func (uuid UUID) Time() (Time, bool) {
if len(uuid) != 16 {
return 0, false
}
time := int64(binary.BigEndian.Uint32(uuid[0:4]))
time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32
time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48
return Time(time), true
}
// ClockSequence returns the clock sequence encoded in uuid. It returns false
// if uuid is not valid. The clock sequence is only well defined for version 1
// and 2 UUIDs.
func (uuid UUID) ClockSequence() (int, bool) {
if len(uuid) != 16 {
return 0, false
}
return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff, true
}