{-| 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 import Ledger.Types import Ledger.Currency import Ledger.Amount import Ledger.RawTransaction 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. ledgerFromTimeLog :: TimeLog -> RawLedger ledgerFromTimeLog tl = RawLedger [] [] (entriesFromTimeLogEntries $ timelog_entries tl) "" -- | Convert time log entries to ledger entries. entriesFromTimeLogEntries :: [TimeLogEntry] -> [Entry] -- | When there is a trailing clockin entry, provide the missing clockout. -- "Now" would be ideal but requires IO, for now we make it the same as -- clockin time. entriesFromTimeLogEntries [clockin@(TimeLogEntry _ t _)] = entriesFromTimeLogEntries [clockin, (TimeLogEntry 'o' t "")] entriesFromTimeLogEntries [clockin,clockout] = [ Entry { edate = indate, estatus = True, ecode = "", edescription = accountname, ecomment = "", etransactions = [ RawTransaction accountname amount "", RawTransaction "assets:TIME" (-amount) "" ], epreceding_comment_lines=""} ] where accountname = tlcomment clockin 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) showDateFrom :: UTCTime -> String showDateFrom = formatTime defaultTimeLocale "%Y/%m/%d"