自1970年1月1日00:00:00 GMT Erlang以来的秒数

Muz*_*hua 3 unix erlang timezone mnesia

我正在与远程服务器进行交互.此远程服务器位于不同的时区.部分身份验证要求我生成:

"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"

的文件erlang:now().揭示它可以让我the elapsed time since 00:00 GMT, January 1, 1970 (zero hour) on the assumption that the underlying OS supports this.它返回一个size=3元组{MegaSecs, Secs, MicroSecs}.我尝试使用,element(2,erlang:now())但远程服务器发送给我这条消息:

Timestamp expired: Given timestamp (1970-01-07T14:44:42Z)
not within 600s of server time (2012-01-26T09:51:26Z)
自1970年1月1日起,这三个参数中的哪一个是所需的秒数?我做的不对吗?是不是我必须用做universal timecalendar:universal_time() ?

UPDATE
作为一个更新,我设法利用这个关掉时间过期的问题:
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.
但问题仍然存在.必须有一种方法,一种基本的Erlang方式来获得这个,可能是BIF,对吗?

phi*_*hag 7

您必须从结果中计算UNIX时间(自1970年以来的秒数)now(),如下所示:

{MegaSecs, Secs, MicroSecs} = now().
UnixTime = MegaSecs * 1000000 + Secs.
Run Code Online (Sandbox Code Playgroud)

只使用元组的第二个条目将告诉您自上一个十进制万亿分钟以来的秒数(自UNIX纪元以来的秒数).

[2017编辑] now已弃用,但erlang:timestamp()不是,并返回与之相同的格式now.

  • 另请注意,即使调用完全相同,now()也可以保证在不同的线程中产生不同的结果. (2认同)