webuhu

webuhu

Review my code - duration measurement & convert to a human easy readable scientific format

Hi all, as I’m always trying to improve my code.
Is anyone of you Elixir Pros interested into review my following code?

This module will convert duration measurements into a human easy readable scientific format.
What I mean by this.
I always hated if an uptime / or downtime measurement told me e.g. up since 1 y 21 d 5 h 5 min 5 s.
In this case 1 y 21 d would be totally enough for myself.

So the conversion scheme I’m using here is very opinionated.
In case of any unit value below 10, I will additionally add the value of the next unit. Above I don’t.
e.g.

- `1 s _ ms` - `9 s _ ms`
- `10 s ` - `59 s`

I would be very thankful if anybody would give me his opinion to my code below.
If you would do something in Elixir best practice completely different for this use case …

defmodule Tool.Timer do
  @moduledoc """
  Time measurement tool for performance & uptime testing.
  Can convert a duration to a human easy understandable scientific format.
  """

  @base_unit :nanosecond
  @unit_shift_factor 10

  @second 1
  @minute @second * 60
  @hour @minute * 60
  @day @hour * 24
  @year @day * 365

  @doc """
  Returns monotonic system time in @base_unit
  """
  @spec now() :: integer()
  def now, do: System.monotonic_time(@base_unit)

  @doc """
  Calculate time difference to a given start time (e.g. from `now/0`).
  Return duration in @base_unit.
  """
  @spec diff(integer(), integer()) :: integer()
  def diff(start_time, end_time \\ now()) do
    end_time - start_time
  end

  @doc """
  Calculate time difference to a given start time (e.g. from `now/0`).
  Return duration in a human easy understandable scientific format.
  In: `-576460349095935368`
  Out:
  - `1 ns` - `999 ns`
  - `1 μs _ ns` - `9 μs _ ns`
  - `10 μs` - `999 μs`
  - `1 ms _ μs` - `9 ms _ μs`
  - `10 ms ` - `999 ms`
  - `1 s _ ms` - `9 s _ ms`
  - `10 s ` - `59 s`
  - `1 min _ s` - `59 min _ s`
  - `1 h _ min` - `23 h _ min`
  - `1 d _ h` - `364 d _ h`
  - `1 y _ d` - `_ y _ d`
  """
  @spec diff_human(integer(), integer(), System.time_unit()) :: binary()
  def diff_human(start_time, end_time \\ now(), base_unit \\ @base_unit) do
    diff_base = diff(start_time, end_time)
    diff_nano = System.convert_time_unit(diff_base, base_unit, :nanosecond)
    time_conversion(diff_nano)
  end

  # everything below system precision
  defp time_conversion(time) when time === 0,
    do: "< #{system_precision()}"

  # nanoseconds (ns) only
  defp time_conversion(time) when time < 1_000,
    do: unit_fraction_string(time, :nanosecond)

  # microseconds (μs) + nanoseconds (ns)
  defp time_conversion(time) when time < 1_000 * @unit_shift_factor do
    list_to_string([
      unit_fraction_string(time, :microsecond),
      unit_fraction_string(time, :nanosecond)
    ])
  end

  # microseconds (μs) only
  defp time_conversion(time) when time < 1_000_000,
    do: unit_fraction_string(time, :microsecond)

  # milliseconds (ms) + microseconds (μs)
  defp time_conversion(time) when time < 1_000_000 * @unit_shift_factor do
    list_to_string([
      unit_fraction_string(time, :millisecond),
      unit_fraction_string(time, :microsecond)
    ])
  end

  # milliseconds (ms) only
  defp time_conversion(time) when time < 1_000_000_000,
    do: unit_fraction_string(time, :millisecond)

  # seconds (s) + milliseconds (ms)
  defp time_conversion(time) when time < 1_000_000_000 * @unit_shift_factor do
    list_to_string([
      unit_fraction_string(time, :second),
      unit_fraction_string(time, :millisecond)
    ])
  end

  # seconds (s) only
  defp time_conversion(time) when time < @minute * 1_000_000_000,
    do: unit_fraction_string(time, :second)

  # minutes (min) + seconds (s)
  defp time_conversion(time) when time < @hour * 1_000_000_000 do
    list_to_string([
      unit_subset_fraction_string(time, :minute, @minute, 60),
      unit_subset_fraction_string(time, :second, @second, 60)
    ])
  end

  # hours (h) + minutes (min)
  defp time_conversion(time) when time < @day * 1_000_000_000 do
    list_to_string([
      unit_subset_fraction_string(time, :hour, @hour, 60),
      unit_subset_fraction_string(time, :minute, @minute, 60)
    ])
  end

  # days (d) + hours (h)
  defp time_conversion(time) when time < @year * 1_000_000_000 do
    list_to_string([
      unit_subset_fraction_string(time, :day, @day, 365),
      unit_subset_fraction_string(time, :hour, @hour, 24)
    ])
  end

  # years (y) + days (d)
  @spec time_conversion(integer()) :: binary()
  defp time_conversion(time) do
    list_to_string([
      unit_subset_fraction_string(time, :year, @year, 365),
      unit_subset_fraction_string(time, :day, @day, 365)
    ])
  end

  # will return the human readable system precision
  @spec system_precision :: binary
  defp system_precision do
    parts_per_second = System.convert_time_unit(1, :second, :native)
    precision_nano = round(1 / parts_per_second * 1_000_000_000)

    # to avoid a infinite loop on value 0
    precision =
      case precision_nano do
        x when x > 0 -> x
        _ -> 1
      end

    time_conversion(precision)
  end

  # unit fraction for everything below seconds
  @spec unit_fraction_string(integer(), atom()) :: binary()
  defp unit_fraction_string(time, unit) do
    fraction =
      time
      |> System.convert_time_unit(:nanosecond, unit)
      |> Integer.mod(1_000)

    human_readable_unit(fraction, unit)
  end

  # unit fraction for everything above seconds
  @spec unit_subset_fraction_string(integer(), atom(), integer(), integer()) :: binary()
  defp unit_subset_fraction_string(time, unit, fraction_divisor, subset_divisor) do
    fraction =
      time
      |> System.convert_time_unit(:nanosecond, :second)
      |> Integer.floor_div(fraction_divisor)
      |> Integer.mod(subset_divisor)

    human_readable_unit(fraction, unit)
  end

  # join value with unit
  @spec human_readable_unit(integer(), atom()) :: binary()
  defp human_readable_unit(fraction, unit) do
    cond do
      fraction > 0 -> Integer.to_string(fraction) <> " " <> unit_abbreviation(unit)
      true -> nil
    end
  end

  # unit abbreviations
  @spec unit_abbreviation(atom()) :: binary()
  defp unit_abbreviation(:nanosecond), do: "ns"
  defp unit_abbreviation(:microsecond), do: "μs"
  defp unit_abbreviation(:millisecond), do: "ms"
  defp unit_abbreviation(:second), do: "s"
  defp unit_abbreviation(:minute), do: "min"
  defp unit_abbreviation(:hour), do: "h"
  defp unit_abbreviation(:day), do: "d"
  defp unit_abbreviation(:year), do: "y"

  # join list to string
  @spec list_to_string(list()) :: binary()
  defp list_to_string(list) do
    list
    # remove nil values from list
    |> Enum.filter(& &1)
    |> Enum.join(" ")
  end
