In my project erlang:now is converted to a high precision timestamp (bigint) for storage in MySQL:
timestamp({Mega, Secs, Micro}) ->
Mega*1000*1000*1000*1000 + Secs * 1000 * 1000 + Micro.
I now convert the timestamp back to the orginal {Mega, Secs, Micro} tuple using:
time_tuple(Timestamp) ->
TimeList = erlang:integer_to_list(Timestamp),
Mega = erlang:list_to_integer(string:substr(TimeList, 1, 4)),
Sec = erlang:list_to_integer(string:substr(TimeList, 5, 6)),
Micro = erlang:list_to_integer(string:substr(TimeList, 11, 6)),
{Mega, Sec, Micro}.
The string conversion / substr feels like an ugly, and possible incorrect, hack. What would be a more elegant way?
I might be missing something, but why don't you just use division and modulo for that?
> {Mega, Sec, Micro} = now().
> Timestamp = Mega * 1000000 * 1000000 + Sec * 1000000 + Micro.
> {Mega1, Sec1, Micro1} = {Timestamp div 1000000000000,
Timestamp div 1000000 rem 1000000,
Timestamp rem 1000000}.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With