hledger/Ledger/TimeLog.hs

65 lines
2.2 KiB
Haskell
Raw Normal View History

{-|
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 Ledger.Utils
2008-10-03 04:12:59 +04:00
import Ledger.Types
import Ledger.Currency
import Ledger.Amount
import Ledger.RawTransaction
2008-10-03 06:37:19 +04:00
import Ledger.Entry
import Ledger.RawLedger
instance Show TimeLogEntry where
show t = printf "%s %s %s" (show $ tlcode t) (tldatetime t) (tlcomment t)
instance Show TimeLog where
show tl = printf "TimeLog with %d entries" $ length $ timelog_entries tl
-- | Convert a time log to a ledger.
2008-10-03 02:17:04 +04:00
ledgerFromTimeLog :: TimeLog -> RawLedger
2008-10-09 01:08:42 +04:00
ledgerFromTimeLog tl = RawLedger [] [] (entriesFromTimeLogEntries $ timelog_entries tl) ""
-- | Convert time log entries to ledger entries.
2008-10-03 06:37:19 +04:00
entriesFromTimeLogEntries :: [TimeLogEntry] -> [Entry]
2008-10-09 01:08:42 +04:00
entriesFromTimeLogEntries [] = []
entriesFromTimeLogEntries [i] = entriesFromTimeLogEntries [i, clockoutFor i]
entriesFromTimeLogEntries (i:o:rest) = [entryFromTimeLogInOut i o] ++ entriesFromTimeLogEntries rest
-- | When there is a trailing clockin entry, provide the missing clockout.
2008-10-09 01:08:42 +04:00
-- "Now" would be preferable but requires IO, for now use the clockin time.
clockoutFor (TimeLogEntry _ t _) = TimeLogEntry 'o' t ""
2008-10-09 01:08:42 +04:00
-- | Convert a timelog clockin and clockout entry to an equivalent ledger
-- entry, representing the time expenditure.
entryFromTimeLogInOut :: TimeLogEntry -> TimeLogEntry -> Entry
entryFromTimeLogInOut i o =
Entry {
edate = indate, -- ledger uses outdate
estatus = True,
ecode = "",
edescription = "",
2008-10-09 01:08:42 +04:00
ecomment = "",
etransactions = txns,
epreceding_comment_lines=""
}
where
2008-10-09 01:08:42 +04:00
acctname = tlcomment i
indate = showDateFrom intime
outdate = showDateFrom outtime
intime = parsedatetime $ tldatetime i
outtime = parsedatetime $ tldatetime o
2008-10-09 11:07:48 +04:00
hours = realToFrac (diffUTCTime outtime intime) / 3600
2008-10-09 01:08:42 +04:00
amount = Amount (getcurrency "h") hours 1
txns = [RawTransaction acctname amount "", RawTransaction "assets:TIME" (-amount) ""]
showDateFrom :: UTCTime -> String
showDateFrom = formatTime defaultTimeLocale "%Y/%m/%d"