now() and arithmetic

Ulf Wiger etxuwig@REDACTED
Mon May 6 10:27:48 CEST 2002


On Mon, 6 May 2002, Bruce Fitzsimons wrote:

>> Keep in mind that now() does 'interesting' things if you rapidly
>> change the system time. If you really want to know what the system
>> thinks the time is, then time/0 may be a better choice.
>
>Its more for TimeToLive calculations, and the complications of
>converting time+date from/to a serial time value annoy me enough
>to (possibly foolishly) disregard the possibility. Does TZ
>actually affect now() on some platforms? Is this the "rapidly
>changing system time" or have you had some crazy users playing
>with the time?

No, it's manually changing the system time (or semi-automatically
via e.g. rdate) that causes problems for erlang:now().
Matthias was probably referring to this thread, and related
problems:

http://www.erlang.org/ml-archive/erlang-questions/200203/msg00033.html


The following code calculates the difference between two
erlang:now() values without using bignums. It is significantly(?)
cheaper than e.g. the calculation used in timer:tc/3.

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).

(Of course, the difference won't be significant unless the
calculation is performed extremely often -- which is probably
never true for timer:tc/3.)

/Uffe
-- 
Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson Telecom AB, ATM Multiservice Networks




More information about the erlang-questions mailing list