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
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,7 @@ render x =
<> " us"

-- | Parses an interval. This is not a general purpose parser. It only supports
-- the formats that PostgreSQL generates. For example, it will fail to parse an
-- interval like @"1 week"@ because PostgreSQL never uses weeks when rendering
-- intervals.
-- the formats that PostgreSQL generates.
parse :: A.Parser Interval
parse =
-- Start with parsers that have non-empty prefixes, in order to avoid
Expand Down Expand Up @@ -636,6 +634,7 @@ parsePostgresVerbose = do
flip A.sepBy " " $
A.choice
[ Years <$> A.signed A.decimal <* maybePlural " year",
Weeks <$> A.signed A.decimal <* maybePlural " week",
Months <$> A.signed A.decimal <* maybePlural " mon",
Days <$> A.signed A.decimal <* maybePlural " day",
Hours <$> A.signed A.decimal <* maybePlural " hour",
Expand All @@ -652,6 +651,7 @@ parsePostgres = do
flip A.sepBy " " $
A.choice
[ Years <$> A.signed A.decimal <* maybePlural " year",
Weeks <$> A.signed A.decimal <* maybePlural " week",
Months <$> A.signed A.decimal <* maybePlural " mon",
Days <$> A.signed A.decimal <* maybePlural " day"
]
Expand Down Expand Up @@ -688,6 +688,7 @@ maybePlural word = (<>) <$> A.string word <*> A.option "" "s"
-- are accepted, like years and months.
data Component
= Years !Integer
| Weeks !Integer
| Months !Integer
| Days !Integer
| Hours !Integer
Expand All @@ -701,6 +702,7 @@ data Component
fromComponent :: Component -> Maybe Interval
fromComponent c = case c of
Years y -> fromYears =<< Bits.toIntegralSized y
Weeks w -> fromWeeks =<< Bits.toIntegralSized w
Months m -> fromMonths <$> Bits.toIntegralSized m
Days d -> fromDays <$> Bits.toIntegralSized d
Hours h -> fromHours =<< Bits.toIntegralSized h
Expand All @@ -722,6 +724,7 @@ fromComponents =
negateComponent :: Component -> Component
negateComponent c = case c of
Years y -> Years (-y)
Weeks w -> Weeks (-w)
Months m -> Months (-m)
Days d -> Days (-d)
Hours h -> Hours (-h)
Expand Down
21 changes: 21 additions & 0 deletions source/test-suite/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,27 @@ spec = H.describe "Database.PostgreSQL.Simple.Interval" $ do
let actual = Attoparsec.parseOnly I.parse "-infinity"
actual `H.shouldBe` Right (I.MkInterval minBound minBound minBound)

H.describe "weeks" $ do
H.it "succeeds with zero" $ do
let actual = Attoparsec.parseOnly I.parse "0 weeks"
actual `H.shouldBe` Right (I.MkInterval 0 0 0)

H.it "succeeds with implicit positive" $ do
let actual = Attoparsec.parseOnly I.parse "1 week"
actual `H.shouldBe` Right (I.MkInterval 0 7 0)

H.it "succeeds with explicit positive" $ do
let actual = Attoparsec.parseOnly I.parse "+2 weeks"
actual `H.shouldBe` Right (I.MkInterval 0 14 0)

H.it "succeeds with negative" $ do
let actual = Attoparsec.parseOnly I.parse "-3 weeks"
actual `H.shouldBe` Right (I.MkInterval 0 (-21) 0)

H.it "succeeds with verbose" $ do
let actual = Attoparsec.parseOnly I.parse "@ 4 weeks"
actual `H.shouldBe` Right (I.MkInterval 0 28 0)

Monad.forM_ intervalStyles $ \(_, field) ->
Monad.forM_ examples $ \example -> do
let input = field example
Expand Down