Skip to content

Commit b9b16da

Browse files
Xitian9simonmichael
authored andcommitted
Correct finding latest date in queryEndDate Or queries and simplify date
comparison code.
1 parent ace729d commit b9b16da

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

hledger-lib/Hledger/Query.hs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import Data.Monoid ((<>))
5959
#endif
6060
import qualified Data.Text as T
6161
import Data.Time.Calendar
62-
import Safe (readDef, headDef)
62+
import Safe (readDef, maximumByDef, maximumDef, minimumDef)
6363
import Text.Megaparsec
6464
import Text.Megaparsec.Char
6565

@@ -475,28 +475,26 @@ queryDateSpan' (Date span) = span
475475
queryDateSpan' (Date2 span) = span
476476
queryDateSpan' _ = nulldatespan
477477

478-
-- | What is the earliest of these dates, where Nothing is latest ?
478+
-- | What is the earliest of these dates, where Nothing is earliest ?
479479
earliestMaybeDate :: [Maybe Day] -> Maybe Day
480-
earliestMaybeDate mds = head $ sortBy compareMaybeDates mds ++ [Nothing]
480+
earliestMaybeDate = minimumDef Nothing
481481

482482
-- | What is the latest of these dates, where Nothing is earliest ?
483483
latestMaybeDate :: [Maybe Day] -> Maybe Day
484-
latestMaybeDate = headDef Nothing . sortBy (flip compareMaybeDates)
484+
latestMaybeDate = maximumDef Nothing
485485

486-
-- | What is the earliest of these dates, ignoring Nothings ?
486+
-- | What is the earliest of these dates, where Nothing is the latest ?
487487
earliestMaybeDate' :: [Maybe Day] -> Maybe Day
488-
earliestMaybeDate' = headDef Nothing . sortBy compareMaybeDates . filter isJust
488+
earliestMaybeDate' = minimumDef Nothing . filter isJust
489489

490-
-- | What is the latest of these dates, ignoring Nothings ?
490+
-- | What is the latest of these dates, where Nothing is the latest ?
491491
latestMaybeDate' :: [Maybe Day] -> Maybe Day
492-
latestMaybeDate' = headDef Nothing . sortBy (flip compareMaybeDates) . filter isJust
493-
494-
-- | Compare two maybe dates, Nothing is earliest.
495-
compareMaybeDates :: Maybe Day -> Maybe Day -> Ordering
496-
compareMaybeDates Nothing Nothing = EQ
497-
compareMaybeDates Nothing (Just _) = LT
498-
compareMaybeDates (Just _) Nothing = GT
499-
compareMaybeDates (Just a) (Just b) = compare a b
492+
latestMaybeDate' = maximumByDef Nothing compareNothingMax
493+
where
494+
compareNothingMax Nothing Nothing = EQ
495+
compareNothingMax (Just _) Nothing = LT
496+
compareNothingMax Nothing (Just _) = GT
497+
compareNothingMax (Just a) (Just b) = compare a b
500498

501499
-- | The depth limit this query specifies, or a large number if none.
502500
queryDepth :: Query -> Int
@@ -719,6 +717,22 @@ tests_Query = tests "Query" [
719717
parseAmountQueryTerm "-0.23" @?= (Eq,(-0.23))
720718
-- ,test "number beginning with decimal mark" $ parseAmountQueryTerm "=.23" @?= (AbsEq,0.23) -- XXX
721719

720+
,test "queryStartDate" $ do
721+
let small = Just $ fromGregorian 2000 01 01
722+
big = Just $ fromGregorian 2000 01 02
723+
queryStartDate False (And [Date $ DateSpan small Nothing, Date $ DateSpan big Nothing]) @?= big
724+
queryStartDate False (And [Date $ DateSpan small Nothing, Date $ DateSpan Nothing Nothing]) @?= small
725+
queryStartDate False (Or [Date $ DateSpan small Nothing, Date $ DateSpan big Nothing]) @?= small
726+
queryStartDate False (Or [Date $ DateSpan small Nothing, Date $ DateSpan Nothing Nothing]) @?= Nothing
727+
728+
,test "queryEndDate" $ do
729+
let small = Just $ fromGregorian 2000 01 01
730+
big = Just $ fromGregorian 2000 01 02
731+
queryEndDate False (And [Date $ DateSpan Nothing small, Date $ DateSpan Nothing big]) @?= small
732+
queryEndDate False (And [Date $ DateSpan Nothing small, Date $ DateSpan Nothing Nothing]) @?= small
733+
queryEndDate False (Or [Date $ DateSpan Nothing small, Date $ DateSpan Nothing big]) @?= big
734+
queryEndDate False (Or [Date $ DateSpan Nothing small, Date $ DateSpan Nothing Nothing]) @?= Nothing
735+
722736
,test "matchesAccount" $ do
723737
assertBool "" $ (Acct "b:c") `matchesAccount` "a:bb:c:d"
724738
assertBool "" $ not $ (Acct "^a:b") `matchesAccount` "c:a:b"

0 commit comments

Comments
 (0)