timex icon indicating copy to clipboard operation
timex copied to clipboard

Relative formatting of future `Date`-structs is off by 1 day

Open HansGlimmerfors opened this issue 1 year ago • 0 comments

Steps to reproduce

iex(1)> Timex.format(Timex.shift(Date.utc_today(), days: 1), "{relative}", :relative)
{:ok, "in 3 hours"}
iex(2)> Timex.format(Timex.shift(Date.utc_today(), days: 2), "{relative}", :relative)
{:ok, "tomorrow"}

(note that the number of hours returned by the first call will vary depending on the current system time, e.g. calling the function at 12:00 should instead result in {:ok, "in 12 hours"})

Description of issue

I would expect the relative date of today + 1 day to result in tomorrow, but instead it is today + 2 days that gives tomorrow. The reason for this is due to Timex.Format.DateTime.Formatters.Relative.lformat/3 which calls Timex.to_naive_datetime/1 on the passed date. The returned value of Timex.to_naive_datetime/1 for a Date-struct is the same date as the Date-struct, but with a time of 00:00:00. This means that a call to Timex.Format.DateTime.Formatters.Relative.lformat/3 when the passed date is a Date-struct in the future, in x days will always be off by 1 day.

I haven't found any examples of past days having incorrect offsets, though! 👍

HansGlimmerfors avatar Dec 25 '24 21:12 HansGlimmerfors