fix the timelog parser, make timelog reports work

This commit is contained in:
Simon Michael 2008-10-03 13:41:27 +00:00
parent 48700f323f
commit 9ad1310f60
3 changed files with 27 additions and 11 deletions

View File

@ -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

View File

@ -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"

3
README
View File

@ -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