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

发布于 2024-12-29 03:13:34 字数 1331 浏览 4 评论 0原文

我正在与远程服务器交互。该远程服务器位于不同的时区。身份验证的一部分要求我提供:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2025-01-05 03:13:34

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

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

仅使用元组的第二个条目即可告诉您自最后一个十进制万亿次以来的时间(以秒为单位) (自 UNIX 纪元以来的秒数)。

[2017年编辑]
now 已弃用,但 erlang:timestamp() 并未弃用,并且返回与 now 相同的格式。

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.

屋檐 2025-01-05 03:13:34

这 3 个参数中哪一个是自 1970 年 1 月 1 日以来所需的秒数?

他们三个,集体。查看给定的时间戳。现在是 1970 年 1 月 7 日。大概 Secs 将介于 0(含)和 1,000,000(不含)之间。一百万秒只有 11.574 天。您需要使用兆秒和秒。由于容错率为 600 秒,您可以忽略 erlang:now() 响应中的微秒部分。

Which of these 3 parameters is the required number of seconds since Jan 1, 1970 ?

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 from erlang:now().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文