hledger/Ledger/TimeLog.hs
2008-10-03 13:41:27 +00:00

74 lines
2.2 KiB
Haskell

{-|
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
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
ledgerFromTimeLog :: TimeLog -> RawLedger
ledgerFromTimeLog tl =
RawLedger [] [] (entriesFromTimeLogEntries $ timelog_entries tl) ""
entriesFromTimeLogEntries :: [TimeLogEntry] -> [Entry]
entriesFromTimeLogEntries [clockin] =
entriesFromTimeLogEntries [clockin, clockoutNowEntry]
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)
clockoutNowEntry = TimeLogEntry ' ' "" ""
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"