calendar_interval icon indicating copy to clipboard operation
calendar_interval copied to clipboard

TBD: change precision representation

Open wojtekmach opened this issue 4 years ago • 0 comments

Currently the precision is an atom:

iex> ~I[2019-01-01].precision
:day

and so if there's a function that just wants to print, say, day from a given interval it needs to check it like that:

def print_day(%CalendarInterval{precision: precision} = i when precision in [:day, :minute, :second, {:microsecond, 0}, {:microsecond, 1}, ..., {:microsecond, 6}] do
  IO.puts "#{i.day}"
end
def print_day(%CalendarInterval{} =i) do
  raise ArgumentError, "#{inspect(i)} does not have :day precision"
end

An idea is to store which time components we have instead:

iex> ~I[2019-01-01].components
#MapSet<[:year, :month, :day]>
def print_day(%CalendarInterval{} = i) do
  if :day in i.components do
    IO.puts "#{i.day}"
  else
    raise ArgumentError, "#{inspect(i)} does not have :day precision"
  end
end

This is how https://github.com/domainlanguage/time-count handles it.

wojtekmach avatar Sep 28 '19 11:09 wojtekmach