tags:

views:

53

answers:

1

Is there any easy way to convert from erlang datetime notation to the now/0 notation?

Basically I need the inverse of this function:

{Date, Time} = calendar:now_to_datetime(now()).

So something like

{Megaseconds, Seconds, Microsecods} = datetime_to_now({Date, Time})
+2  A: 

I retract my question. My google-fu was not warmed up yet it seems. The Following does exactly what I want.

-define(GREGORIAN_SECONDS_1970, 62167219200).

datetime_to_now(DateTime) ->
    GSeconds = calendar:datetime_to_gregorian_seconds(DateTime),
    ESeconds = GSeconds - ?GREGORIAN_SECONDS_1970,
    {ESeconds div 1000000, ESeconds rem 1000000, 0}.
Olives
what does GREGORIAN_SECONDS_1970 magical number stands for?
Vanya
Erlang's now() calcuates that number of megasecond/seconds/microseconds from Janurary 1st 1970. However, datetime_to_gregorian_seconds calcuates the number of seconds from the start of the calendar. The magic number is the number of seconds up until 1970.I guess it could've been written-define(GREGORIAN_SECONDS_1970, calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0,0,0}}).
Olives