diff --git a/Ledger/Parse.hs b/Ledger/Parse.hs index e8157f429..88d157a1d 100644 --- a/Ledger/Parse.hs +++ b/Ledger/Parse.hs @@ -153,7 +153,7 @@ reservedOp = P.reservedOp lexer -- parsers ledgerfile :: Parser RawLedger -ledgerfile = ledger <|> ledgerfromtimelog +ledgerfile = try (ledger) <|> ledgerfromtimelog ledger :: Parser RawLedger ledger = do @@ -330,12 +330,13 @@ o 2007/03/10 17:26:02 -} timelog :: Parser TimeLog timelog = do - entries <- many timelogentry + entries <- many timelogentry "timelog entry" eof return $ TimeLog entries timelogentry :: Parser TimeLogEntry timelogentry = do + many (commentline <|> blankline) code <- oneOf "bhioO" many1 spacenonewline date <- ledgerdate diff --git a/Ledger/TimeLog.hs b/Ledger/TimeLog.hs index 8f4eabdf4..986cf6af1 100644 --- a/Ledger/TimeLog.hs +++ b/Ledger/TimeLog.hs @@ -1,12 +1,17 @@ {-| -A 'TimeLog' is a parsed timelog file (generated by timeclock.el). -It contains zero or more 'TimeLogEntry's. +A 'TimeLog' is a parsed timelog file (generated by timeclock.el), +containing zero or more 'TimeLogEntry's. It can be converted to a +'RawLedger' for querying. -} module Ledger.TimeLog where +import System.Locale (defaultTimeLocale) +import Data.Time.Clock (UTCTime, diffUTCTime) +import Data.Time.Format (parseTime, formatTime) + import Ledger.Utils import Ledger.Types import Ledger.Currency @@ -40,20 +45,29 @@ entriesFromTimeLogEntries [clockin,clockout] = ecomment = "", etransactions = [ RawTransaction accountname amount "", - RawTransaction "TIME" (-amount) "" + RawTransaction "assets:TIME" (-amount) "" ], epreceding_comment_lines=""} ] where accountname = tlcomment clockin - intime = tldatetime clockin - indate = dateFrom $ tldatetime clockin - outtime = tldatetime clockout - amount = hours 0 -- read $ outtime - intime + indate = showDateFrom intime + intime = parseDateTime $ tldatetime clockin + outtime = parseDateTime $ tldatetime clockout + hours = fromRational (toRational (diffUTCTime outtime intime) / 3600) -- whatever + amount = Amount (getcurrency "h") hours 1 entriesFromTimeLogEntries many = (entriesFromTimeLogEntries $ take 2 many) ++ (entriesFromTimeLogEntries $ drop 2 many) clockoutNowEntry = TimeLogEntry ' ' "" "" -dateFrom = id + +parseDateTime :: String -> UTCTime +parseDateTime s = fromMaybe err parsed + where + err = error $ printf "could not parse timestamp \"%s\"" s + parsed = parseTime defaultTimeLocale "%Y/%m/%d %H:%M:%S" s + +showDateFrom :: UTCTime -> String +showDateFrom = formatTime defaultTimeLocale "%Y/%m/%d" diff --git a/README b/README index 17ce098a8..8de5e65b6 100644 --- a/README +++ b/README @@ -30,4 +30,5 @@ possible, see the ledger manual for more info: Bugs: -* timelog parser doesn't work +* timelog balance report shows all accounts when it shouldn't +* timelog balance total is a little off compared to ledger