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

Matthias Lang matthias@REDACTED
Sat Feb 2 17:32:12 CET 2008


Hi,

 > Is it there anything like the opposite of  now_to_universal_time(Now)?

The documentation for erlang:now() tells us that the now() epoch is 00:00 GMT,
January 1, 1970. Putting that on a spoon:

  Seconds = calendar:datetime_to_gregorian_seconds(Datetime) - 62167219200,
  Now = {Seconds div 1000000, Seconds rem 1000000, 0}.

 > Anyway, is there any easy way to use a larger timeout than it is
 > representable by a single integer?

If you mean "I don't like the limit 'receive' has and I want it to be
higher", then, no, there is no easy way.

If you mean "is there some way I can make a delay longer than the
limit imposed by the timeout for 'receive'?", then the answer is sure,
there are lots of ways. Remember what I said about not blocking code
for extended periods? Here's one way to do it:

  sleep(Milliseconds) when Milliseconds =< 60000 ->
       receive after Milliseconds -> done end;
  
  sleep(Milliseconds) ->
       sleep(60000),
       my_timer:sleep(Milliseconds - 60000).

Finally, an attempt to put this tedious thread out of its misery:

  %% Same as timer:send_after, except that the first argument is
  %% an absolute time in the format returned by calendar:now_to_universal_time
  send_on(Datetime, Pid, Message) ->
    End = calendar:datetime_to_gregorian_seconds(Datetime),
    Now = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
    Diff = (End - Now),  
    timer:send_after(1000 * Diff, Pid, Message).

Matthias



More information about the erlang-questions mailing list