4 MINDS

4MINDS Gestão de Conhecimento

erlang datetime to erlang now




Function to convert erlang datetime to erlang now(), used to mochijson2.


datetime_to_now(DateTime) ->
  BaseDate = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}) * 1000000,
  MicroSeconds = calendar:datetime_to_gregorian_seconds(DateTime) * 1000000,
ErlangSystemTime = MicroSeconds - BaseDate,
MegaSecs = ErlangSystemTime div 1000000000000, 
Secs = ErlangSystemTime div 1000000 - MegaSecs * 1000000,
MicroSecs = ErlangSystemTime rem 1000000,
{MegaSecs, Secs, MicroSecs}.