Erlang获取当前时间

编程

可以用erlang:now()和os:timestamp()来当前时间" title="获取当前时间">获取当前时间,返回的是形如{MegaSecs, Secs, MicroSecs}这样的元组,如{1396,707464,903000}。

调用erlang:now()的系统开销似乎比os:timestamp()要大。

时间格式转换:

1> erlang:now().
{1396,707464,903000}
2> T = erlang:timestamp().
** exception error: undefined function erlang:timestamp/0
3> T = os:timestamp().
{1396,707548,396000}
4> T2 = calendar:now_to_universal_time(T).
{{2014,4,5},{14,19,8}}
5> T3 = calendar:now_to_local_time(T).    
{{2014,4,5},{22,19,8}}

 

参考:

now() -> Timestamp

Types:

Timestamp = timestamp()
timestamp() =
{MegaSecs :: integer() >= 0,
Secs :: integer() >= 0,
MicroSecs :: integer() >= 0}
Returns the tuple {MegaSecs, Secs, MicroSecs} which is the elapsed time since 00:00 GMT, January 1, 1970 (zero hour) on the assumption that the underlying OS supports this. Otherwise, some other point in time is chosen. It is also guaranteed that subsequent calls to this BIF returns continuously increasing values. Hence, the return value from now() can be used to generate unique time-stamps, and if it is called in a tight loop on a fast machine the time of the node can become skewed.

It can only be used to check the local time of day if the time-zone info of the underlying operating system is properly configured.

If you do not need the return value to be unique and monotonically increasing, use os:timestamp/0 instead to avoid some overhead.

 

timestamp() -> Timestamp

Types:

Timestamp = erlang:timestamp()  %%这里erlang:timestamp()应该改为os:timestamp()吧
Timestamp = {MegaSecs, Secs, MicroSecs}
Returns a tuple in the same format as erlang:now/0. The difference is that this function returns what the operating system thinks (a.k.a. the wall clock time) without any attempts at time correction. The result of two different calls to this function is not guaranteed to be different.

The most obvious use for this function is logging. The tuple can be used together with the function calendar:now_to_universal_time/1 or calendar:now_to_local_time/1 to get calendar time. Using the calendar time together with the MicroSecs part of the return tuple from this function allows you to log timestamps in high resolution and consistent with the time in the rest of the operating system.
 

以上是 Erlang获取当前时间 的全部内容, 来源链接: utcz.com/z/517561.html

回到顶部