| 1 |
package uuid |
| 2 |
|
| 3 |
import ( |
| 4 |
"time" |
| 5 |
) |
| 6 |
|
| 7 |
const ( |
| 8 |
gregorianToUNIXOffset = 122192928e9 |
| 9 |
|
| 10 |
// set the following to the number of 100ns ticks of the actual |
| 11 |
// resolution of your system's clock |
| 12 |
defaultSpinResolution = 1024 |
| 13 |
) |
| 14 |
|
| 15 |
// 4.1.4. Timestamp https://www.ietf.org/rfc/rfc4122.txt |
| 16 |
// |
| 17 |
// The timestamp is a 60-bit value. For UUID version 1, this is |
| 18 |
// |
| 19 |
// represented by Coordinated Universal Time (UTC) as a count of 100- |
| 20 |
// nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of |
| 21 |
// Gregorian reform to the Christian calendar). |
| 22 |
// |
| 23 |
// For systems that do not have UTC available, but do have the local |
| 24 |
// time, they may use that instead of UTC, as long as they do so |
| 25 |
// consistently throughout the system. However, this is not recommended |
| 26 |
// since generating the UTC from local time only needs a time zone |
| 27 |
// offset. |
| 28 |
// |
| 29 |
// For UUID version 3 or 5, the timestamp is a 60-bit value constructed |
| 30 |
// from a name as described in Section 4.3. |
| 31 |
// |
| 32 |
// For UUID version 4, the timestamp is a randomly or pseudo-randomly |
| 33 |
// generated 60-bit value, as described in Section 4.4. |
| 34 |
type Timestamp uint64 |
| 35 |
|
| 36 |
// Converts Unix formatted time to RFC4122 UUID formatted times |
| 37 |
// UUID UTC base time is October 15, 1582. |
| 38 |
// Unix base time is January 1, 1970. |
| 39 |
// Converts time to 100 nanosecond ticks since epoch. Uses time.Now |
| 40 |
func Now() Timestamp { |
| 41 |
return Timestamp(time.Now().UnixNano()/100 + gregorianToUNIXOffset) |
| 42 |
} |
| 43 |
|
| 44 |
// Converts UUID Timestamp to UTC time.Time |
| 45 |
// Note some higher clock resolutions will lose accuracy if above 100 ns ticks |
| 46 |
func (o Timestamp) Time() time.Time { |
| 47 |
return time.Unix(0, int64((o-gregorianToUNIXOffset)*100)).UTC() |
| 48 |
} |
| 49 |
|
| 50 |
// Returns the timestamp as modified by the duration |
| 51 |
func (o Timestamp) Add(pDuration time.Duration) Timestamp { |
| 52 |
return o + Timestamp(pDuration/100) |
| 53 |
} |
| 54 |
|
| 55 |
// Returns the timestamp as modified by the duration |
| 56 |
func (o Timestamp) Sub(pDuration time.Duration) Timestamp { |
| 57 |
return o - Timestamp(pDuration/100) |
| 58 |
} |
| 59 |
|
| 60 |
// Converts UUID Timestamp to time.Time and then calls the Stringer |
| 61 |
func (o Timestamp) String() string { |
| 62 |
return o.Time().String() |
| 63 |
} |
| 64 |
|
| 65 |
// 4.2.1.2. System Clock Resolution https://www.ietf.org/rfc/rfc4122.txt |
| 66 |
// |
| 67 |
// The timestamp is generated from the system time, whose resolution may |
| 68 |
// be less than the resolution of the UUID timestamp. |
| 69 |
// |
| 70 |
// If UUIDs do not need to be frequently generated, the timestamp can |
| 71 |
// simply be the system time multiplied by the number of 100-nanosecond |
| 72 |
// intervals per system time interval. |
| 73 |
// |
| 74 |
// If a system overruns the generator by requesting too many UUIDs |
| 75 |
// within a single system time interval, the UUID service MUST either |
| 76 |
// return an error, or stall the UUID generator until the system clock |
| 77 |
// catches up. |
| 78 |
// |
| 79 |
// A high resolution timestamp can be simulated by keeping a count of |
| 80 |
// the number of UUIDs that have been generated with the same value of |
| 81 |
// the system time, and using it to construct the low order bits of the |
| 82 |
// timestamp. The count will range between zero and the number of |
| 83 |
// 100-nanosecond intervals per system time interval. |
| 84 |
// |
| 85 |
// Note: If the processors overrun the UUID generation frequently, |
| 86 |
// additional node identifiers can be allocated to the system, which |
| 87 |
// will permit higher speed allocation by making multiple UUIDs |
| 88 |
// potentially available for each time stamp value. |
| 89 |
|
| 90 |
type spinner struct { |
| 91 |
// the amount of ids based on the Timestamp |
| 92 |
Count, Resolution uint |
| 93 |
|
| 94 |
// the tracked spin stamp |
| 95 |
Timestamp |
| 96 |
} |
| 97 |
|
| 98 |
func (o *spinner) next() Timestamp { |
| 99 |
for { |
| 100 |
now := Now() |
| 101 |
// if clock reading changed since last UUID generated |
| 102 |
if o.Timestamp == now { |
| 103 |
o.Count++ |
| 104 |
if o.Count == o.Resolution { |
| 105 |
for Now() < o.Timestamp+Timestamp(o.Resolution) { |
| 106 |
} |
| 107 |
continue |
| 108 |
} |
| 109 |
break |
| 110 |
} |
| 111 |
// reset count of UUIDs with this timestamp |
| 112 |
o.Count = 0 |
| 113 |
o.Timestamp = now |
| 114 |
break |
| 115 |
} |
| 116 |
return o.Timestamp + Timestamp(o.Count) |
| 117 |
} |