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:

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement