[erlang-questions] microseconds since midnight

Ulf Wiger ulf.wiger@REDACTED
Sun Jan 10 12:22:13 CET 2010


Henning Diedrich wrote:
> Hi Ulf,
> 
> As it is now, I don't poll time twice. But I depend on state,using a 
> constant MidNightOffset.

But you do check it twice:

> nightsynctime_offset() ->
>
>     {_, Time_1} = erlang:universaltime(),   %% <- HERE
>     SSMN_1 = calendar:time_to_seconds(Time_1),
>     {MegaSec_2, Seconds_2, _} = now(),      %% <- AND HERE
>     % MidNightOffset =
>     (MegaSec_2 * 1000000) + Seconds_2 - SSMN_1.

Rather than first calling universaltime() and then calling now,
call now() once, and then use calendar:now_to_datetime(Now) to
derive the other representation from that one sample.

When you run small-scale tests without load, you won't notice a
difference, but on a heavily loaded system, the admittedly unlikely
event that the process is scheduled out between the call to 
universaltime() and the call to now() right around midnight, could
well give you a situation where the universaltime() call shows
{23,59,59}, while the now() call shows a time right after midnight.

But as you also talked about sending these values to other nodes,
you will of course also have to contend with the fact that the
system clocks on different nodes can at best be sychronized to
within a few ms using NTP. You should never rely on wall-clock
time for synchronization across the network.

But perhaps I misunderstood your intent?

BR,
Ulf W
-- 
Ulf Wiger
CTO, Erlang Solutions Ltd, formerly Erlang Training & Consulting Ltd
http://www.erlang-solutions.com
---------------------------------------------------

---------------------------------------------------

WE'VE CHANGED NAMES!

Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.

www.erlang-solutions.com



More information about the erlang-questions mailing list