Source File
time.go
Belonging Package
github.com/google/uuid
// Copyright 2016 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 uuidimport ()// A Time represents a time as the number of 100's of nanoseconds since 15 Oct// 1582.type Time int64const (lillian = 2299160 // Julian day of 15 Oct 1582unix = 2440587 // Julian day of 1 Jan 1970epoch = unix - lillian // Days between epochsg1582 = epoch * 86400 // seconds between epochsg1582ns100 = g1582 * 10000000 // 100s of a nanoseconds between epochs)var (timeMu sync.Mutexlasttime uint64 // last time we returnedclockSeq uint16 // clock sequence for this runtimeNow = time.Now // for testing)// UnixTime converts t the number of seconds and nanoseconds using the Unix// epoch of 1 Jan 1970.func ( Time) () (, int64) {= int64( - g1582ns100)= ( % 10000000) * 100/= 10000000return ,}// 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.func () (Time, uint16, error) {defer timeMu.Unlock()timeMu.Lock()return getTime()}func getTime() (Time, uint16, error) {:= timeNow()// If we don't have a clock sequence already, set one.if clockSeq == 0 {setClockSequence(-1)}:= uint64(.UnixNano()/100) + g1582ns100// If time has gone backwards with this clock sequence then we// increment the clock sequenceif <= lasttime {clockSeq = ((clockSeq + 1) & 0x3fff) | 0x8000}lasttime =return Time(), clockSeq, nil}// 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 is used, 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)func () int {defer timeMu.Unlock()timeMu.Lock()return clockSequence()}func clockSequence() int {if clockSeq == 0 {setClockSequence(-1)}return int(clockSeq & 0x3fff)}// SetClockSequence sets the clock sequence to the lower 14 bits of seq. Setting to// -1 causes a new sequence to be generated.func ( int) {defer timeMu.Unlock()timeMu.Lock()setClockSequence()}func setClockSequence( int) {if == -1 {var [2]byterandomBits([:]) // clock sequence= int([0])<<8 | int([1])}:= clockSeqclockSeq = uint16(&0x3fff) | 0x8000 // Set our variantif != clockSeq {lasttime = 0}}// Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in// uuid. The time is only defined for version 1 and 2 UUIDs.func ( UUID) () Time {:= int64(binary.BigEndian.Uint32([0:4]))|= int64(binary.BigEndian.Uint16([4:6])) << 32|= int64(binary.BigEndian.Uint16([6:8])&0xfff) << 48return Time()}// ClockSequence returns the clock sequence encoded in uuid.// The clock sequence is only well defined for version 1 and 2 UUIDs.func ( UUID) () int {return int(binary.BigEndian.Uint16([8:10])) & 0x3fff}
![]() |
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |