Skip to content

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

@HansGlimmerfors

Description

@HansGlimmerfors

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! 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions