Custom UUID based on time I've been using random UUIDs&mdash;from Rust, e.g. Uuid::new_v4()&mdash;for my primary keys in a MySQL database. I recently happened across this, which suggests that UUIDs with an appropriately positioned time component offer some performance improvements.

I created a naive UUID generator like:

fn uuid_new() -&gt; ::uuid::Uuid {

    use ::rand::Rng;

    let now = ::std::time::SystemTime::now()
        .duration_since(::std::time::UNIX_EPOCH)
        .unwrap();

    let (secs, nans): (u64, u32) = (now.as_secs(), now.subsec_nanos());

    let rbts: [u8; 6] = ::rand::thread_rng().gen();

    let bytes = [
        // (secs &gt;&gt; 56) as u8,
        // (secs &gt;&gt; 48) as u8,
        (secs &gt;&gt; 40) as u8,
        (secs &gt;&gt; 32) as u8,
        (secs &gt;&gt; 24) as u8,
        (secs &gt;&gt; 16) as u8,
        (secs &gt;&gt;  8) as u8,
        (secs &gt;&gt;  0) as u8,
        (nans &gt;&gt; 24) as u8,
        (nans &gt;&gt; 16) as u8,
        (nans &gt;&gt;  8) as u8,
        (nans &gt;&gt;  0) as u8,
        rbts[0], rbts[1], rbts[2], rbts[3], rbts[4], rbts[5]
    ];

    ::uuid::Uuid::from_bytes(&amp;bytes).unwrap()

}


I have no requirements for my UUIDs except speed and uniqueness. I'm truncating the first two octets of the secs u64, since they don't seem necessary for the near future.

Am I committing any faux pas here? I don't really know anything about the theory or practice of UUIDs. I'm a little bit worried since the value of get_version_num is gibberish for my UUIDs (though I am not using version number in any way).

Or are there any alternate (Rust) implementations for UUIDs with a time component?
No, that is not one of the five standard-compliant ways to generate a UUID.  What you have written is just an obfuscated concatenation of a timestamp and a random number.

The easiest way might be to use the MySQL UUID() function instead of generating the UUID in Rust.