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?