自 1970 年 1 月 1 日 00:00:00 GMT Erlang 以来的秒数
我正在与远程服务器交互。该远程服务器位于不同的时区。身份验证的一部分要求我提供:
"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()
。显示它可以让我得到自 1970 年 1 月 1 日 00:00 GMT(零时)以来经过的时间 假设底层操作系统支持此。它返回一个 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)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 ?
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)
. It returns a
on the assumption that the underlying OS supports thissize=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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须根据
now()
的结果计算 UNIX 时间(自 1970 年以来的秒数),如下所示:仅使用元组的第二个条目即可告诉您自最后一个十进制万亿次以来的时间(以秒为单位) (自 UNIX 纪元以来的秒数)。
[2017年编辑]
now
已弃用,但erlang:timestamp()
并未弃用,并且返回与now
相同的格式。You have to calculate the UNIX time (seconds since 1970) from the results of
now()
, like this: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, buterlang:timestamp()
is not and returns the same format asnow
did.他们三个,集体。查看给定的时间戳。现在是 1970 年 1 月 7 日。大概
Secs
将介于 0(含)和 1,000,000(不含)之间。一百万秒只有 11.574 天。您需要使用兆秒和秒。由于容错率为 600 秒,您可以忽略erlang:now()
响应中的微秒部分。All three of them, collectively. Look at the given timestamp. It's January 7, 1970. Presumably
Secs
will be between 0 (inclusive) and 1,000,000 (exclusive). One million seconds is only 11.574 days. You need to use the megaseconds as well as the seconds. Since the error tolerance is 600 seconds you can ignore the microseconds part of the response fromerlang:now()
.