[erlang-questions] os:timestamp/0 and erlang:now/0

Karolis Petrauskas k.petrauskas@REDACTED
Fri Mar 13 21:43:10 CET 2015


On Fri, Mar 13, 2015 at 9:41 PM, Darach Ennis <darach@REDACTED> wrote:
> Hi Karolis,
>
> Out of curiosity, why are you comparing os:timestamp() to erlang:now()?

I found a bug in my software, related to the system time. Then I
noticed mismatch of time values in the log messages and log
timestamps. I expected that erlang:now() >= os:timestamp() at any
point in time, but that rendered to be false. That's why I decided to
ask this question here. My bugfix ended up by not using system time in
that place at all.

> There is a possibility that frequent calls to erlang:now() runs ahead of the
> clock as it must deliver unique values (guaranteed) whereas os:timestamp()
> does not have this property. This alone may produce the behaviour you are
> seeing independent of other variables (eg: +c).

Yes, but I observe the opposite.

> I wouldn't have considered either to be reasonably or easily comparable
> against
> each other. However, comparing one fun to precedent values returned by the
> same fun should exhibit the relative behaviour you desire and correlate as
> you
> expect. But comparing os:timestamp to erlang:now doesn't seem supportable
> as they behave differently by design.

Yes, that was the cause for the bug. I was taking system time
(erlang:now()), truncating milliseconds from it and therefore got
rounding errors increased to unexpected level. The following
investigation is just because of curiosity.

> Hence, curious as to your use case and why you are comparing these two
> distinct functions.
>
> Cheers,
>
> Darach.

Karolis



More information about the erlang-questions mailing list