Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timex.shift/2 with time-shift always converts precision to :microsecond in Elixir v1.14+ #731

Open
enerick opened this issue Jan 17, 2023 · 2 comments · May be fixed by #741
Open

Timex.shift/2 with time-shift always converts precision to :microsecond in Elixir v1.14+ #731

enerick opened this issue Jan 17, 2023 · 2 comments · May be fixed by #741

Comments

@enerick
Copy link
Contributor

enerick commented Jan 17, 2023

Steps to reproduce

with Elixir v1.13

iex(1)> now = ~U[2023-01-01 10:20:30Z]
~U[2023-01-01 10:20:30Z]
iex(2)> Timex.shift(now, hours: 1)
~U[2023-01-01 11:20:30Z] # OK

with Elixir v1.14

iex(1)> now = ~U[2023-01-01 10:20:30Z]
~U[2023-01-01 10:20:30Z]
iex(2)> Timex.shift(now, hours: 1)
~U[2023-01-01 11:20:30.000000Z] # precision changes unnecessarily

Description of issue

This causes unnecessary precision conversions in Elixir v1.14 and later.

@aniravi24
Copy link

aniravi24 commented Jan 23, 2023

That change went live in Elixir 1.14.3, so the versions prior to that work fine still.

@railsmechanic
Copy link

railsmechanic commented Mar 30, 2023

Same happend to NaiveDatetime, as it converts the NaiveDateTime to DateTime under the hood.

def shift(%NaiveDateTime{} = datetime, shifts) when is_list(shifts) do
with {:ok, dt} <- DateTime.from_naive(datetime, "Etc/UTC", Timex.Timezone.Database) do
case Timex.shift(dt, shifts) do

It uses the Timex.shift/2 (which operates with DateTime), which leads to the following result:

iex(1)> now = ~N[2023-03-31 10:31:57]
iex(2)> Timex.shift(now, hours: 1)
~N[2023-03-31 11:31:57.000000]

This breaks some of my code (using ecto), with the error "...naive_datetime expects microseconds to be empty...", where just ~N[2023-03-31 10:31:57] is expected. Is this the supposed behavior in the future?

The intended behaviour is to only add microseconds when subsecond precisions is requested. https://github.com/elixir-lang/elixir/blob/5a583c753b96865a7cdec2fb4c1ab9c96b836d24/lib/elixir/lib/calendar/naive_datetime.ex#L379-L387

tfiedlerdejanze added a commit to tfiedlerdejanze/timex that referenced this issue Apr 13, 2023
Resolves bitwalker#731

Unfortunately this broke with elixir-lang/elixir@5a583c7
which was release with Elixir 1.14.3

Elixir 1.13.4:

```
iex(3)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:56:10.136274Z]
~U[2023-04-13 09:56:10Z]
~U[2023-04-13 09:57:10Z]
```

Elixir 1.14.4:

```
iex(1)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:55:16.405357Z]
~U[2023-04-13 09:55:16Z]
~U[2023-04-13 09:56:16.000000Z]
```
tfiedlerdejanze added a commit to tfiedlerdejanze/timex that referenced this issue Apr 13, 2023
Resolves bitwalker#731

Unfortunately this broke with elixir-lang/elixir@5a583c7
which was release with Elixir 1.14.3

Elixir 1.13.4:

```
iex(3)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:56:10.136274Z]
~U[2023-04-13 09:56:10Z]
~U[2023-04-13 09:57:10Z]
```

Elixir 1.14.4:

```
iex(1)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:55:16.405357Z]
~U[2023-04-13 09:55:16Z]
~U[2023-04-13 09:56:16.000000Z]
```
tfiedlerdejanze added a commit to tfiedlerdejanze/timex that referenced this issue Apr 13, 2023
Resolves bitwalker#731

Unfortunately this broke with elixir-lang/elixir@5a583c7
which was release with Elixir 1.14.3

Elixir 1.13.4:

```
iex(3)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:56:10.136274Z]
~U[2023-04-13 09:56:10Z]
~U[2023-04-13 09:57:10Z]
```

Elixir 1.14.4:

```
iex(1)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:55:16.405357Z]
~U[2023-04-13 09:55:16Z]
~U[2023-04-13 09:56:16.000000Z]
```
tfiedlerdejanze added a commit to tfiedlerdejanze/timex that referenced this issue Apr 26, 2023
Resolves bitwalker#731

Unfortunately this broke with elixir-lang/elixir@5a583c7
which was release with Elixir 1.14.3

Elixir 1.13.4:

```
iex(3)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:56:10.136274Z]
~U[2023-04-13 09:56:10Z]
~U[2023-04-13 09:57:10Z]
```

Elixir 1.14.4:

```
iex(1)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:55:16.405357Z]
~U[2023-04-13 09:55:16Z]
~U[2023-04-13 09:56:16.000000Z]
```
bzztekeroot pushed a commit to bzzt/timex that referenced this issue Oct 2, 2023
Resolves bitwalker#731

Unfortunately this broke with elixir-lang/elixir@5a583c7
which was release with Elixir 1.14.3

Elixir 1.13.4:

```
iex(3)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:56:10.136274Z]
~U[2023-04-13 09:56:10Z]
~U[2023-04-13 09:57:10Z]
```

Elixir 1.14.4:

```
iex(1)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:55:16.405357Z]
~U[2023-04-13 09:55:16Z]
~U[2023-04-13 09:56:16.000000Z]
```
azizk pushed a commit to welect/timex that referenced this issue Jan 4, 2024
Resolves bitwalker#731

Unfortunately this broke with elixir-lang/elixir@5a583c7
which was release with Elixir 1.14.3

Elixir 1.13.4:

```
iex(3)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:56:10.136274Z]
~U[2023-04-13 09:56:10Z]
~U[2023-04-13 09:57:10Z]
```

Elixir 1.14.4:

```
iex(1)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:55:16.405357Z]
~U[2023-04-13 09:55:16Z]
~U[2023-04-13 09:56:16.000000Z]
```
azizk pushed a commit to welect/timex that referenced this issue Jan 4, 2024
Resolves bitwalker#731

Unfortunately this broke with elixir-lang/elixir@5a583c7
which was release with Elixir 1.14.3

Elixir 1.13.4:

```
iex(3)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:56:10.136274Z]
~U[2023-04-13 09:56:10Z]
~U[2023-04-13 09:57:10Z]
```

Elixir 1.14.4:

```
iex(1)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:55:16.405357Z]
~U[2023-04-13 09:55:16Z]
~U[2023-04-13 09:56:16.000000Z]
```
brunobrgs pushed a commit to surgeventures/timex that referenced this issue Feb 15, 2024
Resolves bitwalker#731

Unfortunately this broke with elixir-lang/elixir@5a583c7
which was release with Elixir 1.14.3

Elixir 1.13.4:

```
iex(3)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:56:10.136274Z]
~U[2023-04-13 09:56:10Z]
~U[2023-04-13 09:57:10Z]
```

Elixir 1.14.4:

```
iex(1)> DateTime.utc_now() |> IO.inspect() |> DateTime.truncate(:second) |> IO.inspect() |> Timex.shift(minutes: 1) |> IO.inspect()
~U[2023-04-13 09:55:16.405357Z]
~U[2023-04-13 09:55:16Z]
~U[2023-04-13 09:56:16.000000Z]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants