Search code examples
erlangunix-timestampdatetime-conversion

How to convert Unix Epoch Timestamp to Erlang timestamp format?


I'm trying to add a few extra features to my ejabberd mod_muc_room, but jlib:now_to_utc_string doesn't seem to accept Unix timestamps and requires them to be in Erlang's built-in format. Trying to use "1519633372486003" instead of "{1519,633372,486003}" makes mod_muc_room crash.

I found at least several ways to convert an Erlang timestamp into a Unix timestamp, but I can't find a way to make a reverse conversion.

Is there a way to do that without converting integer to binary and binary to tuple before concatenating the numbers together and converting them back into numbers?


Solution

  • You can use div and rem to extract the three values:

    1> M = 1000000.
    1000000
    2> T = 1519633372486003.
    1519633372486003
    3> {T div M div M, T div M rem M, T rem M}.
    {1519,633372,486003}