mirror of
https://github.com/simonmichael/hledger.git
synced 2024-11-08 07:09:28 +03:00
fix the timelog parser, make timelog reports work
This commit is contained in:
parent
48700f323f
commit
9ad1310f60
@ -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
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user