UUID.New ( )

Function stats

Average user rating
260
274
9999
Support
FileMaker 10.0 +
Date posted
04 October 2010
Last updated
13 May 2011
Version
Recursive function
Yes

Author Info
 Jeremy Bante

10 functions

Average Rating 5.0

author_avatar



 

Function overview

Prototype

UUID.New  (  )


Parameters

No parameters for this function.


Description

Tags:  uuid   primary key  

UUIDTimeNIC creates a numeric universally unique identifier suitable for use as a primary key in FileMaker number fields. The UUID is an encoding of the creation timestamp, a session-specific serial number, and NIC (MAC) address of the machine that created the record.

Examples

Sample input

UUIDTimeNIC


Sample output

1-2-063440622095-2300000-04098-000102857824654

 

Function code

/**
* =====================================
* UUIDTimeNIC
*        https://github.com/jbante/FileMaker-Techniques/blob/master/CustomFunctions/UUID/UUIDTimeNIC.fmfn
*
* PURPOSE:
*        Creates a universally unique identifier suitable for use as a primary
*        key in FileMaker number fields. The UUID is an encoding of the creation
*        timestamp, a serial number, and the NIC (MAC) address of the device that
*        created the UUID. The values returned by this and related functions have
*        a one-to-one correspondence with UUIDs following the RFC 4122 standard
*        all values in the format generated by this function can be converted to
*        RFC 4122, and vice versa.
*
* RETURNS:
*        A 41-digit delimited number of the form:
*        v-r-tttttttttttt-sssssss-ccccc-nnnnnnnnnnnnnnn
*        The sections of the UUID correspond to:
*        v: A UUID version (type) number
*        r: A variant code reserved by the RFC 4122 standard
*        t: The creation timestamp (seconds since 0001-01-01T00:00:00)
*        s: A serial number, reset for each second
*        c: A session key, randomly generated for each session
*        n: The NIC address ("node") of the device that created the UUID
*
* PARAMETERS: none
*
* DEPENDENCIES: none
*
* VARIABLES:
*        $$~UUID_DATA
*
* NOTES:
*        For most practical purposes, FileMaker will ignore the text delimiters
*        in values returned by this function.
*
*        This function generates UUIDs that are meaningfully sortable. Values
*        sort by version, then approximate creation order. Because of the leading
*        sort by version, random (version 4) values (where the creation order
*        cannot be determined by the values) will cluster separately from
*        timestamp-node (version 1) values.
*
*        The value can be converted to RFC 4122 canonical form (hexadecimal) with
*        the UUIDGetAsRFC4122 function. The creation timestamp and NIC (MAC)
*        address can be extracted with the UUIDGetTimestamp and UUIDGetNICAddress
*        functions.
*
* HISTORY:
*        MODIFIED on 2011-04-03 by Jeremy Bante to generate ~serial number values
*        that result in easier-to-read timestamps from the UUIDGetTimestamp
*        function.
*        MODIFIED on 2011-02-25 by Jeremy Bante to use a random node when there
*        isn't a system NIC address, rather than resorting to UUIDRandom.
*        MODIFIED on 2011-02-23 by Jeremy Bante to use a randomly generated
*        session key for the RFC 4122 "clock sequence".
*        MODIFIED on 2011-02-02 by Jeremy Bante to use a format with a bijective
*        relationship with the RFC 4122 format.
*        MODIFIED on 2010-12-06 by Jeremy Bante to store the base 10-encoded node
*        between calls to the function, making successive calls much faster.
*        MODIFIED on 2010-11-22 by Jeremy Bante <http://scr.im/jbantetsg> to
*        create IDs in a format more consistent with the RFC 4122 and RFC 5322
*        standards.
*        MODIFIED on 2010-10-04 by Jeremy Bante to prevent random NIC (MAC)
*        addresses from colliding with actual NIC addresses.
*        CREATED on 2010-09-06 by Jeremy Bante <jeremy@kyologic.com>.
*        INSPIRED by UUID functions created by Tom Robinson and Ray Cologon.
*
* REFERENCES:
*        Key values Best Practice: http://filemakerstandards.org/pages/viewpage.action?pageId=557138
*        RFC 4122 standard UUID specification: http://tools.ietf.org/html/rfc4122
*        Tom Robinson's UUID function: http://www.briandunning.com/cf/969
*        Ray Cologon's uID functions: http://www.nightwing.com.au/FileMaker/demos9/demo910.html
*        UUID functions: https://github.com/jbante/FileMaker-Techniques/tree/master/CustomFunctions/UUID
* =====================================
*/

