refactor
This commit is contained in:
parent
c4fdb5c609
commit
8daa9a33cd
@ -45,7 +45,6 @@ import Commands.Histogram
|
|||||||
import Commands.Print
|
import Commands.Print
|
||||||
import Commands.Register
|
import Commands.Register
|
||||||
import Ledger
|
import Ledger
|
||||||
import Ledger.IO (readLedger)
|
|
||||||
import Options hiding (value)
|
import Options hiding (value)
|
||||||
#ifdef MAKE
|
#ifdef MAKE
|
||||||
import Paths_hledger_make (getDataFileName)
|
import Paths_hledger_make (getDataFileName)
|
||||||
|
|||||||
@ -117,13 +117,19 @@ spanFromSmartDate refdate sdate = DateSpan (Just b) (Just e)
|
|||||||
span (y,m,"") = (startofmonth day, nextmonth day) where day = fromGregorian (read y) (read m) 1
|
span (y,m,"") = (startofmonth day, nextmonth day) where day = fromGregorian (read y) (read m) 1
|
||||||
span (y,m,d) = (day, nextday day) where day = fromGregorian (read y) (read m) (read d)
|
span (y,m,d) = (day, nextday day) where day = fromGregorian (read y) (read m) (read d)
|
||||||
|
|
||||||
|
showDay :: Day -> String
|
||||||
|
showDay day = printf "%04d/%02d/%02d" y m d where (y,m,d) = toGregorian day
|
||||||
|
|
||||||
-- | Convert a smart date string to an explicit yyyy\/mm\/dd string using
|
-- | Convert a smart date string to an explicit yyyy\/mm\/dd string using
|
||||||
-- the provided reference date.
|
-- the provided reference date.
|
||||||
fixSmartDateStr :: Day -> String -> String
|
fixSmartDateStr :: Day -> String -> String
|
||||||
fixSmartDateStr t s = printf "%04d/%02d/%02d" y m d
|
fixSmartDateStr t s = either parseerror id $ fixSmartDateStrEither t s
|
||||||
where
|
|
||||||
(y,m,d) = toGregorian $ fixSmartDate t sdate
|
-- | A safe version of fixSmartDateStr.
|
||||||
sdate = fromparse $ parsewith smartdate $ lowercase s
|
fixSmartDateStrEither :: Day -> String -> Either ParseError String
|
||||||
|
fixSmartDateStrEither t s = case parsewith smartdate (lowercase s) of
|
||||||
|
Right sd -> Right $ showDay $ fixSmartDate t sd
|
||||||
|
Left e -> Left e
|
||||||
|
|
||||||
-- | Convert a SmartDate to an absolute date using the provided reference date.
|
-- | Convert a SmartDate to an absolute date using the provided reference date.
|
||||||
fixSmartDate :: Day -> SmartDate -> Day
|
fixSmartDate :: Day -> SmartDate -> Day
|
||||||
|
|||||||
@ -248,7 +248,13 @@ parseWithCtx :: b -> GenParser Char b a -> String -> Either ParseError a
|
|||||||
parseWithCtx ctx p = runParser p ctx ""
|
parseWithCtx ctx p = runParser p ctx ""
|
||||||
|
|
||||||
fromparse :: Either ParseError a -> a
|
fromparse :: Either ParseError a -> a
|
||||||
fromparse = either (\e -> error $ "parse error at "++ show e) id
|
fromparse = either parseerror id
|
||||||
|
|
||||||
|
parseerror e = error $ showParseError e
|
||||||
|
|
||||||
|
showParseError e = "parse error at " ++ show e
|
||||||
|
|
||||||
|
showDateParseError e = printf "date parse error (%s)" (intercalate ", " $ tail $ lines $ show e)
|
||||||
|
|
||||||
nonspace :: GenParser Char st Char
|
nonspace :: GenParser Char st Char
|
||||||
nonspace = satisfy (not . isSpace)
|
nonspace = satisfy (not . isSpace)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user