erlang time handling (in calendar.erl and erlang.erl)

Matthias Lang matthias@REDACTED
Mon Jan 31 11:27:35 CET 2005

Ulf Wiger writes:

 > erlang:now() periodically samples the
 > system clock and adjusts itself accordingly, there should be
 > no need to insert leap seconds. Event though erlang:now()
 > normally follows the system clock quite closely, there is no
 > guarantee that it will shows accurate time at any given sample.

Rant Mode On.

This clock fudging is my #2 pet peeve about Erlang.

In Erlang, now() tries to solve all timing problems---it promises to
track real time while still preserving intervals. For good measure, it
also promises to return "continuously increasing" values (more
commonly called "strictly increasing").

The result is that nothing behaves when the user makes a dramatic
adjustment to the time (1). Timeouts and intervals are off by 1% (2)
and timestamps can be off by an arbitrarily large amount for an
arbitrarily long time (3).

Solutions? It's probably too late to fix now() since there's a lot of
code out there depending on various, mutually incompatible aspects of
its behaviour. But if you don't have any old code to break, it's not
too hard to disable the clock-fudging. It's in 


A less intrusive alternative is to use a C port program to measure
time intervals (4).


(1) This is not as unreasonable as it sounds. For instance, you
    usually have to configure the clock on a brand new system. It
    seems a bit too Microsoft to say "You have altered the time. 
    Applications may now behave strangely. Click OK to reboot".

(2) This is particularly annoying when your system enters "Spinal Tap
    Emulation Mode" during a customer's stress testing, e.g. claiming
    that the load on a signalling link is 101%

(3) This is particularly annoying when timestamps in logfiles don't
    agree with reality. erlang:universaltime() partially solves this,
    but only to low resolution.
(4) Puzzle for C/POSIX/Unix wizards: how do you measure elapsed time
    in C, to millisecond resolution, in such a way that it doesn't lie
    when the user adjusts the clock. There is a portable solution.

Rant Mode Off.

More information about the erlang-questions mailing list