iex(1)> Timex.Format.DateTime.Formatters.Relative.format(~T[12:00:00], "{relative}")
** (ArithmeticError) bad argument in arithmetic expression: div({:error, :invalid_date}, 31104000)
:erlang.div({:error, :invalid_date}, 31104000)
(timex 3.7.11) lib/format/datetime/formatters/relative.ex:227: Timex.Format.DateTime.Formatters.Relative.do_format/5
iex:1: (file)
iex(1)> Timex.format(~T[12:00:00], "{relative}", :relative)
** (ArithmeticError) bad argument in arithmetic expression: div({:error, :invalid_date}, 31104000)
:erlang.div({:error, :invalid_date}, 31104000)
(timex 3.7.11) lib/format/datetime/formatters/relative.ex:227: Timex.Format.DateTime.Formatters.Relative.do_format/5
iex:1: (file)
iex(1)> Timex.Format.DateTime.Formatters.Relative.lformat(~T[12:00:00], "{relative}", "en")
** (ArithmeticError) bad argument in arithmetic expression: div({:error, :invalid_date}, 31104000)
:erlang.div({:error, :invalid_date}, 31104000)
(timex 3.7.11) lib/format/datetime/formatters/relative.ex:227: Timex.Format.DateTime.Formatters.Relative.do_format/5
iex:1: (file)
iex(1)> Timex.lformat(~T[12:00:00], "{relative}", "en", :relative)
** (ArithmeticError) bad argument in arithmetic expression: div({:error, :invalid_date}, 31104000)
:erlang.div({:error, :invalid_date}, 31104000)
(timex 3.7.11) lib/format/datetime/formatters/relative.ex:227: Timex.Format.DateTime.Formatters.Relative.do_format/5
iex:1: (file)
Steps to reproduce
Description of issue
Timex.Types.calendar_types()andTimex.Types.valid_datetime()includeTime.t()since 2a912f3.However no support for
Time.t()was ever added to align with the updated typespec changes. The docs in e.g.Timex.Format.DateTime.Formattercorrectly state that the datetime formatter only supportsDateTime.t(),Date.t()andNaiveDateTime.t(). This means that typespecs of the following functions don't correspond to what they actually support:Timex.Format.DateTime.Formatters.Relative.format/2Timex.Format.DateTime.Formatters.Relative.format!/2Timex.Format.DateTime.Formatters.Relative.lformat/3Timex.Format.DateTime.Formatters.Relative.lformat!/3The errors seen above in
Steps to reproduceare caused by a call toTimex.Protocol.to_naive_datetime/1(which is referenced by bothTimex.Format.DateTime.Formatters.Relative.lformat/3andTimex.Format.DateTime.Formatters.Relative.relative_to/4) because the protocol is not implemented forTime.Ways to solve
Option 1:
Time.t()from the typespecsTimefrom the guard inTimex.Format.DateTime.Formatter.lformat/3such that the function call instead returns{:error, :invalid_date}when aTime-struct is passedOption 2:
Time.t()I would expect such support to act like the behaviour of a passed 'today'-value of type
DateTimeorNaiveDateTime, i.e. returning values such as{:ok, "8 hours ago"}or{:ok, "in 1 hour"}.I'm not sure if it makes sense to implement
Timex.ProtocolforTimesince a lot of the functions are date-related, so maybe it makes sense to instead make a separate clause ofTimex.Format.DateTime.Formatters.Relative.lformat/3which handlesTimejust slightly differently to howDate,DateTimeandNaiveDateTimeare handled.(Very similar function clause, but call
do_format/5with the passedTime-struct andTime.utc_now()instead ofTimex.Protocol.to_naive_datetime(date)andTimex.Protocol.now()?)