On Sun, Sep 26, 2010 at 1:46 PM, Robert Newson <[email protected]> wrote:
> There are three algorithms available from the uuid generator inside
> couchdb, "utc_random", "random" and "sequential". Only "utc_random"
> includes the current time thus;
>
> utc_random() ->
>    Now = {_, _, Micro} = now(),
>    Nowish = calendar:now_to_universal_time(Now),
>    Nowsecs = calendar:datetime_to_gregorian_seconds(Nowish),
>    Then = calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}),
>    Prefix = io_lib:format("~14.16.0b", [(Nowsecs - Then) * 1000000 + Micro]),
>    list_to_binary(Prefix ++ couch_util:to_hex(crypto:rand_bytes(9))).
>
> It should be possible to extract the time if you ignore the random
> piece on the end. However, you are not forced to use the uuid
> generator. If your documents ids need to contain the time, why not
> specify the ids yourself and thereby control the format precisely?
>
> B.
>
> On Sun, Sep 26, 2010 at 5:28 PM, sleepy <[email protected]> wrote:
>> Some of the UUID generation function use current time to generate UUID.
>> Is CouchDB's UUID generation algorithm also take current time as part of
>> it's generated UUID?
>> If the answer is yes. How can I extract it out from a generated UUID?
>>
>

Also, I sure wouldn't depend on being able to pull the time out of a
UUID. They're intended to be opaque so if things change and it breaks
your code, you don't have any sort of recourse to complain about.
Heck, even if someone happens to switch the algorithm on you it'd
break.

IOW, you have the power to do this, but I'd take heed of Rob's advice
and just generate your own UUIDs client side.

HTH,
Paul Davis

Reply via email to