Skip to content

Commit da99211

Browse files
authored
Clarify that Duration keeps PostgreSQL's coarse interval units (#779)
1 parent 1e41dd1 commit da99211

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
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{month: 2, day: 5, second: 0, microsecond: {315, 6}}` (3) |
46+
| `interval` | `%Duration{month: 14, day: 40, second: 10920, microsecond: {315, 6}}` (3) |
4747
| `array` | `[1, 2, 3]` |
4848
| `composite type` | `{42, "title", "content"}` |
4949
| `range` | `%Postgrex.Range{lower: 1, upper: 5}` |
@@ -60,7 +60,7 @@ iex> Postgrex.query!(pid, "INSERT INTO comments (user_id, text) VALUES (10, 'hey
6060

6161
(2) Timezones will always be normalized to UTC or assumed to be UTC when no information is available, either by PostgreSQL or Postgrex
6262

63-
(3) `%Duration{}` may only be used with Elixir 1.17+. Intervals will only be decoded into a `%Duration{}` struct if the option `interval_decode_type: Duration` is passed to `Postgrex.Types.define/3`.
63+
(3) `%Duration{}` may only be used with Elixir 1.17+. Intervals will only be decoded into a `%Duration{}` struct if the option `interval_decode_type: Duration` is passed to `Postgrex.Types.define/3`. Like `Postgrex.Interval`, the decoded `Duration` keeps the same coarse units returned by PostgreSQL: the time part is stored in `:second` and is not split into `:hour`/`:minute` (e.g. `1 day 02:03:04` decodes to `%Duration{day: 1, second: 7384}`).
6464

6565
(4) Enumerated types (enum) are custom named database types with strings as values.
6666

lib/postgrex/types.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,12 @@ defmodule Postgrex.Types do
363363
364364
* `:interval_decode_type` - The struct that intervals will be decoded
365365
into. Either `Postgrex.Interval` or `Duration` (Elixir 1.17.0+ only).
366-
Defaults to `Postgrex.Interval`.
366+
Defaults to `Postgrex.Interval`. Note that, like `Postgrex.Interval`,
367+
the decoded `Duration` keeps the same coarse units returned by
368+
PostgreSQL: the time part is stored in `:second` and is not split into
369+
`:hour`/`:minute`. For example, the interval `1 day 02:03:04` decodes to
370+
`%Duration{day: 1, second: 7384}`, not `%Duration{day: 1, hour: 2,
371+
minute: 3, second: 4}`. Both represent the same value.
367372
368373
"""
369374
def define(module, extensions, opts \\ []) do

0 commit comments

Comments
 (0)