end

Edit:

  • Updated with the already mentioned changes from david_ex.

Most Liked

kip

kip

ex_cldr Core Team

Here’s another approach using some of my CLDR libs:

defmodule Elapsed do
  @to ~U[2019-10-01 11:00:00.0000Z]
  @from ~U[2019-01-01 09:35:00.000450Z]
  
  def time_in_words(options \\ []) do
    precision = Keyword.get(options, :precision, 3)
    @to
    |> DateTime.diff(@from, :microsecond)
    |> Cldr.Unit.new(:microsecond)
    |> Cldr.Unit.decompose([:year, :month, :day, :hour, :minute])
    |> Enum.take(precision)
    |> Cldr.Unit.to_string(options)
  end
end

Examples:

iex> Elapsed.time_in_words                           
"8 months, 29 days, and 17 hours"
iex> Elapsed.time_in_words locale: "fr"
"8 mois, 29 jours et 17 heures"
iex> Elapsed.time_in_words precision: 2              
"8 months and 29 days"
iex> Elapsed.time_in_words precision: 2, locale: "fr"
"8 mois et 29 jours"
iex> Elapsed.time_in_words precision: 2, list_options: [format: :unit_short]              
"8 months, 29 days"
iex> Elapsed.time_in_words style: :narrow
"8m, 29d, and 17h"
iex> Elapsed.time_in_words style: :narrow, precision: 2
"8m and 29d"
iex> Elapsed.time_in_words style: :narrow, precision: 2, list_options: [format: :unit_narrow]
"8m 29d"
david_ex

david_ex

Here’s some low-hanging fruit:

Instead of

Integer.mod(
        Integer.floor_div(System.convert_time_unit(time, :nanosecond, :second), fraction_divisor),
        subset_divisor
      )

you can pipe:

time
|> System.convert_time_unit(:nanosecond, :second)
|> Integer.floor_div(fraction_divisor),
|> Integer.mod(subset_divisor)

Instead of

defp unit_abbreviation(unit) when unit === :nanosecond, do: "ns"

simply use

defp unit_abbreviation(:nanosecond), do: "ns"

Instead of

Enum.join([Integer.to_string(fraction), unit_abbreviation(unit)], " ")

use

Integer.to_string(fraction) <> " " <> unit_abbreviation(unit)

or

"#{Integer.to_string(fraction)} #{unit_abbreviation(unit)}"

(although using Enum.join is useful when you have many inputs to join).

webuhu

webuhu

I know Timex - and there are some similarities to my implementation.
But for the fun of programming and learning Elixir,
and always trying to avoid any dependencies up first,
I decided to try to find my own solution.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement