diff --git a/README.md b/README.md index 016c4461..3dceafeb 100644 --- a/README.md +++ b/README.md @@ -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}` | diff --git a/lib/postgrex/extensions/interval.ex b/lib/postgrex/extensions/interval.ex index ebd1d540..b0c7d1e3 100644 --- a/lib/postgrex/extensions/interval.ex +++ b/lib/postgrex/extensions/interval.ex @@ -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 = @@ -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} ) diff --git a/test/query_test.exs b/test/query_test.exs index e34ab846..d8f8510e 100644 --- a/test/query_test.exs +++ b/test/query_test.exs @@ -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}}]] = @@ -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} } ] @@ -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