Skip to content

Commit

Permalink
Have Duration return same units as Postgrex.Interval (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski authored Feb 1, 2025
1 parent a6f2020 commit 928e43a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ iex> Postgrex.query!(pid, "INSERT INTO comments (user_id, text) VALUES (10, 'hey
| `timestamp` | `%NaiveDateTime{year: 2013, month: 10, day: 12, hour: 0, minute: 37, second: 14}` |
| `timestamptz` | `%DateTime{year: 2013, month: 10, day: 12, hour: 0, minute: 37, second: 14, time_zone: "Etc/UTC"}` (2) |
| `interval` | `%Postgrex.Interval{months: 14, days: 40, secs: 10920, microsecs: 315}` |
| `interval` | `%Duration{year: 1, month: 2, week: 5, day: 5, hour: 3, minute: 2, second: 0, microsecond: {315, 6}}` (3) |
| `interval` | `%Duration{month: 2, day: 5, second: 0, microsecond: {315, 6}}` (3) |
| `array` | `[1, 2, 3]` |
| `composite type` | `{42, "title", "content"}` |
| `range` | `%Postgrex.Range{lower: 1, upper: 5}` |
Expand Down
12 changes: 0 additions & 12 deletions lib/postgrex/extensions/interval.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,8 @@ defmodule Postgrex.Extensions.Interval do
end

def decode_interval(microseconds, days, months, type_mod, Duration) do
years = div(months, 12)
months = rem(months, 12)
weeks = div(days, 7)
days = rem(days, 7)
seconds = div(microseconds, 1_000_000)
microseconds = rem(microseconds, 1_000_000)
minutes = div(seconds, 60)
seconds = rem(seconds, 60)
hours = div(minutes, 60)
minutes = rem(minutes, 60)
precision = if type_mod, do: type_mod &&& unquote(@precision_mask)

precision =
Expand All @@ -95,12 +87,8 @@ defmodule Postgrex.Extensions.Interval do
else: precision

Duration.new!(
year: years,
month: months,
week: weeks,
day: days,
hour: hours,
minute: minutes,
second: seconds,
microsecond: {microseconds, precision}
)
Expand Down
22 changes: 9 additions & 13 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,22 @@ defmodule QueryTest do
assert [[%Duration{microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '0'", []).rows

assert [[%Duration{year: 100, microsecond: {0, 6}}]] =
assert [[%Duration{month: 1200, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '100 years'", []).rows

assert [[%Duration{month: 10, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '10 months'", []).rows

assert [[%Duration{week: 100, microsecond: {0, 6}}]] =
assert [[%Duration{day: 700, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '100 weeks'", []).rows

assert [[%Duration{day: 5, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '5 days'", []).rows

assert [[%Duration{hour: 100, microsecond: {0, 6}}]] =
assert [[%Duration{second: 360_000, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '100 hours'", []).rows

assert [[%Duration{minute: 10, microsecond: {0, 6}}]] =
assert [[%Duration{second: 600, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '10 minutes'", []).rows

assert [[%Duration{second: 10, microsecond: {0, 6}}]] =
Expand All @@ -173,13 +173,9 @@ defmodule QueryTest do
assert [
[
%Duration{
year: 1,
month: 2,
week: 5,
day: 5,
hour: 3,
minute: 2,
second: 1,
month: 14,
day: 40,
second: 10921,
microsecond: {0, 6}
}
]
Expand Down Expand Up @@ -215,10 +211,10 @@ defmodule QueryTest do
opts = [database: "postgrex_test", backoff_type: :stop, types: Postgrex.ElixirDurationTypes]
{:ok, pid} = P.start_link(opts)

assert [[%Duration{week: 1, day: 3, microsecond: {0, 6}}]] =
assert [[%Duration{day: 10, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '10' DAY", []).rows

assert [[[%Duration{week: 1, day: 3, microsecond: {0, 6}}]]] =
assert [[[%Duration{day: 10, microsecond: {0, 6}}]]] =
P.query!(pid, "SELECT ARRAY[interval '10' DAY]", []).rows
end
end
Expand Down

0 comments on commit 928e43a

Please sign in to comment.