Today I Learned

A Hashrocket project

Legible milliseconds in Elixir using Erlang timer

In Elixir, we often need to set milliseconds. This is especially common when working with GenServer, Task.Supervisor, Agent and others.

While we could write our milliseconds directly:

10800000

or perhaps

10_800_000

Did you know that was 3 hours just by glancing at it? Sure the math is fairly easy, but why make us think? Erlang’s timer to the rescue!

iex()> :timer.hours(3)
10800000

Well that makes our 3 hours pretty legible if you ask me.

We also have minutes and seconds:

iex()> :timer.minutes(11)
660000
iex()> :timer.seconds(455)
455000

Thanks Erlang!

Looking for help? Elixir is quickly gaining momentum for web applications that need concurrency, performance, and the ability to connect to many different clients. The developers at Hashrocket are learning along with the rest of the development community that Elixir and Phoenix are viable Rails alternatives for the right application. Check out the source code for Today I Learned, written in Elixir, and contact us if you need help with your Elixir project.