2 ms·
The literal Elixir code is: def epoched_random_uid, do: epoched_random_uid(&:erlang.system_time/1, &:crypto.strong_rand_bytes/1) def epoched_random_uid(sys
by Confiks 9y ago
The literal Elixir code is:
def epoched_random_uid, do: epoched_random_uid(&:erlang.system_time/1, &:crypto.strong_rand_bytes/1)
def epoched_random_uid(system_time, strong_rand_bytes) do
time = system_time.(:second)
randint = strong_rand_bytes.(4) |> :crypto.bytes_to_integer
bor(time <<< 31, randint >>> 1)
end
So it uses – or at least I hope it does; you may file a bug report – a full 32 bits for the time, but it leaves the sign bit to be zero. I didn't want to deal with negative uids in PostgreSQL (bigint), but they actually would be perfectly valid. The sign bit could be used to expand the timestamp another bit.
You could argue that the "2038 problem" (31 effective bits) in the context of a uid scheme of an application that is bootstrapped today, is actually the "2084 problem", because the wraparound to timestamps in 1970 could be construed as being from 2018.