bitwalker / bitwalker/timex

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

Open
#775 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Elixir
Stars
1.8k
Forks
407
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.