Skip to content

Commit 928e43a

Browse files
authoredFeb 1, 2025··
Have Duration return same units as Postgrex.Interval (#728)
1 parent a6f2020 commit 928e43a

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed
 

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ iex> Postgrex.query!(pid, "INSERT INTO comments (user_id, text) VALUES (10, 'hey
4343
| `timestamp` | `%NaiveDateTime{year: 2013, month: 10, day: 12, hour: 0, minute: 37, second: 14}` |
4444
| `timestamptz` | `%DateTime{year: 2013, month: 10, day: 12, hour: 0, minute: 37, second: 14, time_zone: "Etc/UTC"}` (2) |
4545
| `interval` | `%Postgrex.Interval{months: 14, days: 40, secs: 10920, microsecs: 315}` |
46-
| `interval` | `%Duration{year: 1, month: 2, week: 5, day: 5, hour: 3, minute: 2, second: 0, microsecond: {315, 6}}` (3) |
46+
| `interval` | `%Duration{month: 2, day: 5, second: 0, microsecond: {315, 6}}` (3) |
4747
| `array` | `[1, 2, 3]` |
4848
| `composite type` | `{42, "title", "content"}` |
4949
| `range` | `%Postgrex.Range{lower: 1, upper: 5}` |

‎lib/postgrex/extensions/interval.ex

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,8 @@ defmodule Postgrex.Extensions.Interval do
7777
end
7878

7979
def decode_interval(microseconds, days, months, type_mod, Duration) do
80-
years = div(months, 12)
81-
months = rem(months, 12)
82-
weeks = div(days, 7)
83-
days = rem(days, 7)
8480
seconds = div(microseconds, 1_000_000)
8581
microseconds = rem(microseconds, 1_000_000)
86-
minutes = div(seconds, 60)
87-
seconds = rem(seconds, 60)
88-
hours = div(minutes, 60)
89-
minutes = rem(minutes, 60)
9082
precision = if type_mod, do: type_mod &&& unquote(@precision_mask)
9183

9284
precision =
@@ -95,12 +87,8 @@ defmodule Postgrex.Extensions.Interval do
9587
else: precision
9688

9789
Duration.new!(
98-
year: years,
9990
month: months,
100-
week: weeks,
10191
day: days,
102-
hour: hours,
103-
minute: minutes,
10492
second: seconds,
10593
microsecond: {microseconds, precision}
10694
)

‎test/query_test.exs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,22 +149,22 @@ defmodule QueryTest do
149149
assert [[%Duration{microsecond: {0, 6}}]] =
150150
P.query!(pid, "SELECT interval '0'", []).rows
151151

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

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

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

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

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

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

170170
assert [[%Duration{second: 10, microsecond: {0, 6}}]] =
@@ -173,13 +173,9 @@ defmodule QueryTest do
173173
assert [
174174
[
175175
%Duration{
176-
year: 1,
177-
month: 2,
178-
week: 5,
179-
day: 5,
180-
hour: 3,
181-
minute: 2,
182-
second: 1,
176+
month: 14,
177+
day: 40,
178+
second: 10921,
183179
microsecond: {0, 6}
184180
}
185181
]
@@ -215,10 +211,10 @@ defmodule QueryTest do
215211
opts = [database: "postgrex_test", backoff_type: :stop, types: Postgrex.ElixirDurationTypes]
216212
{:ok, pid} = P.start_link(opts)
217213

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

221-
assert [[[%Duration{week: 1, day: 3, microsecond: {0, 6}}]]] =
217+
assert [[[%Duration{day: 10, microsecond: {0, 6}}]]] =
222218
P.query!(pid, "SELECT ARRAY[interval '10' DAY]", []).rows
223219
end
224220
end

0 commit comments

Comments
 (0)
Please sign in to comment.