Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/library/Database/PostgreSQL/Simple/Interval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Database.PostgreSQL.Simple.Interval

-- * Construction
Unstable.zero,
Unstable.infinity,
Unstable.negativeInfinity,
Unstable.fromMicroseconds,
Unstable.fromMilliseconds,
Unstable.fromSeconds,
Expand Down
20 changes: 17 additions & 3 deletions source/library/Database/PostgreSQL/Simple/Interval/Unstable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ instance Persist.PersistFieldSql Interval where
zero :: Interval
zero = MkInterval 0 0 0

-- | The biggest possible interval.
--
-- >>> infinity
-- MkInterval {months = 2147483647, days = 2147483647, microseconds = 9223372036854775807}
infinity :: Interval
infinity = MkInterval maxBound maxBound maxBound

-- | The smallest possible interval.
--
-- >>> negativeInfinity
-- MkInterval {months = -2147483648, days = -2147483648, microseconds = -9223372036854775808}
negativeInfinity :: Interval
negativeInfinity = MkInterval minBound minBound minBound

-- | Creates an interval from a number of microseconds.
--
-- >>> fromMicroseconds 1
Expand Down Expand Up @@ -605,9 +619,9 @@ parseInfinities =
-- `infinity` is new as of PostgreSQL 17.0.
-- https://www.postgresql.org/message-id/E1r2rB1-005PHm-UL%40gemulon.postgresql.org
A.choice
[ MkInterval minBound minBound minBound <$ "-infinity",
MkInterval maxBound maxBound maxBound <$ "+infinity",
MkInterval maxBound maxBound maxBound <$ "infinity"
[ negativeInfinity <$ "-infinity",
infinity <$ "+infinity",
infinity <$ "infinity"
]

parseIso8601 :: A.Parser [Component]
Expand Down
8 changes: 8 additions & 0 deletions source/test-suite/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ spec = H.describe "Database.PostgreSQL.Simple.Interval" $ do
H.it "works" $ do
I.zero `H.shouldBe` I.MkInterval 0 0 0

H.describe "infinity" $ do
H.it "works" $ do
I.infinity `H.shouldBe` I.MkInterval maxBound maxBound maxBound

H.describe "negativeInfinity" $ do
H.it "works" $ do
I.negativeInfinity `H.shouldBe` I.MkInterval minBound minBound minBound

H.describe "add" $ do
H.it "succeeds with no overflow" $ do
let actual = I.add (I.MkInterval 1 2 3) (I.MkInterval 4 5 6)
Expand Down