Case (
    /*First call to UUIDTimeNIC, check for pre-processed node data*/
    not $~step;
        Let ( [
                $~node = GetValue ( $$~UUID_DATA ; 4 );
                $~step = If ( $~node ; 3 ; /*else*/ 1 )
        ];
            UUIDTimeNIC
        );

    /*Get NIC address, if possible*/
    $~step = 1;
        Let ( [
            $~nicAddress =
                Substitute (
                    GetValue ( Get ( SystemNICAddress ); 1 );
                    ":";
                    ""
                );
            $~i = 0;
            $~step = If ( $~nicAddress ; $~step + 1 ; /*else*/ 3 )
        ];
            UUIDTimeNIC
        );

    /*Parse NIC address from hexadecimal to a (base 10) number*/
    $~step = 2;
        Let ( [
            $~nicDigit = Middle ( $~nicAddress ; 12 - $~i ; 1 );
            $~nicDigit = Position ( "0123456789abcdef" ; $~nicDigit ; 1 ; 1 ) - 1; //convert digit to number
            $~node = $~nicDigit * ( 16 ^ $~i ) + $~node; //add digit to node
            $~step = If ( $~i < 11 ; $~step ; /*else*/ $~step + 1 );
            $~i = $~i + 1
        ];
            UUIDTimeNIC
        );

    /*Concatenate information*/
    $~step = 3;
        Let ( [
            //Set-up timestamp, serial number, and session key
            ~session = GetValue ( $$~UUID_DATA ; 1 );
            ~lastTimestamp = GetValue ( $$~UUID_DATA ; 2 );
            ~serial = GetAsNumber ( GetValue ( $$~UUID_DATA ; 3 ) );

            ~now =
                Mod (
                    GetAsNumber ( Get ( CurrentHostTimeStamp ) );
                    165208454460
                );
            ~session =
                Case (
                    IsEmpty ( ~session ); //new session
                        Floor ( Random * 16384 );

                    ~serial ≥ .9999999 //serial number overflow
                    and ~now = ~lastTimestamp
                    or ~now < ~lastTimestamp; //clock change
                        Mod ( ~session + 1 ; 16384 );

                    /*else*/
                        ~session
                );
            ~magnitude =
                Max (
                    Left ( Substitute ( ~serial ; "." ; "" ) ; 1 );
                    1
                );
            ~serial =
                Case (
                    ~now ≠ ~lastTimestamp or ~serial = .9999999 ; 0;
                    ~serial = 0 ; .1;
                    ~serial < .7 ; ~serial + 10 ^ ( 0 - ~magnitude );
                    /* Else */ ~serial + 10 ^ -7
                );
            ~node =
                If ( $~node;
                    $~node;
                /*Else, new random node*/
                    //set multicast bit to avoid colliding with NIC addresses
                    ( Floor ( Random * 128 ) *2 + 1 ) * 1099511627776
                    + Floor ( Random * 1099511627776 )
                );

            //Store data for future reference
            $$~UUID_DATA = List ( ~session ; ~now ; ~serial ; ~node );

            //purge variables
            $~i = "";
            $~nicAddress = "";
            $~nicDigit = "";
            $~node = "";
            $~step = ""
        ];
            //v-r-tttt tttt tttt-sss ssss-ccccc-nnnnn nnnnn nnnnn
            "1-2-" //version and reserved variant code
            & Right ( "000000000000" & ~now ; 12 )
            & "-"
            & Left ( Substitute ( ~serial ; "." ; "" ) & "0000000" ; 7 )
            & "-"
            & Right ( "00000" & ~session ; 5 )
            & "-"
            & Right ( "000000000000000" & ~node ; 15 )
        )
)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/274

    Prototype: UUID.New( )
    Function Author: Jeremy Bante (http://www.fmfunctions.com/mid/260)
    Last updated: 13 May 2011
    Version: 4.0

*/
// ===================================

 

Comments

metalox
16 April 2014



Hi, I'd like to use this function but get various errors when trying to create the custom function, specifically "The specified parameter cannot be found." staring with .9999999 at line 132. I have a German Locale - deciamal "," instaed of "." - could this be a problem? (Wild speculation) - Using FileMaker13.0v2 Advanced on Mac OS X 10.9.2
(Edited by metalox on 17/04/14 )
 Bug report

 

 

 

 

 

Top Tags

Text Parsing  (33)
List  (32)
Date  (28)
XML  (27)
Format  (23)
Sql  (22)
Dev  (20)
Debug  (17)
Layout  (15)
Interface  (15)
Text  (14)
Variables  (13)
Filter  (12)
Layout Objects  (11)
Design  (10)
Array  (8)