i-n-g-m-a-r
Should DateTime.diff/3 round numbers?
I noticed some weird anomalies in a dataset.
Turns out I was mixing Postgres and Elixir numbers.
DateTime.diff(~U[2026-03-18 05:50:08.653909Z], ~U[2026-03-18 05:50:04.655608Z], :second)
# 3
DateTime.diff(~U[2026-03-18 05:50:08.653909Z], ~U[2026-03-18 05:50:04.655608Z], :millisecond)
# 3998
select extract(epoch from ('2026-03-18 05:50:08.653909Z'::timestamptz - '2026-03-18 05:50:04.655608Z'::timestamptz))::integer
# 4
The Postgres number (4) seems reasonable.
Most Liked
LostKobrakai
The docs for DateTime.diff state the current behaviour:
Fractional results are not supported and are truncated.
You could always implement your own rounding or propose adding an option to DateTime.diff, but changing the default behaviour would be a breaking change.
3
LostKobrakai
Truncate is a common name used for that operation, e.g. on Kernel — Elixir v1.19.5. While it matches the rounding-towards-zero rounding strategy (one of many) it tries to tell that no rounding is happening. It’s not looking a the fractional remain to determine what needs to happen, but it ignores it outright of it is calculated in the first place.
2
Popular in Questions
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Other popular topics
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I would like to know what is the best IDE for elixir development?
New







