Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions version7.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,19 @@ func getV7Time() (milli, seq int64) {

nano := timeNow().UnixNano()
milli = nano / nanoPerMilli
// Sequence number is between 0 and 3906 (nanoPerMilli>>8)
seq = (nano - milli*nanoPerMilli) >> 8
if hasSubMilliClock {
// Sequence number is between 0 and 3906 (nanoPerMilli>>8)
seq = (nano - milli*nanoPerMilli) >> 8
} else {
// The platform's wall clock has only millisecond resolution (e.g. on
// wasm), so the sub-millisecond fraction above would always be zero.
// Fill the 12-bit rand_a field with random data instead, as permitted
// by RFC 9562. Ordering within a millisecond is still preserved by the
// lastV7time+1 step below.
var b [2]byte
randomBits(b[:])
seq = int64(b[0]&0x0f)<<8 | int64(b[1])
}
now := milli<<12 + seq
if now <= lastV7time {
now = lastV7time + 1
Expand Down
12 changes: 12 additions & 0 deletions version7_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2026 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.

// +build !js,!wasip1

package uuid

// hasSubMilliClock reports whether the platform's wall clock is guaranteed to
// have sub-millisecond resolution. Non-wasm targets provide it, so v7 derives
// the rand_a field from a real sub-millisecond timestamp fraction.
const hasSubMilliClock = true
14 changes: 14 additions & 0 deletions version7_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2026 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.

// +build js wasip1

package uuid

// hasSubMilliClock reports whether the platform's wall clock is guaranteed to
// have sub-millisecond resolution. On the wasm targets (js and wasip1) the
// resolution is host-dependent and sub-millisecond precision is not
// guaranteed, so v7 fills the rand_a field with random bits instead of a
// timestamp fraction.
const hasSubMilliClock = false