Example of a module using the macro provided
defmodule MyModule do
require Now
use Bitwise, only_operators: true
# generates a prefix based on system time
def gen_prefix do
Now.system_time() &&& 281474976710655
end
end
-
Now.timestamp
->:erlang.timestamp
in v18 and:erlang.now
before -
Now.system_time
->:erlang.system_time
in v18 and a conversion of the triple tuple of:erlang.now
converted in ms.
Warning: although :erlang.timestamp
has the same format as :erlang.now
it is not equivalent, see Find out more
This project is an atempt to answer question posted here on stackoverflow
:erlang.now()
is deprecated in v18, how to make a smooth transition.
defmodule MyModule_v17 do
use Bitwise, only_operators: true
def gen_trans_prefix do
{gs, s, ms} = :erlang.now
(gs * 1000000000000 + s * 1000000 + ms) &&& 281474976710655
end
end
best I came up with:
defmodule MyModule_v18 do
use Bitwise, only_operators: true
Kernel.if Keyword.get(:erlang.module_info, :exports) |> Enum.any?(fn({:system_time, 1}) -> true; (_) -> false end) do
def gen_trans_prefix do
:erlang.system_time(:micro_seconds) &&& 281474976710655
end
else
def gen_trans_prefix do
{gs, s, ms} = :erlang.now
(gs * 1000000000000 + s * 1000000 + ms) &&& 281474976710655
end
end
end
define erlc_options
in mix and use it in MyModule
this is based on input from @potatosalad and jose project
This is already covered in the "Time and Time Correction in Erlang" documentation and also in the "Time Goes On" postscript to the wonderful "Learn You Some Erlang" book. as suggested by Steve Vinoski