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
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.
josevalim
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
I happened across this article recently - the linked section digs into which flavors of time you might want for various applications:
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









