[erlang-questions] Newbie question: wait until an absolute time

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Fri Feb 1 16:04:26 CET 2008


Alpár Jüttner skrev:
> Dear all,
> 
> Is there any simple way in erlang to wait until a certain absolute time
> or to do something at a specific time in the future ? Namely, I'm
> looking for the absolute counterpart of the functions sleep(),
> send_after(), apply_after() and the 'after TimeOut ->'.
> 
> Is it there anything like the opposite of  now_to_universal_time(Now)?
> 
> Anyway, is there any easy way to use a larger timeout than it is
> representable by a single integer?

As has been noted already, you cannot use timeout values that are larger 
than a smallint. A way to do it is to split the timeout into shorter 
waits, and then calculate the next timeout. Whether you want to wait in 
five-minute intervals or several hours at a time, is pretty much up to 
you. There are pros and cons with each. If the system idles a lot, you 
might not want to trigger activity unnecessarily; if it practically 
never idles, you might priorities readiness to handle things like 
unexpected jumps in the system time (when some operator changes the
clock 8 hours back or forth in a running system...)

A reasonably efficient diff between now() timestamps can look like this:

diff({M,S,U}, {M,S1,U1}) ->
     ((S-S1) * 1000) + ((U-U1) div 1000);
diff({M,S,U}, {M1,S1,U1}) ->
     ((M-M1)*1000000+(S-S1))*1000 + ((U-U1) div 1000).

The main trick is to avoid creating bignums. At least this used to be 
quicker, when I benchmarked it years ago. That pesky erlang team tends 
to change things in ways that sometimes invalidates old assumptions,
though. (:

BR,
Ulf W



More information about the erlang-questions mailing list