dojox.uuid.generateTimeBasedUuid._generator.generateUuidString
dojo.require("dojox.uuid.generateTimeBasedUuid");
defined in dojox/uuid/generateTimeBasedUuid.js
JavaScript code running in a browser doesn't have access to the IEEE 802.3 address of the computer, so if a node value isn't supplied, we generate a random pseudonode value instead.
Usage
function (/*String?*/ node) (view source)
if(node){ dojox.uuid.assert(dojox.uuid.generateTimeBasedUuid.isValidNode(node)); }else{ if(dojox.uuid.generateTimeBasedUuid._uniformNode){ node = dojox.uuid.generateTimeBasedUuid._uniformNode; }else{ if(!_uuidPseudoNodeString){ var pseudoNodeIndicatorBit = 0x8000; var random15bitNumber = Math.floor( (Math.random() % 1) * Math.pow(2, 15) ); var leftmost4HexCharacters = (pseudoNodeIndicatorBit | random15bitNumber).toString(HEX_RADIX); _uuidPseudoNodeString = leftmost4HexCharacters + _generateRandomEightCharacterHexString(); } node = _uuidPseudoNodeString; } } if(!_uuidClockSeqString){ var variantCodeForDCEUuids = 0x8000; // 10--------------, i.e. uses only first two of 16 bits. var random14bitNumber = Math.floor( (Math.random() % 1) * Math.pow(2, 14) ); _uuidClockSeqString = (variantCodeForDCEUuids | random14bitNumber).toString(HEX_RADIX); } // Maybe we should think about trying to make the code more readable to // newcomers by creating a class called "WholeNumber" that encapsulates // the methods and data structures for working with these arrays that // hold 4 16-bit numbers? And then these variables below have names // like "wholeSecondsPerHour" rather than "arraySecondsPerHour"? var now = new Date(); var millisecondsSince1970 = now.valueOf(); // milliseconds since midnight 01 January, 1970 UTC. var nowArray = _get64bitArrayFromFloat(millisecondsSince1970); if(!_cachedMillisecondsBetween1582and1970){ var arraySecondsPerHour = _get64bitArrayFromFloat(60 * 60); var arrayHoursBetween1582and1970 = _get64bitArrayFromFloat(dojox.uuid.generateTimeBasedUuid._generator.GREGORIAN_CHANGE_OFFSET_IN_HOURS); var arraySecondsBetween1582and1970 = _multiplyTwo64bitArrays(arrayHoursBetween1582and1970, arraySecondsPerHour); var arrayMillisecondsPerSecond = _get64bitArrayFromFloat(1000); _cachedMillisecondsBetween1582and1970 = _multiplyTwo64bitArrays(arraySecondsBetween1582and1970, arrayMillisecondsPerSecond); _cachedHundredNanosecondIntervalsPerMillisecond = _get64bitArrayFromFloat(10000); } var arrayMillisecondsSince1970 = nowArray; var arrayMillisecondsSince1582 = _addTwo64bitArrays(_cachedMillisecondsBetween1582and1970, arrayMillisecondsSince1970); var arrayHundredNanosecondIntervalsSince1582 = _multiplyTwo64bitArrays(arrayMillisecondsSince1582, _cachedHundredNanosecondIntervalsPerMillisecond); if(now.valueOf() == _dateValueOfPreviousUuid){ arrayHundredNanosecondIntervalsSince1582[3] += _nextIntraMillisecondIncrement; _carry(arrayHundredNanosecondIntervalsSince1582); _nextIntraMillisecondIncrement += 1; if (_nextIntraMillisecondIncrement == 10000) { // If we've gotten to here, it means we've already generated 10,000 // UUIDs in this single millisecond, which is the most that the UUID // timestamp field allows for. So now we'll just sit here and wait // for a fraction of a millisecond, so as to ensure that the next // time this method is called there will be a different millisecond // value in the timestamp field. while (now.valueOf() == _dateValueOfPreviousUuid) { now = new Date(); } } }else{ _dateValueOfPreviousUuid = now.valueOf(); _nextIntraMillisecondIncrement = 1; } var hexTimeLowLeftHalf = arrayHundredNanosecondIntervalsSince1582[2].toString(HEX_RADIX); var hexTimeLowRightHalf = arrayHundredNanosecondIntervalsSince1582[3].toString(HEX_RADIX); var hexTimeLow = _padWithLeadingZeros(hexTimeLowLeftHalf, 4) + _padWithLeadingZeros(hexTimeLowRightHalf, 4); var hexTimeMid = arrayHundredNanosecondIntervalsSince1582[1].toString(HEX_RADIX); hexTimeMid = _padWithLeadingZeros(hexTimeMid, 4); var hexTimeHigh = arrayHundredNanosecondIntervalsSince1582[0].toString(HEX_RADIX); hexTimeHigh = _padWithLeadingZeros(hexTimeHigh, 3); var hyphen = "-"; var versionCodeForTimeBasedUuids = "1"; // binary2hex("0001") var resultUuid = hexTimeLow + hyphen + hexTimeMid + hyphen + versionCodeForTimeBasedUuids + hexTimeHigh + hyphen + _uuidClockSeqString + hyphen + node; resultUuid = resultUuid.toLowerCase(); return resultUuid; // String (a 36 character string, which will look something like "b4308fb0-86cd-11da-a72b-0800200c9a66")
parameter | type | description |
---|---|---|
node | String | Optional. An optional 12-character string to use as the node in the new UUID. |