Path: blob/main/vendor/github.com/google/uuid/time.go
2875 views
// Copyright 2016 Google Inc. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34package uuid56import (7"encoding/binary"8"sync"9"time"10)1112// A Time represents a time as the number of 100's of nanoseconds since 15 Oct13// 1582.14type Time int641516const (17lillian = 2299160 // Julian day of 15 Oct 158218unix = 2440587 // Julian day of 1 Jan 197019epoch = unix - lillian // Days between epochs20g1582 = epoch * 86400 // seconds between epochs21g1582ns100 = g1582 * 10000000 // 100s of a nanoseconds between epochs22)2324var (25timeMu sync.Mutex26lasttime uint64 // last time we returned27clockSeq uint16 // clock sequence for this run2829timeNow = time.Now // for testing30)3132// UnixTime converts t the number of seconds and nanoseconds using the Unix33// epoch of 1 Jan 1970.34func (t Time) UnixTime() (sec, nsec int64) {35sec = int64(t - g1582ns100)36nsec = (sec % 10000000) * 10037sec /= 1000000038return sec, nsec39}4041// GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and42// clock sequence as well as adjusting the clock sequence as needed. An error43// is returned if the current time cannot be determined.44func GetTime() (Time, uint16, error) {45defer timeMu.Unlock()46timeMu.Lock()47return getTime()48}4950func getTime() (Time, uint16, error) {51t := timeNow()5253// If we don't have a clock sequence already, set one.54if clockSeq == 0 {55setClockSequence(-1)56}57now := uint64(t.UnixNano()/100) + g1582ns1005859// If time has gone backwards with this clock sequence then we60// increment the clock sequence61if now <= lasttime {62clockSeq = ((clockSeq + 1) & 0x3fff) | 0x800063}64lasttime = now65return Time(now), clockSeq, nil66}6768// ClockSequence returns the current clock sequence, generating one if not69// already set. The clock sequence is only used for Version 1 UUIDs.70//71// The uuid package does not use global static storage for the clock sequence or72// the last time a UUID was generated. Unless SetClockSequence is used, a new73// random clock sequence is generated the first time a clock sequence is74// requested by ClockSequence, GetTime, or NewUUID. (section 4.2.1.1)75func ClockSequence() int {76defer timeMu.Unlock()77timeMu.Lock()78return clockSequence()79}8081func clockSequence() int {82if clockSeq == 0 {83setClockSequence(-1)84}85return int(clockSeq & 0x3fff)86}8788// SetClockSequence sets the clock sequence to the lower 14 bits of seq. Setting to89// -1 causes a new sequence to be generated.90func SetClockSequence(seq int) {91defer timeMu.Unlock()92timeMu.Lock()93setClockSequence(seq)94}9596func setClockSequence(seq int) {97if seq == -1 {98var b [2]byte99randomBits(b[:]) // clock sequence100seq = int(b[0])<<8 | int(b[1])101}102oldSeq := clockSeq103clockSeq = uint16(seq&0x3fff) | 0x8000 // Set our variant104if oldSeq != clockSeq {105lasttime = 0106}107}108109// Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in110// uuid. The time is only defined for version 1, 2, 6 and 7 UUIDs.111func (uuid UUID) Time() Time {112var t Time113switch uuid.Version() {114case 6:115time := binary.BigEndian.Uint64(uuid[:8]) // Ignore uuid[6] version b0110116t = Time(time)117case 7:118time := binary.BigEndian.Uint64(uuid[:8])119t = Time((time>>16)*10000 + g1582ns100)120default: // forward compatible121time := int64(binary.BigEndian.Uint32(uuid[0:4]))122time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32123time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48124t = Time(time)125}126return t127}128129// ClockSequence returns the clock sequence encoded in uuid.130// The clock sequence is only well defined for version 1 and 2 UUIDs.131func (uuid UUID) ClockSequence() int {132return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff133}134135136