adamu

adamu

System.system_time vs System.os_time

Does anybody know the practical implications of using System.os_time/0 vs System.system_time/0?

I notice DateTime.utc_now/0 uses os_time/0.

There is some mention of the differences in the docs for system_time/0:

It is the VM view of the os_time/0. They may not match in
case of time warps although the VM works towards aligning
them. This time is not monotonic.

Likewise, the Erlang docs also discuss some implications:

OS System Time

The operating systems view of POSIX time. To retrieve it, call os:system_time(). This may or may not be an accurate view of POSIX time. This time may typically be adjusted both backwards and forwards without limitation. That is, time warps may be observed.

Erlang System Time

The Erlang runtime systems view of POSIX time. To retrieve it, call erlang:system_time().

This time may or may not be an accurate view of POSIX time, and may or may not align with OS system time. The runtime system works towards aligning the two system times. Depending on the time warp mode used, this can be achieved by letting Erlang system time perform a time warp.

However, besides the talk of time warps and what exactly each of the functions return, there is never really any advice about when you would want one or the other.

Most Liked

BartOtten

BartOtten

As most servers use Network Time Protocol (NTP) the time may jump as time drifts and it’s synchronized with the external clock. Have seen this happen in production and the results weren’t pretty. Scanner imports were rejected because the servers’ clock jumped backward, even when it was a small amount.

Those are the cases where system_time seems to be designed for, where ‘system’ is ‘Erlang’. You can tune how it behaves when such jump (or drift) happens. For instance: only allow ‘time’ to go forward, no jumps (using correction).

As a result the time you get might not immediately be the time you expect, but it’s more robust and predictable from a systems’ perspective.

Recommendations

os_time for user observable time as it matches expectation and wall clock. Examples:

  • the new log entry has a timestamp matching the clock on the wall
  • the clock shown on the screen matches the clock on the wall

system_time for internal time(rs) & time measuring as external interference with the clock is dampened and the clock is more predictable for processes. Examples:

  • order events by timestamp will always match order by incremental id
  • processes expected to run on a strict interval will not alert due to a time jump

ps. I am not an expert
pps. There is no better way then getting the answer from an expert than posting a wrong answer. So when it’s wrong we will find out soon.

13
Post #6
josevalim

josevalim

Creator of Elixir

I would say OS time is correct. For an embedded device Or desktop application, if the os time changes, then it should be immediately reflected in the app. The same for log timestamps and so on. For servers I may not Matter anyway as they are usually in UTC and do not observe jumps.

al2o3cr

al2o3cr

I happened across this article recently - the linked section digs into which flavors of time you might want for various applications:

adamu

adamu

That seems to make sense, but if that’s the case then what is the point of system time and time warps existing at all?

Quoting the erlang docs again:

The number of problems that arise when you always expect the wall clock time on the system to be correct can be immense. Erlang therefore introduced the “corrected estimate of time”, or the “time correction”, many years ago.

And

As we align Erlang system time with OS system time by changing the time offset, we can enable a time correction that tries to adjust the frequency of the Erlang monotonic clock to be as correct as possible. This makes time measurements using Erlang monotonic time more accurate and precise.

This is saying that there are problems being solved and accuracy and precision benefits to Erlang monotonic time, but it does not mention why Erlang system time (based on Erlang monotonic time) would be preferable to OS system time.

learn you some Erlang also discusses this, but again does not make a recommendation between System and OS time.

adamu

adamu

Thanks for the link, super interesting. It covers the difference between monotonic and wall time well, but it doesn’t really cover OS Time vs System Time. The erlang docs seem to suggest that for getting the wall time, System Time should be the default, however, DateTime in Elixir uses OS Time. I wonder if there’s a reason for that?

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement