@@ -59,7 +59,7 @@ import Data.Monoid ((<>))
59
59
#endif
60
60
import qualified Data.Text as T
61
61
import Data.Time.Calendar
62
- import Safe (readDef , headDef )
62
+ import Safe (readDef , maximumByDef , maximumDef , minimumDef )
63
63
import Text.Megaparsec
64
64
import Text.Megaparsec.Char
65
65
@@ -475,28 +475,26 @@ queryDateSpan' (Date span) = span
475
475
queryDateSpan' (Date2 span ) = span
476
476
queryDateSpan' _ = nulldatespan
477
477
478
- -- | What is the earliest of these dates, where Nothing is latest ?
478
+ -- | What is the earliest of these dates, where Nothing is earliest ?
479
479
earliestMaybeDate :: [Maybe Day ] -> Maybe Day
480
- earliestMaybeDate mds = head $ sortBy compareMaybeDates mds ++ [ Nothing ]
480
+ earliestMaybeDate = minimumDef Nothing
481
481
482
482
-- | What is the latest of these dates, where Nothing is earliest ?
483
483
latestMaybeDate :: [Maybe Day ] -> Maybe Day
484
- latestMaybeDate = headDef Nothing . sortBy ( flip compareMaybeDates)
484
+ latestMaybeDate = maximumDef Nothing
485
485
486
- -- | What is the earliest of these dates, ignoring Nothings ?
486
+ -- | What is the earliest of these dates, where Nothing is the latest ?
487
487
earliestMaybeDate' :: [Maybe Day ] -> Maybe Day
488
- earliestMaybeDate' = headDef Nothing . sortBy compareMaybeDates . filter isJust
488
+ earliestMaybeDate' = minimumDef Nothing . filter isJust
489
489
490
- -- | What is the latest of these dates, ignoring Nothings ?
490
+ -- | What is the latest of these dates, where Nothing is the latest ?
491
491
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
500
498
501
499
-- | The depth limit this query specifies, or a large number if none.
502
500
queryDepth :: Query -> Int
@@ -719,6 +717,22 @@ tests_Query = tests "Query" [
719
717
parseAmountQueryTerm " -0.23" @?= (Eq ,(- 0.23 ))
720
718
-- ,test "number beginning with decimal mark" $ parseAmountQueryTerm "=.23" @?= (AbsEq,0.23) -- XXX
721
719
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
+
722
736
,test " matchesAccount" $ do
723
737
assertBool " " $ (Acct " b:c" ) `matchesAccount` " a:bb:c:d"
724
738
assertBool " " $ not $ (Acct " ^a:b" ) `matchesAccount` " c:a:b"
0 commit comments