Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of seconds since January 1, 1970 00:00:00 GMT Erlang

I am interacting with a Remote Server. This Remote Server is in a different Time Zone. Part of the Authentication requires me to produce the:

"The number of seconds since January 1, 1970 00:00:00 GMT
The server will only accept requests where the timestamp
is within 600s of the current time"

The documentation of erlang:now(). reveals that it can get me the the elapsed time since 00:00 GMT, January 1, 1970 (zero hour) on the assumption that the underlying OS supports this. It returns a size=3 tuple, {MegaSecs, Secs, MicroSecs}. I tried using element(2,erlang:now()) but the remote server sends me this message:

Timestamp expired: Given timestamp (1970-01-07T14:44:42Z)
not within 600s of server time (2012-01-26T09:51:26Z)
Which of these 3 parameters is the required number of seconds since Jan 1, 1970 ? What aren't i doing right ? Is there something i have to do with the universal time as in calendar:universal_time() ?

UPDATE
As an update, i managed to switch off the time-expired problem by using this:
seconds_1970()->
    T1 = {{1970,1,1},{0,0,0}},
    T2 = calendar:universal_time(),
    {Days,{HH,Mins,Secs}} = calendar:time_difference(T1,T2),
    (Days * 24 * 60 * 60) + (HH * 60 * 60) + (Mins * 60) + Secs.
However, the question still remains. There must be a way, a fundamental Erlang way of getting this, probably a BIF, right ?
like image 992
Muzaaya Joshua Avatar asked Dec 22 '22 03:12

Muzaaya Joshua


1 Answers

You have to calculate the UNIX time (seconds since 1970) from the results of now(), like this:

{MegaSecs, Secs, MicroSecs} = now().
UnixTime = MegaSecs * 1000000 + Secs.

Just using the second entry of the tuple will tell you the time in seconds since the last decimal trillionellium (in seconds since the UNIX epoch).

[2017 Edit] now is deprecated, but erlang:timestamp() is not and returns the same format as now did.

like image 77
phihag Avatar answered Jan 12 '23 22:01

phihag